From 1f55a7dd0efe213f881121d496c59e53088acbf9 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Thu, 26 Jan 2023 16:38:54 +0500 Subject: [PATCH 1/5] Assign child's origin facility id to OOC vaccine events --- .../BaseChildImmunizationActivity.java | 17 ++++++++- .../fragment/BaseChildRegisterFragment.java | 2 + .../child/provider/RegisterQueryProvider.java | 16 ++++---- .../child/util/ChildJsonFormUtils.java | 18 +++++++-- .../org/smartregister/child/util/Utils.java | 38 +++++++++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/opensrp-child/src/main/java/org/smartregister/child/activity/BaseChildImmunizationActivity.java b/opensrp-child/src/main/java/org/smartregister/child/activity/BaseChildImmunizationActivity.java index 92af5c219..6a341431d 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/activity/BaseChildImmunizationActivity.java +++ b/opensrp-child/src/main/java/org/smartregister/child/activity/BaseChildImmunizationActivity.java @@ -1516,7 +1516,22 @@ private void saveVaccine(VaccineRepository vaccineRepository, VaccineWrapper tag vaccine.setName(tag.getName()); vaccine.setDate(tag.getUpdatedVaccineDate().toDate()); vaccine.setAnmId(getOpenSRPContext().allSharedPreferences().fetchRegisteredANM()); - vaccine.setLocationId(ChildJsonFormUtils.getProviderLocationId(this)); + + // Assign Location Id as per the child's home facility + if (Utils.childBelongsToCurrentFacility(getChildDetails().getColumnmaps())) { + vaccine.setLocationId(ChildJsonFormUtils.getProviderLocationId(this)); + } else if (Utils.isChildTemporaryOOC(childDetails.getColumnmaps())) { + String locationId = Utils.getLocationIdFromChildTempOOCEvent(vaccine.getBaseEntityId()); + if (StringUtils.isEmpty(locationId)) // Pickup current faciilty id + locationId = ChildJsonFormUtils.getProviderLocationId(this); + vaccine.setLocationId(locationId); + } else { + String locationId = childDetails.getColumnmaps().get(AllConstants.LOCATION_ID); + if (StringUtils.isEmpty(locationId)) + locationId = ChildJsonFormUtils.getProviderLocationId(this); + vaccine.setLocationId(locationId); + } + vaccine.setChildLocationId(ChildJsonFormUtils.getChildLocationId(getOpenSRPContext().allSharedPreferences().fetchDefaultLocalityId(vaccine.getAnmId()), getOpenSRPContext().allSharedPreferences())); String lastChar = vaccine.getName().substring(vaccine.getName().length() - 1); diff --git a/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseChildRegisterFragment.java b/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseChildRegisterFragment.java index 6dc613d07..3e8e5748c 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseChildRegisterFragment.java +++ b/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseChildRegisterFragment.java @@ -569,4 +569,6 @@ public void setOverDueCount(int overDueCount) { public int getOverDueCount() { return overDueCount; } + + } diff --git a/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java b/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java index a7b715f76..4814834fc 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java +++ b/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java @@ -17,10 +17,10 @@ public String getObjectIdsQuery(String mainCondition, String filters) { strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters); } - return "SELECT " + getDemographicTable() + ".object_id " + - "FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " + - "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " + - "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + + return "SELECT " + getDemographicTable() + ".base_entity_id " + + "FROM " + getDemographicTable() + " " + getDemographicTable() + " " + + "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + getChildDetailsTable() + ".id " + +// "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + strMainCondition + strFilters; } @@ -47,10 +47,10 @@ public String getCountExecuteQuery(String mainCondition, String filters) { strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters); } - return "SELECT count(" + getDemographicTable() + ".object_id) " + - "FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " + - "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " + - "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + + return "SELECT count(" + getDemographicTable() + ".base_entity_id) " + + "FROM " + getDemographicTable() + " " + getDemographicTable() + " " + + "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + getChildDetailsTable() + ".id " + +// "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + strMainCondition + strFilters; } diff --git a/opensrp-child/src/main/java/org/smartregister/child/util/ChildJsonFormUtils.java b/opensrp-child/src/main/java/org/smartregister/child/util/ChildJsonFormUtils.java index a0d2de2a1..b47b21322 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/util/ChildJsonFormUtils.java +++ b/opensrp-child/src/main/java/org/smartregister/child/util/ChildJsonFormUtils.java @@ -1777,6 +1777,7 @@ public static boolean processMoveToCatchment(org.smartregister.Context openSRPCo String teamId = allSharedPreferences.fetchDefaultTeamId(providerId); String locationId = allSharedPreferences.fetchUserLocalityId(providerId); + String fromLocationId = ""; // SAve Child's origin location for (int i = 0; i < clients.length(); i++) { JSONObject client = clients.getJSONObject(i); @@ -1787,6 +1788,7 @@ public static boolean processMoveToCatchment(org.smartregister.Context openSRPCo } client.put("teamId", teamId); + fromLocationId = client.getString("locationId"); client.put("locationId", locationId); } @@ -1802,7 +1804,7 @@ public static boolean processMoveToCatchment(org.smartregister.Context openSRPCo processMoveToCatchmentPermanent(openSRPContext.applicationContext(), openSRPContext.allSharedPreferences(), eventPairList); processTriggerClientProcessorAndUpdateFTS(openSRPContext, clients); } else { - processMoveToCatchmentTemporary(openSRPContext, events, clients, moveToCatchmentEvent.isCreateEvent()); + processMoveToCatchmentTemporary(openSRPContext, events, clients, moveToCatchmentEvent.isCreateEvent(), fromLocationId); } return true; @@ -1827,9 +1829,9 @@ private static void processTriggerClientProcessorAndUpdateFTS(org.smartregister. Timber.i("Moved %s client(s) to new catchment area.", clientIds.size()); } - public static void processMoveToCatchmentTemporary(org.smartregister.Context opensrpContext, JSONArray events, JSONArray clients, boolean createEvent) throws Exception { + public static void processMoveToCatchmentTemporary(org.smartregister.Context opensrpContext, JSONArray events, JSONArray clients, boolean createEvent, String fromLocationId) throws Exception { if (createEvent) { - Event moveToCatchmentSyncEvent = createMoveToCatchmentSyncEvent(opensrpContext, clients); + Event moveToCatchmentSyncEvent = createMoveToCatchmentSyncEvent(opensrpContext, clients, fromLocationId); convertAndPersistEvent(moveToCatchmentSyncEvent); } @@ -2042,7 +2044,7 @@ public static Event createMoveToCatchmentEvent(Context context, Identifiers toId } } - public static Event createMoveToCatchmentSyncEvent(org.smartregister.Context opensrpContext, JSONArray clientList) { + public static Event createMoveToCatchmentSyncEvent(org.smartregister.Context opensrpContext, JSONArray clientList, String fromLocationId) { try { if (clientList == null) { @@ -2065,6 +2067,14 @@ public static Event createMoveToCatchmentSyncEvent(org.smartregister.Context ope } event.addObs(new Obs(FORM_SUBMISSION_FIELD, DATA_TYPE, MoveToMyCatchmentUtils.MOVE_TO_CATCHMENT_IDENTIFIERS_FORM_FIELD, "", val, new ArrayList<>(), null, MoveToMyCatchmentUtils.MOVE_TO_CATCHMENT_IDENTIFIERS_FORM_FIELD)); + + // Add info for origin's location Id + String formSubmissionField = "From_LocationId"; + List vall = new ArrayList<>(); + vall.add(fromLocationId); + event.addObs(new Obs(FORM_SUBMISSION_FIELD, DATA_TYPE, formSubmissionField, "", vall, new ArrayList<>(), null, + formSubmissionField)); + event.setBaseEntityId(clientBaseEntityId); addMetaData(opensrpContext.applicationContext(), event, new Date()); diff --git a/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java b/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java index 962ed1439..f54c42b9b 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java +++ b/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java @@ -1,5 +1,6 @@ package org.smartregister.child.util; +import static org.smartregister.child.util.MoveToMyCatchmentUtils.MOVE_TO_CATCHMENT_SYNC_EVENT; import static org.smartregister.immunization.util.VaccinatorUtils.translate; import android.app.Activity; @@ -53,6 +54,7 @@ import org.smartregister.commonregistry.CommonPersonObject; import org.smartregister.commonregistry.CommonPersonObjectClient; import org.smartregister.commonregistry.CommonRepository; +import org.smartregister.domain.db.EventClient; import org.smartregister.domain.tag.FormTag; import org.smartregister.growthmonitoring.domain.Height; import org.smartregister.growthmonitoring.domain.HeightWrapper; @@ -69,6 +71,7 @@ import org.smartregister.immunization.service.intent.VaccineIntentService; import org.smartregister.repository.AllSharedPreferences; import org.smartregister.repository.BaseRepository; +import org.smartregister.repository.EventClientRepository; import org.smartregister.repository.UniqueIdRepository; import java.text.DateFormat; @@ -779,4 +782,39 @@ public static boolean isChildHasNFCCard(Map detailsMap) { return StringUtils.isNotEmpty(detailsMap.get(Constants.KEY.NFC_CARD_IDENTIFIER)) && !Constants.TRUE.equalsIgnoreCase(detailsMap.getOrDefault(Constants.KEY.NFC_CARD_BLACKLISTED, Constants.FALSE)); } + + public static boolean childBelongsToCurrentFacility(@NotNull Map childDetails) { + String caseId = childDetails.get(DBConstants.KEY.BASE_ENTITY_ID); + CommonPersonObjectClient client = ChildDbUtils.fetchCommonPersonObjectClientByBaseEntityId(caseId); + + String outOfArea = getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false); + return client != null && (Constants.FALSE.equalsIgnoreCase(outOfArea) || StringUtils.isEmpty(outOfArea)); + } + + public static boolean isChildTemporaryOOC(@NotNull Map childDetails) { + return !childBelongsToCurrentFacility(childDetails) && (ChildLibrary.getInstance() + .getProperties().isTrue(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT) + && Boolean.valueOf(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false))); + } + + public static String getLocationIdFromChildTempOOCEvent(String baseEntityId) { + EventClientRepository eventClientRepository = ChildLibrary.getInstance().eventClientRepository(); + String query = "SELECT "+ EventClientRepository.event_column.json.name() +" FROM " + + EventClientRepository.Table.event.name() + + " WHERE " + + "eventType = '"+ MOVE_TO_CATCHMENT_SYNC_EVENT +"' " + + "AND baseEntityId = '"+ baseEntityId +"' " + + "ORDER BY "+EventClientRepository.event_column.dateCreated.name()+" DESC LIMIT 1"; + List moveToCatchmentSyncEventClient = eventClientRepository.fetchEventClientsCore(query, null); + org.smartregister.domain.Event moveToCatchmentSyncEvent = moveToCatchmentSyncEventClient.get(0).getEvent(); + String locationId = ""; + for (int i = moveToCatchmentSyncEvent.getObs().size() - 1; i > -1; i--) { + org.smartregister.domain.Obs obs = moveToCatchmentSyncEvent.getObs().get(i); + + if (obs != null && "From_LocationId".equals(obs.getFormSubmissionField())) { + locationId = (String) obs.getValues().get(0); + } + } + return locationId; + } } \ No newline at end of file From 8d48e8bf6c2613b952e0e2eecdbb804963f95eaf Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Thu, 2 Feb 2023 12:21:44 +0500 Subject: [PATCH 2/5] Revert irrelevant code --- .../child/provider/RegisterQueryProvider.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java b/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java index 4814834fc..a7b715f76 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java +++ b/opensrp-child/src/main/java/org/smartregister/child/provider/RegisterQueryProvider.java @@ -17,10 +17,10 @@ public String getObjectIdsQuery(String mainCondition, String filters) { strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters); } - return "SELECT " + getDemographicTable() + ".base_entity_id " + - "FROM " + getDemographicTable() + " " + getDemographicTable() + " " + - "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + getChildDetailsTable() + ".id " + -// "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + + return "SELECT " + getDemographicTable() + ".object_id " + + "FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " + + "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " + + "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + strMainCondition + strFilters; } @@ -47,10 +47,10 @@ public String getCountExecuteQuery(String mainCondition, String filters) { strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters); } - return "SELECT count(" + getDemographicTable() + ".base_entity_id) " + - "FROM " + getDemographicTable() + " " + getDemographicTable() + " " + - "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".base_entity_id = " + getChildDetailsTable() + ".id " + -// "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + + return "SELECT count(" + getDemographicTable() + ".object_id) " + + "FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " + + "LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " + + "LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " + strMainCondition + strFilters; } From aff9f4d29bf7c672a2d76d667dc6dc24017d944e Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 21 Feb 2023 18:57:00 +0500 Subject: [PATCH 3/5] Bump up library version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 711a29853..b8921ff25 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.0.1-SNAPSHOT +VERSION_NAME=1.0.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Child Library From a02535da55f792236e3d1a4532f37b8fb85c06db Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 24 Feb 2023 12:43:58 +0500 Subject: [PATCH 4/5] Increase code coverage --- .../org/smartregister/child/util/Utils.java | 2 +- .../smartregister/child/utils/UtilsTest.java | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java b/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java index ffb65735a..794a98ab5 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java +++ b/opensrp-child/src/main/java/org/smartregister/child/util/Utils.java @@ -794,7 +794,7 @@ public static boolean childBelongsToCurrentFacility(@NotNull Map public static boolean isChildTemporaryOOC(@NotNull Map childDetails) { return !childBelongsToCurrentFacility(childDetails) && (ChildLibrary.getInstance() .getProperties().isTrue(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT) - && Boolean.valueOf(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false))); + && Boolean.parseBoolean(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false))); } public static String getLocationIdFromChildTempOOCEvent(String baseEntityId) { diff --git a/opensrp-child/src/test/java/org/smartregister/child/utils/UtilsTest.java b/opensrp-child/src/test/java/org/smartregister/child/utils/UtilsTest.java index 30ecf9156..cfea9d777 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/utils/UtilsTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/utils/UtilsTest.java @@ -1,5 +1,9 @@ package org.smartregister.child.utils; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; + import android.app.Activity; import android.graphics.Typeface; import android.view.View; @@ -11,6 +15,8 @@ import com.google.common.collect.ImmutableMap; +import net.sqlcipher.database.SQLiteDatabase; + import org.joda.time.DateTime; import org.junit.Assert; import org.junit.Before; @@ -33,8 +39,11 @@ import org.smartregister.child.ChildLibrary; import org.smartregister.child.R; import org.smartregister.child.domain.ChildMetadata; +import org.smartregister.child.provider.RegisterQueryProvider; +import org.smartregister.child.util.ChildAppProperties; import org.smartregister.child.util.Constants; import org.smartregister.child.util.Utils; +import org.smartregister.domain.db.EventClient; import org.smartregister.growthmonitoring.domain.Height; import org.smartregister.growthmonitoring.domain.HeightWrapper; import org.smartregister.growthmonitoring.domain.Weight; @@ -46,6 +55,7 @@ import org.smartregister.immunization.domain.Vaccine; import org.smartregister.immunization.repository.VaccineRepository; import org.smartregister.repository.AllSharedPreferences; +import org.smartregister.repository.EventClientRepository; import org.smartregister.repository.Repository; import org.smartregister.util.AppProperties; @@ -53,6 +63,7 @@ import java.util.Arrays; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; @PrepareForTest({VaccineRepo.class, ImmunizationLibrary.class}) @@ -522,4 +533,72 @@ public void testGetWeeksDueWithNullDateReturnsZero() { int weeksDue = Utils.getWeeksDue(date); Assert.assertEquals(0, weeksDue); } + + @Test + public void testChildBelongsToCurrentFacilityShouldReturnTrueWhenChildExistsInDB() { + HashMap childDetails = new HashMap<>(); + childDetails.put("base_entity_id", "case_id"); + childDetails.put("is_out_of_catchment", "false"); + ArrayList> childDetailsList = new ArrayList<>(); + childDetailsList.add(childDetails); + + EventClientRepository eventClientRepository = Mockito.mock(EventClientRepository.class); + Repository repository = Mockito.mock(Repository.class); + Mockito.doReturn(eventClientRepository).when(childLibrary).eventClientRepository(); + Mockito.doReturn(repository).when(childLibrary).getRepository(); + Mockito.when(repository.getReadableDatabase()).thenReturn(Mockito.mock(SQLiteDatabase.class)); + Mockito.doReturn(childDetailsList).when(eventClientRepository).rawQuery(any(SQLiteDatabase.class), anyString()); + ChildMetadata metadata = Mockito.mock(ChildMetadata.class); + RegisterQueryProvider registerQueryProvider = Mockito.mock(RegisterQueryProvider.class); + Mockito.doReturn(metadata).when(childLibrary).metadata(); + Mockito.doReturn(registerQueryProvider).when(metadata).getRegisterQueryProvider(); + Mockito.doReturn("").when(registerQueryProvider).mainRegisterQuery(); + Mockito.doReturn("").when(registerQueryProvider).getDemographicTable(); + + Assert.assertTrue(Utils.childBelongsToCurrentFacility(childDetails)); + } + + @Test + public void testIsChildTemporaryOOCShouldReturnTrueWhenOOCEvent() { + EventClientRepository eventClientRepository = Mockito.mock(EventClientRepository.class); + Repository repository = Mockito.mock(Repository.class); + Mockito.doReturn(eventClientRepository).when(childLibrary).eventClientRepository(); + Mockito.doReturn(repository).when(childLibrary).getRepository(); + Mockito.when(repository.getReadableDatabase()).thenReturn(Mockito.mock(SQLiteDatabase.class)); + Mockito.doReturn(null).when(eventClientRepository).rawQuery(any(SQLiteDatabase.class), anyString()); + ChildMetadata metadata = Mockito.mock(ChildMetadata.class); + RegisterQueryProvider registerQueryProvider = Mockito.mock(RegisterQueryProvider.class); + Mockito.doReturn(metadata).when(childLibrary).metadata(); + Mockito.doReturn(registerQueryProvider).when(metadata).getRegisterQueryProvider(); + Mockito.doReturn("").when(registerQueryProvider).mainRegisterQuery(); + Mockito.doReturn("").when(registerQueryProvider).getDemographicTable(); + Mockito.doReturn(true).when(appProperties).isTrue(eq(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT)); + + Map childDetails = new HashMap<>(); + childDetails.put("base_entity_id", "case_id"); + childDetails.put("is_out_of_catchment", "true"); + + Assert.assertTrue(Utils.isChildTemporaryOOC(childDetails)); + } + + @Test + public void testGetLocationIdFromChildTempOOCEventRetursCorrectLocation() { + EventClientRepository repository = Mockito.mock(EventClientRepository.class); + Mockito.when(childLibrary.eventClientRepository()).thenReturn(repository); + + List eventClientList = new ArrayList<>(); + org.smartregister.domain.Event event = new org.smartregister.domain.Event(); + event.setBaseEntityId("case_id"); + org.smartregister.domain.Obs obs = new org.smartregister.domain.Obs(); + obs.setFormSubmissionField("From_LocationId"); + obs.addToValueList("location_123"); + event.addObs(obs); + EventClient eventClient = new EventClient(event); + eventClientList.add(eventClient); + + Mockito.doReturn(eventClientList).when(repository).fetchEventClientsCore(anyString(), any()); + + String locationId = Utils.getLocationIdFromChildTempOOCEvent("case_id"); + Assert.assertEquals("location_123", locationId); + } } From 0255f2b11c3d770bf21505f564bfbded0b6113bd Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Mon, 15 May 2023 16:42:08 +0500 Subject: [PATCH 5/5] Fix ANR during MoveToCatchment process --- .../fragment/BaseAdvancedSearchFragment.java | 53 +++++++++++++------ .../child/util/MoveToMyCatchmentUtils.java | 1 - 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseAdvancedSearchFragment.java b/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseAdvancedSearchFragment.java index d6f57fc5c..adb3eb307 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseAdvancedSearchFragment.java +++ b/opensrp-child/src/main/java/org/smartregister/child/fragment/BaseAdvancedSearchFragment.java @@ -1,5 +1,6 @@ package org.smartregister.child.fragment; +import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; @@ -7,6 +8,7 @@ import android.content.IntentFilter; import android.database.Cursor; import android.net.ConnectivityManager; +import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.Html; @@ -75,21 +77,6 @@ public abstract class BaseAdvancedSearchFragment extends BaseChildRegisterFragme implements ChildAdvancedSearchContract.View, ChildRegisterFragmentContract.View, View.OnClickListener { public static final String START_DATE = "start_date"; public static final String END_DATE = "end_date"; - private final Listener moveToMyCatchmentListener = moveToCatchmentEvent -> { - if (moveToCatchmentEvent != null) { - if (ChildJsonFormUtils.processMoveToCatchment(context(), moveToCatchmentEvent)) { - clientAdapter.notifyDataSetChanged(); - ((BaseRegisterActivity) requireActivity()).refreshList(FetchStatus.fetched); - ((BaseRegisterActivity) requireActivity()).switchToBaseFragment(); - - Utils.showToast(requireActivity(), requireActivity().getString(R.string.move_to_catchment_success_message)); - } else { - Utils.showShortToast(requireActivity(), requireActivity().getString(R.string.an_error_occured)); - } - } else { - Utils.showShortToast(requireActivity(), requireActivity().getString(R.string.unable_to_move_to_my_catchment)); - } - }; protected AdvancedSearchTextWatcher advancedSearchTextwatcher = new AdvancedSearchTextWatcher(); protected HashMap searchFormData = new HashMap<>(); protected CheckBox active; @@ -117,12 +104,41 @@ public abstract class BaseAdvancedSearchFragment extends BaseChildRegisterFragme private AdvanceSearchDatePickerDialog endDateDatePicker; private final SimpleDateFormat dateFormatter = new SimpleDateFormat(FormUtils.NATIIVE_FORM_DATE_FORMAT_PATTERN, Locale.getDefault().toString().startsWith("ar") ? Locale.ENGLISH : Locale.getDefault()); + @SuppressLint("StaticFieldLeak") + private final Listener moveToMyCatchmentListener = moveToCatchmentEvent -> { + if (moveToCatchmentEvent != null) { + org.smartregister.util.Utils.startAsyncTask(new AsyncTask() { + + @Override + protected Boolean doInBackground(MoveToCatchmentEvent... moveToCatchmentEvents) { + return ChildJsonFormUtils.processMoveToCatchment(context(), moveToCatchmentEvents[0]); + } + + @Override + protected void onPostExecute(Boolean result) { + super.onPostExecute(result); + hideProgressDialog(); + if (result) { + clientAdapter.notifyDataSetChanged(); + ((BaseRegisterActivity) requireActivity()).refreshList(FetchStatus.fetched); + ((BaseRegisterActivity) requireActivity()).switchToBaseFragment(); + + Utils.showToast(requireActivity(), requireActivity().getString(R.string.move_to_catchment_success_message)); + } else { + Utils.showShortToast(requireActivity(), requireActivity().getString(R.string.an_error_occured)); + } + } + }, new MoveToCatchmentEvent[]{moveToCatchmentEvent}); + } else { + hideProgressDialog(); + Utils.showShortToast(requireActivity(), requireActivity().getString(R.string.unable_to_move_to_my_catchment)); + } + }; @Override protected void initializePresenter() { presenter = getPresenter(); initProgressDialog(); - } private void initProgressDialog() { @@ -130,6 +146,11 @@ private void initProgressDialog() { progressDialog.setCancelable(false); } + private void hideProgressDialog() { + if (progressDialog != null && progressDialog.isShowing()) + progressDialog.dismiss(); + } + protected abstract BaseChildAdvancedSearchPresenter getPresenter(); @Override diff --git a/opensrp-child/src/main/java/org/smartregister/child/util/MoveToMyCatchmentUtils.java b/opensrp-child/src/main/java/org/smartregister/child/util/MoveToMyCatchmentUtils.java index 45c0d68d5..3d5b4c137 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/util/MoveToMyCatchmentUtils.java +++ b/opensrp-child/src/main/java/org/smartregister/child/util/MoveToMyCatchmentUtils.java @@ -54,7 +54,6 @@ protected MoveToCatchmentEvent doInBackground(Void... params) { @Override protected void onPostExecute(MoveToCatchmentEvent result) { listener.onEvent(result); - progressDialog.dismiss(); } }, null); }