Skip to content

Commit

Permalink
Client process when all family members are archived
Browse files Browse the repository at this point in the history
  • Loading branch information
githengi committed Nov 20, 2020
1 parent 3ec5c48 commit 18df397
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion opensrp-reveal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -127,23 +127,24 @@ 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));
});
LocalBroadcastManager.getInstance(RevealApplication.getInstance().getApplicationContext()).registerReceiver(taskGenerationReceiver, filter);
List<String> 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<EventClient> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class TaskGenerationReceiver extends BroadcastReceiver {

private TaskGenerationCallBack taskGenerationCallBack;
private final TaskGenerationCallBack taskGenerationCallBack;

public TaskGenerationReceiver(TaskGenerationCallBack taskGenerationCallBack) {
this.taskGenerationCallBack = taskGenerationCallBack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand All @@ -104,7 +100,6 @@ public boolean archiveClient(String baseEntityId, boolean isFamily) {
}
}

boolean saved;
try {
eventClientRepository.batchInsertEvents(events, 0);
clientJsonObject.put("dateVoided", now);
Expand All @@ -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) {
Expand Down Expand Up @@ -181,11 +174,11 @@ public List<String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 18df397

Please sign in to comment.