← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16486: Data export. Optimized the way organisation units are retrieved.

 

------------------------------------------------------------
revno: 16486
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-08-24 17:24:33 +0200
message:
  Data export. Optimized the way organisation units are retrieved.
modified:
  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/DataValueSetStore.java
  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/SpringDataValueSetStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.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/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-07-10 16:06:48 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-08-24 15:24:33 +0000
@@ -42,19 +42,22 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.scheduling.TaskId;
 
+/**
+ * @author Lars Helge Overland
+ */
 public interface DataValueSetService
 {
-    void writeDataValueSet( String dataSet, String period, String orgUnit, OutputStream out );
+    void writeDataValueSetXml( String dataSet, String period, String orgUnit, OutputStream out );
 
-    void writeDataValueSet( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, OutputStream out );
+    void writeDataValueSetXml( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, boolean includeChildren, OutputStream out );
 
     void writeDataValueSetJson( String ds, String period, String ou, OutputStream outputStream );
 
-    void writeDataValueSetJson( Set<String> dataSet, Date startDate, Date endDate, Set<String> ous, OutputStream outputStream );
+    void writeDataValueSetJson( Set<String> dataSet, Date startDate, Date endDate, Set<String> ous, boolean includeChildren, OutputStream outputStream );
 
     void writeDataValueSetJson( Date lastUpdated, OutputStream outputStream );
     
-    void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, Writer writer );
+    void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, boolean includeChildren, Writer writer );
 
     RootNode getDataValueSetTemplate( DataSet dataSet, Period period, List<String> orgUnits,
         boolean writeComments, String ouScheme, String deScheme );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetStore.java	2014-08-14 05:46:19 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetStore.java	2014-08-24 15:24:33 +0000
@@ -42,13 +42,13 @@
  */
 public interface DataValueSetStore
 {
-    public void writeDataValueSetXml(Set<DataSet> dataSets, Date completeDate, Period period, OrganisationUnit orgUnit,
-                                     Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream out);
-
-    public void writeDataValueSetCsv(Set<Period> periods, Set<OrganisationUnit> orgUnits, Writer writer);
-
-    public void writeDataValueSetJson(Set<DataSet> dataSets, Date completeDate, Period period, OrganisationUnit orgUnit,
-                                      Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream out);
+    public void writeDataValueSetXml( Set<DataSet> dataSets, Date completeDate, Period period,
+        OrganisationUnit orgUnit, Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream out );
+
+    public void writeDataValueSetCsv( Set<DataSet> dataSets, Set<Period> periods, Set<OrganisationUnit> orgUnits, Writer writer );
+
+    public void writeDataValueSetJson( Set<DataSet> dataSets, Date completeDate, Period period,
+        OrganisationUnit orgUnit, Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream out );
 
     void writeDataValueSetJson( Date lastUpdated, OutputStream outputStream );
 }

=== 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-08-20 14:33:02 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-08-24 15:24:33 +0000
@@ -58,6 +58,7 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
@@ -145,7 +146,7 @@
     //--------------------------------------------------------------------------
 
     @Override
-    public void writeDataValueSet( String dataSet, String period, String orgUnit, OutputStream out )
+    public void writeDataValueSetXml( String dataSet, String period, String orgUnit, OutputStream out )
     {
         DataSet dataSet_ = dataSetService.getDataSet( dataSet );
         Period period_ = PeriodType.getPeriodFromIsoString( period );
@@ -180,18 +181,34 @@
     }
 
     @Override
-    public void writeDataValueSet( Set<String> dataSetUids, Date startDate, Date endDate, Set<String> orgUnits,
-        OutputStream out )
+    public void writeDataValueSetXml( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, 
+        boolean includeChildren, OutputStream out )
     {
-        Set<Period> periods = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
-        List<DataSet> dataSets = dataSetService.getDataSetsByUid( dataSetUids );
+        Set<DataSet> ds = new HashSet<>( dataSetService.getDataSetsByUid( dataSets ) );
+        Set<Period> pe = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
+        Set<OrganisationUnit> ou = new HashSet<>( organisationUnitService.getOrganisationUnitsByUid( orgUnits ) );
 
-        if ( periods.isEmpty() )
+        if ( ds.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one data set must be specified" );
+        }
+        
+        if ( pe.isEmpty() )
         {
             throw new IllegalArgumentException( "At least one period must be specified" );
         }
-
-        dataValueSetStore.writeDataValueSetXml( newHashSet( dataSets ), null, null, null, periods, getOrgUnits( orgUnits ), out );
+        
+        if ( ou.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one organisation unit must be specified" );
+        }
+        
+        if ( includeChildren )
+        {
+            ou = new HashSet<>( organisationUnitService.getOrganisationUnitsWithChildren( IdentifiableObjectUtils.getUids( ou ) ) );
+        }
+        
+        dataValueSetStore.writeDataValueSetXml( ds, null, null, null, pe, ou, out );
     }
 
     @Override
