From 008adfc458ff263d5dabe0b7644d52aec23d98ae Mon Sep 17 00:00:00 2001 From: owais Date: Fri, 24 Jun 2022 13:03:52 +0500 Subject: [PATCH 1/4] improve sync behaviour --- .../chw/application/ChwApplication.java | 3 +++ .../chw/service/SchedulesIntentService.java | 10 ++++++++++ .../chw/sync/ChwClientProcessor.java | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/application/ChwApplication.java b/opensrp-chw/src/main/java/org/smartregister/chw/application/ChwApplication.java index 78a7a54153..73c203c49b 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/application/ChwApplication.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/application/ChwApplication.java @@ -357,6 +357,9 @@ public boolean hasReferrals() { public void onVisitEvent(Visit visit) { if (visit != null) { Timber.v("Visit Submitted re processing Schedule for event ' %s ' : %s", visit.getVisitType(), visit.getBaseEntityId()); + if (CoreLibrary.getInstance().isPeerToPeerProcessing() || SyncStatusBroadcastReceiver.getInstance().isSyncing()) + return; + ChwScheduleTaskExecutor.getInstance().execute(visit.getBaseEntityId(), visit.getVisitType(), visit.getDate()); ChildAlertService.updateAlerts(visit.getBaseEntityId()); diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/service/SchedulesIntentService.java b/opensrp-chw/src/main/java/org/smartregister/chw/service/SchedulesIntentService.java index 5c89fdb2d4..ff6d84eef5 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/service/SchedulesIntentService.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/service/SchedulesIntentService.java @@ -5,11 +5,13 @@ import androidx.annotation.Nullable; +import org.smartregister.CoreLibrary; import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.utils.CoreConstants; import org.smartregister.chw.dao.ScheduleDao; import org.smartregister.chw.fp.util.FamilyPlanningConstants; import org.smartregister.chw.schedulers.ChwScheduleTaskExecutor; +import org.smartregister.receiver.SyncStatusBroadcastReceiver; import java.util.Date; import java.util.List; @@ -30,8 +32,16 @@ public SchedulesIntentService() { super("SchedulesIntentService"); } + public boolean isSyncing() { + return CoreLibrary.getInstance().isPeerToPeerProcessing() || SyncStatusBroadcastReceiver.getInstance().isSyncing(); + } + @Override protected void onHandleIntent(@Nullable Intent intent) { + + if (isSyncing()) + return; + // execute all children schedules executeChildVisitSchedules(); diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/sync/ChwClientProcessor.java b/opensrp-chw/src/main/java/org/smartregister/chw/sync/ChwClientProcessor.java index 936f05255c..fd0fdc39ec 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/sync/ChwClientProcessor.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/sync/ChwClientProcessor.java @@ -3,6 +3,7 @@ import android.content.Context; +import org.smartregister.CoreLibrary; import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.sync.CoreClientProcessor; import org.smartregister.chw.core.utils.CoreConstants; @@ -12,6 +13,7 @@ import org.smartregister.domain.db.EventClient; import org.smartregister.domain.jsonmapping.ClientClassification; import org.smartregister.domain.jsonmapping.Table; +import org.smartregister.receiver.SyncStatusBroadcastReceiver; import org.smartregister.sync.ClientProcessorForJava; public class ChwClientProcessor extends CoreClientProcessor { @@ -38,7 +40,9 @@ public void processEvents(ClientClassification clientClassification, Table vacci case CoreConstants.EventType.REMOVE_MEMBER: ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID); case CoreConstants.EventType.REMOVE_CHILD: - ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID); + if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) { + ChwApplication.getInstance().getScheduleRepository().deleteSchedulesByEntityID(baseEntityID); + } break; default: break; @@ -53,11 +57,16 @@ public void processEvents(ClientClassification clientClassification, Table vacci case CoreConstants.EventType.CHILD_VISIT_NOT_DONE: case CoreConstants.EventType.CHILD_REGISTRATION: case CoreConstants.EventType.UPDATE_CHILD_REGISTRATION: - ChildAlertService.updateAlerts(baseEntityID); + if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) { + ChildAlertService.updateAlerts(baseEntityID); + } default: break; } } - ChwScheduleTaskExecutor.getInstance().execute(event.getBaseEntityId(), event.getEventType(), event.getEventDate().toDate()); + + if (!CoreLibrary.getInstance().isPeerToPeerProcessing() && !SyncStatusBroadcastReceiver.getInstance().isSyncing()) { + ChwScheduleTaskExecutor.getInstance().execute(event.getBaseEntityId(), event.getEventType(), event.getEventDate().toDate()); + } } } From b0377c7fcd459ecf64cc2dfcffab087133ee1b7a Mon Sep 17 00:00:00 2001 From: owais Date: Fri, 24 Jun 2022 14:15:28 +0500 Subject: [PATCH 2/4] added test case --- .../application/CoreChwApplicationTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/application/CoreChwApplicationTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/application/CoreChwApplicationTest.java index 665410ad5c..63d3a1e3e3 100644 --- a/opensrp-chw/src/test/java/org/smartregister/chw/application/CoreChwApplicationTest.java +++ b/opensrp-chw/src/test/java/org/smartregister/chw/application/CoreChwApplicationTest.java @@ -1,13 +1,20 @@ package org.smartregister.chw.application; +import static org.mockito.ArgumentMatchers.eq; + import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import org.robolectric.util.ReflectionHelpers; import org.smartregister.chw.BaseUnitTest; +import org.smartregister.chw.anc.domain.Visit; +import org.smartregister.chw.core.utils.CoreConstants; +import org.smartregister.chw.schedulers.ChwScheduleTaskExecutor; import org.smartregister.immunization.ImmunizationLibrary; import org.smartregister.reporting.ReportingLibrary; +import java.util.Date; + public class CoreChwApplicationTest extends BaseUnitTest { @Test @@ -29,4 +36,20 @@ public void testP2PClassifierIsInitialized() { ReflectionHelpers.setField(application, "flavor", flv); Assert.assertNotNull(application.getP2PClassifier()); } + + @Test + public void testOnVisitEventShouldExecuteChwScheduleTaskExecutor() { + Visit visit = new Visit(); + visit.setBaseEntityId("12345-abcde"); + visit.setVisitType(CoreConstants.VisitType.DONE.name()); + visit.setDate(new Date()); + + ChwScheduleTaskExecutor chwScheduleTaskExecutor = Mockito.mock(ChwScheduleTaskExecutor.class); + ReflectionHelpers.setField(ChwScheduleTaskExecutor.getInstance(), "scheduleTaskExecutor", chwScheduleTaskExecutor); + + ChwApplication chwApplication = new ChwApplication(); + chwApplication.onVisitEvent(visit); + + Mockito.verify(chwScheduleTaskExecutor, Mockito.times(1)).execute(eq(visit.getBaseEntityId()), eq(visit.getVisitType()), eq(visit.getDate())); + } } From 855e20994f7f00f613a877084347b4456f0c1137 Mon Sep 17 00:00:00 2001 From: owais Date: Mon, 27 Jun 2022 18:06:25 +0500 Subject: [PATCH 3/4] version updated --- opensrp-chw/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle index 60a7597401..09a8c650d4 100644 --- a/opensrp-chw/build.gradle +++ b/opensrp-chw/build.gradle @@ -51,8 +51,8 @@ android { applicationId "org.smartregister.chw" minSdkVersion androidMinSdkVersion targetSdkVersion androidTargetSdkVersion - versionCode 1 - versionName "0.2.0" + versionCode 2 + versionName "0.2.1" multiDexEnabled true buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l" buildConfigField "boolean", "TIME_CHECK", "false" From a3c1a75788d39020aaea308c6f83fe7a180e06fc Mon Sep 17 00:00:00 2001 From: owais Date: Mon, 27 Jun 2022 19:01:22 +0500 Subject: [PATCH 4/4] trigger build