← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21448: WIP - first cut at ADX export

 

------------------------------------------------------------
revno: 21448
committer: bobjolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-12-14 21:53:09 +0000
message:
  WIP - first cut at ADX export
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/AdxDataService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/DefaultAdxDataService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/AdxDataService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/AdxDataService.java	2015-11-02 18:56:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/AdxDataService.java	2015-12-14 21:53:09 +0000
@@ -77,5 +77,15 @@
      */
     ImportSummaries saveDataValueSet( InputStream in, ImportOptions importOptions, TaskId id );
 
+    /**
+     * Get data. Writes adx export data to output stream.
+     * 
+     * @param in the InputStream.
+     * @param importOptions the importOptions.
+     * @param id the task id, can be null.
+     * 
+     * @return an ImportSummaries collection of ImportSummary for each DataValueSet.
+     */
     void writeDataValueSet( DataExportParams params, OutputStream out );
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/DefaultAdxDataService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/DefaultAdxDataService.java	2015-12-04 05:41:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/DefaultAdxDataService.java	2015-12-14 21:53:09 +0000
@@ -28,8 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Set;
+
 import org.amplecode.staxwax.factory.XMLFactory;
 import org.amplecode.staxwax.reader.XMLReader;
+import org.amplecode.staxwax.writer.XMLWriter;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -41,11 +44,14 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.datavalueset.DataExportParams;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
@@ -54,6 +60,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.notification.Notifier;
@@ -100,6 +107,9 @@
     protected DataValueSetService dataValueSetService;
 
     @Autowired
+    protected DataValueService dataValueService;
+
+    @Autowired
     protected DataElementService dataElementService;
 
     @Autowired
@@ -121,7 +131,97 @@
     @Override
     public void writeDataValueSet( DataExportParams params, OutputStream out )
     {
-        throw new UnsupportedOperationException( "ADX export not supported yet." );
+        // TODO: defensive code around possible missing CODEs
+        
+        // caching map used to lookup category attributes per catoptcombo
+        Map<Integer, Map<String, String> > catOptMap = new HashMap<> ();
+        
+        XMLWriter adxWriter = XMLFactory.getXMLWriter(out);
+        
+        adxWriter.openElement(AdxDataService.ROOT);
+        adxWriter.writeAttribute("xmlns", AdxDataService.NAMESPACE);
+        
+        for (DataSet dataSet : params.getDataSets())
+        {
+            DataElementCategoryCombo categoryCombo = dataSet.getCategoryCombo();
+
+            List<DataElementCategory> categories=categoryCombo.getCategories();
+ 
+            for (DataElementCategoryOptionCombo aoc : categoryCombo.getOptionCombos())
+            {
+                Set<DataElementCategoryOption> catopts = aoc.getCategoryOptions();
+                Map<String,String> attributeDimensions;
+                int aocId = aoc.getId();
+                if (catOptMap.containsKey(aocId))
+                {
+                    attributeDimensions = catOptMap.get(aocId);
+                }
+                else
+                {
+                    attributeDimensions = getExplodedCategoryAttributes(aoc);
+                    catOptMap.put(aocId, attributeDimensions);
+                }
+                
+                for (OrganisationUnit orgUnit : params.getOrganisationUnits()) 
+                {
+                    for (Period period : params.getPeriods()) 
+                    {
+                        adxWriter.openElement(AdxDataService.GROUP);
+                        adxWriter.writeAttribute(AdxDataService.DATASET, dataSet.getCode());
+                        adxWriter.writeAttribute(AdxDataService.PERIOD, AdxPeriod.serialize(period));
+                        adxWriter.writeAttribute(AdxDataService.ORGUNIT, orgUnit.getCode());
+                        for (String attribute : attributeDimensions.keySet())
+                        {
+                            adxWriter.writeAttribute(attribute,attributeDimensions.get(attribute));
+                        }
+
+                        for (DataValue dv : dataValueService.getDataValues(orgUnit, period, dataSet.getDataElements(), aoc)) 
+                        {
+                            adxWriter.openElement(AdxDataService.DATAVALUE);
+                            Map<String,String> dvDimensions = getExplodedCategoryAttributes(dv.getCategoryOptionCombo());
+                            
+                            adxWriter.writeAttribute(AdxDataService.DATAELEMENT, dv.getDataElement().getCode()); 
+                            
+                            DataElementCategoryOptionCombo coc = dv.getCategoryOptionCombo();
+                            
+                            Map<String,String> categoryDimensions;
+                            int cocId = coc.getId();
+                            if (catOptMap.containsKey(cocId))
+                            {
+                                categoryDimensions = catOptMap.get(cocId);
+                            }
+                            else
+                            {
+                                 categoryDimensions = getExplodedCategoryAttributes(coc);
+                                 catOptMap.put(cocId, categoryDimensions);
+                            }
+                            
+                            for (String attribute : categoryDimensions.keySet())
+                            {
+                                adxWriter.writeAttribute(attribute,categoryDimensions.get(attribute));
+                            }
+
+                            if (dv.getDataElement().getValueType().isNumeric())
+                            {
+                                adxWriter.writeAttribute(AdxDataService.VALUE, dv.getValue());
+                            } 
+                            else 
+                            {
+                                adxWriter.writeAttribute(AdxDataService.VALUE, "0");                          
+                                adxWriter.openElement(AdxDataService.ANNOTATION);
+                                adxWriter.writeCharacters(dv.getValue());
+                                adxWriter.closeElement(); // ANNOTATION
+                            }
+                            adxWriter.closeElement(); // DATAVALUE
+                        }
+                        adxWriter.closeElement(); //GROUP    
+                    }
+                }
+            }
+        }
+        adxWriter.closeElement(); // ADX
+        
+        adxWriter.closeWriter();
     }
 
     @Override
