diff --git a/opensrp-reveal/src/main/assets/ec_client_fields_senegal.json b/opensrp-reveal/src/main/assets/ec_client_fields_senegal.json index 2d22ecee72..93a055d1c4 100644 --- a/opensrp-reveal/src/main/assets/ec_client_fields_senegal.json +++ b/opensrp-reveal/src/main/assets/ec_client_fields_senegal.json @@ -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" + } } ] }, diff --git a/opensrp-reveal/src/main/assets/json.form/senegal_spray_form.json b/opensrp-reveal/src/main/assets/json.form/senegal_spray_form.json index c2ff1111db..3edbe7dde3 100644 --- a/opensrp-reveal/src/main/assets/json.form/senegal_spray_form.json +++ b/opensrp-reveal/src/main/assets/json.form/senegal_spray_form.json @@ -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", diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/BaseInteractor.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/BaseInteractor.java index 325a920381..9fc201fee2 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/BaseInteractor.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/interactor/BaseInteractor.java @@ -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; @@ -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; @@ -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; @@ -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); diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/presenter/ListTaskPresenter.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/presenter/ListTaskPresenter.java index 79fb57f6b7..f8cd1af794 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/presenter/ListTaskPresenter.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/presenter/ListTaskPresenter.java @@ -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()) { @@ -931,4 +934,5 @@ private void updateFeatureTaskBusinessStatus(String businessStatus) { listTaskView.setGeoJsonSource(getFeatureCollection(), operationalArea, false); } + } diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/Constants.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/Constants.java index ad916275c5..d007b15670 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/Constants.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/Constants.java @@ -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 { @@ -555,6 +556,8 @@ interface JsonForm { String CDD_SUPERVISION_TASK_COMPLETE = "task_complete"; + String COMPOUND_STRUCTURE = "compoundStructure"; + } interface DateFormat { @@ -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 { diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealJsonFormUtils.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealJsonFormUtils.java index c13091e71d..094ba4289a 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealJsonFormUtils.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealJsonFormUtils.java @@ -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; @@ -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; @@ -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); @@ -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); + } + } }