← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19937: DataValueController, enforcing optional constraints for data values

 

------------------------------------------------------------
revno: 19937
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-09-02 16:05:25 +0200
message:
  DataValueController, enforcing optional constraints for data values
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.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/webapi/controller/DataValueController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java	2015-07-10 05:22:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java	2015-09-02 14:05:25 +0000
@@ -28,6 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.setting.SystemSettingManager.KEY_DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO;
+import static org.hisp.dhis.setting.SystemSettingManager.KEY_DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS;
+import static org.hisp.dhis.setting.SystemSettingManager.KEY_DATA_IMPORT_STRICT_ORGANISATION_UNITS;
+import static org.hisp.dhis.setting.SystemSettingManager.KEY_DATA_IMPORT_STRICT_PERIODS;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import javax.servlet.http.HttpServletResponse;
+
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElement;
@@ -41,6 +52,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.system.util.ValidationUtils;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.webapi.utils.InputUtils;
@@ -53,11 +65,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import javax.servlet.http.HttpServletResponse;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
 /**
  * @author Lars Helge Overland
  */
@@ -88,6 +95,9 @@
 
     @Autowired
     private IdentifiableObjectManager idObjectManager;
+    
+    @Autowired
+    private SystemSettingManager systemSettingManager;
 
     @Autowired
     private InputUtils inputUtils;
@@ -109,6 +119,11 @@
         @RequestParam( required = false ) String comment,
         @RequestParam( required = false ) boolean followUp, HttpServletResponse response ) throws WebMessageException
     {
+        boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting( KEY_DATA_IMPORT_STRICT_PERIODS, false );
+        boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting( KEY_DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS, false );
+        boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting( KEY_DATA_IMPORT_STRICT_ORGANISATION_UNITS, false );
+        boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting( KEY_DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO, false );
+        
         // ---------------------------------------------------------------------
         // Input validation
         // ---------------------------------------------------------------------
@@ -120,27 +135,29 @@
             throw new WebMessageException( WebMessageUtils.conflict( "Illegal data element identifier: " + de ) );
         }
 
-        DataElementCategoryOptionCombo categoryOptionCombo;
-
-        if ( co != null )
-        {
-            categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo( co );
-        }
-        else
-        {
-            categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
-        }
+        DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo( co );
 
         if ( categoryOptionCombo == null )
         {
-            throw new WebMessageException( WebMessageUtils.conflict( "Illegal category option combo identifier: " + co ) );
+            if ( requireCategoryOptionCombo )
+            {
+                throw new WebMessageException( WebMessageUtils.conflict( "Category option combo is required but is not specified" ) );
+            }
+            else if ( co != null )
+            {
+                throw new WebMessageException( WebMessageUtils.conflict( "Illegal category option combo identifier: " + co ) );
+            }
+            else
+            {
+                categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
+            }
         }
 
         DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( cc, cp );
 
         if ( attributeOptionCombo == null )
         {
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Illegal attribute option combo identifier: " + cc + " " + cp ) );
         }
 
         Period period = PeriodType.getPeriodFromIsoString( pe );
@@ -157,36 +174,56 @@
             throw new WebMessageException( WebMessageUtils.conflict( "Illegal organisation unit identifier: " + ou ) );
         }
 
-        boolean isInHierarchy = organisationUnitService.isInUserHierarchy( organisationUnit );
+        boolean inUserHierarchy = organisationUnitService.isInUserHierarchy( organisationUnit );
 
-        if ( !isInHierarchy )
+        if ( !inUserHierarchy )
         {
             throw new WebMessageException( WebMessageUtils.conflict( "Organisation unit is not in the hierarchy of the current user: " + ou ) );
         }
-
-        String valid = ValidationUtils.dataValueIsValid( value, dataElement );
-
-        if ( valid != null )
+        
+        boolean invalidFuturePeriod = period.isFuture() && dataElement.getOpenFuturePeriods() <= 0;
+        
+        if ( invalidFuturePeriod )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( "One or more data sets for data element does not allow future periods: " + de ) );
+        }
+
+        String valueValid = ValidationUtils.dataValueIsValid( value, dataElement );
+
+        if ( valueValid != null )
         {
             throw new WebMessageException( WebMessageUtils.conflict( "Invalid value: " + value + ", must match data element type: " + dataElement.getDetailedType() ) );
         }
 
-        valid = ValidationUtils.commentIsValid( comment );
+        String commentValid = ValidationUtils.commentIsValid( comment );
 
-        if ( valid != null )
+        if ( commentValid != null )
         {
             throw new WebMessageException( WebMessageUtils.conflict( "Invalid comment: " + comment ) );
         }
 
         // ---------------------------------------------------------------------
-        // Future period constraint check //TODO better check
+        // Optional constraints
         // ---------------------------------------------------------------------
 
-        if ( period.isFuture() && dataElement.getOpenFuturePeriods() <= 0 )
-        {
-            throw new WebMessageException( WebMessageUtils.conflict( "One or more data sets for data element does not allow future periods: " + de ) );
-        }
-
+        if ( strictPeriods && !dataElement.getPeriodTypes().contains( period.getPeriodType() ) )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( 
+                "Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid() ) );
+        }
+        
+        if ( strictCategoryOptionCombos && !dataElement.getCategoryCombo().getOptionCombos().contains( categoryOptionCombo ) )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( 
+                "Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid() ) );
+        }
+        
+        if ( strictOrgUnits && !dataElement.hasDataSetOrganisationUnit( organisationUnit ) )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( 
+                "Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid() ) );
+        }
+        
         // ---------------------------------------------------------------------
         // Locking validation
         // ---------------------------------------------------------------------