From 18df3975c320633692f545ed5616c96f3494cefd Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 20 Nov 2020 12:43:21 +0300 Subject: [PATCH] Client process when all family members are archived --- opensrp-reveal/build.gradle | 2 +- .../reveal/interactor/ListTaskInteractor.java | 2 +- .../RevealFamilyOtherMemberInteractor.java | 4 ++-- .../RevealFamilyProfileInteractor.java | 11 +++++----- .../interactor/StructureTasksInteractor.java | 2 +- .../TaskRegisterFragmentInteractor.java | 2 +- .../receiver/TaskGenerationReceiver.java | 2 +- .../reveal/util/InteractorUtils.java | 21 +++++++------------ ...RevealFamilyOtherMemberInteractorTest.java | 2 +- .../reveal/util/InteractorUtilsTest.java | 2 +- 10 files changed, 22 insertions(+), 28 deletions(-) diff --git a/opensrp-reveal/build.gradle b/opensrp-reveal/build.gradle index 1474fbb033..7ac581933e 100644 --- a/opensrp-reveal/build.gradle +++ b/opensrp-reveal/build.gradle @@ -193,7 +193,7 @@ dependencies { exclude group: 'stax', module: 'stax-api' } - implementation('org.smartregister:opensrp-client-core:3.3.0-SNAPSHOT@aar') { + implementation('org.smartregister:opensrp-client-core:3.3.1-SNAPSHOT@aar') { transitive = true exclude group: 'com.google.guava', module: 'guava' exclude group: 'com.android.support', module: 'appcompat-v7' diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/ListTaskInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/ListTaskInteractor.java index 83e11cbaf8..c24c9e490f 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/ListTaskInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/ListTaskInteractor.java @@ -114,7 +114,7 @@ public ListTaskInteractor(ListTaskContract.Presenter presenter) { commonRepository = RevealApplication.getInstance().getContext().commonrepository(SPRAYED_STRUCTURES); structureRepository = RevealApplication.getInstance().getContext().getStructureRepository(); taskRepository = RevealApplication.getInstance().getTaskRepository(); - interactorUtils = new InteractorUtils(taskRepository, eventClientRepository, clientProcessor); + interactorUtils = new InteractorUtils(taskRepository, eventClientRepository); revealApplication = RevealApplication.getInstance(); } diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractor.java index 3ae4670683..56dfee5875 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractor.java @@ -24,7 +24,7 @@ public class RevealFamilyOtherMemberInteractor extends FamilyOtherMemberProfileI public RevealFamilyOtherMemberInteractor() { commonRepository = getInstance().getContext().commonrepository(getInstance().getMetadata().familyMemberRegister.tableName); appExecutors = getInstance().getAppExecutors(); - interactorUtils = new InteractorUtils(getInstance().getTaskRepository(), getInstance().getContext().getEventClientRepository(), (RevealClientProcessor) getInstance().getClientProcessor()); + interactorUtils = new InteractorUtils(getInstance().getTaskRepository(), getInstance().getContext().getEventClientRepository()); } @Override @@ -43,7 +43,7 @@ public void archiveFamilyMember(FamilyOtherMemberProfileContract.BasePresenter p getInstance().getRepository().getWritableDatabase().beginTransaction(); boolean saved; try { - saved = interactorUtils.archiveClient(client.getCaseId(), false); + saved = interactorUtils.archiveClient(client.getCaseId(), false) != null; getInstance().getRepository().getWritableDatabase().setTransactionSuccessful(); } finally { getInstance().getRepository().getWritableDatabase().endTransaction(); diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyProfileInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyProfileInteractor.java index 2212e088d8..cc42e15f42 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyProfileInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/RevealFamilyProfileInteractor.java @@ -69,7 +69,7 @@ public RevealFamilyProfileInteractor(FamilyProfileContract.Presenter presenter) FamilyMetadata familyMetadata = RevealApplication.getInstance().getMetadata(); clientProcessor = (RevealClientProcessor) RevealApplication.getInstance().getClientProcessor(); commonRepository = RevealApplication.getInstance().getContext().commonrepository(familyMetadata.familyMemberRegister.tableName); - interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository, clientProcessor); + interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository); taskRepository = RevealApplication.getInstance().getTaskRepository(); } @@ -127,7 +127,6 @@ public void archiveFamily(String familyBaseEntityId, String structureId) { try { db.beginTransaction(); IntentFilter filter = new IntentFilter(TASK_GENERATED_EVENT); - TaskGenerationReceiver taskGenerationReceiver = new TaskGenerationReceiver(task -> { appExecutors.mainThread().execute(() -> presenter.onArchiveFamilyCompleted(task != null, task)); }); @@ -135,15 +134,17 @@ public void archiveFamily(String familyBaseEntityId, String structureId) { List familyMembers = commonRepository.findSearchIds(String.format( "SELECT %s FROM %s where %s='%s' AND %s IS NULL", BASE_ENTITY_ID, FAMILY_MEMBER, KEY.RELATIONAL_ID, familyBaseEntityId, DATE_REMOVED)); - interactorUtils.archiveClient(familyBaseEntityId, true); + List eventClients = new ArrayList<>(); + eventClients.add(interactorUtils.archiveClient(familyBaseEntityId, true)); for (String baseEntityId : familyMembers) { - interactorUtils.archiveClient(baseEntityId, false); + eventClients.add(interactorUtils.archiveClient(baseEntityId, false)); } taskRepository.cancelTasksForEntity(structureId); taskRepository.archiveTasksForEntity(structureId); - db.execSQL(String.format("update %s set name = null where %s = ? ", STRUCTURES_TABLE, ID_), new String[]{structureId}); db.execSQL(String.format("update %s set %s = null where %s = ? ", SPRAYED_STRUCTURES, STRUCTURE_NAME, ID), new String[]{structureId}); + + clientProcessor.processClient(eventClients, true); db.setTransactionSuccessful(); } catch (Exception e) { Timber.e(e); diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/StructureTasksInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/StructureTasksInteractor.java index 0fcf094fb9..15b8142f9b 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/StructureTasksInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/StructureTasksInteractor.java @@ -79,7 +79,7 @@ protected StructureTasksInteractor(StructureTasksContract.Presenter presenter, A this.appExecutors = appExecutors; this.database = database; this.structureRepository = structureRepository; - this.interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository, clientProcessor); + this.interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository); } @Override diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/TaskRegisterFragmentInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/TaskRegisterFragmentInteractor.java index 2a48a5acf1..9a85e69821 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/TaskRegisterFragmentInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/TaskRegisterFragmentInteractor.java @@ -99,7 +99,7 @@ public TaskRegisterFragmentInteractor(TaskRegisterFragmentContract.Presenter pre super(presenter); this.locationBuffer = locationBuffer; locationRepository = RevealApplication.getInstance().getLocationRepository(); - interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository, clientProcessor); + interactorUtils = new InteractorUtils(RevealApplication.getInstance().getTaskRepository(), eventClientRepository); } private String mainSelect(String mainCondition) { diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/receiver/TaskGenerationReceiver.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/receiver/TaskGenerationReceiver.java index 083f7ed9b0..00f8ef2afa 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/receiver/TaskGenerationReceiver.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/receiver/TaskGenerationReceiver.java @@ -17,7 +17,7 @@ */ public class TaskGenerationReceiver extends BroadcastReceiver { - private TaskGenerationCallBack taskGenerationCallBack; + private final TaskGenerationCallBack taskGenerationCallBack; public TaskGenerationReceiver(TaskGenerationCallBack taskGenerationCallBack) { this.taskGenerationCallBack = taskGenerationCallBack; diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/InteractorUtils.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/InteractorUtils.java index d9371088bc..fcc202b7fa 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/InteractorUtils.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/InteractorUtils.java @@ -23,7 +23,6 @@ import org.smartregister.reveal.util.FamilyConstants.EventType; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import timber.log.Timber; @@ -48,14 +47,11 @@ public class InteractorUtils { private EventClientRepository eventClientRepository; - private RevealClientProcessor clientProcessor; - private RevealJsonFormUtils jsonFormUtils; - public InteractorUtils(TaskRepository taskRepository, EventClientRepository eventClientRepository, RevealClientProcessor clientProcessor) { + public InteractorUtils(TaskRepository taskRepository, EventClientRepository eventClientRepository) { this.taskRepository = taskRepository; this.eventClientRepository = eventClientRepository; - this.clientProcessor = clientProcessor; jsonFormUtils = new RevealJsonFormUtils(); } @@ -85,7 +81,7 @@ public CommonPersonObject fetchSprayDetails(String interventionType, String stru } - public boolean archiveClient(String baseEntityId, boolean isFamily) { + public EventClient archiveClient(String baseEntityId, boolean isFamily) { taskRepository.cancelTasksForEntity(baseEntityId); taskRepository.archiveTasksForEntity(baseEntityId); JSONObject eventsByBaseEntityId = eventClientRepository.getEventsByBaseEntityId(baseEntityId); @@ -104,7 +100,6 @@ public boolean archiveClient(String baseEntityId, boolean isFamily) { } } - boolean saved; try { eventClientRepository.batchInsertEvents(events, 0); clientJsonObject.put("dateVoided", now); @@ -120,16 +115,14 @@ public boolean archiveClient(String baseEntityId, boolean isFamily) { eventJson.put(EventClientRepository.event_column.syncStatus.name(), BaseRepository.TYPE_Unsynced); eventClientRepository.addEvent(baseEntityId, eventJson); - clientProcessor.processClient(Collections.singletonList(new EventClient( + return new EventClient( gson.fromJson(eventJson.toString(), org.smartregister.domain.Event.class), - gson.fromJson(clientJsonObject.toString(), Client.class))), true); - saved = true; + gson.fromJson(clientJsonObject.toString(), Client.class)); } catch (JSONException e) { Timber.e(e); - saved = false; } - return saved; + return null; } public boolean archiveEventsForTask(SQLiteDatabase db, BaseTaskDetails taskDetails) { @@ -181,11 +174,11 @@ public List getFormSubmissionIdsFromEventTask(SQLiteDatabase db, BaseTas try { if (CASE_CONFIRMATION.equals(taskDetails.getTaskCode()) || BCC.equals(taskDetails.getTaskCode())) { - String eventTypeField= null; + String eventTypeField = null; if (CASE_CONFIRMATION.equals(taskDetails.getTaskCode())) { eventTypeField = CASE_CONFIRMATION_FIELD; - } else if(BCC.equals(taskDetails.getTaskCode())) { + } else if (BCC.equals(taskDetails.getTaskCode())) { eventTypeField = BEHAVIOUR_CHANGE_COMMUNICATION; } diff --git a/opensrp-reveal/src/test/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractorTest.java b/opensrp-reveal/src/test/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractorTest.java index c067a65b70..893e8387c4 100644 --- a/opensrp-reveal/src/test/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractorTest.java +++ b/opensrp-reveal/src/test/java/org/smartregister/reveal/interactor/RevealFamilyOtherMemberInteractorTest.java @@ -88,7 +88,7 @@ public void setUp() { Context.bindtypes = new ArrayList<>(); interactor = new RevealFamilyOtherMemberInteractor(); Whitebox.setInternalState(interactor, "commonRepository", commonRepository); - Whitebox.setInternalState(interactor, "interactorUtils", new InteractorUtils(taskRepository, eventClientRepository, clientProcessor)); + Whitebox.setInternalState(interactor, "interactorUtils", new InteractorUtils(taskRepository, eventClientRepository)); } @Test diff --git a/opensrp-reveal/src/test/java/org/smartregister/reveal/util/InteractorUtilsTest.java b/opensrp-reveal/src/test/java/org/smartregister/reveal/util/InteractorUtilsTest.java index 5941997694..e668810ca5 100644 --- a/opensrp-reveal/src/test/java/org/smartregister/reveal/util/InteractorUtilsTest.java +++ b/opensrp-reveal/src/test/java/org/smartregister/reveal/util/InteractorUtilsTest.java @@ -96,7 +96,7 @@ public class InteractorUtilsTest extends BaseUnitTest { @Before public void setUp() { - interactorUtils = new InteractorUtils(taskRepository, eventClientRepository, clientProcessor); + interactorUtils = new InteractorUtils(taskRepository, eventClientRepository); Whitebox.setInternalState(interactorUtils, "jsonFormUtils", jsonFormUtils); formSubmissionId = UUID.randomUUID().toString(); baseEntityId = UUID.randomUUID().toString();