Skip to content

Commit

Permalink
🗃️ Add migration to update null eventIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-on committed Oct 7, 2021
1 parent 890cc6c commit 34a7c1c
Showing 1 changed file with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
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;
import org.smartregister.immunization.util.IMDatabaseUtils;
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(),
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -208,6 +224,75 @@ private static void upgradeToVersion11(Context context, SQLiteDatabase db) {
}
}

private static void upgradeToVersion15(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 ");
}
}
}

private static List<Event> readEvents(Cursor cursor) {
List<Event> 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";
Expand Down

0 comments on commit 34a7c1c

Please sign in to comment.