diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java index 4417f5380..fc07e7472 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java @@ -1568,7 +1568,7 @@ public void getOptionsOpenMRSAttributes(JSONObject item, JSONArray valueOpenMRSA if ((JsonFormConstants.NATIVE_RADIO_BUTTON.equals(item.getString(JsonFormConstants.TYPE)) || JsonFormConstants.EXTENDED_RADIO_BUTTON.equals(item.getString(JsonFormConstants.TYPE))) && item.has(JsonFormConstants.VALUE)) { - String value = item.optString(JsonFormConstants.VALUE); + String value = Utils.extractValueFromJson(item.optString(JsonFormConstants.VALUE)); if (itemOption.has(JsonFormConstants.KEY) && value.equals(itemOption.getString(JsonFormConstants.KEY))) { extractOptionOpenMRSAttributes(valueOpenMRSAttributes, itemOption, item.getString(JsonFormConstants.KEY)); @@ -1725,6 +1725,15 @@ protected void extractOptionOpenMRSAttributes(JSONArray valueOpenMRSAttributes, valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY_PARENT, openmrsEntityParent); valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY, openmrsEntity); valueOpenMRSObject.put(JsonFormConstants.OPENMRS_ENTITY_ID, openmrsEntityId); + if(itemOption.has(JsonFormConstants.TEXT) && itemOption.has(JsonFormConstants.KEY)) { + valueOpenMRSObject.put(JsonFormConstants.VALUE, itemOption.getString(JsonFormConstants.KEY)); + valueOpenMRSObject.put(JsonFormConstants.TEXT, itemOption.getString(JsonFormConstants.TEXT)); + JSONArray options = new JSONArray(); + options.put(itemOption); + // adds the selected option as an option array to fill the human readable value; + valueOpenMRSObject.put(JsonFormConstants.OPTIONS_FIELD_NAME,options); + } + valueOpenMRSAttributes.put(valueOpenMRSObject); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index f576f3ca1..7ba534d22 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -1010,6 +1010,21 @@ public void enableExpansionPanelViews(LinearLayout linearLayout) { okButton.setClickable(true); } + public static String extractValueFromJson(String value) + { + try{ + if(!StringUtils.isEmpty(value) && value.startsWith("{") && value.endsWith("}")) { + JSONObject object = new JSONObject(value); + return object.optString(VALUE); + } + } + catch (Exception e) + { + Timber.e(e); + } + return value; + } + } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index c56405481..6b2a187e8 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -520,5 +520,19 @@ public void testGenerateTranslatableValueWithoutOptionsField () throws Exception JSONObject item = new JSONObject(jsonForm); Assert.assertEquals(expectedJson.toString(), Utils.generateTranslatableValue(item.optString(JsonFormConstants.KEY), item).toString()); } + @Test + public void testExtractValueFromJson() throws JSONException + { + String value = JsonFormConstants.VALUE; + JSONObject object = new JSONObject(); + object.put(JsonFormConstants.VALUE, value); + + String result = Utils.extractValueFromJson(object.toString()); + Assert.assertEquals(result,JsonFormConstants.VALUE); + + String result2 = Utils.extractValueFromJson(value); + Assert.assertEquals(result2,value); + + } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactoryTest.java index 2896ab404..9ea6825d5 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactoryTest.java @@ -5,9 +5,11 @@ import android.view.View; import android.widget.RadioButton; + import com.vijay.jsonwizard.BaseTest; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; +import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.utils.FormUtils; @@ -107,9 +109,13 @@ public void testGetSecondaryDateValueWithEmptyValues() throws Exception { public void testGetOptionTextWithSecondaryValue() throws Exception { 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.\"}}"); 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\"]}]}"); + JSONObject valueObject = new JSONObject(); + valueObject.put(JsonFormConstants.VALUE,"yes"); + valueObject.put(JsonFormConstants.TEXT,""); + jsonObject.put(JsonFormConstants.VALUE, valueObject); String optionText = Whitebox.invokeMethod(new NativeRadioButtonFactory(), "getOptionTextWithSecondaryValue", item, jsonObject); - Assert.assertEquals("Yes", optionText); + Assert.assertEquals("Yes:24-09-2020", optionText); } @Test diff --git a/gradle.properties b/gradle.properties index 680f737e1..6963929ec 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.1.0-PREVIEW-SNAPSHOT +VERSION_NAME=3.1.1-PREVIEW-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard