← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14202: dataValues: if value is null, and type is yes-only, delete dataValue if comment is also null, els...

 

------------------------------------------------------------
revno: 14202
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-03-13 11:12:09 +0100
message:
  dataValues: if value is null, and type is yes-only, delete dataValue if comment is also null, else set value to false
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/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/api/controller/DataValueController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.java	2014-02-12 16:59:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.java	2014-03-13 10:12:09 +0000
@@ -28,12 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-import javax.servlet.http.HttpServletResponse;
-
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.InputUtils;
@@ -58,6 +52,11 @@
 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
  */
@@ -84,27 +83,27 @@
 
     @Autowired
     private DataSetService dataSetService;
-    
+
     @Autowired
     private InputUtils inputUtils;
 
     @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
     @RequestMapping( method = RequestMethod.POST, produces = "text/plain" )
-    public void saveDataValue( 
-        @RequestParam String de, 
-        @RequestParam( required = false ) String co, 
-        @RequestParam( required = false ) String cc, 
-        @RequestParam( required = false ) String cp, 
-        @RequestParam String pe, 
-        @RequestParam String ou, 
-        @RequestParam( required = false ) String value, 
-        @RequestParam( required = false ) String comment, 
+    public void saveDataValue(
+        @RequestParam String de,
+        @RequestParam( required = false ) String co,
+        @RequestParam( required = false ) String cc,
+        @RequestParam( required = false ) String cp,
+        @RequestParam String pe,
+        @RequestParam String ou,
+        @RequestParam( required = false ) String value,
+        @RequestParam( required = false ) String comment,
         @RequestParam( required = false ) boolean followUp, HttpServletResponse response )
     {
         // ---------------------------------------------------------------------
         // Input validation
         // ---------------------------------------------------------------------
-        
+
         DataElement dataElement = dataElementService.getDataElement( de );
 
         if ( dataElement == null )
@@ -131,12 +130,12 @@
         }
 
         DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( response, cc, cp );
-        
+
         if ( attributeOptionCombo == null )
         {
             return;
         }
-        
+
         Period period = PeriodType.getPeriodFromIsoString( pe );
 
         if ( period == null )
@@ -152,9 +151,9 @@
             ContextUtils.conflictResponse( response, "Illegal organisation unit identifier: " + ou );
             return;
         }
-        
+
         boolean isInHierarchy = organisationUnitService.isInUserHierarchy( organisationUnit );
-        
+
         if ( !isInHierarchy )
         {
             ContextUtils.conflictResponse( response, "Organisation unit is not in the hierarchy of the current user: " + ou );
@@ -190,7 +189,7 @@
         // ---------------------------------------------------------------------
         // Assemble and save data value
         // ---------------------------------------------------------------------
-        
+
         String storedBy = currentUserService.getCurrentUsername();
 
         Date now = new Date();
@@ -199,7 +198,7 @@
 
         if ( dataValue == null )
         {
-            dataValue = new DataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, 
+            dataValue = new DataValue( dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo,
                 null, storedBy, now, null );
 
             if ( value != null )
@@ -216,6 +215,19 @@
         }
         else
         {
+            if ( value == null && DataElement.VALUE_TYPE_TRUE_ONLY.equals( dataElement.getType() ) )
+            {
+                if ( comment == null )
+                {
+                    dataValueService.deleteDataValue( dataValue );
+                    return;
+                }
+                else
+                {
+                    value = "false";
+                }
+            }
+
             if ( value != null )
             {
                 dataValue.setValue( StringUtils.trimToNull( value ) );
@@ -237,21 +249,21 @@
             dataValueService.updateDataValue( dataValue );
         }
     }
-    
+
     @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_DELETE')" )
     @RequestMapping( method = RequestMethod.DELETE, produces = "text/plain" )
-    public void deleteDataValue( 
-        @RequestParam String de, 
-        @RequestParam( required = false ) String co, 
-        @RequestParam( required = false ) String cc, 
-        @RequestParam( required = false ) String cp, 
-        @RequestParam String pe, 
+    public void deleteDataValue(
+        @RequestParam String de,
+        @RequestParam( required = false ) String co,
+        @RequestParam( required = false ) String cc,
+        @RequestParam( required = false ) String cp,
+        @RequestParam String pe,
         @RequestParam String ou, HttpServletResponse response )
     {
         // ---------------------------------------------------------------------
         // Input validation
         // ---------------------------------------------------------------------
-        
+
         DataElement dataElement = dataElementService.getDataElement( de );
 
         if ( dataElement == null )
@@ -278,12 +290,12 @@
         }
 
         DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( response, cc, cp );
-        
+
         if ( attributeOptionCombo == null )
         {
             return;
         }
-        
+
         Period period = PeriodType.getPeriodFromIsoString( pe );
 
         if ( period == null )
@@ -299,9 +311,9 @@
             ContextUtils.conflictResponse( response, "Illegal organisation unit identifier: " + ou );
             return;
         }
-        
+
         boolean isInHierarchy = organisationUnitService.isInUserHierarchy( organisationUnit );
-        
+
         if ( !isInHierarchy )
         {
             ContextUtils.conflictResponse( response, "Organisation unit is not in the hierarchy of the current user: " + ou );
@@ -329,24 +341,24 @@
             ContextUtils.conflictResponse( response, "Data value cannot be deleted because it does not exist" );
             return;
         }
-        
+
         dataValueService.deleteDataValue( dataValue );
     }
 
     @RequestMapping( method = RequestMethod.GET )
     public String getDataValue(
-        @RequestParam String de, 
-        @RequestParam( required = false ) String co, 
-        @RequestParam( required = false ) String cc, 
-        @RequestParam( required = false ) String cp, 
-        @RequestParam String pe, 
-        @RequestParam String ou, 
+        @RequestParam String de,
+        @RequestParam( required = false ) String co,
+        @RequestParam( required = false ) String cc,
+        @RequestParam( required = false ) String cp,
+        @RequestParam String pe,
+        @RequestParam String ou,
         Model model, HttpServletResponse response )
     {
         // ---------------------------------------------------------------------
         // Input validation
         // ---------------------------------------------------------------------
-        
+
         DataElement dataElement = dataElementService.getDataElement( de );
 
         if ( dataElement == null )
@@ -373,12 +385,12 @@
         }
 
         DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( response, cc, cp );
-        
+
         if ( attributeOptionCombo == null )
         {
             return null;
         }
-        
+
         Period period = PeriodType.getPeriodFromIsoString( pe );
 
         if ( period == null )
@@ -394,9 +406,9 @@
             ContextUtils.conflictResponse( response, "Illegal organisation unit identifier: " + ou );
             return null;
         }
-        
+
         boolean isInHierarchy = organisationUnitService.isInUserHierarchy( organisationUnit );
-        
+
         if ( !isInHierarchy )
         {
             ContextUtils.conflictResponse( response, "Organisation unit is not in the hierarchy of the current user: " + ou );
@@ -424,12 +436,12 @@
             ContextUtils.conflictResponse( response, "Data value does not exist" );
             return null;
         }
-        
+
         List<String> value = new ArrayList<String>();
         value.add( dataValue.getValue() );
 
         model.addAttribute( "model", value );
-        
+
         return "value";
     }
 }