← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15525: turned-off localestorage for event capture, soon to be replaced with dhis2.storage

 

------------------------------------------------------------
revno: 15525
committer: Abyot Asalefew Gizaw abyota@xxxxxxxxx
branch nick: dhis2
timestamp: Tue 2014-06-03 00:10:38 +0200
message:
  turned-off localestorage for event capture, soon to be replaced with dhis2.storage
modified:
  dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/controllers.js
  dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/directives.js
  dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js
  dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-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-event-capture/src/main/webapp/dhis-web-event-capture/scripts/controllers.js'
--- dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2014-06-02 10:50:49 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2014-06-02 22:10:38 +0000
@@ -9,8 +9,9 @@
                 $filter,
                 $modal,
                 Paginator,
-                TranslationService,
-                storage,
+                TranslationService,                
+                ProgramFactory,
+                ProgramStageFactory,
                 DHIS2EventFactory,       
                 DHIS2EventService,
                 ContextMenuSelectedItem,
@@ -40,12 +41,13 @@
         if( angular.isObject($scope.selectedOrgUnit)){            
             
             //apply translation - by now user's profile is fetched from server.
-            TranslationService.translate();
+            TranslationService.translate();            
             
-            var programs = storage.get('EVENT_PROGRAMS');            
-            if( programs ){                
-                $scope.loadPrograms($scope.selectedOrgUnit);     
-            }            
+            ProgramFactory.getAll().then(function(programs){
+                if(programs){
+                    $scope.loadPrograms($scope.selectedOrgUnit);     
+                }
+            });
         }
     });
     
@@ -66,28 +68,22 @@
         $scope.displayCustomForm = false;
         
         if (angular.isObject($scope.selectedOrgUnit)) {   
-
-            $scope.programs = [];
-            
-            var programs = storage.get('EVENT_PROGRAMS');
-            
-            if( programs && programs != 'undefined' ){
-                for(var i=0; i<programs.length; i++){
-                    var program = storage.get(programs[i].id);   
-                    if(angular.isObject(program)){
-                        if(program.organisationUnits.hasOwnProperty(orgUnit.id)){
-                            $scope.programs.push(program);
-                        }
-                    }                    
-                }
-                
-                if( !angular.isUndefined($scope.programs)){                    
-                    if($scope.programs.length === 1){
-                        $scope.selectedProgram = $scope.programs[0];
-                        $scope.loadEvents();
-                    }                    
-                }
-            }
+            
+            
+            ProgramFactory.getAll().then(function(programs){
+                
+                $scope.programs = [];                
+                angular.forEach(programs, function(program){
+                    if(program.organisationUnits.hasOwnProperty($scope.selectedOrgUnit.id)){
+                        $scope.programs.push(program);
+                    }
+                });
+                
+                if($scope.programs && $scope.programs.length === 1){
+                    $scope.selectedProgram = $scope.programs[0];
+                    $scope.loadEvents();
+                }
+            });
         }        
     };    
     
