← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18508: tracker-capture: some refactoring in processing attribute values to/from web-api/UI

 

------------------------------------------------------------
revno: 18508
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-03-06 15:43:54 +0100
message:
  tracker-capture: some refactoring in processing attribute values to/from web-api/UI
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/dashboard/dashboard-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/enrollment/enrollment-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/profile/profile-controller.js
  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/relationship/relationship-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/overdue-events-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-statistics-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-summary-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/upcoming-events-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/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/dashboard/dashboard-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/dashboard/dashboard-controller.js	2015-03-06 09:37:05 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/dashboard/dashboard-controller.js	2015-03-06 14:43:54 +0000
@@ -105,17 +105,7 @@
             });
             
             setWidgetsSize();
-            
-            AttributesFactory.getAll().then(function(atts){
-                $scope.attributes = [];  
-                $scope.attributesById = [];
-                angular.forEach(atts, function(att){
-                    $scope.attributesById[att.id] = att;
-                });
-
-                CurrentSelection.setAttributesById($scope.attributesById);
-                $scope.broadCastSelections();
-            });            
+            $scope.broadCastSelections();            
         });        
     };    
     
@@ -136,57 +126,66 @@
         
         //get option sets
         $scope.optionSets = [];
-        OptionSetService.getAll().then(function(optionSets){
-            
-            angular.forEach(optionSets, function(optionSet){                            
+        OptionSetService.getAll().then(function(optionSets){            
+            angular.forEach(optionSets, function(optionSet){
                 $scope.optionSets[optionSet.id] = optionSet;
             });
-        
-            //Fetch the selected entity
-            TEIService.get($scope.selectedTeiId, $scope.optionSets).then(function(response){
-                $scope.selectedTei = response.data;
-
-                //get the entity type
-                TEService.get($scope.selectedTei.trackedEntity).then(function(te){                    
-                    $scope.trackedEntity = te;
-
-                    //get enrollments for the selected tei
-                    EnrollmentService.getByEntity($scope.selectedTeiId).then(function(response){                    
-                        var enrollments = angular.isObject(response) && response.enrollments ? response.enrollments : [];
-                        var selectedEnrollment = null;
-                        if(enrollments.length === 1 && enrollments[0].status === 'ACTIVE'){
-                            selectedEnrollment = enrollments[0];
-                        }
-                        
-                        ProgramFactory.getAll().then(function(programs){
-                            $scope.programs = [];
-
-                            $scope.programNames = [];  
-                            $scope.programStageNames = [];        
-                            
-                            //get programs valid for the selected ou and tei
-                            angular.forEach(programs, function(program){
-                                $scope.programNames[program.id] = {id: program.id, name: program.name};
-                                angular.forEach(program.programStages, function(stage){                
-                                    $scope.programStageNames[stage.id] = {id: stage.id, name: stage.name};
+            
+            AttributesFactory.getAll().then(function(atts){
+                
+                $scope.attributesById = [];
+                angular.forEach(atts, function(att){
+                    $scope.attributesById[att.id] = att;
+                });
+
+                CurrentSelection.setAttributesById($scope.attributesById);
+            
+                //Fetch the selected entity
+                TEIService.get($scope.selectedTeiId, $scope.optionSets, $scope.attributesById).then(function(response){
+                    $scope.selectedTei = response.data;
+
+                    //get the entity type
+                    TEService.get($scope.selectedTei.trackedEntity).then(function(te){                    
+                        $scope.trackedEntity = te;
+
+                        //get enrollments for the selected tei
+                        EnrollmentService.getByEntity($scope.selectedTeiId).then(function(response){                    
+                            var enrollments = angular.isObject(response) && response.enrollments ? response.enrollments : [];
+                            var selectedEnrollment = null;
+                            if(enrollments.length === 1 && enrollments[0].status === 'ACTIVE'){
+                                selectedEnrollment = enrollments[0];
+                            }
+
+                            ProgramFactory.getAll().then(function(programs){
+                                $scope.programs = [];
+
+                                $scope.programNames = [];  
+                                $scope.programStageNames = [];        
+
+                                //get programs valid for the selected ou and tei
+                                angular.forEach(programs, function(program){
+                                    $scope.programNames[program.id] = {id: program.id, name: program.name};
+                                    angular.forEach(program.programStages, function(stage){                
+                                        $scope.programStageNames[stage.id] = {id: stage.id, name: stage.name};
+                                    });
+                                    if(program.organisationUnits.hasOwnProperty($scope.selectedOrgUnit.id) &&
+                                       program.trackedEntity.id === $scope.selectedTei.trackedEntity){
+                                        $scope.programs.push(program);
+
+                                        if($scope.selectedProgramId && program.id === $scope.selectedProgramId || selectedEnrollment && selectedEnrollment.program === program.id){
+                                            $scope.selectedProgram = program;
+                                        }
+                                    }                                
                                 });
-                                if(program.organisationUnits.hasOwnProperty($scope.selectedOrgUnit.id) &&
-                                   program.trackedEntity.id === $scope.selectedTei.trackedEntity){
-                                    $scope.programs.push(program);
-                                    
-                                    if($scope.selectedProgramId && program.id === $scope.selectedProgramId || selectedEnrollment && selectedEnrollment.program === program.id){
-                                        $scope.selectedProgram = program;
-                                    }
-                                }                                
+
+                                //prepare selected items for broadcast
+                                CurrentSelection.set({tei: $scope.selectedTei, te: $scope.trackedEntity, prs: $scope.programs, pr: $scope.selectedProgram, prNames: $scope.programNames, prStNames: $scope.programStageNames, enrollments: enrollments, selectedEnrollment: selectedEnrollment, optionSets: $scope.optionSets});                            
+                                getDashboardLayout();                    
                             });
-
-                            //prepare selected items for broadcast
-                            CurrentSelection.set({tei: $scope.selectedTei, te: $scope.trackedEntity, prs: $scope.programs, pr: $scope.selectedProgram, prNames: $scope.programNames, prStNames: $scope.programStageNames, enrollments: enrollments, selectedEnrollment: selectedEnrollment, optionSets: $scope.optionSets});                            
-                            getDashboardLayout();                    
                         });
-                    });
-                });            
-            });    
+                    });            
+                });  
+            });
         });
     }    
     

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/enrollment/enrollment-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/enrollment/enrollment-controller.js	2015-02-27 09:37:56 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/enrollment/enrollment-controller.js	2015-03-06 14:43:54 +0000
@@ -17,7 +17,9 @@
                 DialogService) {
     
     $scope.today = DateUtils.getToday();
-    $scope.selectedOrgUnit = storage.get('SELECTED_OU');  
+    $scope.selectedOrgUnit = storage.get('SELECTED_OU');
+    
+    
     
     //listen for the selected items
     var selections = {};
@@ -43,6 +45,7 @@
         $scope.programExists = args.programExists;
         $scope.programNames = selections.prNames;
         $scope.programStageNames = selections.prStNames;
+        $scope.attributesById = CurrentSelection.getAttributesById();
         
         if($scope.selectedProgram){
             
@@ -171,7 +174,7 @@
                             dateOfIncident: $scope.selectedEnrollment.dateOfIncident ? $scope.selectedEnrollment.dateOfIncident : $scope.selectedEnrollment.dateOfEnrollment
                         };
                         
-        TEIService.update(tei, $scope.optionSets).then(function(updateResponse){            
+        TEIService.update(tei, $scope.optionSets, $scope.attributesById).then(function(updateResponse){            
             
             if(updateResponse.status === 'SUCCESS'){
                 //registration is successful, continue for enrollment               
@@ -220,7 +223,6 @@
     };    
     
     var getProcessedForm = function(){        
-        $scope.attributesById = CurrentSelection.getAttributesById();
         var tei = angular.copy(selections.tei);
         tei.attributes = [];
         var formEmpty = true;

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/profile/profile-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/profile/profile-controller.js	2015-02-27 09:37:56 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/profile/profile-controller.js	2015-03-06 14:43:54 +0000
@@ -13,7 +13,7 @@
     
     //listen for the selected entity
     var selections = {};
-    $scope.$on('dashboardWidgets', function(event, args) {
+    $scope.$on('dashboardWidgets', function(event, args) {        
         selections = CurrentSelection.get();
         $scope.selectedTei = angular.copy(selections.tei);
         $scope.trackedEntity = selections.te;
@@ -82,7 +82,7 @@
             return false;
         }
                 
-        TEIService.update(tei, $scope.optionSets).then(function(updateResponse){
+        TEIService.update(tei, $scope.optionSets, $scope.attributesById).then(function(updateResponse){
             
             if(updateResponse.status !== 'SUCCESS'){//update has failed
                 var dialogOptions = {

=== 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-02-27 09:37:56 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2015-03-06 14:43:54 +0000
@@ -19,16 +19,21 @@
     
     $scope.today = DateUtils.getToday();
     $scope.trackedEntityForm = null;
-    $scope.customForm = null;
-    $scope.optionSets = CurrentSelection.getOptionSets();
-    $scope.attributesById = [];
-    $scope.selectedTei = {};
-    AttributesFactory.getAll().then(function(atts){
-        angular.forEach(atts, function(att){
-            $scope.attributesById[att.id] = att;
+    $scope.customForm = null;    
+    $scope.selectedTei = {};    
+    
+    $scope.attributesById = CurrentSelection.getAttributesById();
+    if(!$scope.attributesById){
+        AttributesFactory.getAll().then(function(atts){
+            angular.forEach(atts, function(att){
+                $scope.attributesById[att.id] = att;
+            });
+            
+            CurrentSelection.setAttributesById($scope.attributesById);
         });
-    });
-            
+    }    
+    
+    $scope.optionSets = CurrentSelection.getOptionSets();        
     if(!$scope.optionSets){
         $scope.optionSets = [];
         OptionSetService.getAll().then(function(optionSets){
@@ -102,7 +107,7 @@
         }
         
         var teiId = '';
-        TEIService.register($scope.tei, $scope.optionSets).then(function(response){
+        TEIService.register($scope.tei, $scope.optionSets, $scope.attributesById).then(function(response){
             
             if(response.status === 'SUCCESS'){
                 

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/relationship/relationship-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/relationship/relationship-controller.js	2015-02-24 16:15:59 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/relationship/relationship-controller.js	2015-03-06 14:43:54 +0000
@@ -3,8 +3,7 @@
                 $rootScope,
                 $modal,                
                 $location,
-                DateUtils,
-                OptionSetService,
+                AttributesFactory,
                 CurrentSelection,
                 RelationshipFactory) {
     $rootScope.showAddRelationshipDiv = false;    
@@ -110,25 +109,8 @@
         var attributes = {};
         
         if(tei && tei.relative && tei.relative.attributes && !tei.relative.processed){
-            angular.forEach(tei.relative.attributes, function(att){
-                var val = att.value;
-                if(att.type === 'trueOnly'){
-                    val = val === 'true' ? true : '';
-                }
-                else{
-                    if(val){
-                        if(att.type === 'date'){
-                            val = DateUtils.formatFromApiToUser(val);
-                        }
-                        if(att.type === 'optionSet' && 
-                                $scope.attributesById[att.attribute] && 
-                                $scope.attributesById[att.attribute].optionSet && 
-                                $scope.attributesById[att.attribute].optionSet.id && 
-                                $scope.optionSets[$scope.attributesById[att.attribute].optionSet.id]){   
-                            val = OptionSetService.getName($scope.optionSets[$scope.attributesById[att.attribute].optionSet.id].options, val);                                
-                        }
-                    }
-                }                
+            angular.forEach(tei.relative.attributes, function(att){                
+                var val = AttributesFactory.formatAttributeValue(att,$scope.attributesById, $scope.optionSets, 'USER');                
                 attributes[att.attribute] = val;
             });
         }
@@ -438,7 +420,7 @@
             });
             tei.relationships.push(relationship);
             
-            TEIService.update(tei, $scope.optionSets).then(function(response){
+            TEIService.update(tei, $scope.optionSets, $scope.attributesById).then(function(response){
                 if(response.status !== 'SUCCESS'){//update has failed
                     var dialogOptions = {
                             headerText: 'relationship_error',

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/overdue-events-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/overdue-events-controller.js	2015-03-06 10:22:47 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/overdue-events-controller.js	2015-03-06 14:43:54 +0000
@@ -10,6 +10,9 @@
                 TEIGridService,
                 AttributesFactory,
                 ProgramFactory,
+                CurrentSelection,
+                OptionSetService,
+                TEIService,
                 storage) {    
     $scope.today = DateUtils.getToday();
     
@@ -18,6 +21,32 @@
     $scope.displayMode = {};
     $scope.printMode = false;
     
+    //get optionsets
+    $scope.optionSets = CurrentSelection.getOptionSets();
+    if(!$scope.optionSets){
+        $scope.optionSets = [];
+        OptionSetService.getAll().then(function(optionSets){
+            angular.forEach(optionSets, function(optionSet){                        
+                $scope.optionSets[optionSet.id] = optionSet;
+            });
+
+            CurrentSelection.setOptionSets($scope.optionSets);
+        });
+    }
+    
+    //get attributes
+    $scope.attributesById = CurrentSelection.getAttributesById();
+    if(!$scope.attributesById){
+        AttributesFactory.getAll().then(function(atts){
+            $scope.attributes = [];  
+            $scope.attributesById = [];
+            angular.forEach(atts, function(att){
+                $scope.attributesById[att.id] = att;
+            });
+            CurrentSelection.setAttributesById($scope.attributesById);
+        });
+    }    
+
     //Paging
     $scope.pager = {pageSize: 50, page: 1, toolBarDisplay: 5};   
     
@@ -43,19 +72,21 @@
                         $scope.programs.push(program);
                     }
                 });
-                if($scope.programs.length === 1){
-                    $scope.selectedProgram = $scope.programs[0];
+                if($scope.programs.length === 0){
+                    $scope.selectedProgram = null;
                 }
                 else{
-                    var continueLoop = true;
-                    for(var i=0; i<programs.length && continueLoop; i++){
-                        if(programs[i].id === $scope.selectedProgram.id){
-                            $scope.selectedProgram = programs[i];
-                            continueLoop = false;
-                        }
+                    if($scope.selectedProgram){
+                        angular.forEach($scope.programs, function(program){                            
+                            if(program.id === $scope.selectedProgram.id){                                
+                                $scope.selectedProgram = program;
+                            }
+                        });
                     }
-                    if(continueLoop){
-                        $scope.selectedProgram = null;
+                    else{                        
+                        if($scope.programs.length === 1){
+                            $scope.selectedProgram = $scope.programs[0];
+                        }                        
                     }
                 }
             });
@@ -104,19 +135,46 @@
                     
                 angular.forEach(data.eventRows, function(row){
                     var overdueEvent = {};
+                    TEIService.reconstructForUser(row.attributes, attributes, $scope.attributesById, $scope.optionSets)
                     angular.forEach(row.attributes, function(att){
-                        overdueEvent[att.attribute] = att.value;
+                        
+                        /*if(att.type === 'trueOnly'){
+                            if(att.value === 'true'){
+                                att.value = true;
+                            }
+                            else{
+                                att.value = '';
+                            }
+                        }
+                        else{
+                            var val = att.value;
+                            if(val){
+                                if(att.type === 'date'){
+                                    val = DateUtils.formatFromApiToUser(val);
+                                }
+                                if(att.type === 'optionSet' && 
+                                        attsById[att.attribute] && 
+                                        attsById[att.attribute].optionSet && 
+                                        attsById[att.attribute].optionSet.id && 
+                                        optionSets[attsById[att.attribute].optionSet.id]){   
+                                    val = OptionSetService.getName(optionSets[attsById[att.attribute].optionSet.id].options, val);                                
+                                }
+                                att.value = val;
+                            }
+                        }*/
+                        
+                        overdueEvent[att.attribute] = val;
+                        
                     });
                     
                     overdueEvent.dueDate = DateUtils.formatFromApiToUser(row.dueDate);
                     overdueEvent.event = row.event;
                     overdueEvent.eventName = $scope.programStages[row.programStage].name;
-                    overdueEvent.orgUnitName = row.orgUnitName;                    
+                    overdueEvent.orgUnitName = row.eventOrgUnitName;                    
                     overdueEvent.followup = row.followup;
                     overdueEvent.program = row.program;
                     overdueEvent.programStage = row.programStage;
                     overdueEvent.trackedEntityInstance = row.trackedEntityInstance;
-                    overdueEvent.orgUnitName = row.registrationOrgUnit;
                     overdueEvent.created = DateUtils.formatFromApiToUser(row.registrationDate);;
                     $scope.overdueEvents.push(overdueEvent);
                     

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-statistics-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-statistics-controller.js	2015-03-06 10:22:47 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-statistics-controller.js	2015-03-06 14:43:54 +0000
@@ -37,19 +37,21 @@
                         $scope.programs.push(program);
                     }
                 });
-                if($scope.programs.length === 1){
-                    $scope.selectedProgram = $scope.programs[0];
+                if($scope.programs.length === 0){
+                    $scope.selectedProgram = null;
                 }
                 else{
-                    var continueLoop = true;
-                    for(var i=0; i<programs.length && continueLoop; i++){
-                        if(programs[i].id === $scope.selectedProgram.id){
-                            $scope.selectedProgram = programs[i];
-                            continueLoop = false;
-                        }
+                    if($scope.selectedProgram){
+                        angular.forEach($scope.programs, function(program){                            
+                            if(program.id === $scope.selectedProgram.id){                                
+                                $scope.selectedProgram = program;
+                            }
+                        });
                     }
-                    if(continueLoop){
-                        $scope.selectedProgram = null;
+                    else{                        
+                        if($scope.programs.length === 1){
+                            $scope.selectedProgram = $scope.programs[0];
+                        }                        
                     }
                 }
             });

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-summary-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-summary-controller.js	2015-03-06 10:22:47 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/program-summary-controller.js	2015-03-06 14:43:54 +0000
@@ -7,6 +7,8 @@
                 TEIGridService,
                 AttributesFactory,
                 ProgramFactory,
+                CurrentSelection,
+                OptionSetService,
                 DHIS2EventFactory,
                 storage) {    
     $scope.today = DateUtils.getToday();
@@ -15,6 +17,18 @@
     $scope.selectedOuMode = $scope.ouModes[0];
     $scope.report = {};
     
+    $scope.optionSets = CurrentSelection.getOptionSets();
+    if(!$scope.optionSets){
+        $scope.optionSets = [];
+        OptionSetService.getAll().then(function(optionSets){
+            angular.forEach(optionSets, function(optionSet){                        
+                $scope.optionSets[optionSet.id] = optionSet;
+            });
+
+            CurrentSelection.setOptionSets($scope.optionSets);
+        });
+    }
+    
     //watch for selection of org unit from tree
     $scope.$watch('selectedOrgUnit', function() {      
         $scope.selectedProgram = null;
@@ -35,19 +49,21 @@
                         $scope.programs.push(program);
                     }
                 });
-                if($scope.programs.length === 1){
-                    $scope.selectedProgram = $scope.programs[0];
+                if($scope.programs.length === 0){
+                    $scope.selectedProgram = null;
                 }
                 else{
-                    var continueLoop = true;
-                    for(var i=0; i<programs.length && continueLoop; i++){
-                        if(programs[i].id === $scope.selectedProgram.id){
-                            $scope.selectedProgram = programs[i];
-                            continueLoop = false;
-                        }
+                    if($scope.selectedProgram){
+                        angular.forEach($scope.programs, function(program){                            
+                            if(program.id === $scope.selectedProgram.id){                                
+                                $scope.selectedProgram = program;
+                            }
+                        });
                     }
-                    if(continueLoop){
-                        $scope.selectedProgram = null;
+                    else{                        
+                        if($scope.programs.length === 1){
+                            $scope.selectedProgram = $scope.programs[0];
+                        }                        
                     }
                 }
             });
@@ -96,7 +112,7 @@
                             false).then(function(data){                     
             
             //process tei grid
-            var teis = TEIGridService.format(data,true, null);     
+            var teis = TEIGridService.format(data,true, $scope.optionSets);     
             $scope.teiList = [];
 
             DHIS2EventFactory.getByOrgUnitAndProgram($scope.selectedOrgUnit.id, 

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/upcoming-events-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/upcoming-events-controller.js	2015-03-06 10:22:47 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/report/upcoming-events-controller.js	2015-03-06 14:43:54 +0000
@@ -10,6 +10,7 @@
                 TEIGridService,
                 AttributesFactory,
                 ProgramFactory,
+                CurrentSelection,
                 storage) {
     $scope.today = DateUtils.getToday();
     
@@ -30,6 +31,18 @@
         }
     });
     
+    
+    AttributesFactory.getAll().then(function(atts){
+        $scope.attributes = [];  
+        $scope.attributesById = [];
+        angular.forEach(atts, function(att){
+            $scope.attributesById[att.id] = att;
+        });
+
+        CurrentSelection.setAttributesById($scope.attributesById);
+        $scope.broadCastSelections();
+    });
+    
     //load programs associated with the selected org unit.
     $scope.loadPrograms = function(orgUnit) {        
         $scope.selectedOrgUnit = orgUnit;        
@@ -41,19 +54,21 @@
                         $scope.programs.push(program);
                     }
                 });
-                if($scope.programs.length === 1){
-                    $scope.selectedProgram = $scope.programs[0];
+                if($scope.programs.length === 0){
+                    $scope.selectedProgram = null;
                 }
                 else{
-                    var continueLoop = true;
-                    for(var i=0; i<programs.length && continueLoop; i++){
-                        if(programs[i].id === $scope.selectedProgram.id){
-                            $scope.selectedProgram = programs[i];
-                            continueLoop = false;
-                        }
+                    if($scope.selectedProgram){
+                        angular.forEach($scope.programs, function(program){                            
+                            if(program.id === $scope.selectedProgram.id){                                
+                                $scope.selectedProgram = program;
+                            }
+                        });
                     }
-                    if(continueLoop){
-                        $scope.selectedProgram = null;
+                    else{                        
+                        if($scope.programs.length === 1){
+                            $scope.selectedProgram = $scope.programs[0];
+                        }                        
                     }
                 }
             });
@@ -111,6 +126,7 @@
                 upcomingEvent.event = row.event;
                 upcomingEvent.eventName = $scope.programStages[row.programStage].name;
                 upcomingEvent.eventOrgUnitName = row.eventOrgUnitName;
+                upcomingEvent.orgUnitName = row.eventOrgUnitName;
                 upcomingEvent.followup = row.followup;
                 upcomingEvent.program = row.program;
                 upcomingEvent.programStage = row.programStage;

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js	2015-03-03 16:55:58 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js	2015-03-06 14:43:54 +0000
@@ -39,7 +39,7 @@
         },
         get: function(){            
             var promise = $http.get(  '../api/userSettings/dhis2-tracker-dashboard' ).then(function(response){                
-                return response.data === "" ? defaultLayout: response.data;
+                return response.data === "" ? defaultLayout : response.data;
             }, function(){
                 return defaultLayout;
             });
@@ -361,13 +361,13 @@
             return promise;
         },
         getByEntity: function( entity ){
-            var promise = $http.get(  '../api/enrollments?trackedEntityInstance=' + entity ).then(function(response){
+            var promise = $http.get(  '../api/enrollments.json?trackedEntityInstance=' + entity ).then(function(response){
                 return convertFromApiToUser(response.data);
             });
             return promise;
         },
         getByEntityAndProgram: function( entity, program ){
-            var promise = $http.get(  '../api/enrollments?trackedEntityInstance=' + entity + '&program=' + program ).then(function(response){
+            var promise = $http.get(  '../api/enrollments.json?trackedEntityInstance=' + entity + '&program=' + program ).then(function(response){
                 return convertFromApiToUser(response.data);
             });
             return promise;
@@ -474,98 +474,32 @@
 
 /* Service for getting tracked entity instances */
 .factory('TEIService', function($http, $q, AttributesFactory, OptionSetService, CurrentSelection, DateUtils) {
-
+    
     return {        
-        convertFromApiToUser: function(promise, optionSets){            
+        convertFromApiToUser: function(promise, optionSets, attsById){            
             promise.then(function(response){
                 var tei = response.data;
-                var attsById = [];                
-                AttributesFactory.getAll().then(function(atts){
-                    angular.forEach(atts, function(att){                        
-                        attsById[att.id] = att;
-                    });
-
-                    angular.forEach(tei.attributes, function(att){
-                        if(attsById[att.attribute]){
-                            att.displayName = attsById[att.attribute].name;
-                        }
-                        
-                        if(att.type === 'trueOnly'){
-                            if(att.value === 'true'){
-                                att.value = true;
-                            }
-                            else{
-                                att.value = '';
-                            }
-                        }
-                        else{
-                            var val = att.value;
-                            if(val){
-                                if(att.type === 'date'){
-                                    val = DateUtils.formatFromApiToUser(val);
-                                }
-                                if(att.type === 'optionSet' && 
-                                        attsById[att.attribute] && 
-                                        attsById[att.attribute].optionSet && 
-                                        attsById[att.attribute].optionSet.id && 
-                                        optionSets[attsById[att.attribute].optionSet.id]){   
-                                    val = OptionSetService.getName(optionSets[attsById[att.attribute].optionSet.id].options, val);                                
-                                }
-                                att.value = val;
-                            }
-                        }                                                
-                    });                    
+                angular.forEach(tei.attributes, function(att){
+                    if(attsById[att.attribute]){
+                        att.displayName = attsById[att.attribute].name;
+                    }
+                    att.value = AttributesFactory.formatAttributeValue(att, attsById, optionSets, 'USER');
                 });    
                 return tei;
             });            
             return promise;
         },
-        convertFromUserToApi: function(_tei, optionSets){            
-            var attsById = [];      
-            var def = $q.defer();
-            
+        convertFromUserToApi: function(_tei, optionSets, attsById){            
             var tei = angular.copy(_tei);
-            AttributesFactory.getAll().then(function(atts){
-                angular.forEach(atts, function(att){                        
-                    attsById[att.id] = att;
-                });
-
-                angular.forEach(tei.attributes, function(att){                        
-                    
-                    if(att.type === 'trueOnly'){
-                        if(att.value){
-                            att.value = 'true';
-                        }
-                        else{
-                            att.value = '';
-                        }
-                    }            
-                    else{
-                        var val = att.value;
-                        if(val){
-                            if(att.type === 'date'){
-                                val = DateUtils.formatFromUserToApi(val);
-                            }
-                            if(att.type === 'optionSet' && 
-                                    attsById[att.attribute] && 
-                                    attsById[att.attribute].optionSet && 
-                                    attsById[att.attribute].optionSet.id && 
-                                    optionSets[attsById[att.attribute].optionSet.id]){   
-                                val = OptionSetService.getCode(optionSets[attsById[att.attribute].optionSet.id].options, val);                                
-                            }
-                            att.value = val;
-                        }
-                    }                                            
-                });     
-                
-                def.resolve(tei);
-            });
-            
-            return def.promise;
+            angular.forEach(tei.attributes, function(att){                        
+                att.value = AttributesFactory.formatAttributeValue(att, attsById, optionSets, 'API');                                                                
+            });            
+            return tei;
         },        
-        get: function(entityUid, optionSets){            
+        get: function(entityUid, optionSets, attributesById){            
             var promise = $http.get(  '../api/trackedEntityInstances/' +  entityUid );
-            this.convertFromApiToUser(promise, optionSets).then(function(response){
+            
+            this.convertFromApiToUser(promise, optionSets, attributesById).then(function(response){
                 return response.data; 
             });
             return promise;
@@ -600,28 +534,20 @@
             });            
             return promise;
         },                
-        update: function(tei, optionSets){   
-            var url = '../api/trackedEntityInstances';
-            var def = $q.defer();
-           
-            this.convertFromUserToApi(tei, optionSets).then(function(formattedTei){                
-                $http.put( url + '/' + formattedTei.trackedEntityInstance , formattedTei ).then(function(response){                    
-                    def.resolve( response.data );
-                });
+        update: function(tei, optionSets, attributesById){           
+            var formattedTei = this.convertFromUserToApi(tei,optionSets, attributesById);
+            var promise = $http.put( '../api/trackedEntityInstances/' + formattedTei.trackedEntityInstance , formattedTei ).then(function(response){                    
+                return response.data;
             });
             
-            return def.promise;
+            return promise;
         },
-        register: function(tei, optionSets){            
-            var url = '../api/trackedEntityInstances';
-            var def = $q.defer();
-            
-            this.convertFromUserToApi(tei, optionSets).then(function(formattedTei){
-                $http.post(url, formattedTei).then(function(response){
-                    def.resolve( response.data );
-                });
-            });
-            return def.promise;
+        register: function(tei, optionSets, attributesById){
+            var formattedTei = this.convertFromUserToApi(tei,optionSets, attributesById);
+            var promise = $http.put( '../api/trackedEntityInstances' , formattedTei ).then(function(response){                    
+                return response.data;
+            });            
+            return promise;            
         },
         processAttributes: function(selectedTei, selectedProgram, selectedEnrollment){
             var def = $q.defer();            
@@ -650,81 +576,38 @@
             }       
             return def.promise;
         },
-        reconstructForWebApi: function(attributes, attributesById, optionSets){
-            
+        reconstructForWebApi: function(attributes, attributesById, optionSets){            
             var registrationAttributes = [];
             var formEmpty = true;
             angular.forEach(attributes, function(att){
-                if(att.valueType === 'trueOnly'){
-                    if(att.value){
-                        registrationAttributes.push({attribute: att.id, value: 'true'});
-                    }
-                    else{
-                        registrationAttributes.push({attribute: att.id, value: ''});
-                    }
-                    
+                var val = AttributesFactory.formatAttributeValue(att, attributesById, optionSets, 'API');
+                registrationAttributes.push({attribute: att.id, value: val});
+                if(val){
                     formEmpty = false;
-                }            
-                else{
-                    var val = att.value;
-                    if(val){
-                        if(att.valueType === 'date'){   
-                            val = DateUtils.formatFromUserToApi(val);
-                        }
-                        if(att.valueType === 'optionSet' && 
-                                attributesById[att.id] && 
-                                attributesById[att.id].optionSet && 
-                                optionSets[attributesById[att.id].optionSet.id]){                        
-                            val = OptionSetService.getCode(optionSets[attributesById[att.id].optionSet.id].options, val);
-                        }
-
-                        registrationAttributes.push({attribute: att.id, value: val});
-                        formEmpty = false;
-                    }
-                }                        
+                }                                     
             });
-
             return {attributes: registrationAttributes, formEmpty: formEmpty};
         },
         reconstructForUser: function(tei, attributes, attributesById, optionSets){
             var registrationAttributes = [];
             var formEmpty = true;
-            angular.forEach(attributes, function(att){            
-                if(att.valueType === 'trueOnly'){ 
-                    if(tei[att.id]){
-                        registrationAttributes.push({attribute: att.id, value: 'true'});
-                        formEmpty = false;                    
-                    }
-                    else{
-                        registrationAttributes.push({attribute: att.id, value: ''});
-                        formEmpty = false;
-                    }
-                }            
-                else{
-                    if(tei[att.id] !== '' && tei[att.id]){
-
-                        var val = tei[att.id];                    
-                        if(att.valueType === 'date'){   
-                            val = DateUtils.formatFromApiToUser(val);
-                        }   
-
-                        if(att.valueType === 'optionSet' && attributesById[att.id] && attributesById[att.id].optionSet && optionSets[attributesById[att.id].optionSet.id]){                        
-                            val = OptionSetService.getName(optionSets[attributesById[att.id].optionSet.id].options, val);
-                        }
-
-                        registrationAttributes.push({attribute: att.id, value: val});
-                        formEmpty = false;
-                    }
-                }                        
+            angular.forEach(attributes, function(att){
+                var _att = att;
+                _att.value = tei[att.id];
+                
+                var val = AttributesFactory.formatAttributeValue(_att, attributesById, optionSets, 'USER');
+                registrationAttributes.push({attribute: att.id, value: val});
+                if(val){
+                    formEmpty = false;
+                }                    
             });
-
             return {attributes: registrationAttributes, formEmpty: formEmpty};
-        }
+        }        
     };
 })
 
 /* Factory for getting tracked entity attributes */
-.factory('AttributesFactory', function($q, $rootScope, TCStorageService, orderByFilter) {      
+.factory('AttributesFactory', function($q, $rootScope, TCStorageService, orderByFilter, DateUtils, OptionSetService) {      
 
     return {
         getAll: function(){
@@ -841,6 +724,49 @@
             teiAttributes = orderByFilter(teiAttributes, '-order');
             teiAttributes.reverse();
             return teiAttributes;
+        },
+        formatAttributeValue: function(att, attsById, optionSets, destination){
+            var val = att.value;
+            var type = '';
+            if(att.type){
+                type = att.type;
+            }            
+            if(att.valueType){
+                type = att.valueType;
+            }
+            if(type === 'trueOnly'){
+                if(destination === 'USER'){
+                    val = val === 'true' ? true : '';
+                }
+                else{
+                    val = val === true ? 'true' : '';
+                }                
+            }
+            else{
+                if(val){
+                    if(type === 'date'){
+                        if(destination === 'USER'){
+                            val = DateUtils.formatFromApiToUser(val);
+                        }
+                        else{
+                            val = DateUtils.formatFromUserToApi(val);
+                        }                        
+                    }
+                    if(type === 'optionSet' && 
+                            attsById[att.attribute] && 
+                            attsById[att.attribute].optionSet && 
+                            attsById[att.attribute].optionSet.id && 
+                            optionSets[attsById[att.attribute].optionSet.id]){
+                        if(destination === 'USER'){
+                            val = OptionSetService.getName(optionSets[attsById[att.attribute].optionSet.id].options, val);                                
+                        }
+                        else{
+                            val = OptionSetService.getCode(optionSets[attsById[att.attribute].optionSet.id].options, val);                                
+                        }                        
+                    }                    
+                }
+            }
+            return val;
         }
     };
 })