← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15529: replaced localstorage with indexedDB storage for event capture

 

------------------------------------------------------------
revno: 15529
committer: Abyot Asalefew Gizaw abyota@xxxxxxxxx
branch nick: dhis2
timestamp: Tue 2014-06-03 08:19:46 +0200
message:
  replaced localstorage with indexedDB storage for event capture
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 22:10:38 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2014-06-03 06:19:46 +0000
@@ -18,6 +18,7 @@
                 ModalService,
                 DialogService) {   
    
+                      
     //selected org unit
     $scope.selectedOrgUnit = '';
     
@@ -34,7 +35,8 @@
     $scope.currentEventOrginialValue = '';   
     $scope.displayCustomForm = false;
     $scope.currentElement = {id: '', update: false};
-    
+    $scope.selectedOrgUnit = '';
+        
     //watch for selection of org unit from tree
     $scope.$watch('selectedOrgUnit', function(newObj, oldObj) {
         
@@ -42,19 +44,16 @@
             
             //apply translation - by now user's profile is fetched from server.
             TranslationService.translate();            
-            
-            ProgramFactory.getAll().then(function(programs){
-                if(programs){
-                    $scope.loadPrograms($scope.selectedOrgUnit);     
-                }
-            });
+
+            $scope.loadPrograms();
+
         }
     });
     
     //load programs associated with the selected org unit.
-    $scope.loadPrograms = function(orgUnit) {        
+    $scope.loadPrograms = function() {        
                 
-        $scope.selectedOrgUnit = orgUnit;
+        //$scope.selectedOrgUnit = orgUnit;
         $scope.selectedProgram = null;
         $scope.selectedProgramStage = null;
 
@@ -67,26 +66,26 @@
         $scope.currentEventOrginialValue = ''; 
         $scope.displayCustomForm = false;
         
-        if (angular.isObject($scope.selectedOrgUnit)) {   
-            
+        if (angular.isObject($scope.selectedOrgUnit)) {    
             
             ProgramFactory.getAll().then(function(programs){
-                
-                $scope.programs = [];                
-                angular.forEach(programs, function(program){
-                    if(program.organisationUnits.hasOwnProperty($scope.selectedOrgUnit.id)){
+                $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){
+                if(angular.isObject($scope.programs) && $scope.programs.length === 1){
                     $scope.selectedProgram = $scope.programs[0];
                     $scope.loadEvents();
                 }
-            });
+                
+            });       
         }        
     };    
     
+        
     //get events for the selected program (and org unit)
     $scope.loadEvents = function(){   
         
@@ -104,11 +103,10 @@
         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);                          
-            ProgramStageFactory.get($scope.selectedProgram.programStages[0].id).then(function(programStage){
+            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; 
 

=== 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 22:10:38 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/directives.js	2014-06-03 06:19:46 +0000
@@ -22,6 +22,12 @@
     return {        
         restrict: 'A',        
         link: function(scope, element, attrs){ 
+           
+            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']
+            });
             
             //when tree has loaded, get selected orgunit - if there is any - and inform angular           
             $(function() {                 
@@ -57,11 +63,11 @@
                         var selected = selection.getSelected()[0];
                         selection.getOrganisationUnit(selected).done(function(data){                            
                             if( data ){
-                                scope.selectedOrgUnit = {id: selected, name: data[selected].n};                                  
-                                scope.$apply();
+                                scope.selectedOrgUnit = {id: selected, name: data[selected].n, programs: []};
+                                scope.$apply();                                                              
                             }                        
                         });
-                    } );
+                    });
                     
                 });
             });
@@ -71,7 +77,7 @@
             selection.responseReceived();
             
             function organisationUnitSelected( orgUnits, orgUnitNames ) {
-                scope.selectedOrgUnit = {id: orgUnits[0], name: orgUnitNames[0]};    
+                scope.selectedOrgUnit = {id: orgUnits[0], name: orgUnitNames[0], programs: []};    
                 scope.$apply();                
             }
         }  

=== 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 22:10:38 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js	2014-06-03 06:19:46 +0000
@@ -58,7 +58,12 @@
         var def = $.Deferred();
         var promise = def.promise();
         
