← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4335: made complete registration support offline

 

------------------------------------------------------------
revno: 4335
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-08-17 17:05:47 +0200
message:
  made complete registration support offline
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/RegisterCompleteDataSetAction.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.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-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2011-08-16 13:48:25 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2011-08-17 15:05:47 +0000
@@ -503,6 +503,11 @@
     $( 'div#headerMessage' ).slideDown( 'fast' );
 }
 
+function getHeaderMessage()
+{
+    return $( 'div#headerMessage' ).html();
+}
+
 /**
  * Updates the text in the header message div with the message.
  * 

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/RegisterCompleteDataSetAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/RegisterCompleteDataSetAction.java	2011-08-13 17:58:47 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/RegisterCompleteDataSetAction.java	2011-08-17 15:05:47 +0000
@@ -33,8 +33,11 @@
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
+import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
 
 import com.opensymphony.xwork2.Action;
@@ -58,7 +61,7 @@
     {
         this.registrationService = registrationService;
     }
-    
+
     private DataSetService dataSetService;
 
     public void setDataSetService( DataSetService dataSetService )
@@ -106,14 +109,22 @@
     {
         CompleteDataSetRegistration registration = new CompleteDataSetRegistration();
 
-        registration.setDataSet( dataSetService.getDataSet( dataSetId ) );
-        registration.setPeriod( PeriodType.createPeriodExternalId( periodId ) );
-        registration.setSource( organisationUnitService.getOrganisationUnit( organisationUnitId ) );
-        registration.setDate( new Date() );
-
-        registrationService.saveCompleteDataSetRegistration( registration );
-
-        log.info( "DataSet registered as complete: " + registration );
+        DataSet dataSet = dataSetService.getDataSet( dataSetId );
+        Period period = PeriodType.createPeriodExternalId( periodId );
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
+
+        if ( registrationService.getCompleteDataSetRegistration( dataSet, period, organisationUnit ) == null )
+        {
+            registration.setDataSet( dataSet );
+            registration.setPeriod( period );
+            registration.setSource( organisationUnit );
+            registration.setDate( new Date() );
+
+            registrationService.saveCompleteDataSetRegistration( registration );
+
+            log.info( "DataSet registered as complete: " + registration );
+        }
+
 
         return SUCCESS;
     }

=== 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-17 13:41:04 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-08-17 15:05:47 +0000
@@ -105,13 +105,17 @@
 		organisationUnitAssociationSetMap = json.metaData.organisationUnitAssociationSetMap;
 		
 		console.log( 'Meta-data loaded' );		
-		uploadDataValuesInLocalStorage();
+		uploadLocalData();
 	} );
 }
 
-function uploadDataValuesInLocalStorage() 
+function uploadLocalData() 
 {
     var dataValues = storageManager.getAllDataValues();
+    var completeDataSets = getCompleteDataSetsLocalVariable();
+
+    var oldHeaderMessage = getHeaderMessage();
+    setHeaderWaitMessage("Syncing local data with server");
 
     for ( var dataValueKey in dataValues ) 
     {
@@ -122,12 +126,36 @@
             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]
+        console.log(completeDataSet);
+
+        $.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();
+    }
     
     updateForms();
 }
@@ -643,6 +671,40 @@
     return params;
 }
 
+function getCompleteDataSetsLocalVariable()
+{
+    var completeDataSets;
+
+    if(localStorage[KEY_COMPLETEDATASETS] == null)
+    {
+        completeDataSets = {}
+    } else {
+        completeDataSets = JSON.parse(localStorage[KEY_COMPLETEDATASETS]);
+    }
+
+    return completeDataSets;
+}
+
+function storeCompleteDataSetLocally(json)
+{
+    var completeDataSets = getCompleteDataSetsLocalVariable();
+    var completeDataSetId = getCompleteDataSetId(json);
+
+    completeDataSets[completeDataSetId] = json;
+
+    localStorage[KEY_COMPLETEDATASETS] = JSON.stringify( completeDataSets );
+}
+
+function clearCompleteDataSetLocally(json)
+{
+    var completeDataSets = getCompleteDataSetsLocalVariable();
+    var completeDataSetId = getCompleteDataSetId(json);
+
+    delete completeDataSets[completeDataSetId];
+
+    localStorage[KEY_COMPLETEDATASETS] = JSON.stringify( completeDataSets );
+}
+
 function validateCompleteDataSet()
 {
     var confirmed = confirm( i18n_confirm_complete );
@@ -656,8 +718,13 @@
         $.getJSON( 'getValidationViolations.action', params).success(function(data) {
             registerCompleteDataSet(data);
         }).error( function() {
-            disableUndoButton();
-            window.alert( i18n_no_response_from_server );
+            // no response from server, fake a positive result and save it anyways
+            registerCompleteDataSet({
+                "response": "success"
+            });
+
+//            disableUndoButton();
+//            window.alert( i18n_no_response_from_server );
         } );
     }
 }
@@ -668,10 +735,14 @@
 
     if ( json.response == 'success' )
     {
-        $.getJSON( 'registerCompleteDataSet.action', params).success(function() {            
+        storeCompleteDataSetLocally(params);
+
+        $.getJSON( 'registerCompleteDataSet.action', params).success(function() {
+            clearCompleteDataSetLocally(params);
         }).error( function() {
-            disableUndoButton();
-            window.alert( i18n_no_response_from_server );
+
+//            disableUndoButton();
+//            window.alert( i18n_no_response_from_server );
         } );
     }
     else
@@ -685,25 +756,20 @@
 function undoCompleteDataSet()
 {
     var confirmed = confirm( i18n_confirm_undo );
-
+    var params = getCurrentCompleteDataSetParams();
+    
     if ( confirmed )
     {
-        var periodId = $( '#selectedPeriodId' ).val();
-        var dataSetId = $( '#selectedDataSetId' ).val();
-
         disableUndoButton();
 
-        $.getJSON( 'undoCompleteDataSet.action', {
-            periodId : periodId,
-            dataSetId : dataSetId,
-            organisationUnitId: currentOrganisationUnitId
-        }, function()
-        {
-        } ).error( function()
-        {
-            disableCompleteButton();
+        $.getJSON( 'undoCompleteDataSet.action', params).success(function() {
+            clearCompleteDataSetLocally(params);
+        }).error( function()
+        {
+            clearCompleteDataSetLocally(params);
 
-            window.alert( i18n_no_response_from_server );
+//            disableCompleteButton();
+//            window.alert( i18n_no_response_from_server );
         } );
     }
 }