← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1913: added "Save successfully" message and ReinitConfirmListener.java

 

------------------------------------------------------------
revno: 1913
committer: Long <thanhlongngo1988>
branch nick: cbhis-mobile
timestamp: Wed 2010-09-08 16:52:06 +0700
message:
  added "Save successfully" message and  ReinitConfirmListener.java
added:
  mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DataValueUploadManager.java
  mobile/dhis-mobile/src/org/hisp/dhis/mobile/util/ReinitConfirmListener.java
modified:
  mobile/dhis-mobile/.mtj
  mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.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/.mtj'
--- mobile/dhis-mobile/.mtj	2010-09-06 03:04:35 +0000
+++ mobile/dhis-mobile/.mtj	2010-09-08 09:52:06 +0000
@@ -8,8 +8,8 @@
       <configuration active="true" name="DefaultFxPhone1">
          <device group="Sun Java(TM) Wireless Toolkit 2.5.2_01 for CLDC" name="DefaultColorPhone"/>
          <symbolSet name="DefaultFxPhone1">
+            <symbol name="screen.isColor" value="true"/>
             <symbol name="MMAPI" value="1.2"/>
-            <symbol name="screen.isColor" value="true"/>
             <symbol name="descriptor" value="FX Emulator"/>
             <symbol name="screen.bitDepth" value="16"/>
             <symbol name="JSR82" value="1.1"/>

=== added file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DataValueUploadManager.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DataValueUploadManager.java	1970-01-01 00:00:00 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/connection/DataValueUploadManager.java	2010-09-08 09:52:06 +0000
@@ -0,0 +1,116 @@
+package org.hisp.dhis.mobile.connection;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+
+import org.hisp.dhis.mobile.model.DataValue;
+import org.hisp.dhis.mobile.model.OrgUnit;
+import org.hisp.dhis.mobile.model.User;
+
+public class DataValueUploadManager
+    extends Thread
+{
+
+    private Hashtable dataValueTable;
+
+    private String url;
+
+    private OrgUnit orgUnit;
+
+    private User user;
+
+    public DataValueUploadManager( Hashtable dataValueTable, String url, OrgUnit orgUnit, User user )
+    {
+        this.dataValueTable = dataValueTable;
+        this.url = url;
+        this.orgUnit = orgUnit;
+        this.user = user;
+    }
+
+    public void run()
+
+    {
+        HttpConnection connection = null;
+        OutputStream opt = null;
+        DataOutputStream dos = null;
+        Enumeration en = null;
+        try
+        {
+            for ( int redirectTimes = 0; redirectTimes < 5; redirectTimes++ )
+            {
+                connection = (HttpConnection) Connector.open( url );
+                configureConnection( connection );
+                int status = connection.getResponseCode();
+                switch ( status )
+                {
+                case HttpConnection.HTTP_SEE_OTHER:
+                case HttpConnection.HTTP_TEMP_REDIRECT:
+                case HttpConnection.HTTP_MOVED_TEMP:
+                case HttpConnection.HTTP_MOVED_PERM:
+                    url = connection.getHeaderField( "location" );
+                default:
+                    break;
+                }
+                System.out.println("Status: " + connection.getResponseCode());
+            }
+
+             int numOfDataValue = dataValueTable.size();
+             opt = connection.openOutputStream();
+             dos = new DataOutputStream( opt );
+            
+             dos.writeInt( numOfDataValue );
+             dos.writeInt( orgUnit.getId() );
+             en = dataValueTable.elements();
+             while ( en.hasMoreElements() )
+             {
+             DataValue dataValue = (DataValue) en.nextElement();
+             dos.writeInt( dataValue.getDataElementId() );
+             dos.writeInt( dataValue.getProgramInstanceId() );
+             dos.writeUTF( dataValue.getValue() );
+             }
+        }
+        catch ( Exception e )
+        {
+            System.out.println( e.getMessage() );
+        }
+        finally
+        {
+            try
+            {
+                dos.close();
+                opt.close();
+                connection.close();
+            }
+            catch ( IOException e )
+            {
+                System.out.println( e.getMessage() );
+            }
+
+        }
+
+    }
+
+    private void configureConnection( HttpConnection connection )
+        throws IOException
+    {
+        String ua = "Profile/" + System.getProperty( "microedition.profiles" ) + " Configuration/"
+            + System.getProperty( "microedition.configuration" );
+        String locale = System.getProperty( "microedition.locale" );
+
+        connection.setRequestProperty( "User-Agent", ua );
+        connection.setRequestProperty( "Accept-Language", locale );
+        connection.setRequestMethod( HttpConnection.GET );
+
+        connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
+        String hash = new String( Base64.encode( user.getUsername() + ":" + user.getPassword() ) );
+        connection.setRequestProperty( "Authorization", "Basic " + hash );
+        connection.setRequestProperty( "Accept", "application/xml" );
+
+    }
+}

