← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8437: Populate with dataValues in forms. Available at /api/dataSets/UID/form?orgUnit=UID&period=PERIOD

 

------------------------------------------------------------
revno: 8437
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-10-08 21:51:22 +0200
message:
  Populate with dataValues in forms. Available at /api/dataSets/UID/form?orgUnit=UID&period=PERIOD
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/FormUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/form/Form.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-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2012-10-08 17:42:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2012-10-08 19:51:22 +0000
@@ -31,15 +31,24 @@
 import org.hisp.dhis.api.utils.FormUtils;
 import org.hisp.dhis.api.webdomain.form.Form;
 import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.period.PeriodType;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.Collection;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -51,8 +60,23 @@
 {
     public static final String RESOURCE_PATH = "/dataSets";
 
-    @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = {"application/json", "text/*"} )
-    public void getFormJson( @PathVariable( "uid" ) String uid, HttpServletRequest request, HttpServletResponse response ) throws IOException
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    @Autowired
+    private PeriodService periodService;
+
+    @Autowired
+    private DataValueService dataValueService;
+
+    // -------------------------------------------------------------------------
+    // Controller
+    // -------------------------------------------------------------------------
+
+    @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = "application/json" )
+    public void getFormJson( @PathVariable( "uid" ) String uid, @RequestParam( value = "orgUnit", required = false ) String orgUnit,
+        @RequestParam( value = "period", required = false ) String period, HttpServletResponse response ) throws IOException
     {
         DataSet dataSet = getEntity( uid );
 
@@ -64,11 +88,22 @@
 
         Form form = FormUtils.fromDataSet( dataSet );
 
+        if ( orgUnit != null && !orgUnit.isEmpty() && period != null && !period.isEmpty() )
+        {
+            OrganisationUnit ou = manager.get( OrganisationUnit.class, orgUnit );
+            Period p = PeriodType.getPeriodFromIsoString( period );
+
+            Collection<DataValue> dataValues = dataValueService.getDataValues( ou, p, dataSet.getDataElements() );
+
+            FormUtils.fillWithDataValues(form, dataValues);
+        }
+
         JacksonUtils.toJson( response.getOutputStream(), form );
     }
 
     @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = {"application/xml", "text/xml"} )
-    public void getFormXml( @PathVariable( "uid" ) String uid, HttpServletRequest request, HttpServletResponse response ) throws IOException
+    public void getFormXml( @PathVariable( "uid" ) String uid, @RequestParam( value = "orgUnit", required = false ) String orgUnit,
+        @RequestParam( value = "period", required = false ) String period, HttpServletResponse response ) throws IOException
     {
         DataSet dataSet = getEntity( uid );
 
@@ -80,18 +115,26 @@
 
         Form form = FormUtils.fromDataSet( dataSet );
 
+        if ( orgUnit != null && !orgUnit.isEmpty() && period != null && !period.isEmpty() )
+        {
+            OrganisationUnit ou = manager.get( OrganisationUnit.class, orgUnit );
+            Period p = PeriodType.getPeriodFromIsoString( period );
+
+            Collection<DataValue> dataValues = dataValueService.getDataValues( ou, p, dataSet.getDataElements() );
+
+            FormUtils.fillWithDataValues(form, dataValues);
+        }
+
         JacksonUtils.toXml( response.getOutputStream(), form );
     }
 
     @RequestMapping( value = "/{uid}/form", method = RequestMethod.POST, consumes = "application/json" )
     public void postFormJson( @PathVariable( "uid" ) String uid, HttpServletRequest request, HttpServletResponse response )
     {
-        System.err.println( "postFormJson" );
     }
 
     @RequestMapping( value = "/{uid}/form", method = RequestMethod.POST, consumes = {"application/xml", "text/xml"} )
     public void postFormXml( @PathVariable( "uid" ) String uid, HttpServletRequest request, HttpServletResponse response )
     {
-        System.err.println( "postFormXml" );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/FormUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/FormUtils.java	2012-10-08 17:59:38 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/FormUtils.java	2012-10-08 19:51:22 +0000
@@ -34,10 +34,9 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.datavalue.DataValue;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
+import java.util.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -161,4 +160,32 @@
 
         return null;
     }
+
+    public static void fillWithDataValues( Form form, Collection<DataValue> dataValues )
+    {
+        Map<String, Field> cacheMap = buildCacheMap( form );
+
+        for ( DataValue dataValue : dataValues )
+        {
+            DataElement dataElement = dataValue.getDataElement();
+            DataElementCategoryOptionCombo categoryOptionCombo = dataValue.getOptionCombo();
+
+            cacheMap.get( dataElement.getUid() + "-" + categoryOptionCombo.getUid() ).setValue( dataValue.getValue() );
+        }
+    }
+
+    private static Map<String, Field> buildCacheMap( Form form )
+    {
+        Map<String, Field> cacheMap = new HashMap<String, Field>();
+
+        for ( Section section : form.getSections() )
+        {
+            for ( Field field : section.getFields() )
+            {
+                cacheMap.put( field.getDataElement() + "-" + field.getCategoryOptionCombo(), field );
+            }
+        }
+
+        return cacheMap;
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/form/Form.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/form/Form.java	2012-10-08 17:42:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/form/Form.java	2012-10-08 19:51:22 +0000
@@ -88,4 +88,14 @@
     {
         this.sections = sections;
     }
+
+    @Override
+    public String toString()
+    {
+        return "Form{" +
+            "name='" + name + '\'' +
+            ", period='" + period + '\'' +
+            ", sections=" + sections +
+            '}';
+    }
 }