@@ -108,108 +104,114 @@
         if( $scope.selectedProgram && $scope.selectedProgram.programStages[0].id){
             
             //because this is single event, take the first program stage
-            $scope.selectedProgramStage = storage.get($scope.selectedProgram.programStages[0].id);   
-            
-            //$scope.customForm = CustomFormService.processCustomForm($scope.selectedProgramStage);
-            $scope.customForm = $scope.selectedProgramStage.dataEntryForm ? $scope.selectedProgramStage.dataEntryForm.htmlCode : null; 
-
-            $scope.programStageDataElements = [];  
-            $scope.eventGridColumns = [];
-            $scope.filterTypes = {};
-
-            $scope.newDhis2Event = {dataValues: []};
-            $scope.currentEvent = {dataValues: []};
-
-            angular.forEach($scope.selectedProgramStage.programStageDataElements, function(prStDe){
-                $scope.programStageDataElements[prStDe.dataElement.id] = prStDe; 
-                
-                //generate grid headers using program stage data elements
-                //create a template for new event
-                //for date type dataelements, filtering is based on start and end dates
-                var dataElement = prStDe.dataElement;
-                var name = dataElement.formName || dataElement.name;
-                $scope.newDhis2Event.dataValues.push({id: dataElement.id, value: ''});                       
-                $scope.eventGridColumns.push({name: name, id: dataElement.id, type: dataElement.type, compulsory: prStDe.compulsory, showFilter: false, show: prStDe.displayInReports});
-                
-                $scope.filterTypes[dataElement.id] = dataElement.type;
-                
-                if(dataElement.type === 'date' || dataElement.type === 'int' ){
-                     $scope.filterText[dataElement.id]= {};
-                }
-                
-            });           
-
-            //Load events for the selected program stage and orgunit
-            DHIS2EventFactory.getByStage($scope.selectedOrgUnit.id, $scope.selectedProgramStage.id, $scope.pager ).then(function(data){
-                
-                if(data.events){
-                    $scope.eventLength = data.events.length;
-                }                
-                
-                $scope.dhis2Events = data.events; 
-                
-                if( data.pager ){
-                    $scope.pager = data.pager;
-                    $scope.pager.toolBarDisplay = 5;
-
-                    Paginator.setPage($scope.pager.page);
-                    Paginator.setPageCount($scope.pager.pageCount);
-                    Paginator.setPageSize($scope.pager.pageSize);
-                    Paginator.setItemCount($scope.pager.total);                    
-                }
-                
-                //process event list for easier tabular sorting
-                if( angular.isObject( $scope.dhis2Events ) ) {
-
-                    for(var i=0; i < $scope.dhis2Events.length; i++){  
-                        
-                        //check if event is empty
-                        if(!angular.isUndefined($scope.dhis2Events[i].dataValues)){                            
-                            
-                            angular.forEach($scope.dhis2Events[i].dataValues, function(dataValue){
-
-                                //converting event.datavalues[i].datavalue.dataelement = value to
-                                //event[dataElement] = value for easier grid display.                                
-                                if($scope.programStageDataElements[dataValue.dataElement]){                                    
-                                    
-                                    var dataElement = $scope.programStageDataElements[dataValue.dataElement].dataElement;
-                                    
-                                    if(angular.isObject(dataElement)){                               
-
-                                        //converting int string value to integer for proper sorting.
-                                        if(dataElement.type == 'int'){
-                                            if( !isNaN(parseInt(dataValue.value)) ){
-                                                dataValue.value = parseInt(dataValue.value);
-                                            }
-                                            else{
-                                                dataValue.value = '';
-                                            }                                        
-                                        }
-                                        else if( dataElement.type == 'trueOnly'){
-                                            if(dataValue.value == 'true'){
-                                                dataValue.value = true;
-                                            }
-                                            else{
-                                                dataValue.value = false;
-                                            }
+            //$scope.selectedProgramStage = storage.get($scope.selectedProgram.programStages[0].id);                          
+            ProgramStageFactory.get($scope.selectedProgram.programStages[0].id).then(function(programStage){
+                
+                $scope.selectedProgramStage = programStage;   
+                
+                //$scope.customForm = CustomFormService.processCustomForm($scope.selectedProgramStage);
+                $scope.customForm = $scope.selectedProgramStage.dataEntryForm ? $scope.selectedProgramStage.dataEntryForm.htmlCode : null; 
+
+                $scope.programStageDataElements = [];  
+                $scope.eventGridColumns = [];
+                $scope.filterTypes = {};
+
+                $scope.newDhis2Event = {dataValues: []};
+                $scope.currentEvent = {dataValues: []};
+
+                angular.forEach($scope.selectedProgramStage.programStageDataElements, function(prStDe){
+                    $scope.programStageDataElements[prStDe.dataElement.id] = prStDe; 
+
+                    //generate grid headers using program stage data elements
+                    //create a template for new event
+                    //for date type dataelements, filtering is based on start and end dates
+                    var dataElement = prStDe.dataElement;
+                    var name = dataElement.formName || dataElement.name;
+                    $scope.newDhis2Event.dataValues.push({id: dataElement.id, value: ''});                       
+                    $scope.eventGridColumns.push({name: name, id: dataElement.id, type: dataElement.type, compulsory: prStDe.compulsory, showFilter: false, show: prStDe.displayInReports});
+
+                    $scope.filterTypes[dataElement.id] = dataElement.type;
+
+                    if(dataElement.type === 'date' || dataElement.type === 'int' ){
+                         $scope.filterText[dataElement.id]= {};
+                    }
+
+                });           
+
+                //Load events for the selected program stage and orgunit
+                DHIS2EventFactory.getByStage($scope.selectedOrgUnit.id, $scope.selectedProgramStage.id, $scope.pager ).then(function(data){
+
+                    if(data.events){
+                        $scope.eventLength = data.events.length;
+                    }                
+
+                    $scope.dhis2Events = data.events; 
+
+                    if( data.pager ){
+                        $scope.pager = data.pager;
+                        $scope.pager.toolBarDisplay = 5;
+
+                        Paginator.setPage($scope.pager.page);
+                        Paginator.setPageCount($scope.pager.pageCount);
+                        Paginator.setPageSize($scope.pager.pageSize);
+                        Paginator.setItemCount($scope.pager.total);                    
+                    }
+
+                    //process event list for easier tabular sorting
+                    if( angular.isObject( $scope.dhis2Events ) ) {
+
+                        for(var i=0; i < $scope.dhis2Events.length; i++){  
+
+                            //check if event is empty
+                            if(!angular.isUndefined($scope.dhis2Events[i].dataValues)){                            
+
+                                angular.forEach($scope.dhis2Events[i].dataValues, function(dataValue){
+
+                                    //converting event.datavalues[i].datavalue.dataelement = value to
+                                    //event[dataElement] = value for easier grid display.                                
+                                    if($scope.programStageDataElements[dataValue.dataElement]){                                    
+
+                                        var dataElement = $scope.programStageDataElements[dataValue.dataElement].dataElement;
+
+                                        if(angular.isObject(dataElement)){                               
+
+                                            //converting int string value to integer for proper sorting.
+                                            if(dataElement.type == 'int'){
+                                                if( !isNaN(parseInt(dataValue.value)) ){
+                                                    dataValue.value = parseInt(dataValue.value);
+                                                }
+                                                else{
+                                                    dataValue.value = '';
+                                                }                                        
+                                            }
+                                            else if( dataElement.type == 'trueOnly'){
+                                                if(dataValue.value == 'true'){
+                                                    dataValue.value = true;
+                                                }
+                                                else{
+                                                    dataValue.value = false;
+                                                }
+                                            }                                    
                                         }                                    
-                                    }                                    
-                                }
-                                
-                                $scope.dhis2Events[i][dataValue.dataElement] = dataValue.value; 
-                            });  
-
-                            delete $scope.dhis2Events[i].dataValues;
-                        }
-                        else{//event is empty, remove from grid
-                            var index = $scope.dhis2Events.indexOf($scope.dhis2Events[i]);                           
-                            $scope.dhis2Events.splice(index,1);
-                            i--;                           
-                        }
-                    }                                  
-                }                
-                $scope.eventFetched = true;
-            });            
+                                    }
+
+                                    $scope.dhis2Events[i][dataValue.dataElement] = dataValue.value; 
+                                });  
+
+                                delete $scope.dhis2Events[i].dataValues;
+                            }
+                            else{//event is empty, remove from grid
+                                var index = $scope.dhis2Events.indexOf($scope.dhis2Events[i]);                           
+                                $scope.dhis2Events.splice(index,1);
+                                i--;                           
+                            }
+                        }                                  
+                    }                
+                    $scope.eventFetched = true;
+                });            
+                
+            });
+            
         }        
     };
     

