← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4669: implemented load/save datavalues in mobile section form

 

------------------------------------------------------------
revno: 4669
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2011-09-24 01:07:18 +0200
message:
  implemented load/save datavalues in mobile section form
added:
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/SaveSectionFormAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveError.vm
  dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveSuccess.vm
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/GetSectionFormAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-light/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.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/java/org/hisp/dhis/de/action/SaveValueAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java	2011-07-19 21:49:16 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/SaveValueAction.java	2011-09-23 23:07:18 +0000
@@ -119,7 +119,7 @@
     {
         this.optionComboId = optionComboId;
     }
-    
+
     private String periodId;
 
     public void setPeriodId( String periodId )
@@ -179,7 +179,8 @@
         {
             if ( value != null )
             {
-                dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(), null, optionCombo );
+                dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(), null,
+                    optionCombo );
                 dataValueService.addDataValue( dataValue );
             }
         }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/GetSectionFormAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/GetSectionFormAction.java	2011-09-23 18:54:49 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/GetSectionFormAction.java	2011-09-23 23:07:18 +0000
@@ -27,12 +27,26 @@
 
 package org.hisp.dhis.light.action;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.dataset.Section;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
+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;
 
+/**
+ * @author mortenoh
+ */
 public class GetSectionFormAction
     implements Action
 {
@@ -40,6 +54,13 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
+
     private DataSetService dataSetService;
 
     public void setDataSetService( DataSetService dataSetService )
@@ -47,6 +68,13 @@
         this.dataSetService = dataSetService;
     }
 
+    private DataValueService dataValueService;
+
+    public void setDataValueService( DataValueService dataValueService )
+    {
+        this.dataValueService = dataValueService;
+    }
+
     // -------------------------------------------------------------------------
     // Input & Output
     // -------------------------------------------------------------------------
@@ -94,6 +122,13 @@
         return dataSet;
     }
 
+    private Map<String, String> dataValues = new HashMap<String, String>();
+
+    public Map<String, String> getDataValues()
+    {
+        return dataValues;
+    }
+
     // -------------------------------------------------------------------------
     // Action Implementation
     // -------------------------------------------------------------------------
@@ -101,9 +136,34 @@
     @Override
     public String execute()
     {
-        Period period = new Period( String.valueOf( periodId ) );
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
+
+        Period period = PeriodType.createPeriodExternalId( periodId );
+
         dataSet = dataSetService.getDataSet( dataSetId );
 
+        for ( Section section : dataSet.getSections() )
+        {
+            for ( DataElement dataElement : section.getDataElements() )
+            {
+                for ( DataElementCategoryOptionCombo optionCombo : dataElement.getCategoryCombo().getOptionCombos() )
+                {
+                    DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, period,
+                        optionCombo );
+
+                    String key = String.format( "DE%dOC%d", dataElement.getId(), optionCombo.getId() );
+                    String value = "";
+
+                    if ( dataValue != null )
+                    {
+                        value = dataValue.getValue();
+                    }
+
+                    dataValues.put( key, value );
+                }
+            }
+        }
+
         return SUCCESS;
     }
 }

=== added file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/SaveSectionFormAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/SaveSectionFormAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/action/SaveSectionFormAction.java	2011-09-23 23:07:18 +0000
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2004-2010, 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.
+ */
+
+package org.hisp.dhis.light.action;
+
+import java.util.Date;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.struts2.ServletActionContext;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
+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 org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.util.ContextUtils;
+
+import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
+
+/**
+ * @author mortenoh
+ */
+public class SaveSectionFormAction
+    implements Action
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private CurrentUserService currentUserService;
+
+    public void setCurrentUserService( CurrentUserService currentUserService )
+    {
+        this.currentUserService = currentUserService;
+    }
+
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
+
+    private DataElementService dataElementService;
+
+    public void setDataElementService( DataElementService dataElementService )
+    {
+        this.dataElementService = dataElementService;
+    }
+
+    private DataElementCategoryService categoryService;
+
+    public void setCategoryService( DataElementCategoryService categoryService )
+    {
+        this.categoryService = categoryService;
+    }
+
+    private DataValueService dataValueService;
+
+    public void setDataValueService( DataValueService dataValueService )
+    {
+        this.dataValueService = dataValueService;
+    }
+
+    // -------------------------------------------------------------------------
+    // Input & Output
+    // -------------------------------------------------------------------------
+
+    private Integer organisationUnitId;
+
+    public void setOrganisationUnitId( Integer organisationUnitId )
+    {
+        this.organisationUnitId = organisationUnitId;
+    }
+
+    private String periodId;
+
+    public void setPeriodId( String periodId )
+    {
+        this.periodId = periodId;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action Implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public String execute()
+    {
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
+
+        Period period = PeriodType.createPeriodExternalId( periodId );
+
+        String storedBy = currentUserService.getCurrentUsername();
+
+        if ( storedBy == null )
+        {
+            storedBy = "[unknown]";
+        }
+
+        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
+            ServletActionContext.HTTP_REQUEST );
+        Map<String, String> parameterMap = ContextUtils.getParameterMap( request );
+
+        for ( String key : parameterMap.keySet() )
+        {
+            if ( key.startsWith( "DE" ) && key.indexOf( "OC" ) != -1 )
+            {
+                String[] splitKey = key.split( "OC" );
+                Integer dataElementId = Integer.parseInt( splitKey[0].substring( 2 ) );
+                Integer optionComboId = Integer.parseInt( splitKey[1] );
+                String value = parameterMap.get( key );
+
+                DataElement dataElement = dataElementService.getDataElement( dataElementId );
+                DataElementCategoryOptionCombo optionCombo = categoryService
+                    .getDataElementCategoryOptionCombo( optionComboId );
+
+                DataValue dataValue = dataValueService
+                    .getDataValue( organisationUnit, dataElement, period, optionCombo );
+
+                value = value.trim();
+
+                if ( dataValue == null )
+                {
+                    if ( value.length() != 0 && value != null )
+                    {
+                        dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(),
+                            null, optionCombo );
+                        dataValueService.addDataValue( dataValue );
+                    }
+                }
+                else
+                {
+                    dataValue.setValue( value );
+                    dataValue.setTimestamp( new Date() );
+                    dataValue.setStoredBy( storedBy );
+
+                    dataValueService.updateDataValue( dataValue );
+                }
+            }
+        }
+
+        return SUCCESS;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml	2011-09-23 18:54:49 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml	2011-09-23 23:07:18 +0000
@@ -19,7 +19,17 @@
   </bean>
 
   <bean id="org.hisp.dhis.light.action.GetSectionFormAction" class="org.hisp.dhis.light.action.GetSectionFormAction">
