Skip to content

Commit

Permalink
Merge pull request #478 from d-tree-org/new-registration-edi-capture
Browse files Browse the repository at this point in the history
New registration edi capture
  • Loading branch information
issyzac authored Oct 26, 2023
2 parents 34b6e48 + fc527df commit 88f7ec0
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
{
"key": "edi_id",
"openmrs_entity_parent": "",
"openmrs_entity": "person_identifier",
"openmrs_entity": "concept",
"openmrs_entity_id": "edi_id",
"type": "edit_text",
"hint": "{{add_anc_registration.step1.ediid.hint}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
{
"key": "edi_id",
"openmrs_entity_parent": "",
"openmrs_entity": "person_identifier",
"openmrs_entity": "concept",
"openmrs_entity_id": "edi_id",
"type": "edit_text",
"hint": "{{family_member_register.step1.ediid.hint}}",
Expand Down
2 changes: 1 addition & 1 deletion opensrp-chw/src/kk/assets/json.form/family_register.json
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@
{
"key": "edi_id",
"openmrs_entity_parent": "",
"openmrs_entity": "person_identifier",
"openmrs_entity": "concept",
"openmrs_entity_id": "edi_id",
"type": "edit_text",
"hint": "{{family_register.step1.ediid.hint}}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.smartregister.chw.util;

import android.view.View;

import org.smartregister.util.AppHealthUtils;

/**
* Author issyzac on 27/09/2023
*/
public class KKAppHealthUtils extends AppHealthUtils {

public KKAppHealthUtils(View view) {
super(view);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.smartregister.chw.domain;

/**
* Author issyzac on 27/09/2023
*/
public enum CleanEDIStatus {

CLEANED ("CLEANED"),
PENDING ("PENDING");

private String value;

private CleanEDIStatus(String value){
this.value = value;
}

public String value(){
return this.value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

import androidx.annotation.VisibleForTesting;

import org.json.JSONArray;
import org.json.JSONObject;
import org.smartregister.chw.core.interactor.CoreFamilyProfileInteractor;
import org.smartregister.clientandeventmodel.Client;
import org.smartregister.clientandeventmodel.Event;
import org.smartregister.domain.db.EventClient;
import org.smartregister.family.contract.FamilyProfileContract;
import org.smartregister.family.domain.FamilyEventClient;
import org.smartregister.family.util.AppExecutors;
import org.smartregister.family.util.JsonFormUtils;
import org.smartregister.family.util.Utils;
import org.smartregister.view.activity.DrishtiApplication;

import java.util.Collections;
import java.util.Date;

import timber.log.Timber;

public class FamilyProfileInteractor extends CoreFamilyProfileInteractor {

Expand All @@ -16,4 +31,89 @@ public FamilyProfileInteractor() {
super();
}

@Override
public void saveRegistration(FamilyEventClient familyEventClient, String jsonString, boolean isEditMode, FamilyProfileContract.InteractorCallBack callBack) {
Runnable runnable = new Runnable() {
public void run() {
final boolean isSaved = FamilyProfileInteractor.this.saveRegistration(familyEventClient, jsonString, isEditMode);
FamilyProfileInteractor.this.appExecutors.mainThread().execute(new Runnable() {
public void run() {
callBack.onRegistrationSaved(isEditMode, isSaved, familyEventClient);
}
});
}
};
this.appExecutors.diskIO().execute(runnable);
}

private boolean saveRegistration(FamilyEventClient familyEventClient, String jsonString, boolean isEditMode) {
try {
Client baseClient = familyEventClient.getClient();
Event baseEvent = familyEventClient.getEvent();
JSONObject eventJson = null;
JSONObject clientJson = null;

if (baseClient != null) {
clientJson = new JSONObject(JsonFormUtils.gson.toJson(baseClient));
if (isEditMode) {
JsonFormUtils.mergeAndSaveClient(this.getSyncHelper(), baseClient);
} else {
this.getSyncHelper().addClient(baseClient.getBaseEntityId(), clientJson);
}
}

if (baseEvent != null) {
eventJson = new JSONObject(JsonFormUtils.gson.toJson(baseEvent));
this.getSyncHelper().addEvent(baseEvent.getBaseEntityId(), eventJson);
}

String newOpenSRPId;
if (isEditMode) {
if (baseClient != null) {
newOpenSRPId = baseClient.getIdentifier(Utils.metadata().uniqueIdentifierKey);
if (newOpenSRPId != null) {
newOpenSRPId.replace("-", "");
String currentOpenSRPId = JsonFormUtils.getString(jsonString, "current_opensrp_id").replace("-", "");
if (!newOpenSRPId.equals(currentOpenSRPId)) {
this.getUniqueIdRepository().open(currentOpenSRPId);
}
}
}
} else if (baseClient != null) {
newOpenSRPId = baseClient.getIdentifier(Utils.metadata().uniqueIdentifierKey);
this.getUniqueIdRepository().close(newOpenSRPId);

//Note: EDI ID registration event is only created during first registration
//of the client. During editing we will not create another registration event

//EDI ID registration fields
JSONObject form = new JSONObject(jsonString);
JSONArray fields = JsonFormUtils.fields(form);

String baseEntityId = baseClient.getBaseEntityId();
String ediId = JsonFormUtils.getFieldValue(fields, "edi_id");

//Process EDI ID registration event
org.smartregister.chw.util.JsonFormUtils.processEdiRegistrationEvent(baseEntityId, ediId, clientJson,
DrishtiApplication.getInstance().getContext().allSharedPreferences(), isEditMode);

}

if (baseClient != null || baseEvent != null) {
newOpenSRPId = JsonFormUtils.getFieldValue(jsonString, "photo");
JsonFormUtils.saveImage(baseEvent.getProviderId(), baseClient.getBaseEntityId(), newOpenSRPId);
}

long lastSyncTimeStamp = this.getAllSharedPreferences().fetchLastUpdatedAtDate(0L);
Date lastSyncDate = new Date(lastSyncTimeStamp);
org.smartregister.domain.Event domainEvent = (org.smartregister.domain.Event)JsonFormUtils.gson.fromJson(eventJson.toString(), org.smartregister.domain.Event.class);
org.smartregister.domain.Client domainClient = (org.smartregister.domain.Client)JsonFormUtils.gson.fromJson(clientJson.toString(), org.smartregister.domain.Client.class);
this.processClient(Collections.singletonList(new EventClient(domainEvent, domainClient)));
this.getAllSharedPreferences().saveLastUpdatedAtDate(lastSyncDate.getTime());
return true;
} catch (Exception var13) {
Timber.e(var13);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.smartregister.family.domain.FamilyEventClient;
import org.smartregister.family.util.JsonFormUtils;
import org.smartregister.family.util.Utils;
import org.smartregister.view.activity.DrishtiApplication;

import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -108,6 +109,20 @@ private boolean saveRegistration(List<FamilyEventClient> familyEventClientList,
}
}

if (baseClient != null && !baseClient.getClientType().equalsIgnoreCase("Family")){

//EDI ID registration fields
JSONObject form = new JSONObject(jsonString);
JSONArray fields = JsonFormUtils.fields(form);

String baseEntityId = baseClient.getBaseEntityId();
String ediId = JsonFormUtils.getFieldValue(fields, "edi_id");

//Process EDI ID registration event
org.smartregister.chw.util.JsonFormUtils.processEdiRegistrationEvent(baseEntityId, ediId, clientJson,
DrishtiApplication.getInstance().getContext().allSharedPreferences(), isEditMode);
}

if (baseClient != null || baseEvent != null) {
imageLocation = null;
if (i == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package org.smartregister.chw.interactor;

import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import org.smartregister.CoreLibrary;
import org.smartregister.chw.contract.LoginJobScheduler;
import org.smartregister.chw.job.IdentifierCleanerWorker;
import org.smartregister.job.DuplicateCleanerWorker;
import org.smartregister.login.interactor.BaseLoginInteractor;
import org.smartregister.view.contract.BaseLoginContract;

import java.util.concurrent.TimeUnit;


/***
* @author rkodev
Expand All @@ -23,11 +32,22 @@ public LoginInteractor(BaseLoginContract.Presenter loginPresenter) {
@Override
protected void scheduleJobsPeriodically() {
scheduler.scheduleJobsPeriodically();
//Schedule your jobs here

}

@Override
protected void scheduleJobsImmediately() {
super.scheduleJobsImmediately();
scheduler.scheduleJobsImmediately();
scheduleIdCleanerWorker();

}

private void scheduleIdCleanerWorker(){
WorkRequest identifierCleaner = new PeriodicWorkRequest.Builder(IdentifierCleanerWorker.class, 1, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(CoreLibrary.getInstance().context().applicationContext()).enqueue(identifierCleaner);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void scheduleJobsPeriodically() {

if (BuildConfig.USE_UNIFIED_REFERRAL_APPROACH)
DocumentConfigurationServiceJob.scheduleJob(DocumentConfigurationServiceJob.TAG,TimeUnit.MINUTES.toMinutes(BuildConfig.DATA_SYNC_DURATION_MINUTES), getFlexValue(BuildConfig.DATA_SYNC_DURATION_MINUTES));



}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.smartregister.chw.job;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import org.smartregister.CoreLibrary;
import org.smartregister.chw.domain.CleanEDIStatus;
import org.smartregister.chw.repository.KKEventClientRepository;

import timber.log.Timber;

/**
* Author issyzac on 27/09/2023
*/
public class IdentifierCleanerWorker extends Worker {

private Context context;
public static final String TAG = "IdentifierCleanerWorker";
KKEventClientRepository repository;

public IdentifierCleanerWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
this.context = context;
repository = new KKEventClientRepository();
}

@NonNull
@Override
public Result doWork() {
CleanEDIStatus cleanEDIStatus = null;
try {
cleanEDIStatus = DoClean();
} catch (Exception e) {
e.printStackTrace();
}
Timber.i("Doing some cleaning work");

// Remove worker if everything has been cleaned
if (cleanEDIStatus != null && cleanEDIStatus.equals(CleanEDIStatus.CLEANED))
WorkManager.getInstance(context).cancelWorkById(this.getId());

return Result.success();
}

private CleanEDIStatus DoClean() throws Exception {
// Call repository to clean the IDs
return repository.removeEdiIdsFromClients();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.smartregister.chw.repository;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.util.Log;

import org.smartregister.repository.BaseRepository;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
* Created by Kassim Sheghembe on 2023-09-27
*/
public class EdiIdCleanUpRepository extends BaseRepository {

private static final String BASE_ENTITY_ID = "baseEntityId";

public static final String CLIENTS_WITH_EDI_IDS_SQL = "WITH clients AS (" +
"SELECT " +
"baseEntityId, " +
"COALESCE(json_extract(json, '$.identifiers.edi_id'), json_extract(json, '$.identifiers.mother_edi_id')) as edi_id " +
"FROM client " +
")" +
"select * from clients where edi_id is not null";

public Map<String, String> clientsWithEdiIds() {

Map<String, String> ediIds = new HashMap<>();

Cursor cursor = null;

try {
cursor = getReadableDatabase().rawQuery(CLIENTS_WITH_EDI_IDS_SQL, new String[]{});

while (cursor.moveToNext()) {
@SuppressLint("Range") String baseEntityId = cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID));
@SuppressLint("Range") String ediId = cursor.getString(cursor.getColumnIndex("edi_id"));

ediIds.put(baseEntityId, ediId);
}

} catch (Exception e) {
Log.e("ERRRRORRRRR", e.getMessage());
Timber.e(e);
}
finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
}

return ediIds;
}
}
Loading

0 comments on commit 88f7ec0

Please sign in to comment.