← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15812: Fixed bug - For program validation, Numbers are being treated as strings.

 

------------------------------------------------------------
revno: 15812
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-06-23 21:01:57 +0800
message:
  Fixed bug - For program validation, Numbers are being treated as strings.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramExpressionService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramValidationService.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-api/src/main/java/org/hisp/dhis/program/ProgramExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramExpressionService.java	2014-05-23 15:28:46 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramExpressionService.java	2014-06-23 13:01:57 +0000
@@ -31,6 +31,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue;
 
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -102,5 +103,13 @@
      * @return the description of an expression
      */
     String getExpressionDescription( String programExpression );
+    
+    /**
+     * Get the Data Element collection of a program expression
+     *
+     * @param programExpression The expression
+     * @return the DataElement collection
+     */
+    Collection<DataElement> getDataElements( String programExpression );
 
 }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2014-05-16 15:26:10 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2014-06-23 13:01:57 +0000
@@ -32,6 +32,8 @@
 import static org.hisp.dhis.program.ProgramExpression.SEPARATOR_ID;
 import static org.hisp.dhis.program.ProgramExpression.SEPARATOR_OBJECT;
 
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -51,8 +53,8 @@
 public class DefaultProgramExpressionService
     implements ProgramExpressionService
 {
-    private static final String regExp = "\\[" + OBJECT_PROGRAM_STAGE_DATAELEMENT + SEPARATOR_OBJECT + "([a-zA-Z0-9\\- ]+["
-        + SEPARATOR_ID + "[a-zA-Z0-9\\- ]+]*)" + "\\]";
+    private static final String regExp = "\\[" + OBJECT_PROGRAM_STAGE_DATAELEMENT + SEPARATOR_OBJECT
+        + "([a-zA-Z0-9\\- ]+[" + SEPARATOR_ID + "[a-zA-Z0-9\\- ]+]*)" + "\\]";
 
     private static final String INVALID_CONDITION = "Invalid condition";
 
@@ -183,4 +185,28 @@
 
         return description.toString();
     }
+
+    @Override
+    public Collection<DataElement> getDataElements( String programExpression )
+    {
+        Collection<DataElement> dataElements = new HashSet<DataElement>();
+
+        Pattern pattern = Pattern.compile( regExp );
+        Matcher matcher = pattern.matcher( programExpression );
+        while ( matcher.find() )
+        {
+            String match = matcher.group();
+            match = match.replaceAll( "[\\[\\]]", "" );
+
+            String[] info = match.split( SEPARATOR_OBJECT );
+            String[] ids = info[1].split( SEPARATOR_ID );
+
+            int dataElementId = Integer.parseInt( ids[1] );
+            DataElement dataElement = dataElementService.getDataElement( dataElementId );
+            dataElements.add( dataElement );
+        }
+
+        return dataElements;
+    }
+    
 }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramValidationService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramValidationService.java	2014-05-16 15:26:10 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramValidationService.java	2014-06-23 13:01:57 +0000
@@ -41,6 +41,7 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueService;
 import org.springframework.transaction.annotation.Transactional;
@@ -152,17 +153,35 @@
                 programStageInstance, entityInstanceDataValueMap );
             String operator = validate.getOperator().getMathematicalOperator();
 
-            if ( (leftSideValue != null && rightSideValue != null && !((operator.equals( "==" ) && leftSideValue
-                .compareTo( rightSideValue ) == 0)
-                || (operator.equals( "<" ) && leftSideValue.compareTo( rightSideValue ) < 0)
-                || (operator.equals( "<=" ) && (leftSideValue.compareTo( rightSideValue ) <= 0))
-                || (operator.equals( ">" ) && leftSideValue.compareTo( rightSideValue ) > 0)
-                || (operator.equals( ">=" ) && leftSideValue.compareTo( rightSideValue ) >= 0) || (operator
-                .equals( "!=" ) && leftSideValue.compareTo( rightSideValue ) == 0))) )
+            if ( leftSideValue != null && rightSideValue != null )
             {
-                ProgramValidationResult validationResult = new ProgramValidationResult( programStageInstance, validate,
-                    leftSideValue, rightSideValue );
-                result.add( validationResult );
+                String expression = validate.getLeftSide().getExpression() + " " + validate.getRightSide().getExpression();
+                if ( isNumberDataExpression( expression ) )
+                {
+                    int leftSide = Integer.parseInt( leftSideValue );
+                    int rightSide = Integer.parseInt( rightSideValue );
+                    if ( !((operator.equals( "==" ) && leftSide == rightSide)
+                        || (operator.equals( "<" ) && leftSide < rightSide)
+                        || (operator.equals( "<=" ) && leftSide <= rightSide)
+                        || (operator.equals( ">" ) && leftSide > rightSide)
+                        || (operator.equals( ">=" ) && leftSide >= rightSide) || (operator.equals( "!=" ) && leftSide != rightSide)) )
+                    {
+                        ProgramValidationResult validationResult = new ProgramValidationResult( programStageInstance,
+                            validate, leftSideValue, rightSideValue );
+                        result.add( validationResult );
+                    }
+                }
+                else if ( !((operator.equals( "==" ) && leftSideValue.compareTo( rightSideValue ) == 0)
+                    || (operator.equals( "<" ) && leftSideValue.compareTo( rightSideValue ) < 0)
+                    || (operator.equals( "<=" ) && (leftSideValue.compareTo( rightSideValue ) <= 0))
+                    || (operator.equals( ">" ) && leftSideValue.compareTo( rightSideValue ) > 0)
+                    || (operator.equals( ">=" ) && leftSideValue.compareTo( rightSideValue ) >= 0) || (operator
+                    .equals( "!=" ) && leftSideValue.compareTo( rightSideValue ) == 0)) )
+                {
+                    ProgramValidationResult validationResult = new ProgramValidationResult( programStageInstance,
+                        validate, leftSideValue, rightSideValue );
+                    result.add( validationResult );
+                }
             }
         }
 
@@ -215,5 +234,23 @@
         }
 
         return programValidation;
+    }  
+    
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private boolean isNumberDataExpression( String programExpression )
+    {
+        Collection<DataElement> dataElements = expressionService.getDataElements( programExpression );
+        
+        for ( DataElement dataElement : dataElements )
+        {
+            if ( dataElement.getType().equals( DataElement.VALUE_TYPE_INT ) )
+            {
+                return true;
+            }
+        }
+        return false;
     }
 }