=== 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-09-06 03:04:35 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/db/Storage.java	2010-09-08 09:52:06 +0000
@@ -31,6 +31,8 @@
 import javax.microedition.rms.RecordEnumeration;
 import javax.microedition.rms.RecordStore;
 import javax.microedition.rms.RecordStoreException;
+import javax.microedition.rms.RecordStoreFullException;
+import javax.microedition.rms.RecordStoreNotFoundException;
 import javax.microedition.rms.RecordStoreNotOpenException;
 import org.hisp.dhis.mobile.model.AbstractModel;
 import org.hisp.dhis.mobile.model.Activity;
@@ -168,17 +170,12 @@
     }
 
     public static void storeDataValue( DataValue dataValue )
+        throws RecordStoreException
     {
         ModelRecordStore modelRecordStore;
-        try
-        {
-            modelRecordStore = new ModelRecordStore( ModelRecordStore.DATAVALUE_DB );
-            modelRecordStore.addRecord( DataValue.dataValueToRecord( dataValue ) );
-        }
-        catch ( RecordStoreException rse )
-        {
-            System.out.println( rse.getMessage() );
-        }
+        modelRecordStore = new ModelRecordStore( ModelRecordStore.DATAVALUE_DB );
+        modelRecordStore.addRecord( DataValue.dataValueToRecord( dataValue ) );
+
     }
 
     public static Hashtable loadDataValues( Activity activity )
@@ -210,62 +207,53 @@
     }
 
     public static void updateDataValue( Activity activity, DataValue newDataValue )
+        throws RecordStoreException
     {
         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() )
         {
-            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 )
             {
-                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 );
-                }
+                int id = re.nextRecordId();
+                byte[] data = DataValue.dataValueToRecord( newDataValue );
+                rs.setRecord( id, data, 0, data.length );
             }
-            re = null;
-            rs = null;
-        }
-        catch ( RecordStoreException rse )
-        {
-            rse.printStackTrace();
-        }
+        }
+        re = null;
+        rs = null;
+
     }
-    
+
     public static void deleteDataValue( Activity activity, DataValue newDataValue )
+        throws RecordStoreException
     {
         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() )
         {
-            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 )
             {
-                if ( re.numRecords() == 1 )
-                {
-                    int id = re.nextRecordId();
-                    rs.deleteRecord( id );
-                }
+                int id = re.nextRecordId();
+                rs.deleteRecord( id );
             }
-            filter = null;
-            re = null;
-            rs = null;
-        }
-        catch ( RecordStoreException rse )
-        {
-            rse.printStackTrace();
-        }
+        }
+        filter = null;
+        re = null;
+        rs = null;
+
     }
 
     public static void saveOrgUnit( OrgUnit orgUnit )

=== 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-09-06 08:24:48 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/ui/DHISMIDlet.java	2010-09-08 09:52:06 +0000
@@ -18,6 +18,8 @@
 import javax.microedition.lcdui.TextField;
 import javax.microedition.midlet.MIDlet;
 import javax.microedition.rms.RecordStoreException;
+
+import org.hisp.dhis.mobile.connection.DataValueUploadManager;
 import org.hisp.dhis.mobile.connection.DownloadManager;
 import org.hisp.dhis.mobile.db.SettingsRectordStore;
 import org.hisp.dhis.mobile.db.Storage;
@@ -29,8 +31,8 @@
 import org.hisp.dhis.mobile.model.ProgramStageForm;
 import org.hisp.dhis.mobile.model.User;
 import org.hisp.dhis.mobile.util.AlertUtil;
-import org.hisp.dhis.mobile.util.DefaultAlertConfirmListener;
 import org.hisp.dhis.mobile.util.DnlActivitiesConfirmAlertListener;
+import org.hisp.dhis.mobile.util.ReinitConfirmListener;
 
 public class DHISMIDlet
     extends MIDlet
@@ -120,7 +122,7 @@
 
     private Command lgnFrmLgnCmd;
 
-    private Command orgUnitBackCmd;
+    // private Command orgUnitBackCmd;
 
     private Command pinFormNextCmd;
 
@@ -229,7 +231,7 @@
             }
             else if ( command == saveCommand )
             {
-                saveDataValue();
+                saveDataValues();
             }
         }
         else if ( displayable == formDownloadList )
