← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2584: Previous refactoring went to fast (forgot about CategoryOptionCombo), so refactored some more to ...

 

------------------------------------------------------------
revno: 2584
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2011-01-17 11:58:00 +0530
message:
  Previous refactoring went to fast (forgot about CategoryOptionCombo), so refactored some more to make up for it :)
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.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/web/api/resources/MobileResource.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java	2011-01-12 09:13:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java	2011-01-17 06:28:00 +0000
@@ -81,7 +81,7 @@
 
         if ( user == null )
         {
-            throw new NotAllowedException( "NO_USER", "No user is logged in." );
+            throw NotAllowedException.NO_USER;
         }
 
         Collection<OrganisationUnit> units = user.getOrganisationUnits();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java	2010-12-27 03:17:34 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java	2011-01-17 06:28:00 +0000
@@ -300,7 +300,7 @@
 
         if ( programStageInstance == null )
         {
-            throw new NotAllowedException( "INVALID_PROGRAM_STAGE" );
+            throw NotAllowedException.INVALID_PROGRAM_STAGE;
         }
 
         programStageInstance.getProgramStage();
@@ -321,7 +321,7 @@
 
         if ( dataElements.size() != dataElementIds.size() )
         {
-            throw new NotAllowedException( "INVALID_PROGRAM_STAGE" );
+            throw NotAllowedException.INVALID_PROGRAM_STAGE;
         }
 
         Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<Integer, org.hisp.dhis.dataelement.DataElement>();
@@ -329,7 +329,7 @@
         {
             if ( !dataElementIds.contains( dataElement.getId() ) )
             {
-                throw new NotAllowedException( "INVALID_PROGRAM_STAGE" );
+                throw NotAllowedException.INVALID_PROGRAM_STAGE;
             }
             dataElementMap.put( dataElement.getId(), dataElement );
         }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java	2011-01-12 09:13:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java	2011-01-17 06:28:00 +0000
@@ -41,6 +41,11 @@
 
     public DataSet getDataSetForLocale( int dataSetId, Locale locale );
 
+    /** Save {@link DataSetValue} to given {@link OrganisationUnit}
+     * @param unit - the Organisation unit to save to
+     * @param dataSetValue - the data set value to save
+     * @throws NotAllowedException if saving is not allowed
+     */
     public void saveDataSetValues( OrganisationUnit unit, DataSetValue dataSetValue )
         throws NotAllowedException;
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java	2011-01-14 16:18:27 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java	2011-01-17 06:28:00 +0000
@@ -204,63 +204,61 @@
 
         org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetValue.getId() );
 
