← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18130: Replace ids of program-stages and data elements to uid in Expressions in program validation.

 

Merge authors:
  Thu Tran (tran-hispvietnam)
------------------------------------------------------------
revno: 18130 [merge]
committer: Tran Chau<tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-01-27 20:45:40 +0700
message:
  Replace ids of program-stages and data elements to uid in Expressions in program validation.
modified:
  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
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java
  dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java
  dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramValidationServiceTest.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/expressionBuilderForm.vm
  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/jsonTrackedEntityDataElements.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-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-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2015-01-27 13:45:40 +0000
@@ -133,7 +133,10 @@
             while ( matcher.find() )
             {
                 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;
@@ -165,11 +168,8 @@
             String[] info = match.split( SEPARATOR_OBJECT );
             String[] ids = info[1].split( SEPARATOR_ID );
 
-            String programStageId = ids[0];
-            ProgramStage programStage = programStageService.getProgramStage( Integer.parseInt( programStageId ) );
-
-            int dataElementId = Integer.parseInt( ids[1] );
-            DataElement dataElement = dataElementService.getDataElement( dataElementId );
+            ProgramStage programStage = programStageService.getProgramStage( ids[0] );
+            DataElement dataElement = dataElementService.getDataElement( ids[1] );
 
             if ( programStage == null || dataElement == null )
             {
@@ -200,9 +200,8 @@
 
             String[] info = match.split( SEPARATOR_OBJECT );
             String[] ids = info[1].split( SEPARATOR_ID );
-
-            int dataElementId = Integer.parseInt( ids[1] );
-            DataElement dataElement = dataElementService.getDataElement( dataElementId );
+            
+            DataElement dataElement = dataElementService.getDataElement( ids[1] );
             dataElements.add( dataElement );
         }
 

=== 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	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramValidationService.java	2015-01-27 13:45:40 +0000
@@ -136,11 +136,12 @@
         Map<String, String> entityInstanceDataValueMap = new HashMap<>( entityInstanceDataValues.size() );
         for ( TrackedEntityDataValue entityInstanceDataValue : entityInstanceDataValues )
         {
-            String key = entityInstanceDataValue.getProgramStageInstance().getProgramStage().getId() + "."
-                + entityInstanceDataValue.getDataElement().getId();
+            String key = entityInstanceDataValue.getProgramStageInstance().getProgramStage().getUid() + "."
+                + entityInstanceDataValue.getDataElement().getUid();
             entityInstanceDataValueMap.put( key, entityInstanceDataValue.getValue() );
         }
-
+System.err.println("\n\n\n ==== entityInstanceDataValueMap : " + entityInstanceDataValueMap );
+System.err.println("\n validation : " + validation );
         // ---------------------------------------------------------------------
         // Validate rules
         // ---------------------------------------------------------------------
@@ -220,9 +221,9 @@
                 String[] info = match.split( SEPARATOR_OBJECT );
                 String[] ids = info[1].split( SEPARATOR_ID );
 
-                int programStageId = Integer.parseInt( ids[0] );
+                String programStageUid = ids[0];
 
-                if ( programStageId == programStage.getId() )
+                if ( programStageUid.equals( programStage.getUid() ) )
                 {
                     flag = true;
                     break;

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java	2015-01-27 13:45:40 +0000
@@ -28,8 +28,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+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.SEPARATOR_ID;
+import static org.hisp.dhis.program.ProgramExpression.SEPARATOR_OBJECT;
+
 import java.sql.ResultSet;
 import java.sql.Statement;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.amplecode.quick.StatementHolder;
@@ -39,7 +46,10 @@
 import org.hisp.dhis.caseaggregation.CaseAggregationCondition;
 import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.jdbc.StatementBuilder;
+import org.hisp.dhis.program.ProgramExpression;
+import org.hisp.dhis.program.ProgramStageService;
 import org.hisp.dhis.system.startup.AbstractStartupRoutine;
 import org.hisp.dhis.system.util.ValidationUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -80,6 +90,12 @@
     @Autowired
     private DataElementCategoryService categoryService;
 
+    @Autowired
+    private ProgramStageService programStageService;
+    
+    @Autowired
+    private DataElementService dataElementService;
+    
     // -------------------------------------------------------------------------
     // Action Implementation
     // -------------------------------------------------------------------------
@@ -298,6 +314,7 @@
         updateFixedAttributeInCaseAggregate( "DEDATEDIFF", CaseAggregationCondition.MINUS_OPERATOR );
         executeSql( "update userroleauthorities set authority='F_ADD_TRACKED_ENTITY_FORM' where authority='F_TRACKED_ENTITY_FORM_ADD'" );
 
+        updateProgramExpressionUid();
     }
 
     // -------------------------------------------------------------------------
@@ -511,6 +528,72 @@
         }
     }
     
+    private void updateProgramExpressionUid()
+    {
+		String regExp = "\\[(" + OBJECT_PROGRAM_STAGE_DATAELEMENT + "|"
+				+ OBJECT_PROGRAM_STAGE + ")" + SEPARATOR_OBJECT + "([0-9]+["
+				+ SEPARATOR_ID + "[a-zA-z0-9]+]*)\\]";
+		
+		StatementHolder holder = statementManager.getHolder();
+
+         try
+         {
+             Statement statement = holder.getStatement();
+
+             ResultSet resultSet = statement
+                 .executeQuery( "select programexpressionid, expression from programexpression" );
+
+             while ( resultSet.next() )
+             {
+            	 int id = resultSet.getInt( "programexpressionid" );
+                 String expression = resultSet.getString( "expression" );
+                 String result = expression;
+                 
+                 Pattern pattern = Pattern.compile( regExp );
+                 Matcher matcher = pattern.matcher( expression );
+                 while ( matcher.find() )
+                 {
+                	 String group = matcher.group();
+                 	 String key = matcher.group(1);
+                	 if( key.equals( OBJECT_PROGRAM_STAGE_DATAELEMENT) )
+                	 {
+	                	 String[] ids = matcher.group(2).split( SEPARATOR_ID );
+
+	                     int programStageId = Integer.parseInt( ids[0] );
+	                	 int deId = Integer.parseInt( ids[1] );
+	                     
+	                	 String programStageUid = programStageService.getProgramStage(programStageId).getUid();
+	                	 String deUid = dataElementService.getDataElement(deId).getUid();
+
+	                	 result = result.replace(group, "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT
+	                             + programStageUid + "." + deUid + "]" );
+                	 }
+                	 else
+                	 {
+                		String[] ids = matcher.group(2).split( SEPARATOR_ID );
+ 	                    int programStageId = Integer.parseInt( ids[0] );
+ 	                    String programStageUid = programStageService.getProgramStage(programStageId).getUid();
+	                	
+ 	                   result = result.replace( group ,
+	                             "[" + OBJECT_PROGRAM_STAGE + ProgramExpression.SEPARATOR_OBJECT
+	                             + programStageUid +  "." + ids[1] + "]" );
+                	 }
+                 }
+                 
+                 executeSql( "UPDATE programexpression SET expression='" + result + "' WHERE programexpressionid=" + id );
+             }
+         }
+         catch ( Exception ex )
+         {
+             log.debug( ex );
+         }
+         finally
+         {
+             holder.close();
+         }
+    }
+    
+    
     private int executeSql( String sql )
     {
         try

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java	2015-01-27 13:45:40 +0000
@@ -97,11 +97,14 @@
 
     private ProgramStageInstance stageInstance;
 
-    private int stageAId;
-
-    private int deAId;
-
-    private int deBId;
+    private DataElement dataElementA;
+    
+    private DataElement dataElementB;
+    
+    private ProgramStage stageA;
+    
+    private ProgramStage stageB;
+
 
     @Override
     public void setUpTest()
@@ -112,11 +115,11 @@
         Program program = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnit );
         programService.addProgram( program );
 
