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 committed Oct 25, 2021
2 parents ca9fbbe + 87eed43 commit 40cfaf0
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 249 deletions.
8 changes: 4 additions & 4 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ android {
resConfigs "en", "fr"
dimension = 'baseDimension'
applicationIdSuffix ".drc"
versionCode 10
versionName "1.0.15"
versionCode 11
versionName "1.0.16"
buildConfigField "String", 'opensrp_url', '"https://wcaro-cd.smartregister.org/opensrp/"'
buildConfigField "String", 'guidebooks_url', '"https://opensrp.s3.amazonaws.com/media/drc/"'
buildConfigField "String", 'opensrp_url_preview', '"https://wcaro-cd-preview.smartregister.org/opensrp/"'
Expand All @@ -270,7 +270,7 @@ android {
buildConfigField "String", 'DEFAULT_LOCATION', '"VILLAGE/COMMUNAUTE"'
buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '5'
buildConfigField "int", "MAX_READ_TIMEOUT", '5'
buildConfigField "int", "DATABASE_VERSION", '11'
buildConfigField "int", "DATABASE_VERSION", '12'
}
nigeria {
dimension = 'baseDimension'
Expand Down Expand Up @@ -401,7 +401,7 @@ dependencies {
//For viewing PDFs in the app
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

implementation 'com.github.lecho:hellocharts-android:v1.5.8@aar'
implementation 'com.github.lecho:hellocharts-android:1.5.8@aar'

testImplementation "org.koin:koin-test:2.0.1"
testImplementation 'junit:junit:4.13'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
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 @@ -98,7 +85,6 @@ private static void upgradeToVersion14(SQLiteDatabase db) {
}
}


private static void upgradeToVersion2(Context context, SQLiteDatabase db) {
try {
db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_EVENT_ID_COL);
Expand Down Expand Up @@ -225,72 +211,7 @@ 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;
RepositoryUtils.updateNullEventIds(db);
}

private static void initializeIndicatorDefinitions(ReportingLibrary reportingLibrary, SQLiteDatabase sqLiteDatabase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,16 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion,
case 11:
upgradeToVersion11(db);
break;
case 12:
upgradeToVersion12(db);
break;
default:
break;
}
upgradeTo++;
}
}

private static void upgradeToVersion11(SQLiteDatabase db) {
try {
db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;");
} catch (Exception e) {
Timber.e(e, "upgradeToVersion11");
}
}


private static void upgradeToVersion2(Context context, SQLiteDatabase db) {
try {
db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_EVENT_ID_COL);
Expand Down Expand Up @@ -159,4 +153,16 @@ private static void upgradeToVersion10(Context context, SQLiteDatabase db) {
Timber.e(e);
}
}

private static void upgradeToVersion11(SQLiteDatabase db) {
try {
db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;");
} catch (Exception e) {
Timber.e(e, "upgradeToVersion11");
}
}

private static void upgradeToVersion12(SQLiteDatabase db) {
RepositoryUtils.updateNullEventIds(db);
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
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 Down Expand Up @@ -165,71 +152,6 @@ private static void upgradeToVersion7(SQLiteDatabase db) {
}

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;
RepositoryUtils.updateNullEventIds(db);
}
}
Loading

0 comments on commit 40cfaf0

Please sign in to comment.