← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10597: allow upload of offline execution dates / data values

 

------------------------------------------------------------
revno: 10597
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-04-17 15:45:28 +0700
message:
  allow upload of offline execution dates / data values
added:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/UploadAnonymousEventAction.java
modified:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.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/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java	2013-04-04 05:07:53 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java	2013-04-17 08:45:28 +0000
@@ -1,5 +1,7 @@
+package org.hisp.dhis.caseentry.action.caseentry;
+
 /*
- * Copyright (c) 2004-2009, University of Oslo
+ * Copyright (c) 2004-2013, University of Oslo
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,8 +27,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-package org.hisp.dhis.caseentry.action.caseentry;
-
 import com.opensymphony.xwork2.Action;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

=== added file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/UploadAnonymousEventAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/UploadAnonymousEventAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/UploadAnonymousEventAction.java	2013-04-17 08:45:28 +0000
@@ -0,0 +1,218 @@
+package org.hisp.dhis.caseentry.action.caseentry;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.opensymphony.xwork2.Action;
+import org.apache.struts2.ServletActionContext;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patientdatavalue.PatientDataValue;
+import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramService;
+import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.program.ProgramStageInstanceService;
+import org.hisp.dhis.user.CurrentUserService;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.servlet.ServletInputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class UploadAnonymousEventAction implements Action
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    @Autowired
+    private ProgramService programService;
+
+    @Autowired
+    private ProgramInstanceService programInstanceService;
+
+    @Autowired
+    private ProgramStageInstanceService programStageInstanceService;
+
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private DataElementService dataElementService;
+
+    @Autowired
+    private CurrentUserService currentUserService;
+
+    @Autowired
+    private PatientDataValueService patientDataValueService;
+
+    private I18nFormat format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this.format = format;
+    }
+
+    // -------------------------------------------------------------------------
+    // Input & Output
+    // -------------------------------------------------------------------------
+
+    private String message;
+
+    public String getMessage()
+    {
+        return message;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action Implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public String execute() throws Exception
+    {
+        ServletInputStream inputStream = ServletActionContext.getRequest().getInputStream();
+
+        Map<String, Object> input = JacksonUtils.getJsonMapper().readValue( inputStream, new TypeReference<HashMap<String, Object>>()
+        {
+        } );
+
+        Map<String, Object> executionDate = (Map<String, Object>) input.get( "executionDate" );
+
+        Integer programId;
+        Integer organisationUnitId;
+
+        try
+        {
+            programId = Integer.parseInt( (String) executionDate.get( "programId" ) );
+            organisationUnitId = Integer.parseInt( (String) executionDate.get( "organisationUnitId" ) );
+        }
+        catch ( NumberFormatException e )
+        {
+            message = e.getMessage();
+            return ERROR;
+        }
+
+        Date date = format.parseDate( (String) executionDate.get( "executionDate" ) );
+
+        if ( programId == null || date == null || organisationUnitId == null )
+        {
+            return INPUT;
+        }
+
+        ProgramStageInstance programStageInstance = saveExecutionDate( programId, organisationUnitId, date );
+
+        Map<String, Object> values = (Map<String, Object>) input.get( "values" );
+
+        if ( values != null )
+        {
+            for ( String dataElementUid : values.keySet() )
+            {
+                Map<String, Object> valueMap = (Map<String, Object>) values.get( dataElementUid );
+                String value = (String) valueMap.get( "value" );
+                Boolean providedElsewhere = (Boolean) valueMap.get( "providedElsewhere" );
+
+                DataElement dataElement = dataElementService.getDataElement( dataElementUid );
+
+                saveDataValue( programStageInstance, dataElement, value, providedElsewhere );
+            }
+        }
+
+        return SUCCESS;
+    }
+
+    private ProgramStageInstance saveExecutionDate( Integer programId, Integer organisationUnitId, Date date )
+    {
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
+        Program program = programService.getProgram( programId );
+        ProgramStage programStage = program.getProgramStages().iterator().next();
+        ProgramInstance programInstance = programInstanceService.getProgramInstances( program ).iterator().next();
+
+        ProgramStageInstance programStageInstance = new ProgramStageInstance();
+        programStageInstance.setProgramInstance( programInstance );
+        programStageInstance.setProgramStage( programStage );
+        programStageInstance.setDueDate( date );
+        programStageInstance.setExecutionDate( date );
+        programStageInstance.setOrganisationUnit( organisationUnit );
+
+        programStageInstanceService.addProgramStageInstance( programStageInstance );
+
+        message = programStageInstance.getId() + "";
+
+        return programStageInstance;
+    }
+
+    private void saveDataValue( ProgramStageInstance programStageInstance, DataElement dataElement, String value, Boolean providedElsewhere )
+    {
+        String storedBy = currentUserService.getCurrentUsername();
+
+        if ( value != null && value.trim().length() == 0 )
+        {
+            value = null;
+        }
+
+        PatientDataValue patientDataValue = patientDataValueService.getPatientDataValue( programStageInstance, dataElement );
+
+        if ( patientDataValue == null && value != null )
+        {
+            patientDataValue = new PatientDataValue( programStageInstance, dataElement, new Date(), value );
+            patientDataValue.setStoredBy( storedBy );
+            patientDataValue.setProvidedElsewhere( providedElsewhere );
+
+            patientDataValueService.savePatientDataValue( patientDataValue );
+        }
+        else
+        {
+            if ( value == null )
+            {
+                patientDataValueService.deletePatientDataValue( patientDataValue );
+            }
+            else
+            {
+                patientDataValue.setValue( value );
+                patientDataValue.setTimestamp( new Date() );
+                patientDataValue.setProvidedElsewhere( providedElsewhere );
+                patientDataValue.setStoredBy( storedBy );
+
+                patientDataValueService.updatePatientDataValue( patientDataValue );
+            }
+        }
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-04-17 07:58:14 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-04-17 08:45:28 +0000
@@ -1131,4 +1131,8 @@
     <property name="programStageInstanceService" ref="org.hisp.dhis.program.ProgramStageInstanceService" />
   </bean>
 
+  <bean id="org.hisp.dhis.caseentry.action.caseentry.UploadAnonymousEventAction"
+    class="org.hisp.dhis.caseentry.action.caseentry.UploadAnonymousEventAction" scope="prototype">
+  </bean>
+
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2013-04-16 05:41:25 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2013-04-17 08:45:28 +0000
@@ -161,6 +161,16 @@
       </result>
     </action>
 
+    <action name="uploadAnonymousEvent"
+      class="org.hisp.dhis.caseentry.action.caseentry.UploadAnonymousEventAction">
+      <result name="success" type="velocity-json">
+        /dhis-web-commons/ajax/jsonResponseSuccess.vm
+      </result>
+      <result name="error" type="velocity-json">
+        /dhis-web-commons/ajax/jsonResponseError.vm
+      </result>
+    </action>
+
     <action name="removeCurrentEncounter"
       class="org.hisp.dhis.caseentry.action.caseentry.RemoveCurrentEncounterAction">
       <result name="success" type="velocity-json">

=== 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-17 06:46:31 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2013-04-17 08:45:28 +0000
@@ -80,38 +80,20 @@
     } );
 }
 
-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 uploadOfflineData( item ) {
+    $.ajax({
+        url: 'uploadAnonymousEvent.action',
+        contentType: 'application/json',
+        data: JSON.stringify( item )
+    } ).done(function(json) {
+        if ( json.response == 'success' ) {
+            DAO.offlineData.remove( item.key, function ( store ) {
+                showOfflineEvents();
+            } );
+        } else {
+
+        }
+    });
 }
 
 function uploadLocalData() {