Skip to content

Commit

Permalink
Merge pull request #1933 from opensrp/update-wcaro-liberia
Browse files Browse the repository at this point in the history
Update WCARO-Liberia to v1.0.1
  • Loading branch information
allan-on authored Oct 7, 2021
2 parents e5bfd02 + 63fca82 commit 6e07088
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ android {
}
liberia {
dimension = 'baseDimension'
versionCode 7
versionName "1.0.0"
versionCode 8
versionName "1.0.1"
buildConfigField "String", 'opensrp_url', '"https://wcaro-lr.smartregister.org/opensrp/"'
buildConfigField "String", 'guidebooks_url', '"https://opensrp.s3.amazonaws.com/media/liberia/"'
buildConfigField "String", 'opensrp_url_debug', '"https://wcaro-stage.smartregister.org/opensrp/"'
Expand All @@ -323,7 +323,7 @@ android {
buildConfigField "String", 'DEFAULT_LOCATION_DEBUG', '"CHA"'
buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '5'
buildConfigField "int", "MAX_READ_TIMEOUT", '5'
buildConfigField "int", "DATABASE_VERSION", '7'
buildConfigField "int", "DATABASE_VERSION", '8'
}
lmh {
dimension = 'baseDimension'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
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.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 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 All @@ -46,6 +59,9 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion,
case 7:
upgradeToVersion7(db);
break;
case 8:
upgradeToVersion8(db);
break;
default:
break;
}
Expand Down Expand Up @@ -147,4 +163,73 @@ private static void upgradeToVersion7(SQLiteDatabase db) {
Timber.e(e, "upgradeToVersion7");
}
}

private static void upgradeToVersion8(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;
}
}

0 comments on commit 6e07088

Please sign in to comment.