Skip to content

Commit

Permalink
Merge pull request #634 from OpenSRP/save-selected-options-keys-for-c…
Browse files Browse the repository at this point in the history
…heckboxes

Save selected options key-value pairs for checkboxes
  • Loading branch information
vincent-karuri committed Aug 28, 2020
2 parents 82dde40 + ef22e2e commit b140a92
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 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=1.15.4-SNAPSHOT
VERSION_NAME=1.15.5.0-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
2 changes: 1 addition & 1 deletion opensrp-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ dependencies {

implementation 'org.smartregister:opensrp-client-utils:0.0.2-SNAPSHOT'

implementation 'org.smartregister:opensrp-plan-evaluator:0.0.19-SNAPSHOT'
implementation 'org.smartregister:opensrp-plan-evaluator:0.1.0-SNAPSHOT'

implementation 'xerces:xercesImpl:2.12.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Obs {

Expand All @@ -19,6 +20,8 @@ public class Obs {
@JsonProperty
private List<Object> values;
@JsonProperty
private Map<String, Object> keyValPairs;
@JsonProperty
private List<Object> humanReadableValues;
@JsonProperty
private String comments;
Expand All @@ -43,9 +46,11 @@ public Obs(String fieldType, String fieldDataType, String fieldCode, String pare
this.formSubmissionField = formSubmissionField;
}

public Obs(String fieldType, String fieldDataType, String fieldCode, String parentCode, List<Object> values, List<Object> humanReadableValues, String comments, String formSubmissionField, boolean saveObsAsArray) {
public Obs(String fieldType, String fieldDataType, String fieldCode, String parentCode,
List<Object> values, List<Object> humanReadableValues, String comments,
String formSubmissionField, boolean saveObsAsArray) {
this(fieldType, fieldDataType, fieldCode, parentCode, values, humanReadableValues, comments, formSubmissionField);
this.saveObsAsArray = saveObsAsArray;
setSaveObsAsArray(saveObsAsArray);
}

public String getFieldType() {
Expand Down Expand Up @@ -115,6 +120,14 @@ public void setHumanReadableValues(List<Object> humanReadableValues) {
this.humanReadableValues = humanReadableValues;
}

public Map<String, Object> getKeyValPairs() {
return keyValPairs;
}

public void setKeyValPairs(Map<String, Object> keyValPairs) {
this.keyValPairs = keyValPairs;
}

public List<Object> getValues() {
return values;
}
Expand Down Expand Up @@ -167,6 +180,10 @@ public Obs withValues(List<Object> values) {
this.values = values;
return this;
}
public Obs withKeyValPairs(Map<String, Object> keyValPairs) {
setKeyValPairs(keyValPairs);
return this;
}

public Obs addToValueList(Object value) {
if (values == null) {
Expand Down
35 changes: 19 additions & 16 deletions opensrp-app/src/main/java/org/smartregister/util/JsonFormUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,31 +410,33 @@ public static void addObservation(Event e, JSONObject jsonObject) {
if (StringUtils.isBlank(value)) { return; }

String type = getString(jsonObject, AllConstants.TYPE);
String entity = getString(jsonObject, OPENMRS_ENTITY);
if (AllConstants.CHECK_BOX.equals(type)) {
try {
List<Object> optionValues = new ArrayList<>();
Map<String, Object> optionKeyVals = new HashMap<>();
if (jsonObject.has(AllConstants.OPTIONS)) {
JSONArray options = jsonObject.getJSONArray(AllConstants.OPTIONS);
for (int i = 0; i < options.length(); i++) {
JSONObject option = options.getJSONObject(i);
boolean optionValue = option.optBoolean(VALUE);
entity = getString(jsonObject, OPENMRS_ENTITY);
if (optionValue) {
if (!optionValue) {
continue;
}
if (CONCEPT.equals(getString(jsonObject, OPENMRS_ENTITY))) {
// For options with concepts create an observation for each
option.put(AllConstants.TYPE, type);
option.put(AllConstants.PARENT_ENTITY_ID, jsonObject.getString(OPENMRS_ENTITY_ID));
option.put(KEY, jsonObject.getString(KEY));
if (CONCEPT.equals(entity)) {
// For options with concepts create an observation for each
createObservation(e, option, String.valueOf(option.getBoolean(VALUE)));
} else {
optionValues.add(option.optString(AllConstants.TEXT));
}
createObservation(e, option, String.valueOf(option.getBoolean(VALUE)));
} else {
String optionText = option.optString(AllConstants.TEXT);
optionValues.add(optionText);
optionKeyVals.put(option.optString(KEY), optionText);
}
}
if (!optionValues.isEmpty()) {
// For options without concepts combine the values into one observation
createObservation(e, jsonObject, optionValues);
createObservation(e, jsonObject, optionKeyVals, optionValues);
}
}
} catch (JSONException e1) {
Expand Down Expand Up @@ -519,22 +521,23 @@ private static void createObservation(Event e, JSONObject jsonObject, String val
}

/**
* This method creates an observation with single or multiple values combined
* This method creates an observation with single or multiple keys and values combined
*
* @param e The event that the observation is added to
* @param e The event that the observation is added to
* @param jsonObject The JSONObject representing the checkbox values
* @param vall A list of option values to be added to the observation
* @param keyValPairs A list of option keys to be added to the observation
* @param values A list of option values to be added to the observation
*/
private static void createObservation(Event e, JSONObject jsonObject, List<Object> vall) {
private static void createObservation(Event e, JSONObject jsonObject, Map<String, Object> keyValPairs, List<Object> values) {
String formSubmissionField = jsonObject.optString(KEY);
String dataType = jsonObject.optString(OPENMRS_DATA_TYPE);
if (StringUtils.isBlank(dataType)) {
dataType = AllConstants.TEXT;
}

e.addObs(new Obs("formsubmissionField", dataType, formSubmissionField,
"", vall, new ArrayList<>(), null, formSubmissionField,
jsonObject.optBoolean(SAVE_OBS_AS_ARRAY)));
"", values, new ArrayList<>(), null, formSubmissionField,
jsonObject.optBoolean(SAVE_OBS_AS_ARRAY)).withKeyValPairs(keyValPairs));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ public class JsonFormUtilsTest {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
formjson = new JSONObject(formresultJson);

}

@Test
Expand Down Expand Up @@ -1458,9 +1457,10 @@ public void testCreateObservationShouldCreateCorrectObservation() throws Excepti
jsonObject.put(KEY, "key");
Event event = new Event();
List values = new ArrayList<>();
Map<String, Object> keyValPairs = new HashMap<>();
values.add("value1");
values.add("value2");
Whitebox.invokeMethod(JsonFormUtils.class, "createObservation", event, jsonObject, values);
Whitebox.invokeMethod(JsonFormUtils.class, "createObservation", event, jsonObject, keyValPairs, values);
List<Obs> obsList = event.getObs();
assertEquals(1, obsList.size());
Obs obs = obsList.get(0);
Expand All @@ -1470,5 +1470,6 @@ public void testCreateObservationShouldCreateCorrectObservation() throws Excepti
assertEquals("key", obs.getFieldCode());
assertFalse(obs.isSaveObsAsArray());
assertEquals(values, obs.getValues());
assertEquals(keyValPairs, obs.getKeyValPairs());
}
}

0 comments on commit b140a92

Please sign in to comment.