@@ -386,8 +388,8 @@
             else if ( command == pinFormReinitCmd )
             {
                 this.getDisplay().setCurrent(
-                    AlertUtil.getConfirmAlert( "Confirmation", "Are you sure ?", new DefaultAlertConfirmListener(),
-                        this, getPinForm(), getLoginForm() ) );
+                    AlertUtil.getConfirmAlert( "Confirmation", "Are you sure ?", new ReinitConfirmListener(), this,
+                        getPinForm(), getLoginForm() ) );
             }
             else if ( command == pinFormExitCmd )
             {
@@ -396,7 +398,23 @@
         }
     }
 
-    private void saveDataValue()
+    private void saveDataValues()
+    {
+        try
+        {
+            this.saveDataValueToRMS();
+            this.switchDisplayable( AlertUtil.getInfoAlert( "Report", "DataValues successfully saved." ),
+                this.getActivitiesList() );
+        }
+        catch ( RecordStoreException e )
+        {
+            this.switchDisplayable( AlertUtil.getInfoAlert( "Report", "Fail to save datavalues" ),
+                this.getActivitiesList() );
+        }
+    }
+
+    private void saveDataValueToRMS()
+        throws RecordStoreException
     {
         Vector des = programStageForm.getDataElements();
         for ( int i = 0; i < des.size(); i++ )
@@ -412,10 +430,10 @@
             }
         }
         loadDataValues( selectedActivity );
-
     }
 
     private void storeNewDataValue( DataElement de )
+        throws RecordStoreException
     {
         if ( de.getType() == DataElement.TYPE_DATE )
         {
@@ -441,6 +459,7 @@
     }
 
     private void updateDataValue( DataElement de )
+        throws RecordStoreException
     {
 
         if ( de.getType() == DataElement.TYPE_DATE )
@@ -469,7 +488,6 @@
                 Storage.deleteDataValue( selectedActivity,
                     getDataValue( selectedActivity.getTask().getProgStageInstId(), de.getId(), txtField.getString() ) );
                 System.out.println( "Deleting: " + de.getName() );
-
             }
         }
 
@@ -479,7 +497,7 @@
     {
         selectedActivity = (Activity) activitiesVector.elementAt( getActivitiesList().getSelectedIndex() );
         ProgramStageForm formOfActivity = Storage.fetchForm( selectedActivity.getTask().getProgStageId() );
-        this.getForm( formOfActivity );
+        this.renderForm( formOfActivity, getForm() );
     }
 
     private DataValue getDataValue( int progStageId, int dataElementID, String value )
@@ -580,7 +598,7 @@
 
         ProgramStageForm form = Storage.fetchForm( downloadedProgramStage.getId() );
         System.out.println( "Name: " + form.getName() );
-        this.getForm( form );
+        this.renderForm( form, this.getForm() );
     }
 
     // the "Back" command of the downloaded forms list
@@ -954,15 +972,15 @@
     {
         if ( form == null )
         {
-            form = new Form( "form" );
+            form = new Form( "Form" );
             form.addCommand( getBackCommand() );
             form.addCommand( getScreenCommand() );
             form.addCommand( getSaveCommand() );
             form.setCommandListener( this );
-
-            // This is just for test .....
-            ProgramStageForm frm = Storage.fetchForm( 1 );
-            // renderForm( frm, form );
+        }
+        else
+        {
+            form.deleteAll();
         }
         return form;
     }
@@ -1027,25 +1045,25 @@
     }
 
     // Real downloaded forms select
