← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15495: support unwrapped collections in xml writer, minor bugfixes to JacksonJson writer, use Node based...

 

------------------------------------------------------------
revno: 15495
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-06-01 14:21:14 +0200
message:
  support unwrapped collections in xml writer, minor bugfixes to JacksonJson writer, use Node based rendering system in SystemController/UID (only json output for now, will be both xml/json soon)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeHint.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeHint.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeHint.java	2014-05-31 23:48:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeHint.java	2014-06-01 12:21:14 +0000
@@ -36,7 +36,8 @@
     public enum Type
     {
         XML_NAMESPACE,
-        XML_ATTRIBUTE
+        XML_ATTRIBUTE,
+        XML_COLLECTION_WRAPPING
     }
 
     private final Type type;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java	2014-06-01 11:39:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java	2014-06-01 12:21:14 +0000
@@ -49,19 +49,26 @@
 {
     public static final String CONTENT_TYPE = "application/json";
 
+    private final JsonFactory jsonFactory = new JsonFactory();
+
     @Override
     public String contentType()
     {
         return CONTENT_TYPE;
     }
 
+    public JacksonJsonNodeSerializer()
+    {
+
+    }
+
     @Override
     public void serialize( RootNode rootNode, OutputStream outputStream ) throws IOException
     {
-        JsonFactory jsonFactory = new JsonFactory();
         JsonGenerator generator = jsonFactory.createGenerator( outputStream );
 
         renderRootNode( rootNode, generator );
+        generator.flush();
     }
 
     private void renderRootNode( RootNode rootNode, JsonGenerator generator ) throws IOException
@@ -70,54 +77,75 @@
 
         for ( Node node : rootNode.getNodes() )
         {
-            dispatcher( node, generator );
+            dispatcher( node, generator, true );
             generator.flush();
         }
 
         generator.writeEndObject();
     }
 
-    private void renderSimpleNode( SimpleNode simpleNode, JsonGenerator generator ) throws IOException
+    private void renderSimpleNode( SimpleNode simpleNode, JsonGenerator generator, boolean writeKey ) throws IOException
     {
-        generator.writeObjectField( simpleNode.getName(), simpleNode.getValue() );
+        if ( writeKey )
+        {
+            generator.writeObjectField( simpleNode.getName(), simpleNode.getValue() );
+        }
+        else
+        {
+            generator.writeObject( simpleNode.getValue() );
+        }
     }
 
-    private void renderComplexNode( ComplexNode complexNode, JsonGenerator generator ) throws IOException
+    private void renderComplexNode( ComplexNode complexNode, JsonGenerator generator, boolean writeKey ) throws IOException
     {
-        generator.writeObjectFieldStart( complexNode.getName() );
+        if ( writeKey )
+        {
+            generator.writeObjectFieldStart( complexNode.getName() );
+        }
+        else
+        {
+            generator.writeStartObject();
+        }
 
         for ( Node node : complexNode.getNodes() )
         {
-            dispatcher( node, generator );
+            dispatcher( node, generator, true );
         }
 
         generator.writeEndObject();
     }
 
-    private void renderCollectionNode( CollectionNode collectionNode, JsonGenerator generator ) throws IOException
+    private void renderCollectionNode( CollectionNode collectionNode, JsonGenerator generator, boolean writeKey ) throws IOException
     {
-        generator.writeArrayFieldStart( collectionNode.getName() );
+        if ( writeKey )
+        {
+            generator.writeArrayFieldStart( collectionNode.getName() );
+        }
+        else
+        {
+            generator.writeStartArray();
+        }
 
         for ( Node node : collectionNode.getNodes() )
         {
-            dispatcher( node, generator );
+            dispatcher( node, generator, false );
         }
 
         generator.writeEndArray();
     }
 
-    private void dispatcher( Node node, JsonGenerator generator ) throws IOException
+    private void dispatcher( Node node, JsonGenerator generator, boolean writeKey ) throws IOException
     {
         switch ( node.getType() )
         {
             case SIMPLE:
-                renderSimpleNode( (SimpleNode) node, generator );
+                renderSimpleNode( (SimpleNode) node, generator, writeKey );
                 break;
             case COMPLEX:
-                renderComplexNode( (ComplexNode) node, generator );
+                renderComplexNode( (ComplexNode) node, generator, writeKey );
                 break;
             case COLLECTION:
-                renderCollectionNode( (CollectionNode) node, generator );
+                renderCollectionNode( (CollectionNode) node, generator, writeKey );
                 break;
         }
     }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java	2014-06-01 11:39:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java	2014-06-01 12:21:14 +0000