=== modified file 'dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/directives.js'
--- dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/directives.js	2014-06-02 10:50:49 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/directives.js	2014-06-02 22:10:38 +0000
@@ -23,6 +23,49 @@
         restrict: 'A',        
         link: function(scope, element, attrs){ 
             
+            //when tree has loaded, get selected orgunit - if there is any - and inform angular           
+            $(function() {                 
+                
+                var adapters = [];
+                var partial_adapters = [];
+
+                if( dhis2.ou.memoryOnly ) {
+                    adapters = [ dhis2.storage.InMemoryAdapter ];
+                    partial_adapters = [ dhis2.storage.InMemoryAdapter ];
+                } else {
+                    adapters = [ dhis2.storage.IndexedDBAdapter, dhis2.storage.DomLocalStorageAdapter, dhis2.storage.InMemoryAdapter ];
+                    partial_adapters = [ dhis2.storage.IndexedDBAdapter, dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter ];
+                }
+
+                dhis2.ou.store = new dhis2.storage.Store({
+                    name: OU_STORE_NAME,
+                    objectStores: [
+                        {
+                            name: OU_KEY,
+                            adapters: adapters
+                        },
+                        {
+                            name: OU_PARTIAL_KEY,
+                            adapters: partial_adapters
+                        }
+                    ]
+                });
+
+                dhis2.ou.store.open().done( function() {
+                    selection.load();
+                    $( "#orgUnitTree" ).one( "ouwtLoaded", function() {
+                        var selected = selection.getSelected()[0];
+                        selection.getOrganisationUnit(selected).done(function(data){                            
+                            if( data ){
+                                scope.selectedOrgUnit = {id: selected, name: data[selected].n};                                  
+                                scope.$apply();
+                            }                        
+                        });
+                    } );
+                    
+                });
+            });
+            
             //listen to user selection, and inform angular         
             selection.setListenerFunction( organisationUnitSelected );            
             selection.responseReceived();

=== modified file 'dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js'
--- dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js	2014-06-02 10:50:49 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js	2014-06-02 22:10:38 +0000
@@ -7,8 +7,7 @@
 // Instance of the StorageManager
 dhis2.ec.storageManager = new StorageManager();
 
-var DAO = DAO || {};
-
+var EC_STORE_NAME = "dhis2ec";
 var i18n_no_orgunits = 'No organisation unit attached to current user, no data entry possible';
 var i18n_offline_notification = 'You are offline, data will be stored locally';
 var i18n_online_notification = 'You are online';
@@ -22,10 +21,10 @@
 
 var EVENT_VALUES = 'EVENT_VALUES';
 
-DAO.store = new dhis2.storage.Store({
-    name: 'dhis2',
-    adapters: [dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter],
-    objectStores: ['optionSets']
+dhis2.ec.store = new dhis2.storage.Store({
+    name: EC_STORE_NAME,
+    adapters: [dhis2.storage.IndexedDBAdapter, dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter],
+    objectStores: ['eventCapturePrograms', 'programStages', 'optionSets']
 });
 
 (function($) {
@@ -59,12 +58,9 @@
         var def = $.Deferred();
         var promise = def.promise();
         
-        promise = promise.then( getUserProfile );
-        promise = promise.then( getMetaPrograms );     
-        promise = promise.then( getPrograms );      
-        promise = promise.then( getProgramStages );
+        promise = promise.then( getUserProfile );        
         promise.done( function() {           
-            selection.responseReceived();                      
+            selection.responseReceived();            
         });           
         
         def.resolve();
@@ -169,9 +165,9 @@
     var def = $.Deferred();
 
     $.ajax({
-        url: '../api/programs',
+        url: '../api/programs.json',
         type: 'GET',
-        data:'type=3&paging=false'
+        data:'type=3&paging=false&include=id,name,version'
     }).done( function(response) {             
         localStorage[PROGRAMS_METADATA] = JSON.stringify(response.programs);           
         def.resolve(response.programs);
@@ -189,8 +185,19 @@
     var def = $.Deferred();
     var promise = def.promise();
 
-    _.each( _.values( programs ), function ( program ) {        
-        promise = promise.then( getProgram( program.id ) );
+    _.each( _.values( programs ), function ( program ) {   
+        var d = $.Deferred();
+        var p = d.promise();
+        var localProgram = localStorage[program.id];
+        if(localProgram){
+            localProgram = JSON.parse( localProgram );            
+            if(program.version !== localProgram.version){
+                promise = promise.then( getProgram( program.id ) );
+            }else{
+                d.resolve();
+            }
+        }
+        return p;
     });
     
     promise = promise.then(function() {
@@ -203,8 +210,7 @@
 }
 
 function getProgram( id )
-{   
-
+{
     return function() {
         return $.ajax( {
             url: '../api/programs.json?filter=id:eq:' + id +'&include=id,name,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name]',

=== modified file 'dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/services.js	2014-06-02 10:50:49 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/services.js	2014-06-02 22:10:38 +0000
@@ -21,6 +21,69 @@
     };  
 })
 
+/* Factory to fetch programs */
+.factory('ProgramFactory', function($http) {
+    
+    var programUid, programPromise;
+    var programs, programsPromise;
+    var program;
+    return {
+        
+        getAll: function(){
+            if( !programsPromise ){
+                programsPromise = $http.get('../api/programs.json?filter=type:eq:3&include=id,name,version,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name]').then(function(response){
+                //programsPromise = $http.get('../api/programs.json?filter=type:eq:3&include=id,name,version').then(function(response){
+                   programs = response.data.programs;
+                   
+                   angular.forEach(programs, function ( program ) {
+                       var ou = {};
+                       angular.forEach(program.organisationUnits, function(o){
+                           ou[o.id] = o.name;
+                       });
+                       
+                       program.organisationUnits = ou;
+                       var ur = {};
+                       angular.forEach(program.userRoles, function(u){
+                           ur[u.id] = u.name;
+                       });                       
+                       program.userRoles = ur;
+                   });
+                   return programs;
+                });                        
+            }
+            return programsPromise;
+        },
+        
+        get: function(uid){
+            if( programUid !== uid ){
+                programPromise = $http.get('../api/programs.json?filter=id:eq:' + uid +'&include=id,name,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name]').then(function(response){
+                    programUid = response.data.id; 
+                    program = response.data;                     
+                    return program;
+                });
+            }
+            return programPromise;
+        }
+    };
+})
+
+/* Factory to fetch programStages */
+.factory('ProgramStageFactory', function($http) {  
+    
+    var programStageUid, promise;   
+    return {        
+        get: function(uid){
+            if( programStageUid !== uid ){
+                promise = $http.get( '../api/programStages.json?filter=id:eq:' + uid +'&include=id,name,version,description,minDaysFromStart,repeatable,dataEntryForm,programStageDataElements[displayInReports,allowProvidedElsewhere,allowDateInFuture,compulsory,dataElement[id,name,type,optionSet[id,name,options]]]').then(function(response){
+                   programStageUid = response.data.programStages[0].id;
+                   return response.data.programStages[0];
+                });
+            }
+            return promise;
+        }
+    };    
+})
+
 /* factory for handling events */
 .factory('DHIS2EventFactory', function($http) {