Skip to content

Commit

Permalink
Merge branch 'master' into flavor-nigeria-dcis
Browse files Browse the repository at this point in the history
  • Loading branch information
zzainulabidin authored Oct 11, 2021
2 parents b58d650 + 6e07088 commit 1c84228
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 7 deletions.
12 changes: 6 additions & 6 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,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/"'
Expand All @@ -249,7 +249,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'
}
Expand Down Expand Up @@ -328,8 +328,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 @@ -340,7 +340,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,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
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 1c84228

Please sign in to comment.