diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle index ed5abd487a..2c1704d343 100644 --- a/opensrp-chw/build.gradle +++ b/opensrp-chw/build.gradle @@ -255,8 +255,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/"' @@ -268,7 +268,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' } guinea { dimension = 'baseDimension' @@ -382,7 +382,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' 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 16879b2df8..f50fdad831 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,20 +1,12 @@ 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; @@ -22,20 +14,15 @@ 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(), @@ -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); @@ -225,72 +211,7 @@ 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; + RepositoryUtils.updateNullEventIds(db); } private static void initializeIndicatorDefinitions(ReportingLibrary reportingLibrary, SQLiteDatabase sqLiteDatabase) { diff --git a/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index dcefb40ea5..a7075c42af 100644 --- a/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -45,6 +45,9 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, case 11: upgradeToVersion11(db); break; + case 12: + upgradeToVersion12(db); + break; default: break; } @@ -52,15 +55,6 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, } } - 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); @@ -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); + } } diff --git a/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index 539e9cb2cb..d0b52a8cc2 100644 --- a/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,19 +1,11 @@ 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; @@ -21,18 +13,13 @@ 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(), @@ -165,71 +152,6 @@ private static void upgradeToVersion7(SQLiteDatabase db) { } private static void upgradeToVersion8(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; + RepositoryUtils.updateNullEventIds(db); } } \ No newline at end of file diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index 5deb177ca4..abcb2e487f 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -1,10 +1,20 @@ package org.smartregister.chw.util; +import static org.smartregister.repository.BaseRepository.TYPE_Synced; +import static org.smartregister.repository.BaseRepository.TYPE_Valid; + +import android.database.Cursor; import net.sqlcipher.database.SQLiteDatabase; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; +import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.application.CoreChwApplication; import org.smartregister.chw.core.utils.CoreConstants; +import org.smartregister.domain.Event; import org.smartregister.family.util.DBConstants; +import org.smartregister.repository.EventClientRepository; +import org.smartregister.sync.helper.ECSyncHelper; import org.smartregister.util.DatabaseMigrationUtils; import java.util.ArrayList; @@ -14,6 +24,9 @@ public interface RepositoryUtils { + String EVENT_ID = "id"; + String _ID = "_id"; + String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; @@ -50,4 +63,73 @@ static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { } } + static void updateNullEventIds(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, "Problem getting events "); + } + 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, "Problem executing update "); + } + } + } + + 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; + } + + 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; + } + } diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java new file mode 100644 index 0000000000..a7bf8efe18 --- /dev/null +++ b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java @@ -0,0 +1,66 @@ +package org.smartregister.chw.presenter; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.Context; +import org.smartregister.commonregistry.CommonFtsObject; +import org.smartregister.reporting.ReportingLibrary; +import org.smartregister.reporting.contract.ReportContract; +import org.smartregister.reporting.domain.IndicatorQuery; +import org.smartregister.reporting.domain.ReportIndicator; +import org.smartregister.repository.Repository; + +import java.util.ArrayList; +import java.util.List; + +public class JobAidsDashboardFragmentPresenterTest { + + private JobAidsDashboardFragmentPresenter presenter; + + @Mock + ReportContract.View view; + + @Mock + ReportContract.Model model; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + ReportingLibrary.init(Mockito.mock(Context.class), Mockito.mock(Repository.class), + Mockito.mock(CommonFtsObject.class), 1, 1); + presenter = new JobAidsDashboardFragmentPresenter(view); + ReflectionHelpers.setField(presenter, "model", model); + } + + @Test + public void fetchDailyTalliesCallsModelGetDailyTallies() { + presenter.fetchIndicatorsDailytallies(); + Mockito.verify(model).getIndicatorsDailyTallies(); + } + + @Test + public void canAddAListOfIndicators() { + List indicators = new ArrayList<>(); + indicators.add(Mockito.mock(ReportIndicator.class)); + presenter.addIndicators(indicators); + Mockito.verify(model, Mockito.atLeast(1)).addIndicator(Mockito.any(ReportIndicator.class)); + } + + @Test + public void canAddIndicatorQueries() { + List indicatorQueries = new ArrayList<>(); + indicatorQueries.add(Mockito.mock(IndicatorQuery.class)); + presenter.addIndicatorQueries(indicatorQueries); + Mockito.verify(model, Mockito.atLeast(1)).addIndicatorQuery(Mockito.any(IndicatorQuery.class)); + } + + @Test + public void canGetView() { + Assert.assertTrue(presenter.getView() instanceof ReportContract.View); + } +} diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java new file mode 100644 index 0000000000..598fbb6163 --- /dev/null +++ b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java @@ -0,0 +1,134 @@ +package org.smartregister.chw.util; + +import net.sqlcipher.MatrixCursor; +import net.sqlcipher.database.SQLiteDatabase; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.smartregister.chw.application.ChwApplication; +import org.smartregister.repository.EventClientRepository; + +@RunWith(RobolectricTestRunner.class) +@Config(application = ChwApplication.class, sdk = 22) +public class RepositoryUtilsTest { + + @Mock + private SQLiteDatabase database; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void updateNullEventIdsUpdatesCorrectEvents() { + MatrixCursor matrixCursor = new MatrixCursor(new String[]{"json"}); + matrixCursor.addRow(new Object[]{getSampleEventJSONString()}); + Mockito.doReturn(matrixCursor).when(database).query(ArgumentMatchers.eq(EventClientRepository.Table.event.name()), + ArgumentMatchers.any(String[].class), ArgumentMatchers.eq("eventId IS NULL AND validationStatus = ?"), + ArgumentMatchers.any(String[].class), ArgumentMatchers.isNull(), ArgumentMatchers.isNull(), ArgumentMatchers.isNull()); + RepositoryUtils.updateNullEventIds(database); + Mockito.verify(database).execSQL("UPDATE event SET eventId = '3b598b80-13ee-4a9a-8cd1-8e66fa76bbe9', " + + "syncStatus = 'Synced' WHERE formSubmissionId = '45a294f5-ec2f-4233-847a-6f7910a6e63f';"); + } + + private static String getSampleEventJSONString() { + return "{\n" + + "\n" + + " baseEntityId: \"158cb2d0-a78f-4087-9ccc-0a0de9724548\",\n" + + " childLocationId: \"Nairobi\",\n" + + " duration: 0,\n" + + " entityType: \"ec_family_member\",\n" + + " eventDate: \"2020-12-20T00:00:00.000Z\",\n" + + " eventType: \"Family Member Registration\",\n" + + " formSubmissionId: \"45a294f5-ec2f-4233-847a-6f7910a6e63f\",\n" + + " locationId: \"bd657bc4-fad6-40b1-bdc4-38a8f1ddfdc4\",\n" + + " obs: [\n" + + " {\n" + + " fieldCode: \"\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"surname\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"Jeff\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"fam_name\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"Koinange\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"dob_unknown\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"formsubmissionField\",\n" + + " formSubmissionField: \"dob_unknown\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"true\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"last_interacted_with\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"formsubmissionField\",\n" + + " formSubmissionField: \"last_interacted_with\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"1608491344580\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\n" + + " fieldDataType: \"start\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"start\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"2020-12-20 19:08:16\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\n" + + " fieldDataType: \"end\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"end\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"2020-12-20 19:09:04\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + " providerId: \"onatester\",\n" + + " team: \"ONA\",\n" + + " teamId: \"127edf8b-d021-41b9-a74a-71896397ed42\",\n" + + " version: 1608491344583,\n" + + " clientApplicationVersion: 13,\n" + + " clientDatabaseVersion: 15,\n" + + " dateCreated: \"2020-12-20T19:09:04.583Z\",\n" + + " type: \"Event\",\n" + + "_id: \"3b598b80-13ee-4a9a-8cd1-8e66fa76bbe9\",\n" + + "_rev: \"v1\"" + + "}"; + } + +} diff --git a/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index 7dcee515f0..e7fbba4475 100644 --- a/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,23 +1,17 @@ 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 android.database.Cursor; import net.sqlcipher.database.SQLiteDatabase; -import org.apache.commons.lang3.StringUtils; import org.joda.time.format.DateTimeFormat; -import org.json.JSONObject; import org.smartregister.chw.anc.AncLibrary; import org.smartregister.chw.anc.domain.Visit; import org.smartregister.chw.anc.domain.VisitDetail; import org.smartregister.chw.anc.repository.VisitDetailsRepository; import org.smartregister.chw.anc.repository.VisitRepository; import org.smartregister.chw.anc.util.NCUtils; -import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.application.CoreChwApplication; import org.smartregister.chw.core.repository.ScheduleRepository; import org.smartregister.chw.core.rule.PNCHealthFacilityVisitRule; @@ -42,7 +36,6 @@ import org.smartregister.reporting.ReportingLibrary; import org.smartregister.repository.AlertRepository; import org.smartregister.repository.EventClientRepository; -import org.smartregister.sync.helper.ECSyncHelper; import org.smartregister.util.DatabaseMigrationUtils; import java.util.ArrayList; @@ -55,9 +48,6 @@ 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(), "Upgrading database from version " + oldVersion + " to " @@ -319,7 +309,7 @@ private static List getEvents(SQLiteDatabase db, String[] selectionArgs) try { String[] myStringArray = {"json"}; cursor = db.query(EventClientRepository.Table.event.name(), myStringArray, " eventType = ? ", selectionArgs, null, null, " eventDate ASC ", null); - events = readEvents(cursor); + events = RepositoryUtils.readEvents(cursor); } catch (Exception e) { Timber.e(e); } finally { @@ -330,45 +320,6 @@ private static List getEvents(SQLiteDatabase db, String[] selectionArgs) return events; } - 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 processHFNextVisitDateObs(List events, SQLiteDatabase sqLiteDatabase) { // Save missing PNC Health Facility visit (next visit) details for (Event event : events) { @@ -471,32 +422,6 @@ private static void upgradeToVersion20(SQLiteDatabase db) { } private static void upgradeToVersion21(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 "); - } - } + RepositoryUtils.updateNullEventIds(db); } }