← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15534: use nodes to write out DVS template (now supports json and xml)

 

------------------------------------------------------------
revno: 15534
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-06-03 11:14:08 +0200
message:
  use nodes to write out DVS template (now supports json and xml)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.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/AbstractNode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java	2014-06-02 22:47:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java	2014-06-03 09:14:08 +0000
@@ -79,6 +79,15 @@
     }
 
     @Override
+    public <T extends Node> void addNodes( List<T> nodes )
+    {
+        for ( Node node : nodes )
+        {
+            addNode( node );
+        }
+    }
+
+    @Override
     public List<Node> getNodes()
     {
         return nodes;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java	2014-06-01 14:30:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java	2014-06-03 09:14:08 +0000
@@ -28,8 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  */
 
-import org.hisp.dhis.node.exception.InvalidTypeException;
-
 import java.util.List;
 
 /**
@@ -41,7 +39,9 @@
 
     NodeType getType();
 
-    <T extends Node> T addNode( T node ) throws InvalidTypeException;
+    <T extends Node> T addNode( T node );
+
+    <T extends Node> void addNodes( List<T> nodes );
 
     List<Node> getNodes();
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-04-20 19:17:42 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-06-03 09:14:08 +0000
@@ -31,6 +31,7 @@
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
+import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.scheduling.TaskId;
 
@@ -53,10 +54,10 @@
     void writeDataValueSetJson( Set<String> dataSet, Date startDate, Date endDate, Set<String> ous, OutputStream outputStream );
 
     void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, Writer writer );
-    
-    void writeDataValueSetTemplate( OutputStream out, DataSet dataSet, Period period, List<String> orgUnits,
-        boolean comment, String orgUnitIdScheme, String dataElementIdScheme ) throws IOException;
-    
+
+    RootNode getDataValueSetTemplate( DataSet dataSet, Period period, List<String> orgUnits,
+        boolean writeComments, String ouScheme, String deScheme );
+
     ImportSummary saveDataValueSet( InputStream in );
 
     ImportSummary saveDataValueSetJson( InputStream in );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-06-01 14:24:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-06-03 09:14:08 +0000
@@ -28,29 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.apache.commons.lang.StringUtils.trimToNull;
-import static org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty.UUID;
-import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
-import static org.hisp.dhis.system.notification.NotificationLevel.INFO;
-import static org.hisp.dhis.system.util.ConversionUtils.wrap;
-import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Writer;
-import java.nio.charset.Charset;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-
+import com.csvreader.CsvReader;
 import org.amplecode.quick.BatchHandler;
 import org.amplecode.quick.BatchHandlerFactory;
 import org.amplecode.staxwax.factory.XMLFactory;
@@ -77,6 +55,11 @@
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
+import org.hisp.dhis.node.NodeHint;
+import org.hisp.dhis.node.types.CollectionNode;
+import org.hisp.dhis.node.types.ComplexNode;
+import org.hisp.dhis.node.types.RootNode;
+import org.hisp.dhis.node.types.SimpleNode;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
@@ -90,8 +73,24 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import com.csvreader.CsvReader;
-import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.commons.lang.StringUtils.trimToNull;
+import static org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty.UUID;
+import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
+import static org.hisp.dhis.system.notification.NotificationLevel.INFO;
+import static org.hisp.dhis.system.util.ConversionUtils.wrap;
+import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
 
 /**
  * @author Lars Helge Overland
@@ -238,116 +237,107 @@
         dataValueSetStore.writeDataValueSetCsv( getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), writer );
     }
 
-    public void writeDataValueSetTemplate( OutputStream out, DataSet dataSet, Period period, List<String> orgUnits,
-        boolean comment, String orgUnitIdScheme, String dataElementIdScheme ) throws IOException
+
+    @Override
+    public RootNode getDataValueSetTemplate( DataSet dataSet, Period period, List<String> orgUnits,
+        boolean writeComments, String ouScheme, String deScheme )
     {
-        ToXmlGenerator generator = (ToXmlGenerator) JacksonUtils.getXmlMapper().getFactory().createGenerator( out );
-            
-        try
+        RootNode rootNode = new RootNode( "dataValueSet" );
+        rootNode.addHint( NodeHint.Type.NAMESPACE, DxfNamespaces.DXF_2_0 );
+        rootNode.addHint( NodeHint.Type.COMMENT, "Data set: " + dataSet.getDisplayName() + " (" + dataSet.getUid() + ")" );
+
+        CollectionNode collectionNode = rootNode.addNode( new CollectionNode( "dataValues" ) );
+        collectionNode.addHint( NodeHint.Type.WRAP_COLLECTION, false );
+
+        if ( orgUnits.isEmpty() )
         {
-            XMLStreamWriter staxWriter = generator.getStaxWriter();
-    
-            if ( comment )
+            for ( DataElement dataElement : dataSet.getDataElements() )
             {
-                staxWriter.writeComment( "Data set: " + dataSet.getDisplayName() + " (" + dataSet.getUid() + ")" );
+                CollectionNode collection = getDataValueTemplate( dataElement, deScheme, null, ouScheme, period, writeComments );
+                collectionNode.addNodes( collection.getNodes() );
             }
-    
-            staxWriter.writeStartElement( "", "dataValueSet", DxfNamespaces.DXF_2_0 );
-    
-            if ( orgUnits.isEmpty() )
+        }
+        else
+        {
+            for ( String orgUnit : orgUnits )
             {
+                OrganisationUnit organisationUnit = identifiableObjectManager.search( OrganisationUnit.class, orgUnit );
+
+                if ( organisationUnit == null )
+                {
+                    continue;
+                }
+
                 for ( DataElement dataElement : dataSet.getDataElements() )
                 {
-                    writeDataValue( dataElement, dataElementIdScheme, null, orgUnitIdScheme, period, comment, staxWriter );
-                }
-            }
-            else
-            {
-                for ( String orgUnit : orgUnits )
-                {
-                    OrganisationUnit organisationUnit = identifiableObjectManager.search( OrganisationUnit.class, orgUnit );
-    
-                    if ( organisationUnit == null )
-                    {
-                        continue;
-                    }
-    
-                    if ( comment )
-                    {
-                        if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( orgUnitIdScheme.toLowerCase() ) )
-                        {
-                            staxWriter.writeComment( "Org unit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getCode() + ")" );
-                        }
-                        else
-                        {
-                            staxWriter.writeComment( "Org unit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getUid() + ")" );
-                        }    
-                    }
-    
-                    for ( DataElement dataElement : dataSet.getDataElements() )
-                    {
-                        writeDataValue( dataElement, dataElementIdScheme, organisationUnit, orgUnitIdScheme, period, comment, staxWriter );
-                    }
-                }
-            }
-    
-            staxWriter.writeEndElement();
-            staxWriter.flush();
-        }
-        catch ( XMLStreamException ignored )
-        {
-            ignored.printStackTrace();
-        }
+                    CollectionNode collection = getDataValueTemplate( dataElement, deScheme, organisationUnit, ouScheme, period, writeComments );
+                    collectionNode.addNodes( collection.getNodes() );
+                }
+            }
+        }
+
+        return rootNode;
     }
-    
-    private void writeDataValue( DataElement dataElement, String deScheme, OrganisationUnit organisationUnit, String ouScheme, Period period, boolean comment, XMLStreamWriter staxWriter ) throws XMLStreamException
+
+    private CollectionNode getDataValueTemplate( DataElement dataElement, String deScheme, OrganisationUnit organisationUnit, String ouScheme, Period period, boolean comment )
     {
+        CollectionNode collectionNode = new CollectionNode( "dataValues" );
+        collectionNode.addHint( NodeHint.Type.WRAP_COLLECTION, false );
+
         for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getSortedOptionCombos() )
         {
+            ComplexNode complexNode = collectionNode.addNode( new ComplexNode( "dataValue" ) );
+
             String label = dataElement.getDisplayName();
-    
+
             if ( !categoryOptionCombo.isDefault() )
             {
                 label += " " + categoryOptionCombo.getDisplayName();
             }
-    
+
             if ( comment )
             {
-                staxWriter.writeComment( "Data element: " + label );
+                complexNode.addHint( NodeHint.Type.COMMENT, "Data element: " + label );
             }
-    
-            staxWriter.writeStartElement( "", "dataValue", DxfNamespaces.DXF_2_0 );
-    
+
             if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( deScheme.toLowerCase() ) )
             {
-                staxWriter.writeAttribute( "dataElement", dataElement.getCode() );
+                SimpleNode simpleNode = complexNode.addNode( new SimpleNode( "dataElement", dataElement.getCode() ) );
+                simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
             }
             else
             {
-                staxWriter.writeAttribute( "dataElement", dataElement.getUid() );
+                SimpleNode simpleNode = complexNode.addNode( new SimpleNode( "dataElement", dataElement.getUid() ) );
+                simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
             }
-    
-            staxWriter.writeAttribute( "categoryOptionCombo", categoryOptionCombo.getUid() );
-            
-            staxWriter.writeAttribute( "period", period != null ? period.getIsoDate() : "" );
-    
+
+            SimpleNode simpleNode = complexNode.addNode( new SimpleNode( "categoryOptionCombo", categoryOptionCombo.getUid() ) );
+            simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
+
+            simpleNode = complexNode.addNode( new SimpleNode( "period", period != null ? period.getIsoDate() : "" ) );
+            simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
+
             if ( organisationUnit != null )
             {
                 if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( ouScheme.toLowerCase() ) )
                 {
-                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getCode() == null ? "" : organisationUnit.getCode() );
+                    simpleNode = complexNode.addNode( new SimpleNode( "orgUnit", organisationUnit.getCode() == null ? "" : organisationUnit.getCode() ) );
+                    simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
                 }
                 else
                 {
-                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getUid() == null ? "" : organisationUnit.getUid() );
+                    simpleNode = complexNode.addNode( new SimpleNode( "orgUnit", organisationUnit.getUid() == null ? "" : organisationUnit.getUid() ) );
+                    simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
                 }
             }
-    
-            staxWriter.writeAttribute( "value", "" );
-            staxWriter.writeEndElement();
+
+            simpleNode = complexNode.addNode( new SimpleNode( "value", "" ) );
+            simpleNode.addHint( NodeHint.Type.ATTRIBUTE, true );
         }
+
+        return collectionNode;
     }
-    
+
     public ImportSummary saveDataValueSet( InputStream in )
     {
         return saveDataValueSet( in, ImportOptions.getDefaultImportOptions(), null );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-06-02 22:47:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-06-03 09:14:08 +0000
@@ -45,6 +45,7 @@
 import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.importexport.ImportStrategy;
+import org.hisp.dhis.node.NodeHint;
 import org.hisp.dhis.node.NodeService;
 import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.RootNode;
@@ -73,6 +74,7 @@
 import java.io.InputStream;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -211,6 +213,7 @@
         if ( include != null || exclude != null )
         {
             RootNode rootNode = new RootNode( "metadata" );
+            rootNode.addHint( NodeHint.Type.COMMENT, "Metadata exported on " + new Date() );
 
             if ( hasPaging )
             {
@@ -224,7 +227,9 @@
 
             rootNode.addNode( filterService.filterProperties( getEntityClass(), entityList, include, exclude ) );
 
-            nodeService.serialize( rootNode, "application/json", response.getOutputStream() );
+            // response.setContentType( MediaType.APPLICATION_XML_VALUE );
+            // nodeService.serialize( rootNode, MediaType.APPLICATION_XML_VALUE, response.getOutputStream() );
+            nodeService.serialize( rootNode, MediaType.APPLICATION_JSON_VALUE, response.getOutputStream() );
         }
         else
         {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java	2014-05-31 13:18:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java	2014-06-03 09:14:08 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.i18n.I18nService;
+import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
@@ -51,7 +52,6 @@
 import org.hisp.dhis.webapi.webdomain.form.Form;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
-import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -59,6 +59,7 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.xml.transform.Transformer;
@@ -174,8 +175,8 @@
         JacksonUtils.toJson( response.getOutputStream(), form );
     }
 
-    @RequestMapping( value = "/{uid}/dataValueSet", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } )
-    public void getDvs( @PathVariable( "uid" ) String uid,
+    @RequestMapping( value = "/{uid}/dataValueSet", method = RequestMethod.GET )
+    public @ResponseBody RootNode getDvs( @PathVariable( "uid" ) String uid,
         @RequestParam( value = "orgUnitIdScheme", defaultValue = "ID", required = false ) String orgUnitIdScheme,
         @RequestParam( value = "dataElementIdScheme", defaultValue = "ID", required = false ) String dataElementIdScheme,
         @RequestParam( value = "period", defaultValue = "", required = false ) String period,
@@ -188,14 +189,11 @@
         if ( dataSet == null )
         {
             ContextUtils.notFoundResponse( response, "Object not found for uid: " + uid );
-            return;
+            return null;
         }
 
         Period pe = periodService.getPeriod( period );
-
-        response.setContentType( MediaType.APPLICATION_XML_VALUE );
-
-        dataValueSetService.writeDataValueSetTemplate( response.getOutputStream(), dataSet, pe, orgUnits, comment, orgUnitIdScheme, dataElementIdScheme );
+        return dataValueSetService.getDataValueSetTemplate( dataSet, pe, orgUnits, comment, orgUnitIdScheme, dataElementIdScheme );
     }
 
     @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } )

=== 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-06-01 14:30:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemController.java	2014-06-03 09:14:08 +0000
@@ -97,7 +97,7 @@
 
         RootNode rootNode = new RootNode( "codes" );
         CollectionNode collectionNode = rootNode.addNode( new CollectionNode( "codes" ) );
-        collectionNode.addHint( NodeHint.Type.XML_COLLECTION_WRAPPING, false );
+        collectionNode.addHint( NodeHint.Type.WRAP_COLLECTION, false );
 
         for ( int i = 0; i < n; i++ )
         {