@@ -236,33 +253,65 @@
     }
 
     @Override
-    public void writeDataValueSetJson( Set<String> dataSetUids, Date startDate, Date endDate, Set<String> orgUnits,
-        OutputStream outputStream )
+    public void writeDataValueSetJson( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits,
+        boolean includeChildren, OutputStream outputStream )
     {
-        Set<Period> periods = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
-        List<DataSet> dataSets = dataSetService.getDataSetsByUid( dataSetUids );
+        Set<DataSet> ds = new HashSet<>( dataSetService.getDataSetsByUid( dataSets ) );
+        Set<Period> pe = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
+        Set<OrganisationUnit> ou = new HashSet<>( organisationUnitService.getOrganisationUnitsByUid( orgUnits ) );
 
-        if ( periods.isEmpty() )
+        if ( ds.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one data set must be specified" );
+        }
+        
+        if ( pe.isEmpty() )
         {
             throw new IllegalArgumentException( "At least one period must be specified" );
         }
+        
+        if ( ou.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one organisation unit must be specified" );
+        }
+        
+        if ( includeChildren )
+        {
+            ou = new HashSet<>( organisationUnitService.getOrganisationUnitsWithChildren( IdentifiableObjectUtils.getUids( ou ) ) );
+        }
 
-        dataValueSetStore.writeDataValueSetJson( newHashSet( dataSets ), null, null, null, periods, getOrgUnits( orgUnits ),
-            outputStream );
+        dataValueSetStore.writeDataValueSetJson( ds, null, null, null, pe, ou, outputStream );
     }
 
     @Override
     public void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits,
-        Writer writer )
+        boolean includeChildren, Writer writer )
     {
-        Set<Period> periods = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
+        Set<DataSet> ds = new HashSet<>( dataSetService.getDataSetsByUid( dataSets ) );
+        Set<Period> pe = new HashSet<>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
+        Set<OrganisationUnit> ou = new HashSet<>( organisationUnitService.getOrganisationUnitsByUid( orgUnits ) );
 
-        if ( periods.isEmpty() )
+        if ( ds.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one data set must be specified" );
+        }
+        
+        if ( pe.isEmpty() )
         {
             throw new IllegalArgumentException( "At least one period must be specified" );
         }
+        
+        if ( ou.isEmpty() )
+        {
+            throw new IllegalArgumentException( "At least one organisation unit must be specified" );
+        }
+        
+        if ( includeChildren )
+        {
+            ou = new HashSet<>( organisationUnitService.getOrganisationUnitsWithChildren( IdentifiableObjectUtils.getUids( ou ) ) );
+        }
 
-        dataValueSetStore.writeDataValueSetCsv( periods, getOrgUnits( orgUnits ), writer );
+        dataValueSetStore.writeDataValueSetCsv( ds, pe, ou, writer );
     }
 
     @Override
@@ -721,25 +770,6 @@
         summary.setDataSetComplete( DateUtils.getMediumDateString( completeDate ) );
     }
 
-    private Set<OrganisationUnit> getOrgUnits( Set<String> orgUnits )
-    {
-        Set<OrganisationUnit> organisationUnits = new HashSet<>();
-
-        for ( String ou : orgUnits )
-        {
-            OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit( ou );
-
-            if ( orgUnit == null )
-            {
-                throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + ou );
-            }
-
-            organisationUnits.add( orgUnit );
-        }
-
-        return organisationUnits;
-    }
-
     private Map<String, OrganisationUnit> getUuidOrgUnitMap()
     {
         Map<String, OrganisationUnit> orgUnitMap = new HashMap<>();

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-22 12:35:55 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-24 15:24:33 +0000
@@ -84,7 +84,8 @@
     }
 
     @Override
-    public void writeDataValueSetJson( Set<DataSet> dataSets, Date completeDate, Period period, OrganisationUnit orgUnit, Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream outputStream )
+    public void writeDataValueSetJson( Set<DataSet> dataSets, Date completeDate, Period period, OrganisationUnit orgUnit, 
+        Set<Period> periods, Set<OrganisationUnit> orgUnits, OutputStream outputStream )
     {
         DataValueSet dataValueSet = new StreamingJsonDataValueSet( outputStream );
 
@@ -96,13 +97,13 @@
     }
 
     @Override
