← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15808: Data value sets resource, better input validation

 

------------------------------------------------------------
revno: 15808
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-06-23 13:10:21 +0200
message:
  Data value sets resource, better input validation
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.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/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-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2014-04-29 09:26:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2014-06-23 11:10:21 +0000
@@ -305,7 +305,11 @@
 
     public Collection<OrganisationUnit> getOrganisationUnitsWithChildren( String uid )
     {
-        return getOrganisationUnitWithChildren( getOrganisationUnit( uid ).getId() );
+        OrganisationUnit unit = getOrganisationUnit( uid );
+        
+        int id = unit != null ? unit.getId() : -1;
+        
+        return getOrganisationUnitWithChildren( id );
     }
 
     public Collection<OrganisationUnit> getOrganisationUnitWithChildren( int id )

=== 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-06 09:06:24 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-06-23 11:10:21 +0000
@@ -103,6 +103,7 @@
     private static final String ERROR_INVALID_DATA_SET = "Invalid data set: ";
     private static final String ERROR_INVALID_PERIOD = "Invalid period: ";
     private static final String ERROR_INVALID_ORG_UNIT = "Invalid org unit: ";
+    private static final String ERROR_INVALID_START_END_DATE = "Invalid start and/or end date: ";
     private static final String ERROR_OBJECT_NEEDED_TO_COMPLETE = "Must be provided to complete data set";
 
     @Autowired
@@ -139,6 +140,7 @@
     // DataValueSet implementation
     //--------------------------------------------------------------------------
 
+    @Override
     public void writeDataValueSet( String dataSet, String period, String orgUnit, OutputStream out )
     {
         DataSet dataSet_ = dataSetService.getDataSet( dataSet );
@@ -169,18 +171,6 @@
         dataValueSetStore.writeDataValueSetXml( dataSet_, completeDate, period_, orgUnit_, dataSet_.getDataElements(), wrap( period_ ), wrap( orgUnit_ ), out );
     }
 
-    public void writeDataValueSet( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, OutputStream out )
-    {
-        Set<Period> periods = new HashSet<Period>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
-
-        if ( periods.isEmpty() )
-        {
-            throw new IllegalArgumentException( "At least one period must be specified" );
-        }
-
-        dataValueSetStore.writeDataValueSetXml( null, null, null, null, getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), out );
-    }
-
     @Override
     public void writeDataValueSetJson( String dataSet, String period, String orgUnit, OutputStream outputStream )
     {
@@ -213,31 +203,23 @@
     }
 
     @Override
+    public void writeDataValueSet( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, OutputStream out )
+    {
+        dataValueSetStore.writeDataValueSetXml( null, null, null, null, getDataElements( dataSets ), getPeriods( startDate, endDate ), getOrgUnits( orgUnits ), out );
+    }
+
+    @Override
     public void writeDataValueSetJson( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, OutputStream outputStream )
     {
-        Set<Period> periods = new HashSet<Period>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
-
-        if ( periods.isEmpty() )
-        {
-            throw new IllegalArgumentException( "At least one period must be specified" );
-        }
-
-        dataValueSetStore.writeDataValueSetJson( null, null, null, null, getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), outputStream );
+        dataValueSetStore.writeDataValueSetJson( null, null, null, null, getDataElements( dataSets ), getPeriods( startDate, endDate ), getOrgUnits( orgUnits ), outputStream );
     }
 
+    @Override
     public void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, Writer writer )
     {
-        Set<Period> periods = new HashSet<Period>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
-
-        if ( periods.isEmpty() )
-        {
-            throw new IllegalArgumentException( "At least one period must be specified" );
-        }
-
-        dataValueSetStore.writeDataValueSetCsv( getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), writer );
+        dataValueSetStore.writeDataValueSetCsv( getDataElements( dataSets ), getPeriods( startDate, endDate ), getOrgUnits( orgUnits ), writer );
     }
-
-
+    
     @Override
     public RootNode getDataValueSetTemplate( DataSet dataSet, Period period, List<String> orgUnits,
         boolean writeComments, String ouScheme, String deScheme )
@@ -665,6 +647,23 @@
         return dataElements;
     }
 
+    private Set<Period> getPeriods( Date startDate, Date endDate )
+    {
+        if ( startDate == null || endDate == null || endDate.before( startDate ) )
+        {
+            throw new IllegalArgumentException( ERROR_INVALID_START_END_DATE + startDate + ", " + endDate );
+        }
+        
+        Set<Period> periods = new HashSet<Period>( periodService.getPeriodsBetweenDates( startDate, endDate ) );
+
+        if ( periods.isEmpty() )
+        {
+            throw new IllegalArgumentException( "No periods exist for start/end date: " + startDate + ", " + endDate );
+        }
+        
+        return periods;
+    }
+
     private Set<OrganisationUnit> getOrgUnits( Set<String> orgUnits )
     {
         Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();

=== 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-05-22 12:40:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2014-06-23 11:10:21 +0000
@@ -126,6 +126,7 @@
             log.info( "Get XML bulk data value set for start date: " + startDate + ", end date: " + endDate );
 
             Set<String> ous = getOrganisationUnits( orgUnit, children );
+            
             dataValueSetService.writeDataValueSet( dataSet, startDate, endDate, ous, response.getOutputStream() );
         }
     }
@@ -158,6 +159,7 @@
             log.info( "Get JSON bulk data value set for start date: " + startDate + ", end date: " + endDate );
 
             Set<String> ous = getOrganisationUnits( orgUnit, children );
+            
             dataValueSetService.writeDataValueSetJson( dataSet, startDate, endDate, ous, response.getOutputStream() );
         }
     }