← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10593: reworked the way data is stored offline

 

------------------------------------------------------------
revno: 10593
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-04-17 13:46:31 +0700
message:
  reworked the way data is stored offline
modified:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.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-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-04-16 13:25:17 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-04-17 06:46:31 +0000
@@ -2,8 +2,7 @@
 
 var PROGRAMS_STORE = 'anonymousPrograms';
 var PROGRAM_STAGES_STORE = 'anonymousProgramStages';
-var EXECUTION_DATES_STORE = 'anonymousExecutionDates';
-var DATA_VALUES_STORE = 'anonymousDataValues';
+var OFFLINE_DATA_STORE = 'anonymousExecutionDates';
 
 function initalizeProgramStages() {
     DAO.programStages = new dhis2.storage.Store( {name: PROGRAM_STAGES_STORE, adapter: 'dom-ss'}, function(store) {
@@ -41,27 +40,22 @@
     } );
 }
 
-function initializeExecutionDates() {
-    DAO.executionDates = new dhis2.storage.Store( {name: EXECUTION_DATES_STORE, adapter: 'dom'}, function(store) {
-        $( document ).trigger('dhis2.anonymous.executionDatesInitialized');
-    });
-}
-
-function initializeDataValues() {
-    DAO.dataValues = new dhis2.storage.Store( {name: DATA_VALUES_STORE, adapter: 'dom'}, function(store) {
-        $( document ).trigger('dhis2.anonymous.dataValuesInitialized');
+function initializeOfflineData() {
+    DAO.offlineData = new dhis2.storage.Store( {name: OFFLINE_DATA_STORE, adapter: 'dom'}, function(store) {
+        $( document ).trigger('dhis2.anonymous.offlineData');
     });
 }
 
 function showOfflineEvents() {
-    DAO.executionDates.fetchAll(function(store, arr) {
+    DAO.offlineData.fetchAll(function(store, arr) {
         var target = $( '#offlineEventList' );
         target.children().remove();
 
         if ( arr.length > 0 ) {
             var template = $( '#offline-event-template' );
 
-            $.each( arr, function ( idx, event ) {
+            $.each( arr, function ( idx, item ) {
+                var event = item.executionDate;
                 event.index = idx + 1;
                 var tmpl = _.template( template.html() );
                 var html = tmpl(event);
@@ -80,31 +74,50 @@
 var haveLocalData = false;
 
 function checkOfflineData() {
-    DAO.executionDates.fetchAll( function ( store, arr ) {
-        if ( arr.length > 0 ) {
-            haveLocalData = true;
-        }
-
+    DAO.offlineData.fetchAll( function ( store, arr ) {
+        haveLocalData = arr.length > 0;
         $( document ).trigger('dhis2.anonymous.checkOfflineData');
     } );
 }
 
-function uploadExecutionDate( key, programId, executionDate, organisationUnitId ) {
-    return ajaxExecutionDate(programId, "0", executionDate, organisationUnitId ).done(function(json) {
-        if ( json.response == 'success' ) {
-            // console.log( key + " turned into " + json.message );
-
-            DAO.executionDates.remove(key, function(store) {
-                showOfflineEvents();
-            });
-        }
-    } );
+function uploadOfflineData( item, key, programId, executionDate, organisationUnitId ) {
+    key = item.key;
+    programId = item.executionDate.programId;
+    executionDate = item.executionDate.executionDate;
+    organisationUnitId = item.executionDate.organisationUnitId;
+
+    if(key.indexOf('local') != -1) {
+        ajaxExecutionDate(programId, "0", executionDate, organisationUnitId ).done(function(json) {
+            if ( json.response == 'success' ) {
+                // console.log( key + " turned into " + json.message );
+
+                if ( !item.values || _.keys(item.values).length == 0 ) {
+                    DAO.offlineData.remove( key, function ( store ) {
+                        showOfflineEvents();
+                    } );
+                } else {
+                    // change key from old local-prefixed to actual psid
+                    DAO.offlineData.fetch( key, function ( store, arr ) {
+                        var obj = arr[0];
+                        obj.executionDate.programInstanceId = json.message;
+                        delete obj.key;
+
+                        store.add(json.message, obj, function(store) {
+                            DAO.offlineData.remove( key, function ( store ) {
+                                showOfflineEvents();
+                            } );
+                        });
+                    } );
+                }
+            }
+        } );
+    }
 }
 
 function uploadLocalData() {
     setHeaderWaitMessage( i18n_uploading_data_notification );
 
-    DAO.executionDates.fetchAll( function ( store, arr ) {
+    DAO.offlineData.fetchAll( function ( store, arr ) {
         if(arr.length == 0) {
             setHeaderDelayMessage( i18n_sync_success );
             return;
@@ -115,7 +128,7 @@
 
         $.each(arr, function(idx, item) {
             promise = promise.pipe(function () {
-                uploadExecutionDate(item.key, item.programId, item.executionDate, item.organisationUnitId);
+                uploadOfflineData( item );
             });
         });
 
@@ -151,8 +164,7 @@
         } );
 
         initalizeProgramStages();
-        initializeExecutionDates();
-        initializeDataValues();
+        initializeOfflineData();
     } );
 
     $( document ).bind( 'dhis2.online', function ( event, loggedIn ) {
@@ -699,7 +711,7 @@
 
     if( s.indexOf("local") != -1) {
         if ( confirm( i18n_comfirm_delete_event ) ) {
-            DAO.executionDates.remove(programStageId, function(store) {
+            DAO.offlineData.remove(programStageId, function(store) {
                 // redisplay list
                 showOfflineEvents();
             });
@@ -861,7 +873,7 @@
                 }
             } ).fail( function () {
                 if(programStageInstanceId == 0) {
-                    DAO.executionDates.keys(function(store, keys) {
+                    DAO.offlineData.keys(function(store, keys) {
                         var i = 100;
 
                         for(; i<10000; i++) {
@@ -874,8 +886,9 @@
                         jQuery( "#executionDate" ).css( 'background-color', SUCCESS_COLOR );
                         showUpdateEvent( programStageInstanceId );
 
-                        var data = createExecutionDate(programId, programStageInstanceId, executionDate, organisationUnitId);
-                        DAO.executionDates.add(programStageInstanceId, data);
+                        var data = {};
+                        data.executionDate = createExecutionDate(programId, programStageInstanceId, executionDate, organisationUnitId);
+                        DAO.offlineData.add(programStageInstanceId, data);
                     });
                 } else {
                     // if we have a programStageInstanceId, just reuse that one

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js	2013-04-17 03:59:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/entry.js	2013-04-17 06:46:31 +0000
@@ -285,7 +285,7 @@
  
     function handleHttpError( errorCode )
     {
-        if( getProgramType() == 3 && DAO.dataValues ) {
+        if( getProgramType() == 3 && DAO.offlineData ) {
             var data = {
                 providedElsewhere: byId( providedElsewhereId ) != null ? byId( providedElsewhereId ).checked : false,
                 value: value != '' ? htmlEncode( value ) : value
@@ -294,20 +294,16 @@
             var dataValueKey = $( '#programStageInstanceId' ).val();
             var key = dataElementUid;
 
-            DAO.dataValues.fetch( dataValueKey, function ( store, arr ) {
-                if ( arr.length == 0 ) {
-                    var obj = {
-                        key: data
-                    };
-
-                    store.add( dataValueKey, obj );
-                } else {
-                    var obj = arr[0];
-                    obj[key] = data;
-
-                    store.add( dataValueKey, obj );
+            DAO.offlineData.fetch( dataValueKey, function ( store, arr ) {
+                var obj = arr[0];
+
+                if ( !obj.values ) {
+                    obj.values = {};
                 }
 
+                obj.values[key] = data;
+
+                store.add( dataValueKey, obj );
                 markValue( resultColor );
             } );
         } else {