-    public Form getForm( ProgramStageForm selectedForm )
-    {
-        if ( form == null )
-        {
-            form = new Form( "From" );
-            form.addCommand( getBackCommand() );
-            form.addCommand( getScreenCommand() );
-            form.addCommand( getSaveCommand() );
-            form.setCommandListener( this );
-            renderForm( selectedForm, form );
-        }
-        else
-        {
-            form.deleteAll();
-            renderForm( selectedForm, form );
-        }
-
-        return form;
-    }
+    // public Form getForm( ProgramStageForm selectedForm )
+    // {
+    // if ( form == null )
+    // {
+    // form = new Form( "From" );
+    // form.addCommand( getBackCommand() );
+    // form.addCommand( getScreenCommand() );
+    // form.addCommand( getSaveCommand() );
+    // form.setCommandListener( this );
+    // renderForm( selectedForm, form );
+    // }
+    // else
+    // {
+    // form.deleteAll();
+    // renderForm( selectedForm, form );
+    // }
+    //
+    // return form;
+    // }
 
     /**
      * Returns an initiliazed instance of backCommand component.
@@ -1220,9 +1238,8 @@
         {
             DownloadManager downloadManager = new DownloadManager( this, getUrl().getString() + "user", user,
                 DownloadManager.DOWNLOAD_ORGUNIT );
+            switchDisplayable( null, getWaitForm( "Connecting", "Please wait..." ) );
             downloadManager.start();
-            switchDisplayable( null, getWaitForm( "Connecting", "Please wait..." ) );
-
         }
         else
         {
@@ -1245,8 +1262,8 @@
                 + this.orgUnit.getProgramFormsLink()
                     .substring( this.orgUnit.getProgramFormsLink().indexOf( "orgUnits" ) ) );
 
-           this.saveOrgUnit( this.orgUnit ); 
-           // settingsRecord.put( "adminPass", adminPass.getString() );
+            this.saveOrgUnit( this.orgUnit );
+            // settingsRecord.put( "adminPass", adminPass.getString() );
             settingsRecord.save();
         }
         catch ( RecordStoreException rse )
@@ -1271,12 +1288,13 @@
         }
     }
 
-    private void browseForms()
-    {
-        // loadSettings();
-        downloadManager = new DownloadManager( this, serverUrl + "forms", user, DownloadManager.DOWNLOAD_FORMS );
-        downloadManager.start();
-    }
+    // private void browseForms()
+    // {
+    // // loadSettings();
+    // downloadManager = new DownloadManager( this, serverUrl + "forms", user,
+    // DownloadManager.DOWNLOAD_FORMS );
+    // downloadManager.start();
+    // }
 
     public void displayFormsForDownload( Vector forms )
     {
@@ -1420,30 +1438,22 @@
 
     public void sendRecordedData()
     {
-        System.out.println( "The form is:  " + programStageForm.getName() + "  with an ID of:  "
-            + programStageForm.getId() );
-
-        Vector des = programStageForm.getDataElements();
-
-        for ( int i = 0; i < des.size(); i++ )
-        {
-            DataElement de = (DataElement) des.elementAt( i );
-            if ( de.getType() == DataElement.TYPE_DATE )
-            {
-                DateField dateField = (DateField) formElements.get( de );
-                System.out.println( de.getName() + " or  " + de.getId() + "   val   " + dateField.getDate() );
-            }
-            else if ( de.getType() == DataElement.TYPE_INT )
-            {
-                TextField intField = (TextField) formElements.get( de );
-                System.out.println( de.getName() + " or  " + de.getId() + "   val   " + intField.getString() );
-            }
-            else
-            {
-                TextField txtField = (TextField) formElements.get( de );
-                System.out.println( de.getName() + " or  " + de.getId() + "   val   " + txtField.getString() );
-            }
-        }
+        // Need more test
+        
+        // try
+        // {
+        // this.saveDataValueToRMS();
+        // }
+        // catch ( Exception e )
+        // {
+        // System.out.println(e.getMessage());
+        // }
+        // DataValueUploadManager uploadManager = new DataValueUploadManager(
+        // dataValueTable,
+        // "http://localhost:8080/dhis-web-api/importDataValue.action";, orgUnit,
+        // user );
+        // uploadManager.start();
+
     }
 
     public void error( String error )

=== added file 'mobile/dhis-mobile/src/org/hisp/dhis/mobile/util/ReinitConfirmListener.java'
--- mobile/dhis-mobile/src/org/hisp/dhis/mobile/util/ReinitConfirmListener.java	1970-01-01 00:00:00 +0000
+++ mobile/dhis-mobile/src/org/hisp/dhis/mobile/util/ReinitConfirmListener.java	2010-09-08 09:52:06 +0000
@@ -0,0 +1,25 @@
+package org.hisp.dhis.mobile.util;
+
+import javax.microedition.lcdui.Command;
+import javax.microedition.lcdui.Displayable;
+
+import org.hisp.dhis.mobile.db.ModelRecordStore;
+import org.hisp.dhis.mobile.db.Storage;
+import org.hisp.dhis.mobile.ui.DHISMIDlet;
+
+public class ReinitConfirmListener
+    extends AlertConfirmListener
+{
+    public void commandAction( Command c, Displayable d )
+    {
+        if ( c.getCommandType() == Command.OK )
+        {
+            Storage.clear( ModelRecordStore.DATAVALUE_DB );
+            ((DHISMIDlet) this.midlet).switchDisplayable( null, nextScreen );
+        }
+        else if ( c.getCommandType() == Command.CANCEL )
+        {
+            ((DHISMIDlet) this.midlet).switchDisplayable( null, currentScrren );
+        }
+    }
+}