← Back to team overview

dhis-mobile-devs team mailing list archive

[Branch ~dhis-mobile-devs/dhis-mobile/lwuit-tracking] Rev 232: Limit the number of instances saved in the PatientRecordStore

 

------------------------------------------------------------
revno: 232
committer: sherylyn.marie
branch nick: lwuit-tracking
timestamp: Thu 2014-06-26 16:43:40 +0800
message:
  Limit the number of instances saved in the PatientRecordStore
modified:
  src/org/hisp/dhis/mobile/recordstore/PatientRecordStore.java
  src/org/hisp/dhis/mobile/view/VisitScheduleListView.java


--
lp:~dhis-mobile-devs/dhis-mobile/lwuit-tracking
https://code.launchpad.net/~dhis-mobile-devs/dhis-mobile/lwuit-tracking

Your team DHIS mobile developers is subscribed to branch lp:~dhis-mobile-devs/dhis-mobile/lwuit-tracking.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis-mobile-devs/dhis-mobile/lwuit-tracking/+edit-subscription
=== modified file 'src/org/hisp/dhis/mobile/recordstore/PatientRecordStore.java'
--- src/org/hisp/dhis/mobile/recordstore/PatientRecordStore.java	2014-05-09 09:04:54 +0000
+++ src/org/hisp/dhis/mobile/recordstore/PatientRecordStore.java	2014-06-26 08:43:40 +0000
@@ -31,6 +31,7 @@
 import java.util.Hashtable;
 import java.util.Vector;
 
+import javax.microedition.rms.RecordComparator;
 import javax.microedition.rms.RecordEnumeration;
 import javax.microedition.rms.RecordStore;
 import javax.microedition.rms.RecordStoreException;
@@ -53,6 +54,10 @@
 {
     public static final String PATIENT_DB = "PATIENT";
 
+    public static final int MAX_NO_OF_RECORDS = 50;
+
+    public static final int RECORDS_TO_DELETE = 5;
+
     public static boolean savePatients( Vector patientVector )
         throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException, IOException
     {
@@ -72,6 +77,7 @@
     {
         RecordStore recordStore = null;
         RecordEnumeration recordEnumeration = null;
+        RecordEnumeration allRecords = null;
         PatientFilter patientFilter = new PatientFilter();
         patientFilter.setPatientID( patient.getId() );
 
@@ -83,16 +89,40 @@
             if ( recordEnumeration.numRecords() > 0 )
             {
                 int id = recordEnumeration.nextRecordId();
-                recordStore.setRecord( id, bite, 0, bite.length );
+                // recordStore.setRecord( id, bite, 0, bite.length );
+                recordStore.deleteRecord( id );
+                recordStore.addRecord( bite, 0, bite.length );
             }
             else
             {
+                // check if PATIENT_DB has reached the max limit
+                if ( recordStore.getNumRecords() >= MAX_NO_OF_RECORDS )
+                {
+                    allRecords = recordStore.enumerateRecords( null, null, true );
+                    Vector ids = new Vector();
+                    int id = 0;
+                    while ( allRecords.hasNextElement() )
+                    {
+                        id = allRecords.nextRecordId();
+                        ids.insertElementAt( "" + id, 0 );
+                    }
+
+                    for ( int i = 0; i < RECORDS_TO_DELETE; i++ )
+                    {
+                        recordStore.deleteRecord( Integer.parseInt( (String) ids.elementAt( i ) ) );
+                    }
+                }
+
                 recordStore.addRecord( bite, 0, bite.length );
             }
         }
         finally
         {
             recordEnumeration.destroy();
+            if ( allRecords != null )
+            {
+                allRecords.destroy();
+            }
             recordStore.closeRecordStore();
             System.gc();
         }

=== modified file 'src/org/hisp/dhis/mobile/view/VisitScheduleListView.java'
--- src/org/hisp/dhis/mobile/view/VisitScheduleListView.java	2014-06-24 03:59:44 +0000
+++ src/org/hisp/dhis/mobile/view/VisitScheduleListView.java	2014-06-26 08:43:40 +0000
@@ -10,6 +10,7 @@
 import org.hisp.dhis.mobile.model.Patient;
 import org.hisp.dhis.mobile.model.Program;
 import org.hisp.dhis.mobile.recordstore.OfflinePatientRecordStore;
+import org.hisp.dhis.mobile.recordstore.PatientRecordStore;
 import org.hisp.dhis.mobile.util.RecordStoreUtil;
 
 import com.sun.lwuit.Command;
@@ -130,7 +131,7 @@
         }
         else if ( ae.getCommand().getCommandName().equals( "Save All" ) )
         {
-            if ( instanceInfos.size() >= 50 )
+            if ( instanceInfos.size() >= PatientRecordStore.MAX_NO_OF_RECORDS )
             {
                 nameBasedMIDlet.getAlertBoxView( "Please refine your search before saving data.", "Warning" )
                     .showView();