-        ProgramStage stageA = new ProgramStage( "StageA", program );
+        stageA = new ProgramStage( "StageA", program );
         stageA.setSortOrder( 1 );
-        stageAId = programStageService.saveProgramStage( stageA );
+        programStageService.saveProgramStage( stageA );
 
-        ProgramStage stageB = new ProgramStage( "StageB", program );
+        stageB = new ProgramStage( "StageB", program );
         stageB.setSortOrder( 2 );
         programStageService.saveProgramStage( stageB );
 
@@ -126,11 +129,11 @@
         program.setProgramStages( programStages );
         programService.updateProgram( program );
 
-        DataElement dataElementA = createDataElement( 'A' );
-        DataElement dataElementB = createDataElement( 'B' );
+        dataElementA = createDataElement( 'A' );
+        dataElementB = createDataElement( 'B' );
 
-        deAId = dataElementService.addDataElement( dataElementA );
-        deBId = dataElementService.addDataElement( dataElementB );
+        dataElementService.addDataElement( dataElementA );
+        dataElementService.addDataElement( dataElementB );
 
         TrackedEntityInstance entityInstance = createTrackedEntityInstance( 'A', organisationUnit );
         entityInstanceService.addTrackedEntityInstance( entityInstance );
@@ -146,9 +149,9 @@
         dataValueService.saveTrackedEntityDataValue( dataValueB );
 
         programExpressionA = new ProgramExpression( "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT
-            + ProgramExpression.SEPARATOR_OBJECT + stageAId + "." + deAId + "]", "A" );
+            + ProgramExpression.SEPARATOR_OBJECT + stageA.getUid() + "." + dataElementA.getUid() + "]", "A" );
         programExpressionB = new ProgramExpression( "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT
-            + ProgramExpression.SEPARATOR_OBJECT + stageAId + "." + deBId + "]", "B" );
+            + ProgramExpression.SEPARATOR_OBJECT + stageA.getUid() + "." + dataElementB.getUid() + "]", "B" );
     }
 
     @Test
@@ -210,8 +213,8 @@
         programExpressionService.addProgramExpression( programExpressionA );
 
         Map<String, String> dataValueMap = new HashMap<>();
-        dataValueMap.put( stageAId + "." + deAId, "1" );
-        dataValueMap.put( stageAId + "." + deBId, "2" );
+        dataValueMap.put( stageA.getUid() + "." + dataElementA.getUid(), "1" );
+        dataValueMap.put( stageA.getUid() + "." + dataElementB.getUid(), "2" );
 
         String value = programExpressionService.getProgramExpressionValue( programExpressionA, stageInstance,
             dataValueMap );

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramValidationServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramValidationServiceTest.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramValidationServiceTest.java	2015-01-27 13:45:40 +0000
@@ -169,18 +169,18 @@
         dataValueService.saveTrackedEntityDataValue( dataValueD );
 
         ProgramExpression programExpressionA = new ProgramExpression( "["
-            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + psIdA + "."
-            + deIdA + "]", "A" );
+            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + stageA.getUid() + "."
+            + dataElementA.getUid() + "]", "A" );
         ProgramExpression programExpressionB = new ProgramExpression( "["
-            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + psIdA + "."
-            + deIdB + "]", "B" );
+            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + stageA.getUid() + "."
+            + dataElementB.getUid() + "]", "B" );
 
         ProgramExpression programExpressionC = new ProgramExpression( "["
-            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + psIdB + "."
-            + deIdA + "]", "C" );
+            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + stageB.getUid() + "."
+            + dataElementA.getUid() + "]", "C" );
         ProgramExpression programExpressionD = new ProgramExpression( "["
-            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + psIdB + "."
-            + deIdB + "]", "D" );
+            + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT + stageB.getUid() + "."
+            + dataElementB.getUid() + "]", "D" );
 
         validationA = new ProgramValidation( "A", programExpressionA, programExpressionB, program );
         validationA.setOperator( Operator.valueOf( "equal_to" ) );

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/expressionBuilderForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/expressionBuilderForm.vm	2014-11-03 13:35:42 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/expressionBuilderForm.vm	2014-11-12 06:31:11 +0000
@@ -26,7 +26,7 @@
 					<select id='leftStage' name='leftStage' onchange="getLeftPrgramStageDataElements();" style='width:320px;'>
 						<option value=''>[$i18n.getString( "please_select" )]</option>
 						#foreach($stage in $program.programStages)
