Skip to content

Commit

Permalink
Update event tagging with extra details
Browse files Browse the repository at this point in the history
- Update event tagging to include App Version Name & Data Strategy
- Unit tests
- Migrate client core and native form libraries
  • Loading branch information
ndegwamartin committed Jul 19, 2022
1 parent 5ccc0f2 commit 7e75a19
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=5.0.0-BETA-SNAPSHOT
VERSION_NAME=5.0.0-BETA2-SNAPSHOT
VERSION_CODE=3
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
8 changes: 4 additions & 4 deletions opensrp-immunization/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ android {

dependencies {

testImplementation 'junit:junit:4.12'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
testImplementation 'junit:junit:4.13.2'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.6'

implementation('org.smartregister:opensrp-client-core:6.0.0-ALPHA-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-core:6.0.0-BETA2-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.github.bmelnychuk', module: 'atv'
exclude group: 'jakarta.annotation', module: 'jakarta.annotation-api'
exclude group: 'com.ibm.fhir', module: 'fhir-model'
exclude group: 'com.ibm.fhir', module: 'fhir-path'
}

implementation('org.smartregister:opensrp-client-native-form:3.0.0-ALPHA-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-native-form:3.0.0-BETA-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'recyclerview-v7'
exclude group: 'com.google.code.gson', module: 'gson'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
*/
public class ImmunizationLibrary {

public static List<String> COMBINED_VACCINES = new ArrayList<>();
public static Map<String, String> COMBINED_VACCINES_MAP = new HashMap<>();
private static ImmunizationLibrary instance;
private static boolean allowExpiredVaccineEntry;
private static Map<String, VaccineCache> vaccineCacheMap = new HashMap<>();
private final Repository repository;
private final Context context;
private final Map<String, String> conditionalVaccinesMap = new HashMap<>();
private EventClientRepository eventClientRepository;
private VaccineRepository vaccineRepository;
private RecurringServiceRecordRepository recurringServiceRecordRepository;
Expand All @@ -45,33 +50,29 @@ public class ImmunizationLibrary {
private CommonFtsObject commonFtsObject;
private int applicationVersion;
private int databaseVersion;
private String applicationVersionName;
private Map<String, Object> jsonMap = new HashMap<>();
private static boolean allowExpiredVaccineEntry;
private static Map<String, VaccineCache> vaccineCacheMap = new HashMap<>();

public static List<String> COMBINED_VACCINES = new ArrayList<>();
public static Map<String, String> COMBINED_VACCINES_MAP = new HashMap<>();
private final Map<String, String> conditionalVaccinesMap = new HashMap<>();
private String currentConditionalVaccine;
private boolean allowSyncImmediately = false;
private List<VaccineRepo.Vaccine> skippableVaccines = new ArrayList<>();
private VaccineOverdueCountRepository vaccineOverdueCountRepository;

private long vaccineSyncTime = -1;

private ImmunizationLibrary(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, int databaseVersion) {
private ImmunizationLibrary(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, String applicationVersionName, int databaseVersion) {
this.repository = repository;
this.context = context;
this.commonFtsObject = commonFtsObject;
this.applicationVersion = applicationVersion;
this.applicationVersionName = applicationVersionName;
this.databaseVersion = databaseVersion;
setCurrentConditionalVaccine(null);
setSkippableVaccines(Arrays.asList(VaccineRepo.Vaccine.bcg2, VaccineRepo.Vaccine.measles1));
}

public static void init(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, int databaseVersion) {
public static void init(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, String applicationVersionName, int databaseVersion) {
if (instance == null) {
instance = new ImmunizationLibrary(context, repository, commonFtsObject, applicationVersion, databaseVersion);
instance = new ImmunizationLibrary(context, repository, commonFtsObject, applicationVersion, applicationVersionName, databaseVersion);

allowExpiredVaccineEntry = instance.getProperties().isTrue(IMConstants.APP_PROPERTIES.VACCINE_EXPIRED_ENTRY_ALLOW);

Expand All @@ -85,6 +86,37 @@ public static void init(Context context, Repository repository, CommonFtsObject
}
}

/**
* This init method is deprecated, use {@link #init(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, String applicationVersionName, int databaseVersion.
*/
@Deprecated
public static void init(Context context, Repository repository, CommonFtsObject commonFtsObject, int applicationVersion, int databaseVersion) {
init(context, repository, commonFtsObject, applicationVersion, null, databaseVersion);
}

public static <T> T assetJsonToJava(Map<String, Object> jsonMap, android.content.Context context, String fileName, Class<T> clazz, Type type) {
return AssetHandler.assetJsonToJava(jsonMap, context, fileName, clazz, type);
}

public static ImmunizationLibrary getInstance() {
if (instance == null) {
throw new IllegalStateException(" Instance does not exist!!! Call " + ImmunizationLibrary.class
.getName() + ".init method in the onCreate method of your Application class ");
}
return instance;
}

public static Map<String, VaccineCache> getVaccineCacheMap() {
return vaccineCacheMap;
}

/**
* Public method to clear the instance/destroy useful for testing
*/
public static void destroy() {
instance = null;
}

public Map<String, String> getConditionalVaccinesMap() {
return conditionalVaccinesMap;
}
Expand All @@ -93,10 +125,6 @@ public <T> T assetJsonToJava(String fileName, Class<T> clazz, Type type) {
return AssetHandler.assetJsonToJava(jsonMap, context.applicationContext(), fileName, clazz, type);
}

public static <T> T assetJsonToJava(Map<String, Object> jsonMap, android.content.Context context, String fileName, Class<T> clazz, Type type) {
return AssetHandler.assetJsonToJava(jsonMap, context, fileName, clazz, type);
}

public EventClientRepository eventClientRepository() {
if (eventClientRepository == null) {
eventClientRepository = new EventClientRepository();
Expand Down Expand Up @@ -163,6 +191,10 @@ public int getApplicationVersion() {
return applicationVersion;
}

public String getApplicationVersionName() {
return applicationVersionName;
}

public int getDatabaseVersion() {
return databaseVersion;
}
Expand All @@ -171,14 +203,6 @@ public Locale getLocale() {
return ImmunizationLibrary.getInstance().context().applicationContext().getResources().getConfiguration().locale;
}

public static ImmunizationLibrary getInstance() {
if (instance == null) {
throw new IllegalStateException(" Instance does not exist!!! Call " + ImmunizationLibrary.class
.getName() + ".init method in the onCreate method of your Application class ");
}
return instance;
}

public Map<String, Object> getVaccinesConfigJsonMap() {
return jsonMap;
}
Expand Down Expand Up @@ -208,10 +232,6 @@ public boolean isAllowExpiredVaccineEntry() {
return allowExpiredVaccineEntry;
}

public static Map<String, VaccineCache> getVaccineCacheMap() {
return vaccineCacheMap;
}

public boolean isExpiredVaccineCardRed() {
return getProperties().isTrue(IMConstants.APP_PROPERTIES.EXPIRED_CARD_AS_RED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ protected void onHandleIntent(Intent intent) {
jsonArray.put(jsonObject);
}

JsonFormUtils
.createServiceEvent(getApplicationContext(), serviceRecord, EVENT_TYPE, ENTITY_TYPE, jsonArray);
JsonFormUtils.createServiceEvent(serviceRecord, EVENT_TYPE, ENTITY_TYPE, jsonArray, ImmunizationLibrary.getInstance().context());
recurringServiceRecordRepository.close(serviceRecord.getId());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ protected void onHandleIntent(Intent intent) {
jsonObject.put(JsonFormUtils.VALUE, vaccine.getCalculation());
jsonArray.put(jsonObject);

JsonFormUtils.createVaccineEvent(getApplicationContext(), vaccine, getEventType(), getEntityType(), jsonArray);
JsonFormUtils.createVaccineEvent(vaccine, getEventType(), getEntityType(), jsonArray, ImmunizationLibrary.getInstance().context());
//log out of catchment service since this is required in some of the hia2 report indicators
if (vaccine.getBaseEntityId() == null || vaccine.getBaseEntityId().isEmpty() || new Integer(1).equals(vaccine.getOutOfCatchment())) {
JsonFormUtils.createVaccineEvent(getApplicationContext(), vaccine, getEventTypeOutOfCatchment(), getEntityType(), jsonArray);
JsonFormUtils.createVaccineEvent(vaccine, getEventTypeOutOfCatchment(), getEntityType(), jsonArray, ImmunizationLibrary.getInstance().context());
}

vaccineRepository.close(vaccine.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.AllConstants;
import org.smartregister.clientandeventmodel.Event;
import org.smartregister.domain.Observation;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.domain.ServiceRecord;
import org.smartregister.immunization.domain.Vaccine;
import org.smartregister.repository.EventClientRepository;

import java.util.Date;

import timber.log.Timber;

/**
* Created by keyman on 31/07/2017.
*/
public class JsonFormUtils extends org.smartregister.util.JsonFormUtils {

public static void createVaccineEvent(Context context, Vaccine vaccine, String eventType, String entityType,
JSONArray fields) {
/**
* This createVaccineEvent method is deprecated, use {@link #createVaccineEvent(Vaccine vaccine, String eventType, String entityType, JSONArray fields, org.smartregister.Context context)} instead which adds application version name.
*/
@Deprecated
public static void createVaccineEvent(Context context, Vaccine vaccine, String eventType, String entityType, JSONArray fields) {

org.smartregister.Context openSRPContext = ImmunizationLibrary.getInstance().context();
createVaccineEvent(vaccine, eventType, entityType, fields, openSRPContext);
}

public static void createVaccineEvent(Vaccine vaccine, String eventType, String entityType, JSONArray fields, org.smartregister.Context context) {
try {
EventClientRepository db = ImmunizationLibrary.getInstance().eventClientRepository();
EventClientRepository db = context.getEventClientRepository();

Event event = (Event) new Event()
.withBaseEntityId(vaccine.getBaseEntityId())
Expand All @@ -40,8 +54,16 @@ public static void createVaccineEvent(Context context, Vaccine vaccine, String e
event.setTeamId(vaccine.getTeamId());
event.setChildLocationId(vaccine.getChildLocationId());
event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, vaccine.getProgramClientId());
event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());

try {
addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
} catch (JSONException jsonException) {
Timber.e(jsonException);
}

event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
event.setClientApplicationVersionName(ImmunizationLibrary.getInstance().getApplicationVersionName());
event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());

if (fields != null && fields.length() != 0)
Expand Down Expand Up @@ -72,10 +94,19 @@ public static void createVaccineEvent(Context context, Vaccine vaccine, String e
}
}

public static void createServiceEvent(Context context, ServiceRecord serviceRecord, String eventType, String entityType,
JSONArray fields) {
/**
* This createServiceEvent method is deprecated, use {@link #createServiceEvent(ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields, org.smartregister.Context context)} instead which adds application version name.
*/
@Deprecated
public static void createServiceEvent(Context context, ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields) {

org.smartregister.Context openSRPContext = ImmunizationLibrary.getInstance().context();
createServiceEvent(serviceRecord, eventType, entityType, fields, openSRPContext);
}

public static void createServiceEvent(ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields, org.smartregister.Context context) {
try {
EventClientRepository db = ImmunizationLibrary.getInstance().eventClientRepository();
EventClientRepository db = context.getEventClientRepository();

Event event = (Event) new Event()
.withBaseEntityId(serviceRecord.getBaseEntityId())
Expand All @@ -94,8 +125,16 @@ public static void createServiceEvent(Context context, ServiceRecord serviceReco
event.setTeamId(serviceRecord.getTeamId());
event.setChildLocationId(serviceRecord.getChildLocationId());
event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, serviceRecord.getProgramClientId());
event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());

try {
addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
} catch (JSONException jsonException) {
Timber.e(jsonException);
}

event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
event.setClientApplicationVersionName(ImmunizationLibrary.getInstance().getApplicationVersionName());
event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());

if (fields != null && fields.length() != 0)
Expand All @@ -107,7 +146,6 @@ public static void createServiceEvent(Context context, ServiceRecord serviceReco
}
}


if (event != null) {

JSONObject eventJson = new JSONObject(JsonFormUtils.gson.toJson(event));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.smartregister.immunization.customshadows;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.smartregister.Context;
import org.smartregister.immunization.domain.ServiceRecord;
import org.smartregister.immunization.domain.Vaccine;
import org.smartregister.immunization.util.JsonFormUtils;
Expand All @@ -23,9 +23,9 @@ public class ShadowJsonFormUtils {
private static HashMap<String, HashMap<Integer, ArrayList<Object>>> methodCalls = new HashMap<>();

@Implementation
public static void createVaccineEvent(Context context, Vaccine vaccine, String eventType, String entityType, JSONArray fields) {
public static void createVaccineEvent( Vaccine vaccine, String eventType, String entityType, JSONArray fields,Context context) {
// TODO: Make this dynamic and reusable for other instances
String methodName = "createVaccineEvent(Context, Vaccine, String, String, JSONArray)";
String methodName = "createVaccineEvent(Vaccine, String, String, JSONArray, Context)";

int count = 0;

Expand All @@ -47,9 +47,9 @@ public static void createVaccineEvent(Context context, Vaccine vaccine, String e
}

@Implementation
public static void createServiceEvent(Context context, ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields) {
public static void createServiceEvent(ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields, Context context) {
// TODO: Make this dynamic and reusable for other instances
String methodName = "createServiceEvent(Context, ServiceRecord, String, String, JSONArray)";
String methodName = "createServiceEvent(ServiceRecord, String, String, JSONArray, Context)";

int count = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onHandleIntentShouldGenerateValidJsonFieldsForEventCreation() {

org.smartregister.Context context = Mockito.mock(org.smartregister.Context.class);
Mockito.doReturn(new AppProperties()).when(context).getAppProperties();
ImmunizationLibrary.init(context, Mockito.mock(Repository.class), Mockito.mock(CommonFtsObject.class), 1, 1);
ImmunizationLibrary.init(context, Mockito.mock(Repository.class), Mockito.mock(CommonFtsObject.class), 1, "1.0.0", 1);

RecurringServiceRecordRepository recurringServiceRecordRepository = Mockito.mock(RecurringServiceRecordRepository.class);
RecurringServiceTypeRepository recurringServiceTypeRepository = Mockito.mock(RecurringServiceTypeRepository.class);
Expand Down Expand Up @@ -86,6 +86,6 @@ public void onHandleIntentShouldGenerateValidJsonFieldsForEventCreation() {
recurringIntentService.onHandleIntent(null);

HashMap<String, HashMap<Integer, ArrayList<Object>>> methodCalls = ShadowJsonFormUtils.getMethodCalls();
Assert.assertEquals(2, methodCalls.get("createServiceEvent(Context, ServiceRecord, String, String, JSONArray)").size());
Assert.assertEquals(2, methodCalls.get("createServiceEvent(ServiceRecord, String, String, JSONArray, Context)").size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onHandleIntentShouldGenerateValidJsonFieldsForEventCreation() {

org.smartregister.Context context = Mockito.mock(org.smartregister.Context.class);
Mockito.doReturn(new AppProperties()).when(context).getAppProperties();
ImmunizationLibrary.init(context, Mockito.mock(Repository.class), Mockito.mock(CommonFtsObject.class), 1, 1);
ImmunizationLibrary.init(context, Mockito.mock(Repository.class), Mockito.mock(CommonFtsObject.class), 1, "1.0.0", 1);

VaccineRepository vaccineRepository = Mockito.mock(VaccineRepository.class);
ReflectionHelpers.setField(vaccineIntentService, "vaccineRepository", vaccineRepository);
Expand All @@ -69,6 +69,6 @@ public void onHandleIntentShouldGenerateValidJsonFieldsForEventCreation() {
vaccineIntentService.onHandleIntent(null);

HashMap<String, HashMap<Integer, ArrayList<Object>>> methodCalls = ShadowJsonFormUtils.getMethodCalls();
Assert.assertEquals(2, methodCalls.get("createVaccineEvent(Context, Vaccine, String, String, JSONArray)").size());
Assert.assertEquals(2, methodCalls.get("createVaccineEvent(Vaccine, String, String, JSONArray, Context)").size());
}
}
Loading

0 comments on commit 7e75a19

Please sign in to comment.