-        promise = promise.then( getUserProfile );        
+        promise = promise.then( dhis2.ec.store.open );
+        promise = promise.then( getUserProfile );     
+        promise = promise.then( getMetaPrograms );     
+        promise = promise.then( getPrograms );     
+        promise = promise.then( getProgramStages );    
+        promise = promise.then( getOptionSets );    
         promise.done( function() {           
             selection.responseReceived();            
         });           
@@ -167,10 +172,9 @@
     $.ajax({
         url: '../api/programs.json',
         type: 'GET',
-        data:'type=3&paging=false&include=id,name,version'
-    }).done( function(response) {             
-        localStorage[PROGRAMS_METADATA] = JSON.stringify(response.programs);           
-        def.resolve(response.programs);
+        data:'type=3&paging=false&include=id,name,version,programStages[id,version,programStageDataElements[dataElement[optionSet[id,version]]]]'
+    }).done( function(response) {                     
+        def.resolve( response.programs );
     });
     
     return def.promise(); 
@@ -182,38 +186,49 @@
         return;
     }
     
+    var mainDef = $.Deferred();
+    var mainPromise = mainDef.promise();
+
     var def = $.Deferred();
     var promise = def.promise();
 
-    _.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{
+    var builder = $.Deferred();
+    var build = builder.promise();
+
+    _.each( _.values( programs ), function ( program ) {
+        build = build.then(function() {
+            var d = $.Deferred();
+            var p = d.promise();
+            dhis2.ec.store.get('eventCapturePrograms', program.id).done(function(obj) {
+                if(!obj || obj.version !== program.version) {
+                    promise = promise.then( getProgram( program.id ) );
+                }
+
                 d.resolve();
-            }
-        }
-        return p;
-    });
-    
-    promise = promise.then(function() {
-        return $.Deferred().resolve( programs );
-    });
-    
-    def.resolve( programs );
-    
-    return promise;   
+            });
+
+            return p;
+        });
+    });
+
+    build.done(function() {
+        def.resolve();
+
+        promise = promise.done( function () {
+            mainDef.resolve( programs );
+        } );
+    });
+
+    builder.resolve();
+
+    return mainPromise;
 }
 
 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]',
+            url: '../api/programs.json?filter=id:eq:' + id +'&include=id,name,version,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name]',
             type: 'GET'
         }).done( function( response ){
             
@@ -233,7 +248,7 @@
 
                 program.userRoles = ur;
 
-                localStorage[program.id] = JSON.stringify(program);
+                dhis2.ec.store.set( 'eventCapturePrograms', program );
 
             });         
         });
@@ -246,34 +261,113 @@
         return;
     }
     
