diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle index 48162860a6..8a5cc43e84 100644 --- a/opensrp-chw/build.gradle +++ b/opensrp-chw/build.gradle @@ -237,8 +237,8 @@ android { chad { dimension = 'baseDimension' applicationIdSuffix ".chad" - versionCode 2 - versionName "1.0.2" + versionCode 3 + versionName "1.0.3" buildConfigField "String", 'opensrp_url', '"https://wcaro-td.smartregister.org/opensrp/"' buildConfigField "String", 'guidebooks_url', '"https://opensrp.s3.amazonaws.com/media/chad/"' buildConfigField "String", 'opensrp_url_debug', '"https://wcaro-stage.smartregister.org/opensrp/"' @@ -247,7 +247,7 @@ android { buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS_DEBUG", '{"Clinic" , "CHSS" , "CHA"}' buildConfigField "String", 'DEFAULT_LOCATION', '"Village"' buildConfigField "String", 'DEFAULT_LOCATION_DEBUG', '"CHA"' - buildConfigField "int", "DATABASE_VERSION", '14' + buildConfigField "int", "DATABASE_VERSION", '15' buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '5' buildConfigField "int", "MAX_READ_TIMEOUT", '5' } diff --git a/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index f3de88ea1e..16879b2df8 100644 --- a/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,12 +1,20 @@ 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 net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; import org.smartregister.chw.anc.repository.VisitRepository; +import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.util.ChildDBConstants; import org.smartregister.chw.util.RepositoryUtils; +import org.smartregister.domain.Event; import org.smartregister.domain.db.Column; import org.smartregister.immunization.repository.RecurringServiceRecordRepository; import org.smartregister.immunization.repository.VaccineRepository; @@ -14,15 +22,20 @@ import org.smartregister.reporting.ReportingLibrary; import org.smartregister.repository.AlertRepository; import org.smartregister.repository.EventClientRepository; +import org.smartregister.sync.helper.ECSyncHelper; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.List; import timber.log.Timber; public class ChwRepositoryFlv { private static final String TAG = ChwRepositoryFlv.class.getCanonicalName(); + 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(), @@ -56,7 +69,7 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, upgradeToVersion10(db, oldVersion); break; case 11: - upgradeToVersion11(context,db); + upgradeToVersion11(context, db); break; case 12: upgradeToVersion12(db); @@ -67,6 +80,9 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, case 14: upgradeToVersion14(db); break; + case 15: + upgradeToVersion15(db); + break; default: break; } @@ -208,6 +224,75 @@ private static void upgradeToVersion11(Context context, SQLiteDatabase db) { } } + private static void upgradeToVersion15(SQLiteDatabase db) { + List 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 "); + } + } + } + + private static List readEvents(Cursor cursor) { + List events = new ArrayList<>(); + ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); + try { + if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { + 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(); + } + } + } catch (Exception e) { + Timber.e(e); + } finally { + cursor.close(); + } + 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 initializeIndicatorDefinitions(ReportingLibrary reportingLibrary, SQLiteDatabase sqLiteDatabase) { String childIndicatorsConfigFile = "config/child-reporting-indicator-definitions.yml"; String ancIndicatorConfigFile = "config/anc-reporting-indicator-definitions.yml";