+    <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
     <property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
+    <property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
+  </bean>
+
+  <bean id="org.hisp.dhis.light.action.SaveSectionFormAction" class="org.hisp.dhis.light.action.SaveSectionFormAction">
+    <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
+    <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+    <property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />
+    <property name="categoryService" ref="org.hisp.dhis.dataelement.DataElementCategoryService" />
+    <property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
   </bean>
 
   <bean id="org.hisp.dhis.light.action.ProvideContentAction" class="org.hisp.dhis.light.action.ProvideContentAction"

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-light/src/main/resources/struts.xml	2011-09-23 18:11:09 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/resources/struts.xml	2011-09-23 23:07:18 +0000
@@ -32,6 +32,11 @@
       <param name="page">/dhis-web-light/dataEntry.vm</param>
     </action>
 
+    <action name="saveSectionForm" class="org.hisp.dhis.light.action.SaveSectionFormAction">
+      <result name="success" type="velocity">/dhis-web-light/main.vm</result>
+      <param name="page">/dhis-web-light/saveSuccess.vm</param>
+    </action>
+
     <action name="dashboard" class="org.hisp.dhis.light.action.ProvideContentAction">
       <result name="success" type="velocity">/dhis-web-light/dashboard_page.vm</result>
       <param name="page">/dhis-web-light/dashboard.vm</param>

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm'
--- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm	2011-09-23 18:40:39 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm	2011-09-23 23:07:18 +0000
@@ -1,20 +1,21 @@
 
 <h2>$dataSet.name</h2>
 
-<form>
+<form action="saveSectionForm.action" method="POST">
+
+<input type="hidden" id="organisationUnitId" name="organisationUnitId" value="$organisationUnitId" />
+<input type="hidden" id="dataSetId" name="dataSetId" value="$dataSetId" />
+<input type="hidden" id="periodId" name="periodId" value="$periodId" />
 
 #foreach( $section in $dataSet.sections )
 <div class="header-box">
 	<h3>$section.name</h3>
 	<p>
 		#foreach( $dataElement in $section.dataElements)
-			#if( $section.categoryCombo.name == "default" )
-				<label>$dataElement.name</label> <input type="text" size="24" /><br />
-			#else
-				#foreach( $optionCombo in $section.categoryCombo.optionCombos )
-				<label>$dataElement.name $optionCombo.name</label> <input type="text" size="24" /><br />
-				#end
-			#end	
+			#foreach( $optionCombo in $section.categoryCombo.optionCombos )
+			#set( $key = "DE${dataElement.id}OC${optionCombo.id}" )
+			<label>$dataElement.name $optionCombo.name</label> <input type="text" size="24" name="$key" value="$dataValues.get($key)" /> <br />
+			#end
 		#end
 	</p>
 </div>
@@ -22,8 +23,8 @@
 
 <div class="header-box">
 	<p>
-		<input type="submit" style="width: 48%" value="Submit"/>
-		<input type="reset" style="width: 48%" value="Reset"/>
+		<input type="submit" style="width: 48%" value="Save"/>
+		<input type="reset" style="width: 48%" value="Clear"/>
 	</p>
 </div>
 

=== added file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveError.vm'
--- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveError.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveError.vm	2011-09-23 23:07:18 +0000
@@ -0,0 +1,2 @@
+
+ERROR!

=== added file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveSuccess.vm'
--- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveSuccess.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/saveSuccess.vm	2011-09-23 23:07:18 +0000
@@ -0,0 +1,2 @@
+
+SUCCESS!