← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5042: (mobile) wip, proper validation of data entry type (server side)

 

------------------------------------------------------------
revno: 5042
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-10-26 13:49:43 +0200
message:
  (mobile) wip, proper validation of data entry type (server side)
modified:
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java
  dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm


--
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-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java	2011-10-26 08:43:56 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java	2011-10-26 11:49:43 +0000
@@ -278,36 +278,103 @@
                     .getDataValue( organisationUnit, dataElement, period, optionCombo );
 
                 value = value.trim();
-
-                if ( value == null || value.length() == 0 || !SectionFormUtils.isInteger( value ) )
+                Boolean valueIsEmpty = (value == null || value.length() == 0);
+
+                // validate types
+                Boolean correctType = true;
+                String type = dataElement.getType();
+                String numberType = dataElement.getNumberType();
+
+                if ( !valueIsEmpty )
+                {
+                    if ( type.equals( DataElement.VALUE_TYPE_STRING ) )
+                    {
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_BOOL ) )
+                    {
+                        if ( !valueIsEmpty && !SectionFormUtils.isBoolean( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid boolean" );
+                        }
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_DATE ) )
+                    {
+                        if ( !SectionFormUtils.isDate( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid date (YYYY-MM-DD)" );
+                        }
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_INT )
+                        && numberType.equals( DataElement.VALUE_TYPE_NUMBER ) )
+                    {
+                        if ( !SectionFormUtils.isNumber( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid number" );
+                        }
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_INT )
+                        && numberType.equals( DataElement.VALUE_TYPE_INT ) )
+                    {
+                        if ( !SectionFormUtils.isInteger( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid integer" );
+                        }
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_INT )
+                        && numberType.equals( DataElement.VALUE_TYPE_POSITIVE_INT ) )
+                    {
+                        if ( !SectionFormUtils.isPositiveInteger( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid positive integer" );
+                        }
+                    }
+                    else if ( type.equals( DataElement.VALUE_TYPE_INT )
+                        && numberType.equals( DataElement.VALUE_TYPE_NEGATIVE_INT ) )
+                    {
+                        if ( !SectionFormUtils.isNegativeInteger( value ) )
+                        {
+                            correctType = false;
+                            typeViolations.put( key, "Invalid negative integer" );
+                        }
+                    }
+                }
+
+                // nothing entered
+                if ( valueIsEmpty || !correctType )
                 {
                     if ( dataValue != null )
                     {
                         dataValueService.deleteDataValue( dataValue );
                     }
-
-                    continue;
-                }
-
-                if ( dataValue == null )
-                {
-                    needsValidation = true;
-
-                    dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(),
-                        null, optionCombo );
-                    dataValueService.addDataValue( dataValue );
-                }
-                else
-                {
-                    if ( !dataValue.getValue().equals( value ) )
+                }
+
+                if ( correctType && !valueIsEmpty )
+                {
+                    if ( dataValue == null )
                     {
                         needsValidation = true;
 
-                        dataValue.setValue( value );
-                        dataValue.setTimestamp( new Date() );
-                        dataValue.setStoredBy( storedBy );
-
-                        dataValueService.updateDataValue( dataValue );
+                        dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(),
+                            null, optionCombo );
+                        dataValueService.addDataValue( dataValue );
+                    }
+                    else
+                    {
+                        if ( !dataValue.getValue().equals( value ) )
+                        {
+                            needsValidation = true;
+
+                            dataValue.setValue( value );
+                            dataValue.setTimestamp( new Date() );
+                            dataValue.setStoredBy( storedBy );
+
+                            dataValueService.updateDataValue( dataValue );
+                        }
                     }
                 }
             }
@@ -337,7 +404,8 @@
 
         validationRuleViolations = sectionFormUtils.getValidationRuleViolations( organisationUnit, dataSet, period );
 
-        if ( needsValidation && (validationViolations.size() > 0 || validationRuleViolations.size() > 0) )
+        if ( needsValidation
+            && (!validationViolations.isEmpty() || !validationRuleViolations.isEmpty() || !typeViolations.isEmpty()) )
         {
             return ERROR;
         }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java	2011-10-24 11:36:21 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java	2011-10-26 11:49:43 +0000
@@ -55,6 +55,8 @@
 import org.hisp.dhis.validation.ValidationResult;
 import org.hisp.dhis.validation.ValidationRule;
 import org.hisp.dhis.validation.ValidationRuleService;
+import org.joda.time.format.DateTimeFormatter;
+import org.joda.time.format.ISODateTimeFormat;
 
 /**
  * @author mortenoh
@@ -219,6 +221,20 @@
     // Static Utils
     // -------------------------------------------------------------------------
 
+    public static boolean isNumber( String value )
+    {
+        try
+        {
+            Double.parseDouble( value );
+        }
+        catch ( NumberFormatException e )
+        {
+            return false;
+        }
+
+        return true;
+    }
+
     public static boolean isInteger( String value )
     {
         try
@@ -233,6 +249,16 @@
         return true;
     }
 
+    public static boolean isPositiveInteger( String value )
+    {
+        return valueHigher( value, 0 );
+    }
+
+    public static boolean isNegativeInteger( String value )
+    {
+        return valueLower( value, 0 );
+    }
+
     public static boolean valueHigher( String value, int max )
     {
         int integerValue;
@@ -272,4 +298,25 @@
 
         return false;
     }
+
+    public static boolean isBoolean( String value )
+    {
+        return value.equals( "true" ) || value.equals( "false" );
+    }
+
+    public static boolean isDate( String value )
+    {
+        DateTimeFormatter sdf = ISODateTimeFormat.yearMonthDay();
+
+        try
+        {
+            sdf.parseDateTime( value );
+            return true;
+        }
+        catch ( IllegalArgumentException e )
+        {
+        }
+
+        return false;
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm'
--- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm	2011-10-26 08:43:56 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm	2011-10-26 11:49:43 +0000
@@ -42,23 +42,29 @@
 	                #end
                 #end
 
+				#if( $typeViolations.get( $key ) )
+					#set( $typeViolation = $typeViolations.get( $key ) )
+                    <br /><span style="color: #990000;">$typeViolation</span>
+                #end
+
 				#if( $dataElement.type == "string" )
-					<input type="text" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="text" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#elseif( $dataElement.type == "bool" )
 					<select name="$key" style="width: 100%;">
-						<option value="true">Yes</option>
-						<option value="false">No</option>
+						<option value="">[ No Value ]</option>
+						<option value="true" #if($!dataValues.get($key) == "true")selected="selected"#end>Yes</option>
+						<option value="false" #if($!dataValues.get($key) == "false")selected="selected"#end>No</option>
 					</select>
 				#elseif( $dataElement.type == "date" )
-					<input type="date" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="date" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#elseif( $dataElement.type == "int" && $dataElement.numberType == "number" )
-					<input type="text" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="text" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#elseif( $dataElement.type == "int" && $dataElement.numberType == "int" )
-					<input type="number" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="number" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#elseif( $dataElement.type == "int" && $dataElement.numberType == "positiveNumber" )
-					<input type="number" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="number" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#elseif( $dataElement.type == "int" && $dataElement.numberType == "negativeNumber" )
-					<input type="number" size="24" name="$key" value="$!dataValues.get($key)" />
+					<input type="number" maxlength="255" size="24" name="$key" value="$!dataValues.get($key)" />
 				#end
 			#end
 		#end