← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4347: made uploadLocalData correctly chain ajax-calls

 

------------------------------------------------------------
revno: 4347
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-08-18 12:02:32 +0200
message:
  made uploadLocalData correctly chain ajax-calls
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm


--
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-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties	2011-08-18 07:04:26 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties	2011-08-18 10:02:32 +0000
@@ -130,4 +130,6 @@
 operation_not_available_offline		= This operation is not available in off-line mode
 online_notification					= You are online
 offline_notification				= You are offline - data will be stored locally
+need_to_sync_notification			= There is data stored locally, please sync with server
+sync_now							= Sync
 uploading_data_notification			= Uploading locally stored data to the server

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-08-18 09:04:06 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-08-18 10:02:32 +0000
@@ -66,9 +66,26 @@
 
     $( document ).bind( 'dhis2.online', function( event, loggedIn ) {
         if ( loggedIn ) {
-            if( isHeaderMessageVisible() ) {
+            if( haveLocalData()) {
+                var message = i18n_need_to_sync_notification + '<button id="sync_button" type="button">' + i18n_sync_now + '</button>';
+                
+                if(isHeaderMessageVisible())
+                {
+                    updateHeaderMessage( message );
+                } 
+                else 
+                {
+                    setHeaderMessage( message );
+                }
+                
+                $("#sync_button").bind("click", uploadLocalData);
+            }
+            else if( isHeaderMessageVisible() )
+            {
                 updateHeaderMessage( i18n_online_notification );
-            } else {
+            }
+            else
+            {
                 setHeaderMessage( i18n_online_notification );
             }
         } 
@@ -105,57 +122,132 @@
 		organisationUnitAssociationSetMap = json.metaData.organisationUnitAssociationSetMap;
 		
 		console.log( 'Meta-data loaded' );		
-		uploadLocalData();
+//		uploadLocalData();
 	} );
 }
 
+function haveLocalData()
+{
+    var dataValues = storageManager.getAllDataValues();
+    var completeDataSets = getCompleteDataSetsLocalVariable();
+
+    if(dataValues != null || completeDataSets != null)
+    {
+        return true;
+    }
+
+    return false;
+}
+
 function uploadLocalData() 
 {
+    if(!haveLocalData())
+    {
+        return;
+    }
+
     var dataValues = storageManager.getAllDataValues();
     var completeDataSets = getCompleteDataSetsLocalVariable();
 
-    var oldHeaderMessage = getHeaderMessage();
     setHeaderWaitMessage( i18n_uploading_data_notification );
 
-    for ( var dataValueKey in dataValues ) 
-    {
-        var dataValue = dataValues[dataValueKey];
+    var dataValuesArray = [];
+
+    for(var dataValueKey in dataValues)
+    {
+        dataValuesArray.push(dataValueKey);
+    }
+
+    var completeDataSetsArray = [];
+    
+    for(var completeDataSetKey in completeDataSets)
+    {
+        completeDataSetsArray.push(completeDataSetKey);
+    }
+
+    function pushCompleteDataSets(array) {
+        if(array.length < 1)
+        {
+            return;
+        }
+
+        var key = array[0];
+        var value = completeDataSets[key];
+
+        console.log("Upload CompleteDataSet: " + key + ", with value: " + value);
+
+        $.ajax({
+            url: 'registerCompleteDataSet.action',
+            data: value,
+            dataType: 'json',
+            success: function( data, textStatus, jqXHR ) {
+//                clearCompleteDataSetLocally(value);
+                console.log( 'Successfully saved complete dataset with value: ' + value );
+                (array = array.slice(1)).length && pushCompleteDataSets(array);
+
+                if(array.length < 1)
+                {
+                    if ( isHeaderMessageVisible() )
+                    {
+                        updateHeaderMessage(i18n_online_notification);
+                    }
+                    else
+                    {
+                        setHeaderMessage(i18n_online_notification);
+                    }
+                }
+            }
+        });
+    };
+
+    (function pushDataValues(array) {
+        if(array.length < 1)
+        {
+            if ( isHeaderMessageVisible() )
+            {
+                updateHeaderMessage(i18n_online_notification);
+            }
+            else
+            {
+                setHeaderMessage(i18n_online_notification);
+            }
+            
+            return;
+        }
+
+        var key = array[0];
+        var value = dataValues[key];
+
+        console.log("Upload DataValue: " + key + ", with value: " + value);
 
         $.ajax( {
             url: 'saveValue.action',
-            data: dataValue,
-            dataType: 'json',
-            dataValue: dataValue,
-            async: false,
-            success: function( data, textStatus, jqXHR ) {
-                storageManager.clearDataValueJSON( this.dataValue );
-                console.log( 'Successfully saved data value with value: ' + this.dataValue );
-            }
-        } );
-    }
-
-    for( var completeDataSetKey in completeDataSets )
-    {
-        var completeDataSet = completeDataSets[completeDataSetKey];
-
-        $.ajax({
-            url: 'registerCompleteDataSet.action',
-            data: completeDataSet,
-            dataType: 'json',
-            completeDataSet: completeDataSet,
-            async: false,
-            success: function( data, textStatus, jqXHR ) {
-                clearCompleteDataSetLocally(this.completeDataSet);
-            }
-        } );
-    }
-
-    if ( oldHeaderMessage.length > 0 ) {
-        setHeaderMessage( oldHeaderMessage );
-    } else {
-        hideHeaderMessage();
-    }
-    
+            data: value,
+            dataType: 'json',
+            success: function( data, textStatus, jqXHR ) {
+//                storageManager.clearDataValueJSON( value );
+                console.log( 'Successfully saved data value with value: ' + value );
+                (array = array.slice(1)).length && pushDataValues(array);
+
+                if(array.length < 1 && completeDataSetsArray.length > 0)
+                {
+                    pushCompleteDataSets(completeDataSetsArray);
+                }
+                else
+                {
+                    if ( isHeaderMessageVisible() )
+                    {
+                        updateHeaderMessage(i18n_online_notification);
+                    }
+                    else
+                    {
+                        setHeaderMessage(i18n_online_notification);
+                    }
+                }
+            }
+        } );
+    })(dataValuesArray);
+
     updateForms();
 }
 
@@ -677,7 +769,7 @@
 
     if( localStorage[KEY_COMPLETEDATASETS] == null )
     {
-        completeDataSets = {};
+        return null;
     } else {
         completeDataSets = JSON.parse( localStorage[KEY_COMPLETEDATASETS] );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm	2011-08-18 07:04:26 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/select.vm	2011-08-18 10:02:32 +0000
@@ -25,6 +25,8 @@
 var i18n_operation_not_available_offline = '$encoder.jsEscape( $i18n.getString( "operation_not_available_offline" ) , "'")';
 var i18n_online_notification = '$encoder.jsEscape( $i18n.getString( "online_notification" ) , "'")';
 var i18n_offline_notification = '$encoder.jsEscape( $i18n.getString( "offline_notification" ) , "'")';
+var i18n_need_to_sync_notification = '$encoder.jsEscape( $i18n.getString( "need_to_sync_notification" ) , "'")';
+var i18n_sync_now = '$encoder.jsEscape( $i18n.getString( "sync_now" ) , "'")';
 var i18n_uploading_data_notification = '$encoder.jsEscape( $i18n.getString( "uploading_data_notification" ) , "'")';
 
 </script>