Skip to content

Commit

Permalink
Merge fbb0e3a into 0ed80eb
Browse files Browse the repository at this point in the history
  • Loading branch information
bennsimon committed May 6, 2020
2 parents 0ed80eb + fbb0e3a commit 94aaee4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.vijay.jsonwizard.utils.FormUtils;
import com.vijay.jsonwizard.utils.ImageUtils;
import com.vijay.jsonwizard.utils.PermissionUtils;
import com.vijay.jsonwizard.utils.Utils;
import com.vijay.jsonwizard.utils.ValidationStatus;
import com.vijay.jsonwizard.views.CustomTextView;
import com.vijay.jsonwizard.views.JsonFormFragmentView;
Expand Down Expand Up @@ -425,6 +426,7 @@ private static void setSpinnerError(MaterialSpinner spinner, String spinnerError
public void onSaveClick(LinearLayout mainView) {
validateAndWriteValues();
checkAndStopCountdownAlarm();
Utils.clearGeneratedLogicForRepeatingGroup(formFragment);
boolean isFormValid = isFormValid();
if (isFormValid || Boolean.valueOf(mainView.getTag(R.id.skip_validation).toString())) {
Intent returnIntent = new Intent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.vijay.jsonwizard.customviews.ExpansionPanelGenericPopupDialog;
import com.vijay.jsonwizard.domain.Form;
import com.vijay.jsonwizard.event.BaseEvent;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.rules.RuleConstant;
import com.vijay.jsonwizard.views.CustomTextView;
import com.vijay.jsonwizard.widgets.DatePickerFactory;
Expand Down Expand Up @@ -621,34 +622,30 @@ public static Iterable<Object> readYamlFile(String fileName, Context context) {
}

/**
*
* Translates a yaml file specified by {@param fileName} and returns its String representation
*
* @param fileName
* @param context
* @return Translated Yaml file in its String representation
*
*/
public static String getTranslatedYamlFile(String fileName, Context context) {
return getTranslatedString(getAssetFileAsString(fileName, context), context);
}

/**
*
* Gets the contents of a file specified by {@param fileName} from the assets folder as a {@link String}
*
* @param fileName
* @param context
* @return A file from the assets folder as a String
*
*/
public static String getAssetFileAsString(String fileName, Context context) {
InputStream inputStream = null;
String fileContents = "";
try {
inputStream = context.getAssets().open(fileName);
fileContents = convertStreamToString(inputStream);
} catch (IOException e) {
} catch (IOException e) {
Timber.e(e);
} finally {
closeCloseable(inputStream);
Expand All @@ -673,9 +670,8 @@ private static void closeScanner(Scanner scanner) {
}

/**
*
* Returns file contents for the file at {@param filePath} as a String
*
* <p>
* Defaults to an empty {@link String} if the file does not exist or is empty
*
* @param filePath
Expand Down Expand Up @@ -751,6 +747,35 @@ public static Set<String> convertJsonArrayToSet(@Nullable JSONArray jsonArray) {
}
return null;
}


/***
*
* removes the generated rules by repeating group
*/
public static void clearGeneratedLogicForRepeatingGroup(JsonFormFragment formFragment) {
JSONObject form = formFragment.getJsonApi().getmJSONObject();
JSONArray jsonArray = FormUtils.getMultiStepFormFields(form);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
if (jsonObject.has(JsonFormConstants.RELEVANCE) &&
jsonObject.optJSONObject(JsonFormConstants.RELEVANCE).has(RuleConstant.RULES_ENGINE) &&
jsonObject.optJSONObject(JsonFormConstants.RELEVANCE).optJSONObject(RuleConstant.RULES_ENGINE)
.has(JsonFormConstants.JSON_FORM_KEY.EX_RULES) &&
jsonObject.optJSONObject(JsonFormConstants.RELEVANCE).optJSONObject(RuleConstant.RULES_ENGINE)
.optJSONObject(JsonFormConstants.JSON_FORM_KEY.EX_RULES).has(RuleConstant.RULES_DYNAMIC)) {
jsonArray.optJSONObject(i).remove(JsonFormConstants.RELEVANCE);
}
if (jsonObject.has(JsonFormConstants.CALCULATION) &&
jsonObject.optJSONObject(JsonFormConstants.CALCULATION).has(RuleConstant.RULES_ENGINE) &&
jsonObject.optJSONObject(JsonFormConstants.CALCULATION).optJSONObject(RuleConstant.RULES_ENGINE)
.has(JsonFormConstants.JSON_FORM_KEY.EX_RULES) &&
jsonObject.optJSONObject(JsonFormConstants.CALCULATION).optJSONObject(RuleConstant.RULES_ENGINE)
.optJSONObject(JsonFormConstants.JSON_FORM_KEY.EX_RULES).has(RuleConstant.RULES_DYNAMIC)) {
jsonArray.optJSONObject(i).remove(JsonFormConstants.CALCULATION);
}
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.JsonApi;
import com.vijay.jsonwizard.rules.RuleConstant;
import com.vijay.jsonwizard.views.CustomTextView;
import com.vijay.jsonwizard.widgets.DatePickerFactory;
Expand Down Expand Up @@ -330,4 +332,16 @@ public void testGetStringValueShouldReturnStringValue() throws Exception {
String expected = "yes, no, don't know";
Assert.assertEquals(expected, WhiteboxImpl.invokeMethod(utils, "getStringValue", jsonObject));
}

@Test
public void testClearGeneratedLogicForRepeatingGroupDeleteGeneratedRules() throws JSONException {
String formSample = "{\"count\":\"1\",\"step1\":{\"fields\":[{\"key\":\"diagnostic_test_result_d724bdfb19584539af33b1826ea0b989\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"hint\":\"The result of the test conducted\",\"type\":\"spinner\",\"values\":[\"Positive\",\"Negative\",\"Inconclusive\"],\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-dynamic\":[{\"key\":\"d724bdfb19584539af33b1826ea0b989\"},{\"name\":\"step4_diagnostic_test_result_d724bdfb19584539af33b1826ea0b989\",\"description\":\"diagnostic_test_result_d724bdfb19584539af33b1826ea0b989\",\"priority\":1,\"actions\":\"isRelevant = true\",\"condition\":\"step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Pregnancy Test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Malaria test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'HIV test - Rapid Test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'HIV test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Syphilis Test - VDRL' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'HIV EID' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Hep B test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Hep C test' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'TB-urine LAM' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'TB Screening' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Midstream urine Gram-staining' || step4_diagnostic_test_d724bdfb19584539af33b1826ea0b989 == 'Malaria - MRDT'\"}]}}}}]}}";
JSONObject form = new JSONObject(formSample);
JsonFormFragment jsonFormFragment = Mockito.mock(JsonFormFragment.class);
JsonApi jsonApi = Mockito.mock(JsonApi.class);
Mockito.when(jsonFormFragment.getJsonApi()).thenReturn(jsonApi);
Mockito.when(jsonApi.getmJSONObject()).thenReturn(form);
Utils.clearGeneratedLogicForRepeatingGroup(jsonFormFragment);
Assert.assertFalse(form.optJSONObject(JsonFormConstants.STEP1).optJSONArray(JsonFormConstants.FIELDS).optJSONObject(0).has(JsonFormConstants.RELEVANCE));
}
}
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.8.1-SNAPSHOT
VERSION_NAME=1.8.2-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down

0 comments on commit 94aaee4

Please sign in to comment.