@@ -442,4 +542,50 @@
 
         log.debug( "DXF attributes: " + attributes );
     }
+    
+    private Map<String, String> getExplodedCategoryAttributes( DataElementCategoryOptionCombo coc)
+    {
+        Map<String, String> categoryAttributes = new HashMap<>();
+        for (DataElementCategory category : coc.getCategoryCombo().getCategories())
+        {
+            categoryAttributes.put(category.getCode(), category.getCategoryOption(coc).getCode());
+        }
+        return categoryAttributes;       
+    }
+    
+    Map<Integer, Map<String, String>  > createCatOptMap()
+    {
+        Map<Integer, Map<String, String> > catOptMap = new HashMap<> ();
+        
+        for (DataElementCategoryOptionCombo coc : categoryService.getAllDataElementCategoryOptionCombos())
+        {
+            int id = coc.getId();
+            Map<String,String> categoryCodes = new HashMap<>();
+            DataElementCategoryCombo catCombo = coc.getCategoryCombo();
+            Set<DataElementCategoryOption> catOptions = coc.getCategoryOptions();
+            for (DataElementCategory category : catCombo.getCategories())
+            {
+                categoryCodes.put(category.getCode(), category.getCategoryOption(coc).getCode());
+            }
+            catOptMap.put(id, categoryCodes);
+        }
+        return catOptMap;
+    }
+    
+   
+    /**
+      
+      select distinct de.categorycomboid from dataset ds 
+      join datasetmembers dsm on ds.datasetid=dsm.datasetid 
+      join dataelement de on dsm.dataelementid=de.dataelementid;
+     
+     
+     select coc.categoryoptioncomboid, cat.code, co.code from categoryoptioncombos_categoryoptions cocco
+  inner join dataelementcategoryoption co on cocco.categoryoptionid = co.categoryoptionid
+  inner join categories_categoryoptions cco on co.categoryoptionid = cco.categoryoptionid
+  inner join categoryoptioncombo coc on coc.categoryoptioncomboid = cocco.categoryoptioncomboid 
+  inner join dataelementcategory cat on cco.categoryid = cat.categoryid
+  where coc.name != 'default' ;
+
+     */
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-12-04 16:01:39 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-12-14 21:53:09 +0000
@@ -118,6 +118,26 @@
         dataValueSetService.writeDataValueSetXml( params, response.getOutputStream() );
     }
 
+    @RequestMapping( method = RequestMethod.GET, produces = CONTENT_TYPE_XML_ADX )
+    public void getDataValueSetXmlAdx(
+        @RequestParam Set<String> dataSet,
+        @RequestParam( required = false ) Set<String> period,
+        @RequestParam( required = false ) Date startDate,
+        @RequestParam( required = false ) Date endDate,
+        @RequestParam Set<String> orgUnit,
+        @RequestParam( required = false ) boolean children,
+        @RequestParam( required = false ) Date lastUpdated,
+        @RequestParam( required = false ) Integer limit,
+        IdSchemes idSchemes, HttpServletResponse response ) throws IOException
+    {
+        response.setContentType( CONTENT_TYPE_XML_ADX );
+
+        DataExportParams params = dataValueSetService.getFromUrl( dataSet, period,
+            startDate, endDate, orgUnit, children, lastUpdated, limit, idSchemes );
+
+        adxDataService.writeDataValueSet( params, response.getOutputStream() );
+    }
+
     @RequestMapping( method = RequestMethod.GET, produces = CONTENT_TYPE_JSON )
     public void getDataValueSetJson(
         @RequestParam Set<String> dataSet,
@@ -181,7 +201,7 @@
         }
     }
 
-    @RequestMapping( method = RequestMethod.POST, consumes = "application/xml+adx" )
+    @RequestMapping( method = RequestMethod.POST, consumes = CONTENT_TYPE_XML_ADX )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
     public void postAdxDataValueSet( ImportOptions importOptions,
         HttpServletRequest request, HttpServletResponse response ) throws IOException

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2015-12-08 19:33:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2015-12-14 21:53:09 +0000
@@ -68,6 +68,7 @@
     public static final String CONTENT_TYPE_TEXT = "text/plain; charset=UTF-8";
     public static final String CONTENT_TYPE_CSS = "text/css; charset=UTF-8";
     public static final String CONTENT_TYPE_XML = "application/xml; charset=UTF-8";
+    public static final String CONTENT_TYPE_XML_ADX = "application/xml+adx; charset=UTF-8";
     public static final String CONTENT_TYPE_CSV = "application/csv; charset=UTF-8";
     public static final String CONTENT_TYPE_PNG = "image/png";
     public static final String CONTENT_TYPE_JPG = "image/jpeg";