← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18152: Not allow to add/update 'program validation' if the formula is invalid.

 

------------------------------------------------------------
revno: 18152
committer: Tran Chau<tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-02-02 20:58:48 +0700
message:
  Not allow to add/update 'program validation' if the formula is invalid.
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-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/validation/GetProgramExpressionDescriptionAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/javascript/programValidation.js


--
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	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramExpressionService.java	2015-02-02 13:58:48 +0000
@@ -53,6 +53,8 @@
 {
     String ID = ProgramExpressionService.class.getName();
 
+    public static final String INVALID_CONDITION = "Expression is not well-formed";
+    
     /**
      * Adds an {@link 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	2015-01-27 13:45:40 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2015-02-02 13:58:48 +0000
@@ -29,6 +29,9 @@
  */
 
 import static org.hisp.dhis.program.ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT;
+import static org.hisp.dhis.program.ProgramExpression.OBJECT_PROGRAM_STAGE;
+import static org.hisp.dhis.program.ProgramExpression.REPORT_DATE;
+import static org.hisp.dhis.program.ProgramExpression.DUE_DATE;
 import static org.hisp.dhis.program.ProgramExpression.SEPARATOR_ID;
 import static org.hisp.dhis.program.ProgramExpression.SEPARATOR_OBJECT;
 
@@ -53,10 +56,9 @@
 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 INVALID_CONDITION = "Invalid condition";
+    private static final String regExp = "\\[(" + OBJECT_PROGRAM_STAGE_DATAELEMENT + "|" + OBJECT_PROGRAM_STAGE + ")"
+        + SEPARATOR_OBJECT + "([a-zA-Z0-9\\- ]+[" + SEPARATOR_ID + "([a-zA-Z0-9\\- ]|" + DUE_DATE + "|" + REPORT_DATE
+        + ")+]*)\\]";
 
     // -------------------------------------------------------------------------
     // Dependencies
@@ -134,9 +136,7 @@
             {
                 String key = matcher.group().replaceAll( "[\\[\\]]", "" ).split( SEPARATOR_OBJECT )[1];
 
-System.err.println("\n\n\n ==== key : " + key );
                 String dataValue = dataValueMap.get( key );
-System.err.println("\n\n\n ==== dataValue : " + dataValue );
                 if ( dataValue == null )
                 {
                     return null;
@@ -160,28 +160,51 @@
 
         Pattern pattern = Pattern.compile( regExp );
         Matcher matcher = pattern.matcher( programExpression );
+        int countFormula = 0;
         while ( matcher.find() )
         {
+            countFormula++;
+            
             String match = matcher.group();
+            String key = matcher.group(1);
             match = match.replaceAll( "[\\[\\]]", "" );
 
             String[] info = match.split( SEPARATOR_OBJECT );
             String[] ids = info[1].split( SEPARATOR_ID );
 
             ProgramStage programStage = programStageService.getProgramStage( ids[0] );
-            DataElement dataElement = dataElementService.getDataElement( ids[1] );
-
-            if ( programStage == null || dataElement == null )
+            String name = ids[1];
+            
+            if ( programStage == null )
             {
                 return INVALID_CONDITION;
             }
+            else if ( !name.equals( DUE_DATE ) && !name.equals( REPORT_DATE )  )
+            {
+                DataElement dataElement = dataElementService.getDataElement( name );
+                if( dataElement == null )
+                {
+                    return INVALID_CONDITION;
+                }
+                else
+                {
+                    name = dataElement.getDisplayName();
+                }
+            }
 
             matcher.appendReplacement( description,
-                "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT
-                    + programStage.getDisplayName() + SEPARATOR_ID + dataElement.getName() + "]" );
+                "[" + key + ProgramExpression.SEPARATOR_OBJECT
+                    + programStage.getDisplayName() + SEPARATOR_ID + name + "]" );
         }
 
-        matcher.appendTail( description );
+        StringBuffer tail = new StringBuffer();
+        matcher.appendTail( tail );
+        
+        if( countFormula > 1 || !tail.toString().isEmpty() || ( countFormula == 0 && !tail.toString().isEmpty() ) )
+        {
+            return INVALID_CONDITION;
+        }
+        
 
         return description.toString();
     }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/validation/GetProgramExpressionDescriptionAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/validation/GetProgramExpressionDescriptionAction.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/validation/GetProgramExpressionDescriptionAction.java	2015-02-02 13:58:48 +0000
@@ -62,11 +62,16 @@
         this.programExpression = programExpression;
     }
 
