Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rvl 1289 senegal household compound to structure lookup #1279

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions opensrp-reveal/src/main/assets/ec_client_fields_senegal.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@
"field": "obs.fieldCode",
"concept": "mopUp"
}
},
{
"column_name": "compound_head_name",
"type": "Event",
"json_mapping": {
"field": "obs.fieldCode",
"concept": "compoundheadname"
}
},
{
"column_name": "structure_head_name",
"type": "Event",
"json_mapping": {
"field": "obs.fieldCode",
"concept": "structureheadname"
}
},
{
"column_name": "compound_structure",
"type": "Event",
"json_mapping": {
"field": "obs.fieldCode",
"concept": "compoundStructure"
}
}
]
},
Expand Down
24 changes: 23 additions & 1 deletion opensrp-reveal/src/main/assets/json.form/senegal_spray_form.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,35 @@
}
}
},
{
"key": "compoundStructure",
"openmrs_entity_parent": "",
"openmrs_entity": "",
"openmrs_entity_id": "",
"type": "multi_select_list",
"dialogTitle": "Select name of the head of the compound",
"buttonText": "+ Select name of the head of the compound",
"searchHint": "Type Name head of the compound",
"text_size": "8sp",
"maxSelectable": "1",
"v_required": {
"value": "true",
"err": "Required field"
},
"relevance": {
"step1:compoundheadstructure": {
"type": "string",
"ex": "equalTo(., \"No\")"
}
}
},
{
"key": "structureheadname",
"openmrs_entity_parent": "",
"openmrs_entity": "",
"openmrs_entity_id": "",
"type": "edit_text",
"hint": "If the head of the compound does not live in this structure, who is the head of this structure? ",
"hint": "Enter the name of the head of this structure",
"text_size": "8sp",
"v_required": {
"value": "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.smartregister.reveal.util.Constants.JsonForm;
import org.smartregister.reveal.util.Constants.Properties;
import org.smartregister.reveal.util.Constants.StructureType;
import org.smartregister.reveal.util.Country;
import org.smartregister.reveal.util.FamilyConstants.TABLE_NAME;
import org.smartregister.reveal.util.PreferencesUtil;
import org.smartregister.reveal.util.TaskUtils;
Expand Down Expand Up @@ -92,6 +93,7 @@
import static org.smartregister.reveal.util.Constants.Intervention.LARVAL_DIPPING;
import static org.smartregister.reveal.util.Constants.Intervention.MOSQUITO_COLLECTION;
import static org.smartregister.reveal.util.Constants.Intervention.PAOT;
import static org.smartregister.reveal.util.Constants.JsonForm.COMPOUND_STRUCTURE;
import static org.smartregister.reveal.util.Constants.JsonForm.ENCOUNTER_TYPE;
import static org.smartregister.reveal.util.Constants.JsonForm.LOCATION_COMPONENT_ACTIVE;
import static org.smartregister.reveal.util.Constants.JsonForm.PHYSICAL_TYPE;
Expand All @@ -105,6 +107,9 @@
import static org.smartregister.reveal.util.Constants.STRUCTURE;
import static org.smartregister.reveal.util.FamilyConstants.TABLE_NAME.FAMILY_MEMBER;
import static org.smartregister.util.JsonFormUtils.ENTITY_ID;
import static org.smartregister.util.JsonFormUtils.KEY;
import static org.smartregister.util.JsonFormUtils.VALUE;
import static org.smartregister.util.JsonFormUtils.VALUES;
import static org.smartregister.util.JsonFormUtils.getJSONObject;
import static org.smartregister.util.JsonFormUtils.getString;

Expand Down Expand Up @@ -212,6 +217,21 @@ private org.smartregister.domain.Event saveEvent(JSONObject jsonForm, String enc
JSONObject metadata = getJSONObject(jsonForm, METADATA);
Event event = JsonFormUtils.createEvent(fields, metadata, Utils.getFormTag(), entityId, encounterType, bindType);
JSONObject eventJson = new JSONObject(gson.toJson(event));
if(BuildConfig.BUILD_COUNTRY.equals(Country.SENEGAL)){
JSONObject compoundStructureField = JsonFormUtils.getFieldJSONObject(fields,COMPOUND_STRUCTURE);
JSONArray obsList = (JSONArray) eventJson.get("obs");
for(int i =0; i < obsList.length();i++){
JSONObject obs = (JSONObject) obsList.get(i);
if(obs.get("formSubmissionField").equals(COMPOUND_STRUCTURE)){
JSONObject value = (JSONObject) new JSONArray(compoundStructureField.get(VALUE).toString()).get(0);
JSONArray values = new JSONArray();
values.put(value.get(KEY));
obs.put(VALUES,values);
break;
}

}
}
eventJson.put(DETAILS, getJSONObject(jsonForm, DETAILS));
eventClientRepository.addEvent(entityId, eventJson);
return gson.fromJson(eventJson.toString(), org.smartregister.domain.Event.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ private void startForm(String formName, Feature feature, String sprayStatus, Str
dataCollector);
}

if(JsonForm.SPRAY_FORM_SENEGAL.equals(formName)){
jsonFormUtils.populateCompoundStructureOptions(formJson, org.smartregister.reveal.util.Utils.getOperationalAreaLocation(prefsUtil.getCurrentOperationalArea()));
}
} else if (JsonForm.SPRAY_FORM_REFAPP.equals(formName)) {
jsonFormUtils.populateServerOptions(RevealApplication.getInstance().getServerConfigs(), CONFIGURATION.DATA_COLLECTORS, jsonFormUtils.getFields(formJson).get(JsonForm.DATA_COLLECTOR), prefsUtil.getCurrentDistrict());
} else if (cardDetails instanceof SprayCardDetails && isZambiaIRSLite()) {
Expand Down Expand Up @@ -931,4 +934,5 @@ private void updateFeatureTaskBusinessStatus(String businessStatus) {

listTaskView.setGeoJsonSource(getFeatureCollection(), operationalArea, false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ interface Tables {
String STRUCTURE_TABLE = "structure";
String EC_EVENTS_TABLE = "ec_events";
String EC_EVENTS_SEARCH_TABLE = "ec_events_search";
String SPRAYED_STRUCTURES = "sprayed_structures";
}

interface BusinessStatus {
Expand Down Expand Up @@ -555,6 +556,8 @@ interface JsonForm {

String CDD_SUPERVISION_TASK_COMPLETE = "task_complete";

String COMPOUND_STRUCTURE = "compoundStructure";

}

interface DateFormat {
Expand Down Expand Up @@ -746,6 +749,15 @@ interface DatabaseKeys {

String DATA_COLLECTION_DATE = "data_collection_date";


String STRUCTURE_HEAD_NAME = "structure_head_name";

String COMPOUND_HEAD_NAME = "compound_head_name";

String COMPOUND_STRUCTURE = "compound_structure";

String SPRAY_DATE = "spray_date";

}

interface UseContextCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.utils.FormUtils;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -61,6 +64,7 @@
import static org.smartregister.reveal.util.Constants.EventType.IRS_LITE_VERIFICATION;
import static org.smartregister.reveal.util.Constants.EventType.IRS_VERIFICATION;
import static org.smartregister.reveal.util.Constants.JSON_FORM_PARAM_JSON;
import static org.smartregister.reveal.util.Constants.JsonForm.COMPOUND_STRUCTURE;
import static org.smartregister.reveal.util.Constants.JsonForm.JSON_FORM_FOLDER;
import static org.smartregister.reveal.util.Constants.JsonForm.YES;
import static org.smartregister.reveal.util.Constants.LARVAL_DIPPING_EVENT;
Expand Down Expand Up @@ -476,6 +480,20 @@ public void populateForm(Event event, JSONObject formJSON) {
if(Country.KENYA.equals(BuildConfig.BUILD_COUNTRY) && nonEditablefields.contains(key)){
field.put(JsonFormConstants.READ_ONLY,true);
}

if(Country.SENEGAL.equals(BuildConfig.BUILD_COUNTRY) && key.equals(COMPOUND_STRUCTURE)){
populateCompoundStructureOptions(formJSON,Utils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea()));
JSONArray options = field.optJSONArray(OPTIONS);
for(int j=0;j < options.length();j++){
JSONObject option = (JSONObject) options.get(j);
JSONArray value = new JSONArray();
value.put(option);
if(option.get(KEY).equals(obs.getValue())){
field.put(VALUE,value);
break;
}
}
}
}
if (JsonFormConstants.REPEATING_GROUP.equals(field.optString(TYPE))) {
generateRepeatingGroupFields(field, event.getObs(), formJSON);
Expand Down Expand Up @@ -682,4 +700,37 @@ private void populateUserAssignedLocations(JSONObject formJSON, String fieldKey,
Timber.e(e);
}
}

public void populateCompoundStructureOptions(JSONObject form, Location currentOperationalArea){
SQLiteDatabase database = RevealApplication.getInstance().getRepository().getReadableDatabase();
JSONObject option;
JSONObject property;
JSONArray options = new JSONArray();
String locationId = currentOperationalArea.getId();
String query = String.format("SELECT %s,%s FROM %s WHERE %s IS NOT NULL AND %s IN (SELECT %s FROM %s WHERE %s = ? ) ORDER BY %s DESC",Constants.DatabaseKeys.ID,Constants.DatabaseKeys.COMPOUND_HEAD_NAME,Constants.Tables.SPRAYED_STRUCTURES,Constants.DatabaseKeys.COMPOUND_HEAD_NAME,Constants.DatabaseKeys.BASE_ENTITY_ID,Constants.DatabaseKeys.ID_,Constants.DatabaseKeys.STRUCTURES_TABLE,Constants.DatabaseKeys.PARENT_ID,Constants.DatabaseKeys.SPRAY_DATE);
try(Cursor cursor = database.rawQuery(query,new String[]{locationId})){
while (cursor.moveToNext()) {
property = new JSONObject();
property.put("presumed-id","err");
property.put("confirmed-id","err");

String structureId = cursor.getString(cursor.getColumnIndex(Constants.DatabaseKeys.ID));
String compoundHeadName = cursor.getString(cursor.getColumnIndex(Constants.DatabaseKeys.COMPOUND_HEAD_NAME));

option = new JSONObject();
option.put(KEY,structureId);
option.put(TEXT,compoundHeadName);
option.put(JsonFormConstants.MultiSelectUtils.PROPERTY,property);
options.put(option);
}
} catch (Exception e) {
Timber.e(e, "Error find Sprayed Structures with compound head names ");
}
JSONObject compoundStructureField = JsonFormUtils.getFieldJSONObject(JsonFormUtils.fields(form),COMPOUND_STRUCTURE);
try {
compoundStructureField.put(OPTIONS, options);
} catch (JSONException e) {
Timber.e(e, "Error populating %s Options",COMPOUND_STRUCTURE);
}
}
}