-        if ( !dataSetService.getDataSetsBySource( unit ).contains( dataSet ) )
-        {
-            throw new NotAllowedException( "INVALID_DATASET_ASSOCIATION" );
-        }
-
-        Period selectedPeriod = getPeriod( dataSetValue.getPeriodName(), dataSet.getPeriodType() );
-
-        if ( selectedPeriod == null )
-        {
-            throw new NotAllowedException( "INVALID_PERIOD" );
-        }
-
-        if ( isDataSetLocked( unit, dataSet, selectedPeriod ) )
-        {
-            throw new NotAllowedException( "DATASET_LOCKED" );
-        }
-
+        if ( !dataSetAssociatedWithOrgUnit( unit, dataSet ) )
+        {
+            throw NotAllowedException.INVALID_DATASET_ASSOCIATION;
+        }
+
+        Period period = getPeriod( dataSetValue.getPeriodName(), dataSet.getPeriodType() );
+
+        if ( period == null )
+        {
+            throw NotAllowedException.INVALID_PERIOD;
+        }
+
+        if ( dataSetLocked( unit, dataSet, period ) )
+        {
+            throw NotAllowedException.DATASET_LOCKED;
+        }
+
+        Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = getDataElementIdMapping( dataSet );
+
+        for ( DataValue dataValue : dataSetValue.getDataValues() )
+        {
+            org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get( dataValue.getId() );
+
+            if ( dataElement == null )
+            {
+                log.info( "Data value submitted for data element " + dataValue.getId() + ", that is not in data set '"
+                    + dataSet.getName() + "'" );
+                continue;
+            }
+
+            if ( emptyString( dataValue.getValue() ) )
+            {
+                log.info( "Empty data value for data element " + dataValue.getId() + " not saved" );
+                continue;
+            }
+
+            saveValue(unit, period, dataElement, dataValue);
+
+        }
+    }
+
+    private Map<Integer, org.hisp.dhis.dataelement.DataElement> getDataElementIdMapping(
+        org.hisp.dhis.dataset.DataSet dataSet )
+    {
         Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<Integer, org.hisp.dhis.dataelement.DataElement>();
         for ( org.hisp.dhis.dataelement.DataElement dataElement : dataSet.getDataElements() )
         {
             dataElementMap.put( dataElement.getId(), dataElement );
         }
-
-        Set<Integer> handled = new HashSet<Integer>();
-
-        for ( DataValue dv : dataSetValue.getDataValues() )
-        {
-            int elementId = dv.getId();
-
-            if ( handled.contains( elementId ) )
-            {
-                log.info( "Multiple values for element " + elementId + " submitted. Not handling this value." );
-                continue;
-            }
-
-            org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get( elementId );
-
-            if ( dataElement == null )
-            {
-                log.info( "Data element value submitted for data element " + elementId + ", that is not in data set '"
-                    + dataSet.getName() + "'" );
-                handled.add( elementId );
-                continue;
-            }
-
-            if ( emptyString( dv.getValue() ) )
-            {
-                log.info( "Empty data value for data element " + elementId + " not saved" );
-                handled.add( elementId );
-                continue;
-            }
-
-            saveValue(unit, selectedPeriod, dataElement, dv);
-
-            handled.add( elementId );
-        }
-        reportMissingValues( dataSet, handled );
+        return dataElementMap;
+    }
+
+    private boolean dataSetAssociatedWithOrgUnit( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet )
+    {
+        return dataSetService.getDataSetsBySource( unit ).contains( dataSet );
     }
 
     private void saveValue(OrganisationUnit unit, Period period, org.hisp.dhis.dataelement.DataElement dataElement, DataValue dv) {
@@ -288,24 +286,11 @@
 
     }
     
-    private void reportMissingValues( org.hisp.dhis.dataset.DataSet dataSet, Set<Integer> handled )
-    {
-        Collection<org.hisp.dhis.dataelement.DataElement> dataElements = dataSet.getDataElements();
-        for ( org.hisp.dhis.dataelement.DataElement element : dataElements )
-        {
-            if ( !handled.contains( element.getId() ) )
-            {
-                log.info( "Submitted values for dataset '" + dataSet.getName() + "' missing data element '" + element.getName() + "'" );
-
-            }
-        }
-    }
-
     // -------------------------------------------------------------------------
     // Supportive method
     // -------------------------------------------------------------------------
 
-    private boolean isDataSetLocked( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet, Period selectedPeriod )
+    private boolean dataSetLocked( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet, Period selectedPeriod )
     {
         if ( dataSetLockService.getDataSetLockByDataSetPeriodAndSource( dataSet, selectedPeriod, unit ) != null )
             return true;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java	2011-01-12 09:13:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java	2011-01-17 06:28:00 +0000
@@ -31,14 +31,25 @@
     extends Exception
 {
 
+    public static final NotAllowedException INVALID_PROGRAM_STAGE = new NotAllowedException( "INVALID_PROGRAM_STAGE" );
+
+    public static final NotAllowedException INVALID_DATASET_ASSOCIATION = new NotAllowedException( "INVALID_DATASET_ASSOCIATION" );
+
+    public static final NotAllowedException INVALID_PERIOD = new NotAllowedException( "INVALID_PERIOD" );
+
+    public static final NotAllowedException DATASET_LOCKED = new NotAllowedException( "DATASET_LOCKED" );
+
+    public static final NotAllowedException NO_USER = new NotAllowedException( "NO_USER", "No user is logged in." );
+    
+    
     private String reason;
 
-    public NotAllowedException( String reason )
+    private NotAllowedException( String reason )
     {
         this.reason = reason;
     }
 
-    public NotAllowedException( String reason, String message )
+    private NotAllowedException( String reason, String message )
     {
         super( message );
         this.reason = reason;