-    var def = $.Deferred();
-    var promise = def.promise();
-
-    _.each( _.values( programs ), function ( program ) {
-        program = JSON.parse( localStorage[program.id] );
-        _.each( _.values( program.programStages ), function( programStage ) {
-            promise = promise.then( getProgramStage( programStage.id ) );
+    var mainDef = $.Deferred();
+    var mainPromise = mainDef.promise();
+
+    var def = $.Deferred();
+    var promise = def.promise();
+
+    var builder = $.Deferred();
+    var build = builder.promise();
+
+    _.each( _.values( programs ), function ( program ) {
+        build = build.then(function() {
+            var d = $.Deferred();
+            var p = d.promise();
+            dhis2.ec.store.get('programStages', program.programStages[0].id).done(function(obj) {
+                if(!obj || obj.version !== program.programStages[0].version) {
+                    promise = promise.then( getProgramStage( program.programStages[0].id ) );
+                }
+
+                d.resolve();
+            });
+
+            return p;
+        });
+    });
+
+    build.done(function() {
+        def.resolve();
+
+        promise = promise.done( function () {
+            mainDef.resolve( programs );
+        } );
+    });
+
+    builder.resolve();
+
+    return mainPromise;    
+}
+
+function getProgramStage( id )
+{
+    return function() {
+        return $.ajax( {
+            url: '../api/programStages.json?filter=id:eq:' + id +'&include=id,name,dataEntryForm,description,minDaysFromStart,repeatable,programStageDataElements[displayInReports,allowProvidedElsewhere,allowDateInFuture,compulsory,dataElement[id,name,type,optionSet[id]]]',
+            type: 'GET'
+        }).done( function( response ){            
+            _.each( _.values( response.programStages ), function( programStage ) {                
+                dhis2.ec.store.set( 'programStages', programStage );
+            });
+        });
+    };
+}
+
+function getOptionSets( programs )
+{
+    if( !programs ){
+        return;
+    }
+    
+    var mainDef = $.Deferred();
+    var mainPromise = mainDef.promise();
+
+    var def = $.Deferred();
+    var promise = def.promise();
+
+    var builder = $.Deferred();
+    var build = builder.promise();    
+    
+
+    _.each( _.values( programs ), function ( program ) {
+        _.each(_.values( program.programStages[0].programStageDataElements), function(prStDe){
+            build = build.then(function() {
+                var d = $.Deferred();
+                var p = d.promise();
+                dhis2.ec.store.get('optionSets', prStDe.dataElement.optionSet.id).done(function(obj) {                    
+                    if(!obj || obj.version !== prStDe.dataElement.optionSet.version) {
+                        promise = promise.then( getOptionSet( prStDe.dataElement.optionSet.id ) );
+                    }
+                    d.resolve();
+                });
+
+                return p;
+            });
         });        
     });
-    
-    promise = promise.then(function() {
-        return def.resolve();
+
+    build.done(function() {
+        def.resolve();
+
+        promise = promise.done( function () {
+            mainDef.resolve( programs );
+        } );
     });
-    
-    def.resolve();
-    
-    return promise; 
+
+    builder.resolve();
+
+    return mainPromise;    
 }
 
-function getProgramStage( id )
+function getOptionSet( id )
 {
     return function() {
         return $.ajax( {
-            url: '../api/programStages.json?filter=id:eq:' + id +'&include=id,name,description,minDaysFromStart,repeatable,dataEntryForm,programStageDataElements[displayInReports,allowProvidedElsewhere,allowDateInFuture,compulsory,dataElement[id,name,type,optionSet[id,name,options]]]',
+            url: '../api/optionSets.json?filter=id:eq:' + id +'&include=id,name,version,options',
             type: 'GET'
         }).done( function( response ){            
-            _.each( _.values( response.programStages ), function( programStage ) {
-                localStorage[programStage.id] = JSON.stringify(programStage);
+            _.each( _.values( response.optionSets ), function( optionSet ) {                
+                dhis2.ec.store.set( 'optionSets', optionSet );
             });
         });
     };

=== 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 22:10:38 +0000
+++ dhis-2/dhis-web/dhis-web-event-capture/src/main/webapp/dhis-web-event-capture/scripts/services.js	2014-06-03 06:19:46 +0000
@@ -22,66 +22,66 @@
 })
 
 /* Factory to fetch programs */
-.factory('ProgramFactory', function($http) {
+.factory('ProgramFactory', function($q, $rootScope) {  
     
-    var programUid, programPromise;
-    var programs, programsPromise;
-    var program;
+    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']
+    });
+        
     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;
-        },
+            
+            var def = $q.defer();
+            
+            dhis2.ec.store.open().done(function(){
+                dhis2.ec.store.getAll('eventCapturePrograms').done(function(programs){
+                    
+                    $rootScope.$apply(function(){
+                        def.resolve(programs);
+                    });                    
+                });
+            });            
+            
+            return def.promise;            
+        }        
         
-        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) {  
+.factory('ProgramStageFactory', function($q, $rootScope) {  
+
+    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']
+    });
     
-    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];
+            
+            var def = $q.defer();
+            
+            dhis2.ec.store.open().done(function(){
+                dhis2.ec.store.get('programStages', uid).done(function(pst){                    
+                    angular.forEach(pst.programStageDataElements, function(pstDe){   
+                        if(pstDe.dataElement.optionSet){
+                            dhis2.ec.store.get('optionSets', pstDe.dataElement.optionSet.id).done(function(optionSet){
+                                pstDe.dataElement.optionSet = optionSet;                                
+                            });                            
+                        }
+                        $rootScope.$apply(function(){
+                            def.resolve(pst);
+                        });
+                    });                                        
                 });
-            }
-            return promise;
-        }
-    };    
+            });                        
+            return def.promise;            
+        }        
+    };        
 })
 
 /* factory for handling events */