-						<option value='$stage.id'>$encoder.htmlEncode($stage.name)</option>
+						<option value='$stage.uid' id='$stage.id'>$encoder.htmlEncode($stage.name)</option>
 						#end
 					</select>
 				</td>

=== 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-08-06 15:20:54 +0000
+++ 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
@@ -80,41 +80,22 @@
   clearListById('dataElementId');
 
   var programStage = document.getElementById('leftStage');
-  var programStageId = programStage.options[ programStage.selectedIndex ].value;
+  var programStageId = programStage.options[ programStage.selectedIndex ].id;
+  var programStageUid = programStage.options[ programStage.selectedIndex ].value;
   if( programStageId == '' ) return;
 
   jQuery.getJSON("getTrackedEntityDataElements.action", {
     programStageId: programStageId
   }, function( json ) {
-    jQuery('#dataElementId').append('<option value="[PS:'+programStageId+'.DUE_DATE]">' + i18n_due_date + '</option>');
-    jQuery('#dataElementId').append('<option value="[PS:'+programStageId+'.REPORT_DATE]">' + i18n_report_date + '</option>');
+    jQuery('#dataElementId').append('<option value="[PS:' + programStageUid + '.DUE_DATE]">' + i18n_due_date + '</option>');
+    jQuery('#dataElementId').append('<option value="[PS:' + programStageUid + '.REPORT_DATE]">' + i18n_report_date + '</option>');
     for( i in json.dataElements ) {
-      var id = '[DE:' + programStageId + '.' + json.dataElements[i].id + ']';
+      var id = '[DE:' + programStageUid + '.' + json.dataElements[i].id + ']';
       jQuery('#dataElementId').append('<option value="' + id + '">' + json.dataElements[i].name + '</option>');
     }
   });
 }
 
-function getRightPrgramStageDataElements() {
-  clearListById('rightSideDE');
-
-  var programStage = document.getElementById('rightStage');
-  var programStageId = programStage.options[ programStage.selectedIndex ].value;
-  if( programStageId == '' ) return;
-
-  jQuery.getJSON("getTrackedEntityDataElements.action", {
-    programStageId: programStageId
-  }, function( json ) {
-    jQuery('#dataElementId').append('<option value="DUE_DATE">' + i18n_due_date + '</option>');
-    jQuery('#dataElementId').append('<option value="REPORT_DATE">' + i18n_report_date + '</option>');
-    for( i in json.dataElements ) {
-      var id = '[DE:' + programStageId + '.' + json.dataElements[i].id + ']';
-      jQuery('#rightSideDE').append('<option value="' + id + '">' + json.dataElements[i].name + '</option>');
-    }
-  });
-
-}
-
 //------------------------------------------------------------------------------
 // Show Left side form for designing
 //------------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/jsonTrackedEntityDataElements.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/jsonTrackedEntityDataElements.vm	2014-08-11 12:53:29 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/jsonTrackedEntityDataElements.vm	2014-11-12 06:31:11 +0000
@@ -2,7 +2,7 @@
   "dataElements": [
   #foreach( ${dataElement} in $!{dataElements} )
   {
-	"id": "${dataElement.id}",
+	"id": "${dataElement.uid}",
 	"name": "$!encoder.jsonEncode( ${dataElement.name} )",
 	"code": "$!encoder.jsonEncode( ${dataElement.code} )",
 	"type": "${dataElement.type}",