← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8024: Added support for Json datavalueset.

 

------------------------------------------------------------
revno: 8024
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-09-10 15:50:51 +0700
message:
  Added support for Json datavalueset.
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValue.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSet.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.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-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValue.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValue.java	2012-06-13 08:44:07 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValue.java	2012-09-10 08:50:51 +0000
@@ -28,6 +28,15 @@
  */
 
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.common.Dxf2Namespace;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
+
+@JacksonXmlRootElement( localName = "dataValue", namespace = Dxf2Namespace.NAMESPACE )
 public class DataValue
 {
     //--------------------------------------------------------------------------
@@ -65,6 +74,9 @@
     // Getters and setters
     //--------------------------------------------------------------------------
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getDataElement()
     {
         return dataElement;
@@ -75,6 +87,9 @@
         this.dataElement = dataElement;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getPeriod()
     {
         return period;
@@ -85,6 +100,9 @@
         this.period = period;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getOrgUnit()
     {
         return orgUnit;
@@ -95,6 +113,9 @@
         this.orgUnit = orgUnit;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getCategoryOptionCombo()
     {
         return categoryOptionCombo;
@@ -105,6 +126,9 @@
         this.categoryOptionCombo = categoryOptionCombo;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getValue()
     {
         return value;
@@ -115,6 +139,9 @@
         this.value = value;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getStoredBy()
     {
         return storedBy;
@@ -125,6 +152,9 @@
         this.storedBy = storedBy;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getTimestamp()
     {
         return timestamp;
@@ -135,6 +165,9 @@
         this.timestamp = timestamp;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getComment()
     {
         return comment;
@@ -145,6 +178,9 @@
         this.comment = comment;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public Boolean getFollowup()
     {
         return followup;

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSet.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSet.java	2012-04-15 20:48:08 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSet.java	2012-09-10 08:50:51 +0000
@@ -28,10 +28,20 @@
  */
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.common.Dxf2Namespace;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.dxf2.datavalue.DataValue;
 
+@JacksonXmlRootElement( localName = "dataValueSet", namespace = Dxf2Namespace.NAMESPACE )
 public class DataValueSet
 {
     //--------------------------------------------------------------------------
@@ -72,6 +82,9 @@
     // Getters and setters
     //--------------------------------------------------------------------------
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getDataElementIdScheme()
     {
         return dataElementIdScheme;
@@ -82,6 +95,9 @@
         this.dataElementIdScheme = dataElementIdScheme;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getOrgUnitIdScheme()
     {
         return orgUnitIdScheme;
@@ -92,6 +108,9 @@
         this.orgUnitIdScheme = orgUnitIdScheme;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public Boolean getDryRun()
     {
         return dryRun;
@@ -102,6 +121,9 @@
         this.dryRun = dryRun;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getStrategy()
     {
         return strategy;
@@ -112,6 +134,9 @@
         this.strategy = strategy;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getDataSet()
     {
         return dataSet;
@@ -122,6 +147,9 @@
         this.dataSet = dataSet;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getCompleteDate()
     {
         return completeDate;
@@ -132,6 +160,9 @@
         this.completeDate = completeDate;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getPeriod()
     {
         return period;
@@ -142,6 +173,9 @@
         this.period = period;
     }
 
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE )
     public String getOrgUnit()
     {
         return orgUnit;
@@ -152,6 +186,10 @@
         this.orgUnit = orgUnit;
     }
 
+    @JsonProperty( value = "dataValues" )
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlElementWrapper( localName = "dataValues", namespace = Dxf2Namespace.NAMESPACE )
+    @JacksonXmlProperty( localName = "dataValue", namespace = Dxf2Namespace.NAMESPACE )
     public List<DataValue> getDataValues()
     {
         return dataValues;
@@ -166,25 +204,42 @@
     // Logic
     //--------------------------------------------------------------------------
 
+    private Iterator<DataValue> dataValueIterator;
+
+    public void refreshDataValueIterator()
+    {
+        dataValueIterator = dataValues.iterator();
+    }
+
     public boolean hasNextDataValue()
     {
-        return dataValues.iterator().hasNext();
+        if(dataValueIterator == null)
+        {
+            refreshDataValueIterator();
+        }
+
+        return dataValueIterator.hasNext();
     }
-    
+
     public DataValue getNextDataValue()
     {
-        return dataValues.iterator().next();
+        if(dataValueIterator == null)
+        {
+            refreshDataValueIterator();
+        }
+
+        return dataValueIterator.next();
     }
 
     public DataValue getDataValueInstance()
     {
         return new DataValue();
     }
-    
+
     public void close()
     {
     }
-    
+
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2012-04-15 15:05:28 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2012-09-10 08:50:51 +0000
@@ -48,9 +48,15 @@
     
     ImportSummary saveDataValueSet( InputStream in );
 
+    ImportSummary saveDataValueSetJson( InputStream in );
+
     ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions );
-    
+
+    ImportSummary saveDataValueSetJson( InputStream in, ImportOptions importOptions );
+
     ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions, TaskId taskId );
-    
+
+    ImportSummary saveDataValueSetJson( InputStream in, ImportOptions importOptions, TaskId taskId );
+
     ImportSummary saveDataValueSetCsv( Reader reader, ImportOptions importOptions, TaskId id );
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2012-06-07 13:03:34 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2012-09-10 08:50:51 +0000
@@ -36,10 +36,7 @@
 import static org.hisp.dhis.system.util.ConversionUtils.wrap;
 import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
 
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
+import java.io.*;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -66,6 +63,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -178,11 +176,21 @@
         return saveDataValueSet( in, ImportOptions.getDefaultImportOptions(), null );
     }
 
+    public ImportSummary saveDataValueSetJson( InputStream in )
+    {
+        return saveDataValueSetJson( in, ImportOptions.getDefaultImportOptions(), null );
+    }
+
     public ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions )
     {
         return saveDataValueSet( in, importOptions, null );
     }
-    
+
+    public ImportSummary saveDataValueSetJson( InputStream in, ImportOptions importOptions )
+    {
+        return saveDataValueSetJson( in, importOptions, null );
+    }
+
     public ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions, TaskId id )
     {
         try
@@ -198,6 +206,22 @@
         }
     }
 
+    public ImportSummary saveDataValueSetJson( InputStream in, ImportOptions importOptions, TaskId id )
+    {
+        try
+        {
+            DataValueSet dataValueSet = JacksonUtils.fromJson( in, DataValueSet.class );
+            log.info( dataValueSet );
+            return saveDataValueSet( importOptions, id, dataValueSet );
+        }
+        catch ( Exception ex )
+        {
+            log.error( DebugUtils.getStackTrace( ex ) );
+            notifier.notify( id, DATAVALUE_IMPORT, ERROR, "Unfortunately the process failed, check the logs", true );
+            return new ImportSummary( ImportStatus.ERROR, "The import process failed: " + ex.getMessage() );
+        }
+    }
+
     public ImportSummary saveDataValueSetCsv( Reader reader, ImportOptions importOptions, TaskId id )
     {
         try
@@ -237,7 +261,7 @@
         
         Period outerPeriod = PeriodType.getPeriodFromIsoString( dataValueSet.getPeriod() );
         OrganisationUnit outerOrgUnit = dataValueSet.getOrgUnit() != null ? identifiableObjectManager.getObject( OrganisationUnit.class, orgUnitIdScheme, dataValueSet.getOrgUnit() ) : null;
-        
+
         if ( dataSet != null && completeDate != null )
         {
             notifier.notify( id, DATAVALUE_IMPORT, "Completing data set" );
@@ -257,7 +281,8 @@
         int totalCount = 0;
         
         notifier.notify( id, DATAVALUE_IMPORT, "Importing data values" );
-        
+        log.info( "importing data values" );
+
         while ( dataValueSet.hasNextDataValue() )
         {
             org.hisp.dhis.dxf2.datavalue.DataValue dataValue = dataValueSet.getNextDataValue();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2012-06-27 15:14:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2012-09-10 08:50:51 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_JSON;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_XML;
 
 import java.io.IOException;
@@ -104,7 +105,22 @@
         response.setContentType( CONTENT_TYPE_XML );        
         JacksonUtils.toXml( response.getOutputStream(), summary );
     }
-    
+
+    @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
+    public void postJsonDataValueSet( ImportOptions importOptions,
+                                  HttpServletResponse response,
+                                  InputStream in,
+                                  Model model ) throws IOException
+    {
+        ImportSummary summary = dataValueSetService.saveDataValueSetJson( in, importOptions );
+
+        log.info( "Data values set saved " + importOptions );
+
+        response.setContentType( CONTENT_TYPE_JSON );
+        JacksonUtils.toJson( response.getOutputStream(), summary );
+    }
+
     @RequestMapping( method = RequestMethod.POST, consumes = "application/sdmx+xml" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
     public void postSDMXDataValueSet( ImportOptions importOptions,