Skip to content

Commit 3a96e30

Browse files
Merge pull request #654 from opensrp/issue-fix-906
added missing option required for the human readable value population…
2 parents 2e7e21a + d38a7fd commit 3a96e30

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ public void getOptionsOpenMRSAttributes(JSONObject item, JSONArray valueOpenMRSA
15681568
if ((JsonFormConstants.NATIVE_RADIO_BUTTON.equals(item.getString(JsonFormConstants.TYPE)) ||
15691569
JsonFormConstants.EXTENDED_RADIO_BUTTON.equals(item.getString(JsonFormConstants.TYPE))) &&
15701570
item.has(JsonFormConstants.VALUE)) {
1571-
String value = item.optString(JsonFormConstants.VALUE);
1571+
String value = Utils.extractValueFromJson(item.optString(JsonFormConstants.VALUE));
15721572
if (itemOption.has(JsonFormConstants.KEY) && value.equals(itemOption.getString(JsonFormConstants.KEY))) {
15731573
extractOptionOpenMRSAttributes(valueOpenMRSAttributes, itemOption,
15741574
item.getString(JsonFormConstants.KEY));
@@ -1725,6 +1725,15 @@ protected void extractOptionOpenMRSAttributes(JSONArray valueOpenMRSAttributes,
17251725
valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY_PARENT, openmrsEntityParent);
17261726
valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY, openmrsEntity);
17271727
valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY_ID, openmrsEntityId);
1728+
if(itemOption.has(JsonFormConstants.TEXT) && itemOption.has(JsonFormConstants.KEY)) {
1729+
valueOpenMRSObject.put(JsonFormConstants.VALUE, itemOption.getString(JsonFormConstants.KEY));
1730+
valueOpenMRSObject.put(JsonFormConstants.TEXT, itemOption.getString(JsonFormConstants.TEXT));
1731+
JSONArray options = new JSONArray();
1732+
options.put(itemOption);
1733+
// adds the selected option as an option array to fill the human readable value;
1734+
valueOpenMRSObject.put(JsonFormConstants.OPTIONS_FIELD_NAME,options);
1735+
}
1736+
17281737

17291738
valueOpenMRSAttributes.put(valueOpenMRSObject);
17301739
}

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java

+15
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,21 @@ public void enableExpansionPanelViews(LinearLayout linearLayout) {
10101010
okButton.setClickable(true);
10111011
}
10121012

1013+
public static String extractValueFromJson(String value)
1014+
{
1015+
try{
1016+
if(!StringUtils.isEmpty(value) && value.startsWith("{") && value.endsWith("}")) {
1017+
JSONObject object = new JSONObject(value);
1018+
return object.optString(VALUE);
1019+
}
1020+
}
1021+
catch (Exception e)
1022+
{
1023+
Timber.e(e);
1024+
}
1025+
return value;
1026+
}
1027+
10131028
}
10141029

10151030

android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -520,5 +520,19 @@ public void testGenerateTranslatableValueWithoutOptionsField () throws Exception
520520
JSONObject item = new JSONObject(jsonForm);
521521
Assert.assertEquals(expectedJson.toString(), Utils.generateTranslatableValue(item.optString(JsonFormConstants.KEY), item).toString());
522522
}
523+
@Test
524+
public void testExtractValueFromJson() throws JSONException
525+
{
526+
String value = JsonFormConstants.VALUE;
527+
JSONObject object = new JSONObject();
528+
object.put(JsonFormConstants.VALUE, value);
529+
530+
String result = Utils.extractValueFromJson(object.toString());
531+
Assert.assertEquals(result,JsonFormConstants.VALUE);
532+
533+
String result2 = Utils.extractValueFromJson(value);
534+
Assert.assertEquals(result2,value);
535+
536+
}
523537
}
524538

android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactoryTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import android.view.View;
66
import android.widget.RadioButton;
77

8+
89
import com.vijay.jsonwizard.BaseTest;
910
import com.vijay.jsonwizard.R;
1011
import com.vijay.jsonwizard.activities.JsonFormActivity;
12+
import com.vijay.jsonwizard.constants.JsonFormConstants;
1113
import com.vijay.jsonwizard.fragments.JsonFormFragment;
1214
import com.vijay.jsonwizard.interfaces.CommonListener;
1315
import com.vijay.jsonwizard.utils.FormUtils;
@@ -107,9 +109,13 @@ public void testGetSecondaryDateValueWithEmptyValues() throws Exception {
107109
public void testGetOptionTextWithSecondaryValue() throws Exception {
108110
JSONObject jsonObject = new JSONObject("{\"key\":\"user_sub_form\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"native_radio\",\"label\":\"User sub forms?\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"extra_rel\":true,\"has_extra_rel\":\"yes\",\"options\":[{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"specify_info\":\"User sub specify...\",\"specify_widget\":\"normal_edit_text\",\"specify_info_color\":\"#8C8C8C\",\"secondary_suffix\":\"bpm\",\"content_form\":\"user_native_sub_form\",\"secondary_value\":[{\"key\":\"yes\",\"type\":\"date_picker\",\"values\":[\"24-09-2020\"]}]},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\"}],\"value\":\"yes\",\"v_required\":{\"value\":true,\"err\":\"Please specify user native form.\"}}");
109111
JSONObject item = new JSONObject("{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"specify_info\":\"User sub specify...\",\"specify_widget\":\"normal_edit_text\",\"specify_info_color\":\"#8C8C8C\",\"secondary_suffix\":\"bpm\",\"content_form\":\"user_native_sub_form\",\"secondary_value\":[{\"key\":\"yes\",\"type\":\"date_picker\",\"values\":[\"24-09-2020\"]}]}");
112+
JSONObject valueObject = new JSONObject();
113+
valueObject.put(JsonFormConstants.VALUE,"yes");
114+
valueObject.put(JsonFormConstants.TEXT,"");
110115

116+
jsonObject.put(JsonFormConstants.VALUE, valueObject);
111117
String optionText = Whitebox.invokeMethod(new NativeRadioButtonFactory(), "getOptionTextWithSecondaryValue", item, jsonObject);
112-
Assert.assertEquals("Yes", optionText);
118+
Assert.assertEquals("Yes:24-09-2020", optionText);
113119
}
114120

115121
@Test

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION_NAME=3.1.0-PREVIEW-SNAPSHOT
1+
VERSION_NAME=3.1.1-PREVIEW-SNAPSHOT
22
VERSION_CODE=1
33
GROUP=org.smartregister
44
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard

0 commit comments

Comments
 (0)