From 34a7c1c2a912474e37e9b24b367bc9b11437deb2 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 10:57:25 +0300 Subject: [PATCH] :card_file_box: Add migration to update null eventIDs --- .../chw/repository/ChwRepositoryFlv.java | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) 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";