-    private String description;
-
-    public String getDescription()
-    {
-        return description;
+    private String message;
+
+    public String getMessage()
+    {
+        return message;
+    }
+
+    public void setMessage( String message )
+    {
+        this.message = message;
     }
 
     // -------------------------------------------------------------------------
@@ -77,7 +82,12 @@
     public String execute()
         throws Exception
     {
-        description = programExpressionService.getExpressionDescription( programExpression );
+        message = programExpressionService.getExpressionDescription( programExpression );
+
+        if ( message.equals( ProgramExpressionService.INVALID_CONDITION ) )
+        {
+            return ERROR;
+        }
 
         return SUCCESS;
     }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/struts.xml	2014-10-06 12:00:42 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/struts.xml	2015-02-02 13:58:48 +0000
@@ -959,7 +959,9 @@
     <action name="getProgramExpressionDescription"
       class="org.hisp.dhis.trackedentity.action.validation.GetProgramExpressionDescriptionAction">
       <result name="success" type="velocity">
-        /dhis-web-maintenance-program/responseCaseAggDescription.vm
+         /dhis-web-commons/ajax/jsonResponseSuccess.vm
+      </result>  <result name="error" type="velocity">
+         /dhis-web-commons/ajax/jsonResponseError.vm
       </result>
       <param name="onExceptionReturn">plainTextError</param>
     </action>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/javascript/programValidation.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/javascript/programValidation.js	2014-11-12 06:31:11 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/javascript/programValidation.js	2015-02-02 13:58:48 +0000
@@ -132,19 +132,30 @@
 
 function insertText( inputAreaName, inputText ) {
   insertTextCommon(inputAreaName, inputText);
-
   getExpressionText();
 }
 
 
 function getExpressionText() {
-  $.postUTF8("getProgramExpressionDescription.action",
-    {
-      programExpression: $('#expression').val()
-    },
-    function( data ) {
-      setInnerHTML("formulaText", data);
-    }, 'html');
+	$.ajax({
+		url: "getProgramExpressionDescription.action",
+		type: "POST",
+		data:{ programExpression: $('#expression').val() },
+		dataType: "json",
+		success: function( json ){
+			setInnerHTML("formulaText", json.message);
+			  if( json.response == "error" ){
+				$("#formulaText").css("color","red");
+				$("#formulaText").addClass("validateError");
+			  }
+			  else{
+				$("#formulaText").css("color","black");
+				$("#formulaText").removeClass("error");
+			  }
+		}
+	});
+
+
 }
 
 var left = true;
@@ -168,20 +179,25 @@
   dialog.dialog("close");
 }
 
-function validateExpression() {
-  if( checkNotEmpty(jQuery("#expression-container [id=description]"), i18n_description_not_null) == false )
+function validateExpression() {	
+  getExpressionText();
+  if( checkValidationRule(jQuery("#expression-container [id=description]"), i18n_description_not_null) == false )
     return;
-  if( checkNotEmpty(jQuery("#expression-container [id=expression]"), i18n_expression_not_null) == false )
+  if( checkValidationRule(jQuery("#expression-container [id=expression]"), i18n_expression_not_null) == false )
     return;
   insertExpression();
 }
 
-function checkNotEmpty( field, message ) {
+function checkValidationRule( field, message ) {
   if( field.val().length == 0 ) {
     setInnerHTML("exp-" + field.attr("name") + "Info", message);
     $('#expression-container [id=' + field.attr("name") + "]").css("background-color", "#ffc5c5");
     return false;
-  } else {
+  } 
+  else if( $("#formulaText").attr("class") == "validateError" ){
+	return false;
+  }
+  else {
     setInnerHTML("exp-" + field.attr("name") + "Info", '');
     $('#expression-container [id=' + field.attr("name") + "]").css("background-color", "#ffffff");
   }