← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19872: tracker-captuer: program rules engine for attribute support - WIP

 

------------------------------------------------------------
revno: 19872
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-08-27 17:15:20 +0200
message:
  tracker-captuer: program rules engine for attribute support - WIP
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.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-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2015-08-26 12:29:22 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2015-08-27 15:15:20 +0000
@@ -12,7 +12,6 @@
                 CustomFormService,
                 EnrollmentService,
                 DialogService,
-                ModalService,
                 CurrentSelection,
                 OptionSetService,
                 EventUtils,
@@ -61,16 +60,17 @@
         $scope.trackedEntities.available = entities;   
         $scope.trackedEntities.selected = $scope.trackedEntities.available[0];
     });
-    
-    $scope.allProgramRules = [];
-    TrackerRulesFactory.getRules($scope.selectedProgram.id).then(function(rules){                    
-        $scope.allProgramRules = rules;
-    });  
 
     //watch for selection of program
     $scope.$watch('selectedProgram', function() {        
         $scope.trackedEntityForm = null;
         $scope.customForm = null;
+        
+        $scope.allProgramRules = [];
+        TrackerRulesFactory.getRules($scope.selectedProgram.id).then(function(rules){                    
+            $scope.allProgramRules = rules;
+        });
+        
         if($scope.registrationMode === 'REGISTRATION'){
             $scope.getAttributes($scope.registrationMode);
         }        

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js	2015-08-27 14:45:23 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js	2015-08-27 15:15:20 +0000
@@ -1187,8 +1187,13 @@
                         $rootScope.ruleeffects = {};
                     }
 
-                    if(angular.isUndefined( $rootScope.ruleeffects[executingEvent.event] )){
-                        $rootScope.ruleeffects[executingEvent.event] = {};
+                    var ruleEffectKey = executingEvent.event ? executingEvent.event : executingEvent;
+                    if( executingEvent.event && angular.isUndefined( $rootScope.ruleeffects[ruleEffectKey] )){
+                        $rootScope.ruleeffects[ruleEffectKey] = {};
+                    }
+                    
+                    if(!angular.isObject(executingEvent) && angular.isUndefined( $rootScope.ruleeffects[ruleEffectKey] )){
+                        $rootScope.ruleeffects[ruleEffectKey] = {};
                     }
 
                     var updatedEffectsExits = false;
@@ -1210,8 +1215,8 @@
 
                         angular.forEach(rule.programRuleActions, function(action){
                             //In case the effect-hash is not populated, add entries
-                            if(angular.isUndefined( $rootScope.ruleeffects[executingEvent.event][action.id] )){
-                                $rootScope.ruleeffects[executingEvent.event][action.id] =  {
+                            if(angular.isUndefined( $rootScope.ruleeffects[ruleEffectKey][action.id] )){
+                                $rootScope.ruleeffects[ruleEffectKey][action.id] =  {
                                     id:action.id,
                                     location:action.location, 
                                     action:action.programRuleActionType,
@@ -1229,7 +1234,7 @@
                             if(ruleEffective && action.data)
                             {
                                 //Preserve old data for comparison:
-                                var oldData = $rootScope.ruleeffects[executingEvent.event][action.id].data;
+                                var oldData = $rootScope.ruleeffects[ruleEffectKey][action.id].data;
                                 
                                 //The key data might be containing a dollar sign denoting that the key data is a variable.
                                 //To make a lookup in variables hash, we must make a lookup without the dollar sign in the variable name
@@ -1239,37 +1244,37 @@
                                 if(angular.isDefined(variablesHash[nameWithoutBrackets]))
                                 {
                                     //The variable exists, and is replaced with its corresponding value
-                                    $rootScope.ruleeffects[executingEvent.event][action.id].data =
+                                    $rootScope.ruleeffects[ruleEffectKey][action.id].data =
                                         variablesHash[nameWithoutBrackets].variableValue;
                                 }
                                 else if(action.data.indexOf('{') !== -1)
                                 {
                                     //Since the value couldnt be looked up directly, and contains a dollar sign, the expression was more complex
                                     //Now we will have to make a thorough replacement and separate evaluation to find the correct value:
-                                    $rootScope.ruleeffects[executingEvent.event][action.id].data = replaceVariables(action.data, variablesHash);
+                                    $rootScope.ruleeffects[ruleEffectKey][action.id].data = replaceVariables(action.data, variablesHash);
                                     //In a scenario where the data contains a complex expression, evaluate the expression to compile(calculate) the result:
-                                    $rootScope.ruleeffects[executingEvent.event][action.id].data = runExpression($rootScope.ruleeffects[executingEvent.event][action.id].data, action.data, "action:" + action.id, flag, variablesHash);
+                                    $rootScope.ruleeffects[ruleEffectKey][action.id].data = runExpression($rootScope.ruleeffects[ruleEffectKey][action.id].data, action.data, "action:" + action.id, flag, variablesHash);
                                 }
                                 
-                                if(oldData !== $rootScope.ruleeffects[executingEvent.event][action.id].data) {
+                                if(oldData !== $rootScope.ruleeffects[ruleEffectKey][action.id].data) {
                                     updatedEffectsExits = true;
                                 }
                             }
 
                             //Update the rule effectiveness if it changed in this evaluation;
-                            if($rootScope.ruleeffects[executingEvent.event][action.id].ineffect !== ruleEffective)
+                            if($rootScope.ruleeffects[ruleEffectKey][action.id].ineffect !== ruleEffective)
                             {
                                 //There is a change in the rule outcome, we need to update the effect object.
                                 updatedEffectsExits = true;
-                                $rootScope.ruleeffects[executingEvent.event][action.id].ineffect = ruleEffective;
+                                $rootScope.ruleeffects[ruleEffectKey][action.id].ineffect = ruleEffective;
                             }
 
                             //In case the rule is of type "assign variable" and the rule is effective,
                             //the variable data result needs to be applied to the correct variable:
-                            if($rootScope.ruleeffects[executingEvent.event][action.id].action === "ASSIGNVARIABLE" && $rootScope.ruleeffects[executingEvent.event][action.id].ineffect){
+                            if($rootScope.ruleeffects[ruleEffectKey][action.id].action === "ASSIGNVARIABLE" && $rootScope.ruleeffects[ruleEffectKey][action.id].ineffect){
                                 //from earlier evaluation, the data portion of the ruleeffect now contains the value of the variable to be assign.
                                 //the content portion of the ruleeffect defines the name for the variable, when dollar is removed:
-                                var variabletoassign = $rootScope.ruleeffects[executingEvent.event][action.id].content.replace("#{","").replace("}","");
+                                var variabletoassign = $rootScope.ruleeffects[ruleEffectKey][action.id].content.replace("#{","").replace("}","");
 
                                 if(!angular.isDefined(variablesHash[variabletoassign])){
                                     $log.warn("Variable " + variabletoassign + " was not defined.");
@@ -1277,11 +1282,11 @@
 
                                 //Even if the variable is not defined: we assign it:
                                 if(variablesHash[variabletoassign] && 
-                                        variablesHash[variabletoassign].variableValue !== $rootScope.ruleeffects[executingEvent.event][action.id].data){
+                                        variablesHash[variabletoassign].variableValue !== $rootScope.ruleeffects[ruleEffectKey][action.id].data){
                                     //If the variable was actually updated, we assume that there is an updated ruleeffect somewhere:
                                     updatedEffectsExits = true;
                                     //Then we assign the new value:
-                                    variablesHash[variabletoassign].variableValue = $rootScope.ruleeffects[executingEvent.event][action.id].data;
+                                    variablesHash[variabletoassign].variableValue = $rootScope.ruleeffects[ruleEffectKey][action.id].data;
                                 }
                             }
                         });
@@ -1289,7 +1294,7 @@
 
                     //Broadcast rules finished if there was any actual changes to the event.
                     if(updatedEffectsExits){
-                        $rootScope.$broadcast("ruleeffectsupdated", { event: executingEvent.event });
+                        $rootScope.$broadcast("ruleeffectsupdated", { event: ruleEffectKey });
                     }
                 }