dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #08166
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1910: Save and update DataValue
------------------------------------------------------------
revno: 1910
committer: Long <thanhlongngo1988>
branch nick: cbhis-mobile
timestamp: Tue 2010-08-31 16:00:31 +0700
message:
Save and update DataValue
added:
mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueFilter.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueRecordStore.java
modified:
mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DownloadManager.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/ModelRecordStore.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/OrgUnitRecordStore.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/model/DataValue.java
mobile/dhis-mobile/src/org/hisp/dhis/mobile/ui/DHISMIDlet.java
--
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 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DownloadManager.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DownloadManager.java 2010-08-30 09:47:01 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DownloadManager.java 2010-08-31 09:00:31 +0000
@@ -79,7 +79,7 @@
for ( int i = 0; i < programStagesVector.size(); i++ )
{
form = (ProgramStageForm) programStagesVector.elementAt( i );
- completeForm = (ProgramStageForm) download( url + form.getId(), new FormParser() );
+ completeForm = (ProgramStageForm) download( "http://localhost:8080/api/forms/" + form.getId(), new FormParser() );
form.setDataElements( completeForm.getDataElements() );
}
form = null;
=== added file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueFilter.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueFilter.java 1970-01-01 00:00:00 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueFilter.java 2010-08-31 09:00:31 +0000
@@ -0,0 +1,67 @@
+package org.hisp.dhis.mobile.db;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+
+import javax.microedition.rms.RecordFilter;
+
+public class DataValueFilter
+ implements RecordFilter
+{
+ private int proStageInstanceID;
+
+ private int dataElementID;
+
+ public boolean matches( byte[] candidate )
+ {
+
+ ByteArrayInputStream bis = new ByteArrayInputStream( candidate );
+ DataInputStream dis = new DataInputStream( bis );
+ try
+ {
+ if ( dis.readInt() == this.proStageInstanceID && dis.readInt() == this.dataElementID )
+ return true;
+ else
+ return false;
+ }
+ catch ( IOException e )
+ {
+ return false;
+ }
+ finally
+ {
+ try
+ {
+ bis.close();
+ dis.close();
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ public int getProStageInstanceID()
+ {
+ return proStageInstanceID;
+ }
+
+ public void setProStageInstanceID( int proStageInstanceID )
+ {
+ this.proStageInstanceID = proStageInstanceID;
+ }
+
+ public int getDataElementID()
+ {
+ return dataElementID;
+ }
+
+ public void setDataElementID( int dataElementID )
+ {
+ this.dataElementID = dataElementID;
+ }
+
+}
=== added file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueRecordStore.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueRecordStore.java 1970-01-01 00:00:00 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/DataValueRecordStore.java 2010-08-31 09:00:31 +0000
@@ -0,0 +1,68 @@
+package org.hisp.dhis.mobile.db;
+
+import java.util.Hashtable;
+
+import javax.microedition.rms.RecordEnumeration;
+import javax.microedition.rms.RecordStore;
+import javax.microedition.rms.RecordStoreException;
+
+import org.hisp.dhis.mobile.model.Activity;
+import org.hisp.dhis.mobile.model.DataValue;
+
+public class DataValueRecordStore
+{
+
+ private Hashtable dataValueRecordID;
+
+ private String dataBaseName;
+
+ public DataValueRecordStore()
+ {
+ this.dataBaseName = ModelRecordStore.DATAVALUE_DB;
+ }
+
+ public void saveDataValue( DataValue dataValue )
+ {
+ ModelRecordStore modelRecordStore;
+ try
+ {
+ modelRecordStore = new ModelRecordStore( ModelRecordStore.DATAVALUE_DB );
+ modelRecordStore.addRecord( DataValue.dataValueToRecord( dataValue ) );
+ }
+ catch ( RecordStoreException rse )
+ {
+ System.out.println( rse.getMessage() );
+ }
+ }
+
+ public Hashtable loadDataValues( Activity activity )
+ {
+ Hashtable dataValuesTable = new Hashtable();
+ RecordStore rs = null;
+ RecordEnumeration re = null;
+ try
+ {
+ rs = RecordStore.openRecordStore( ModelRecordStore.DATAVALUE_DB, true );
+ re = rs.enumerateRecords( null, null, false );
+ while ( re.hasNextElement() )
+ {
+ DataValue dataValue = DataValue.recordToDataValue( re.nextRecord() );
+ if ( dataValue.getProgramInstanceId() == activity.getTask().getProgStageInstId() )
+ {
+ dataValuesTable.put( String.valueOf( dataValue.getDataElementId() ), dataValue.getValue() );
+ }
+ }
+ re = null;
+ rs = null;
+ return dataValuesTable;
+ }
+ catch ( RecordStoreException rse )
+ {
+ rse.printStackTrace();
+ return null;
+ }
+ }
+
+
+
+}
=== modified file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/ModelRecordStore.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/ModelRecordStore.java 2010-08-28 10:25:49 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/ModelRecordStore.java 2010-08-31 09:00:31 +0000
@@ -16,6 +16,7 @@
public static final String DATAELEMENT_DB = "DATAELEMENT";
public static final String ACTIVITY_DB = "ACTIVITY";
public static final String USER_DB = "USER";
+ public static final String DATAVALUE_DB = "DATAVALUE";
private String dbName;
public ModelRecordStore(String dbName){
=== modified file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/OrgUnitRecordStore.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/OrgUnitRecordStore.java 2010-08-28 10:25:49 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/OrgUnitRecordStore.java 2010-08-31 09:00:31 +0000
@@ -98,7 +98,6 @@
id = new Integer( re.nextRecordId() );
orgUnitByte = OrgUnit.orgUnitToRecord( (OrgUnit) model );
rs.setRecord( id.intValue(), orgUnitByte, 0, orgUnitByte.length );
-
}
// release variable
orgUnitByte = null;
=== modified file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.java 2010-08-30 09:47:01 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.java 2010-08-31 09:00:31 +0000
@@ -26,12 +26,15 @@
*/
package org.hisp.dhis.mobile.db;
+import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.hisp.dhis.mobile.model.AbstractModel;
+import org.hisp.dhis.mobile.model.Activity;
+import org.hisp.dhis.mobile.model.DataValue;
import org.hisp.dhis.mobile.model.OrgUnit;
import org.hisp.dhis.mobile.model.ProgramStageForm;
import org.hisp.dhis.mobile.model.User;
@@ -137,11 +140,9 @@
{
System.out.println( rse.getMessage() );
}
-
}
-
}
-
+
public static Vector loadForms()
{
RecordStore rs = null;
@@ -166,6 +167,78 @@
}
}
+ public static void storeDataValue( DataValue dataValue )
+ {
+ ModelRecordStore modelRecordStore;
+ try
+ {
+ modelRecordStore = new ModelRecordStore( ModelRecordStore.DATAVALUE_DB );
+ modelRecordStore.addRecord( DataValue.dataValueToRecord( dataValue ) );
+ }
+ catch ( RecordStoreException rse )
+ {
+ System.out.println( rse.getMessage() );
+ }
+ }
+
+ public static Hashtable loadDataValues( Activity activity )
+ {
+ Hashtable dataValuesTable = new Hashtable();
+ RecordStore rs = null;
+ RecordEnumeration re = null;
+ try
+ {
+ rs = RecordStore.openRecordStore( ModelRecordStore.DATAVALUE_DB, true );
+ re = rs.enumerateRecords( null, null, false );
+ while ( re.hasNextElement() )
+ {
+ DataValue dataValue = DataValue.recordToDataValue( re.nextRecord() );
+ if ( dataValue.getProgramInstanceId() == activity.getTask().getProgStageInstId() )
+ {
+ dataValuesTable.put( String.valueOf( dataValue.getDataElementId() ), dataValue.getValue() );
+ }
+ }
+ re = null;
+ rs = null;
+ return dataValuesTable;
+ }
+ catch ( RecordStoreException rse )
+ {
+ rse.printStackTrace();
+ return null;
+ }
+ }
+
+ public static void updateDataValue( Activity activity, DataValue newDataValue )
+ {
+ RecordStore rs = null;
+ RecordEnumeration re = null;
+ try
+ {
+ DataValueFilter filter = new DataValueFilter();
+ filter.setDataElementID( newDataValue.getDataElementId() );
+ filter.setProStageInstanceID( activity.getTask().getProgStageInstId() );
+ rs = RecordStore.openRecordStore( ModelRecordStore.DATAVALUE_DB, true );
+ re = rs.enumerateRecords( filter, null, false );
+ while ( re.hasNextElement() )
+ {
+ if ( re.numRecords() == 1 )
+ {
+ int id = re.nextRecordId();
+ byte[] data = DataValue.dataValueToRecord( newDataValue );
+ rs.setRecord( id, data, 0, data.length );
+ System.out.println( newDataValue.getValue() + " ID: " + id );
+ }
+ }
+ re = null;
+ rs = null;
+ }
+ catch ( RecordStoreException rse )
+ {
+ rse.printStackTrace();
+ }
+ }
+
public static void saveOrgUnit( OrgUnit orgUnit )
{
clear( ModelRecordStore.ORGUNIT_DB );
=== modified file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/model/DataValue.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/model/DataValue.java 2010-08-25 12:09:15 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/model/DataValue.java 2010-08-31 09:00:31 +0000
@@ -5,11 +5,18 @@
package org.hisp.dhis.mobile.model;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
/**
- *
+ *
* @author abyotag_adm
*/
-public class DataValue {
+public class DataValue
+{
private int programInstanceId;
@@ -17,47 +24,96 @@
private String value;
- public DataValue(){}
+ public DataValue()
+ {
+ }
/**
* @return the programInstanceId
*/
- public int getProgramInstanceId() {
+ public int getProgramInstanceId()
+ {
return programInstanceId;
}
/**
* @param programInstanceId the programInstanceId to set
*/
- public void setProgramInstanceId(int programInstanceId) {
+ public void setProgramInstanceId( int programInstanceId )
+ {
this.programInstanceId = programInstanceId;
}
/**
* @return the dataElementId
*/
- public int getDataElementId() {
+ public int getDataElementId()
+ {
return dataElementId;
}
/**
* @param dataElementId the dataElementId to set
*/
- public void setDataElementId(int dataElementId) {
+ public void setDataElementId( int dataElementId )
+ {
this.dataElementId = dataElementId;
}
/**
* @return the value
*/
- public String getValue() {
+ public String getValue()
+ {
return value;
}
/**
* @param value the value to set
*/
- public void setValue(String value) {
+ public void setValue( String value )
+ {
this.value = value;
}
+
+ public static DataValue recordToDataValue( byte[] rec )
+ {
+ ByteArrayInputStream bin = new ByteArrayInputStream( rec );
+ DataInputStream din = new DataInputStream( bin );
+ DataValue dataValue = new DataValue();
+
+ try
+ {
+ dataValue.setProgramInstanceId( din.readInt() );
+ dataValue.setDataElementId( din.readInt() );
+ dataValue.setValue( din.readUTF() );
+ }
+ catch ( IOException ioe )
+ {
+ System.out.println( ioe.getMessage() );
+ }
+
+ return dataValue;
+ }
+
+ public static byte[] dataValueToRecord( DataValue dataValue )
+ {
+ ByteArrayOutputStream deOs = new ByteArrayOutputStream();
+ DataOutputStream dout = new DataOutputStream( deOs );
+
+ try
+ {
+ dout.writeInt( dataValue.getProgramInstanceId() );
+ dout.writeInt( dataValue.getDataElementId() );
+ dout.writeUTF( dataValue.getValue() );
+ dout.flush();
+ }
+ catch ( IOException e )
+ {
+ System.out.println( e );
+ e.printStackTrace();
+ }
+ return deOs.toByteArray();
+ }
+
}
=== modified file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/ui/DHISMIDlet.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/ui/DHISMIDlet.java 2010-08-30 09:47:01 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/ui/DHISMIDlet.java 2010-08-31 09:00:31 +0000
@@ -1,5 +1,6 @@
package org.hisp.dhis.mobile.ui;
+import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
@@ -25,6 +26,7 @@
import org.hisp.dhis.mobile.model.AbstractModel;
import org.hisp.dhis.mobile.model.Activity;
import org.hisp.dhis.mobile.model.DataElement;
+import org.hisp.dhis.mobile.model.DataValue;
import org.hisp.dhis.mobile.model.OrgUnit;
import org.hisp.dhis.mobile.model.ProgramStageForm;
import org.hisp.dhis.mobile.model.User;
@@ -54,6 +56,10 @@
private ProgramStageForm programStageForm;
+ private Activity selectedActivity;
+
+ private Hashtable dataValueTable = new Hashtable();
+
private List mainMenuList;
private List formDownloadList;
@@ -108,7 +114,7 @@
private Command deFrmSavCmd;
private Command screenCommand;
-
+
private Command saveCommand;
private Command backCommand;
@@ -223,8 +229,10 @@
else if ( command == screenCommand )
{
sendRecordedData();
- } else if (command == saveCommand){
-
+ }
+ else if ( command == saveCommand )
+ {
+ saveDataValue();
}
}
else if ( displayable == formDownloadList )
@@ -291,7 +299,9 @@
if ( command == actvyPlnListBakCmd )
{
switchDisplayable( null, getMainMenuList() );
- } else if (command == List.SELECT_COMMAND){
+ }
+ else if ( command == List.SELECT_COMMAND )
+ {
this.displaySelectedActivity();
}
}
@@ -387,13 +397,94 @@
}
}
+ private void saveDataValue()
+ {
+ Vector des = programStageForm.getDataElements();
+ for ( int i = 0; i < des.size(); i++ )
+ {
+ DataElement de = (DataElement) des.elementAt( i );
+ if ( dataValueTable.get( String.valueOf( de.getId() ) ) != null )
+ {
+ this.updateDataValue( de );
+ }
+ else
+ {
+ this.storeNewDataValue( de );
+ }
+ }
+ loadDataValues( selectedActivity );
+
+ }
+
+ private void storeNewDataValue( DataElement de )
+ {
+ if ( de.getType() == DataElement.TYPE_DATE )
+ {
+ DateField dateField = (DateField) formElements.get( de );
+ if ( dateField.getDate() != null )
+ {
+ Storage.storeDataValue( getDataValue( selectedActivity.getTask().getProgStageInstId(), de.getId(),
+ String.valueOf( dateField.getDate().getTime() ) ) );
+ System.out.println( "Store new: " + de.getName() );
+ }
+ }
+ else
+ {
+ TextField txtField = (TextField) formElements.get( de );
+ if ( !txtField.getString().equalsIgnoreCase( "" ) )
+ {
+ Storage.storeDataValue( getDataValue( selectedActivity.getTask().getProgStageInstId(), de.getId(),
+ txtField.getString() ) );
+ System.out.println( "Store new: " + de.getName() );
+ }
+ }
+
+ }
+
+ private void updateDataValue( DataElement de )
+ {
+
+ if ( de.getType() == DataElement.TYPE_DATE )
+ {
+ DateField dateField = (DateField) formElements.get( de );
+ if ( dateField.getDate() != null )
+ {
+ Storage.updateDataValue(
+ selectedActivity,
+ getDataValue( selectedActivity.getTask().getProgStageInstId(), de.getId(),
+ String.valueOf( dateField.getDate().getTime() ) ) );
+ System.out.println( "Updating: " + de.getName() );
+ }
+ }
+ else
+ {
+ TextField txtField = (TextField) formElements.get( de );
+ if ( !txtField.getString().equalsIgnoreCase( "" ) )
+ {
+ Storage.updateDataValue( selectedActivity,
+ getDataValue( selectedActivity.getTask().getProgStageInstId(), de.getId(), txtField.getString() ) );
+ System.out.println( "Updating: " + de.getName() );
+ }
+ }
+
+ }
+
private void displaySelectedActivity()
{
- Activity selectedActivity = (Activity) activitiesVector.elementAt( getActivitiesList().getSelectedIndex() );
+ selectedActivity = (Activity) activitiesVector.elementAt( getActivitiesList().getSelectedIndex() );
ProgramStageForm formOfActivity = Storage.fetchForm( selectedActivity.getTask().getProgStageId() );
this.renderForm( formOfActivity, getForm() );
}
+ private DataValue getDataValue( int progStageId, int dataElementID, String value )
+ {
+ DataValue dataValue = new DataValue();
+ dataValue.setProgramInstanceId( progStageId );
+ dataValue.setDataElementId( dataElementID );
+ dataValue.setValue( value );
+ return dataValue;
+ }
+
/**
* Returns an vector of activities loaded from RMS
*/
@@ -425,7 +516,7 @@
private void downloadForms()
{
- DownloadManager manager = new DownloadManager( this, this.url.getString() + "forms/", user,
+ DownloadManager manager = new DownloadManager( this, orgUnit.getProgramFormsLink(), user,
DownloadManager.DOWNLOAD_FORMS );
manager.start();
@@ -481,8 +572,6 @@
AbstractModel downloadedProgramStage = Storage.getForm( index );
- System.out.println( "Selected ID: " + downloadedProgramStage.getId() );
-
ProgramStageForm form = Storage.fetchForm( downloadedProgramStage.getId() );
System.out.println( "Name: " + form.getName() );
this.getForm( form );
@@ -849,19 +938,20 @@
form = new Form( "form" );
form.addCommand( getBackCommand() );
form.addCommand( getScreenCommand() );
- form.addCommand(getSaveCommand());
+ form.addCommand( getSaveCommand() );
form.setCommandListener( this );
// This is just for test .....
ProgramStageForm frm = Storage.fetchForm( 1 );
- renderForm( frm, form );
+// renderForm( frm, form );
}
return form;
}
private Command getSaveCommand()
{
- if (saveCommand == null){
+ if ( saveCommand == null )
+ {
saveCommand = new Command( "Save", Command.SCREEN, 0 );
}
return saveCommand;
@@ -1148,7 +1238,7 @@
private void browseForms()
{
- loadSettings();
+ // loadSettings();
downloadManager = new DownloadManager( this, serverUrl + "forms", user, DownloadManager.DOWNLOAD_FORMS );
downloadManager.start();
}
@@ -1227,7 +1317,7 @@
public void renderForm( ProgramStageForm prStgFrm, Form form )
{
-
+ loadDataValues( selectedActivity );
programStageForm = prStgFrm;
if ( prStgFrm == null )
@@ -1237,7 +1327,6 @@
else
{
form.deleteAll();
-
form.setTitle( prStgFrm.getName() );
Vector des = prStgFrm.getDataElements();
@@ -1247,18 +1336,33 @@
if ( de.getType() == DataElement.TYPE_DATE )
{
DateField dateField = new DateField( de.getName(), DateField.DATE );
+ if ( dataValueTable.get( String.valueOf( de.getId() ) ) != null )
+ {
+ Date date = new Date();
+ date.setTime( Long.parseLong( (String) dataValueTable.get( String.valueOf( de.getId() ) ) ) );
+ dateField.setDate( date );
+ System.out.println("Date in db is: " + date.toString());
+ }
form.append( dateField );
formElements.put( de, dateField );
}
else if ( de.getType() == DataElement.TYPE_INT )
{
TextField intField = new TextField( de.getName(), "", 32, TextField.NUMERIC );
+ if ( dataValueTable.get( String.valueOf( de.getId() ) ) != null )
+ {
+ intField.setString( (String) dataValueTable.get( String.valueOf( de.getId() ) ) );
+ }
form.append( intField );
formElements.put( de, intField );
}
else
{
TextField txtField = new TextField( de.getName(), "", 32, TextField.ANY );
+ if ( dataValueTable.get( String.valueOf( de.getId() ) ) != null )
+ {
+ txtField.setString( (String) dataValueTable.get( String.valueOf( de.getId() ) ) );
+ }
form.append( txtField );
formElements.put( de, txtField );
}
@@ -1268,6 +1372,11 @@
switchDisplayable( null, form );
}
+ private void loadDataValues( Activity activity )
+ {
+ dataValueTable = Storage.loadDataValues( selectedActivity );
+ }
+
public void saveOrgUnit( OrgUnit orgunit )
{
this.orgUnit = orgunit;