-    public void writeDataValueSetCsv( Set<Period> periods, Set<OrganisationUnit> orgUnits, Writer writer )
+    public void writeDataValueSetCsv( Set<DataSet> dataSets, Set<Period> periods, Set<OrganisationUnit> orgUnits, Writer writer )
     {
         DataValueSet dataValueSet = new StreamingCsvDataValueSet( new CsvWriter( writer, CSV_DELIM ) );
 
-        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet( getDataValueSql( null, periods, orgUnits ) );
+        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet( getDataValueSql( dataSets, periods, orgUnits ) );
 
-        writeDataValueSet( sqlRowSet, null, null, null, null, periods, orgUnits, dataValueSet );
+        writeDataValueSet( sqlRowSet, dataSets, null, null, null, periods, orgUnits, dataValueSet );
     }
 
     @Override

=== 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	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2014-08-24 15:24:33 +0000
@@ -119,7 +119,7 @@
 
             log.info( "Get XML data value set for data set: " + ds + ", period: " + period + ", org unit: " + ou );
 
-            dataValueSetService.writeDataValueSet( ds, period, ou, response.getOutputStream() );
+            dataValueSetService.writeDataValueSetXml( ds, period, ou, response.getOutputStream() );
         }
         else
         {
@@ -127,7 +127,7 @@
 
             Set<String> ous = getOrganisationUnits( orgUnit, children );
             
-            dataValueSetService.writeDataValueSet( dataSet, startDate, endDate, ous, response.getOutputStream() );
+            dataValueSetService.writeDataValueSetXml( dataSet, startDate, endDate, ous, false, response.getOutputStream() );
         }
     }
 
@@ -160,7 +160,7 @@
 
             Set<String> ous = getOrganisationUnits( orgUnit, children );
             
-            dataValueSetService.writeDataValueSetJson( dataSet, startDate, endDate, ous, response.getOutputStream() );
+            dataValueSetService.writeDataValueSetJson( dataSet, startDate, endDate, ous, false, response.getOutputStream() );
         }
     }
 
@@ -178,7 +178,7 @@
         Set<String> ous = getOrganisationUnits( orgUnit, children );
 
         response.setContentType( CONTENT_TYPE_CSV );
-        dataValueSetService.writeDataValueSetCsv( dataSet, startDate, endDate, ous, response.getWriter() );
+        dataValueSetService.writeDataValueSetCsv( dataSet, startDate, endDate, ous, false, response.getWriter() );
     }
 
     private Set<String> getOrganisationUnits( Set<String> orgUnits, boolean children )

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java	2014-08-24 15:24:33 +0000
@@ -43,6 +43,7 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.struts2.ServletActionContext;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -116,18 +117,8 @@
     {
         //TODO reimplement to use web api
         
-        Set<String> orgUnits = new HashSet<>();
-        
-        for ( OrganisationUnit unit : selectionTreeManager.getReloadedSelectedOrganisationUnits() )
-        {
-            Collection<OrganisationUnit> children = organisationUnitService.getOrganisationUnitWithChildren( unit.getId() );
-            
-            for ( OrganisationUnit child : children )
-            {
-                orgUnits.add( child.getUid() );
-            }
-        }
-        
+        Set<String> orgUnits = new HashSet<>( IdentifiableObjectUtils.getUids( selectionTreeManager.getSelectedOrganisationUnits() ) );
+                
         HttpServletResponse response = ServletActionContext.getResponse();
         
         if ( FORMAT_CSV.equals( exportFormat ) )
@@ -136,13 +127,13 @@
             
             Writer writer = new OutputStreamWriter( getZipOut( response, getFileName( EXTENSION_CSV ) ) );
             
-            dataValueSetService.writeDataValueSetCsv( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, writer );
+            dataValueSetService.writeDataValueSetCsv( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, true, writer );
         }
         else
         {
             ContextUtils.configureResponse( response, CONTENT_TYPE_XML, true, getFileName( EXTENSION_XML_ZIP ), true );
             
-            dataValueSetService.writeDataValueSet( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, getZipOut( response, getFileName( EXTENSION_XML ) ) );
+            dataValueSetService.writeDataValueSetXml( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, true, getZipOut( response, getFileName( EXTENSION_XML ) ) );
         }
         
         return SUCCESS;