Skip to content

Commit

Permalink
Merge pull request #1924 from opensrp/add-migration-to-populate-eventId
Browse files Browse the repository at this point in the history
Update WCARO Togo: Add migration to update null eventIds
  • Loading branch information
allan-on authored Oct 1, 2021
2 parents 3d203fb + 0fe840e commit d181985
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 31 deletions.
10 changes: 5 additions & 5 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ android {
togo {
dimension = 'baseDimension'
applicationIdSuffix ".togo"
versionCode 24
versionName "1.2.6"
versionCode 25
versionName "1.2.7"
buildConfigField "int", "OPENMRS_UNIQUE_ID_INITIAL_BATCH_SIZE", '1000'
buildConfigField "int", "OPENMRS_UNIQUE_ID_BATCH_SIZE", '500'
buildConfigField "String", 'opensrp_url', '"https://wcaro-tg.smartregister.org/opensrp/"'
Expand All @@ -305,9 +305,9 @@ android {
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS", '{"National", "Regional" , "District" , "Formation sanitaire", "Supervisor", "Village"}'
buildConfigField "String", 'DEFAULT_LOCATION_DEBUG', '"Village"'
buildConfigField "String", 'DEFAULT_LOCATION', '"Village"'
buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '5'
buildConfigField "int", "MAX_READ_TIMEOUT", '5'
buildConfigField "int", "DATABASE_VERSION", '20'
buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '10'
buildConfigField "int", "MAX_READ_TIMEOUT", '10'
buildConfigField "int", "DATABASE_VERSION", '21'
}
liberia {
dimension = 'baseDimension'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
package org.smartregister.chw.sync;

import org.smartregister.chw.dao.EventDao;
import org.smartregister.sync.intent.SyncIntentService;

public class ChwSyncIntentService extends SyncIntentService {

@Override
protected void handleSync() {
// fetch the last downloaded serverVersion before any unsyced data
Long serverVersion = EventDao.getMinimumVerifiedServerVersion();
if (serverVersion != null)
org.smartregister.util.Utils.getAllSharedPreferences().saveLastSyncDate(serverVersion);

// flag all contentious events as unsynced
EventDao.markEventsForReUpload();
super.handleSync();
}

@Override
public int getEventPullLimit() {
return 1000;
Expand All @@ -25,6 +12,6 @@ public int getEventPullLimit() {

@Override
protected Integer getEventBatchSize(){
return 180;
return 50;
} // Should this be configurable?
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.smartregister.chw.repository;

import static org.smartregister.repository.BaseRepository.TYPE_Synced;
import static org.smartregister.repository.BaseRepository.TYPE_Valid;

import android.content.Context;
import android.database.Cursor;

import net.sqlcipher.database.SQLiteDatabase;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.json.JSONObject;
import org.smartregister.chw.anc.AncLibrary;
Expand Down Expand Up @@ -51,6 +55,9 @@

public class ChwRepositoryFlv {

private static final String EVENT_ID = "id";
private static final String _ID = "_id";

public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) {
Timber.w(ChwRepository.class.getName(),
"Upgrading database from version " + oldVersion + " to "
Expand Down Expand Up @@ -109,21 +116,16 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion,
case 20:
upgradeToVersion20(db);
break;
case 21:
upgradeToVersion21(db);
break;
default:
break;
}
upgradeTo++;
}
}

private static void upgradeToVersion20(SQLiteDatabase db) {
try {
db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;");
} catch (Exception e) {
Timber.e(e, "upgradeToVersion20");
}
}

private static void upgradeToVersion2(Context context, SQLiteDatabase db) {
try {
db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_EVENT_ID_COL);
Expand Down Expand Up @@ -336,6 +338,7 @@ private static List<Event> readEvents(Cursor cursor) {
while (!cursor.isAfterLast()) {
String json = cursor.getString(cursor.getColumnIndex("json"));
Event event = syncHelper.convert(new JSONObject(json), Event.class);
event.setEventId(getEventId(json));
events.add(event);
cursor.moveToNext();
}
Expand All @@ -348,6 +351,24 @@ private static List<Event> readEvents(Cursor cursor) {
return events;
}

private static String getEventId(String jsonString) {
JSONObject jsonObject;
String eventId = null;
if (StringUtils.isNotEmpty(jsonString)) {
try {
jsonObject = new JSONObject(jsonString);
if (jsonObject.has(EVENT_ID)) {
eventId = jsonObject.getString(EVENT_ID);
}else if (jsonObject.has(_ID)) {
eventId = jsonObject.getString(_ID);
}
} catch (Exception ex) {
Timber.e(ex);
}
}
return eventId;
}

private static void processHFNextVisitDateObs(List<Event> events, SQLiteDatabase sqLiteDatabase) {
// Save missing PNC Health Facility visit (next visit) details
for (Event event : events) {
Expand Down Expand Up @@ -420,17 +441,17 @@ private static void upgradeToVersion17(SQLiteDatabase db) {
Timber.e(e, "upgradeToVersion17");
}
}

private static void upgradeToVersion18(SQLiteDatabase db) {
try {
ReportingLibrary reportingLibraryInstance = ReportingLibrary.getInstance();
initializeIndicatorDefinitions(reportingLibraryInstance, db);
} catch (Exception e){
} catch (Exception e) {
Timber.e(e, "upgradeToVersion18");
}
}
private static void upgradeToVersion19(Context context, SQLiteDatabase db) {

private static void upgradeToVersion19(Context context, SQLiteDatabase db) {
try {
db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_IS_VOIDED_COL);
db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_IS_VOIDED_COL_INDEX);
Expand All @@ -440,4 +461,42 @@ private static void upgradeToVersion19(Context context, SQLiteDatabase db) {
Timber.e(e);
}
}

private static void upgradeToVersion20(SQLiteDatabase db) {
try {
db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;");
} catch (Exception e) {
Timber.e(e, "upgradeToVersion20");
}
}

private static void upgradeToVersion21(SQLiteDatabase db) {
List<Event> events = new ArrayList<>();
String eventTableName = EventClientRepository.Table.event.name();
String eventIdCol = EventClientRepository.event_column.eventId.name();
String eventSyncStatusCol = EventClientRepository.event_column.syncStatus.name();
String eventValidCol = EventClientRepository.event_column.validationStatus.name();
String jsonCol = EventClientRepository.event_column.json.name();
String formSubmissionCol = EventClientRepository.event_column.formSubmissionId.name();

Cursor cursor;
String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?";
try {
cursor = db.query(eventTableName, new String[]{jsonCol},
selection, new String[]{TYPE_Valid}, null, null, null);
events = readEvents(cursor);
} catch (Exception ex) {
Timber.e(ex);
}
String updateSQL;
for (Event event : events) {
updateSQL = String.format("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';", eventTableName,
eventIdCol, event.getEventId(), eventSyncStatusCol, TYPE_Synced, formSubmissionCol, event.getFormSubmissionId());
try {
db.execSQL(updateSQL);
} catch (Exception e) {
Timber.e(e, "upgradeToVersion21 ");
}
}
}
}

0 comments on commit d181985

Please sign in to comment.