@@ -51,6 +51,8 @@
 {
     public static final String CONTENT_TYPE = "application/xml";
 
+    private final XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
+
     @Override
     public String contentType()
     {
@@ -60,13 +62,13 @@
     @Override
     public void serialize( RootNode rootNode, OutputStream outputStream ) throws IOException
     {
-        XMLOutputFactory factory = XMLOutputFactory.newInstance();
         XMLStreamWriter writer;
 
         try
         {
-            writer = factory.createXMLStreamWriter( outputStream );
+            writer = xmlFactory.createXMLStreamWriter( outputStream );
             renderRootNode( rootNode, writer );
+            writer.flush();
         }
         catch ( XMLStreamException e )
         {
@@ -125,16 +127,22 @@
         writeEndElement( writer );
     }
 
-    private void renderCollectionNode( CollectionNode collectionNode, XMLStreamWriter writer ) throws XMLStreamException, IOException
+    private void renderCollectionNode( CollectionNode collectionNode, XMLStreamWriter writer, boolean useWrapping ) throws XMLStreamException, IOException
     {
-        writeStartElement( collectionNode, writer );
+        if ( useWrapping )
+        {
+            writeStartElement( collectionNode, writer );
+        }
 
         for ( Node node : collectionNode.getNodes() )
         {
             dispatcher( node, writer );
         }
 
-        writeEndElement( writer );
+        if ( useWrapping )
+        {
+            writeEndElement( writer );
+        }
     }
 
     private void dispatcher( Node node, XMLStreamWriter writer ) throws IOException, XMLStreamException
@@ -156,7 +164,14 @@
                 renderComplexNode( (ComplexNode) node, writer );
                 break;
             case COLLECTION:
-                renderCollectionNode( (CollectionNode) node, writer );
+                boolean useWrapping = true;
+
+                if ( node.haveHint( NodeHint.Type.XML_COLLECTION_WRAPPING ) )
+                {
+                    useWrapping = (boolean) node.getHint( NodeHint.Type.XML_COLLECTION_WRAPPING ).getValue();
+                }
+
+                renderCollectionNode( (CollectionNode) node, writer, useWrapping );
                 break;
         }
     }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemController.java	2014-05-23 11:11:47 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemController.java	2014-06-01 12:21:14 +0000
@@ -31,6 +31,12 @@
 import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.dxf2.metadata.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.node.NodeHint;
+import org.hisp.dhis.node.NodeService;
+import org.hisp.dhis.node.exception.InvalidTypeException;
+import org.hisp.dhis.node.types.CollectionNode;
+import org.hisp.dhis.node.types.RootNode;
+import org.hisp.dhis.node.types.SimpleNode;
 import org.hisp.dhis.scheduling.TaskCategory;
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.SystemInfo;
@@ -41,6 +47,7 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -76,35 +83,33 @@
     @Autowired
     private Notifier notifier;
 
+    @Autowired
+    private NodeService nodeService;
+
     //--------------------------------------------------------------------------
     // UID Generator
     //--------------------------------------------------------------------------
 
     @RequestMapping( value = { "/uid", "/id" }, method = RequestMethod.GET )
-    public void getUid( @RequestParam( required = false ) Integer n, HttpServletResponse response ) throws IOException
+    public void getUid( @RequestParam( required = false, defaultValue = "1" ) Integer n, HttpServletResponse response )
+        throws IOException, InvalidTypeException
     {
-        response.setContentType( ContextUtils.CONTENT_TYPE_JSON );
-
-        List<String> codes = new ArrayList<String>();
-
-        if ( n == null )
-        {
-            codes.add( CodeGenerator.generateCode() );
-        }
-        else
-        {
-            if ( n > 10000 )
-            {
-                n = 10000;
-            }
-
-            for ( int i = 0; i < n; i++ )
-            {
-                codes.add( CodeGenerator.generateCode() );
-            }
-        }
-
-        JacksonUtils.toJson( response.getOutputStream(), codes );
+        if ( n > 10000 )
+        {
+            n = 10000;
+        }
+
+        RootNode rootNode = new RootNode( "codes" );
+        CollectionNode collectionNode = rootNode.addNode( new CollectionNode( "codes" ) );
+        collectionNode.addHint( new NodeHint( NodeHint.Type.XML_COLLECTION_WRAPPING, false ) );
+
+        for ( int i = 0; i < n; i++ )
+        {
+            collectionNode.addNode( new SimpleNode( "code", CodeGenerator.generateCode() ) );
+        }
+
+        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
+        nodeService.serialize( rootNode, MediaType.APPLICATION_JSON_VALUE, response.getOutputStream() );
     }
 
     @RequestMapping( value = "/tasks/{category}", method = RequestMethod.GET, produces = { "*/*", "application/json" } )