Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue fix 839 #901

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions opensrp-anc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,9 @@ task javadoc(type: Javadoc) {
classpath += configurations.compile
}

def jacocoReportPath,sourceDirs

def jacocoReportPath, sourceDirs
task coveralls {
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoTestReport/merged.xml"
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/merged.xml"
sourceDirs = ["$project.projectDir/src/main/java"]
}

Expand Down
2 changes: 2 additions & 0 deletions opensrp-anc/src/main/assets/json.form/anc_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
"label_info_title": "{{anc_profile.step2.lmp_known.label_info_title}}",
"label_text_style": "bold",
"max_date": "today",
"secondary_value_field":"lmp_known_date",
"options": [
{
"key": "yes",
Expand Down Expand Up @@ -494,6 +495,7 @@
"label_info_text": "{{anc_profile.step2.ultrasound_done.label_info_text}}",
"label_info_title": "{{anc_profile.step2.ultrasound_done.label_info_title}}",
"label_text_style": "bold",
"secondary_value_field": "ultrasound_done_date",
"options": [
{
"key": "yes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description: step2_lmp_known_date
priority: 1
condition: "step2_lmp_known == 'yes'"
actions:
- "calculation = helper.getDateToday()"
- "calculation = helper.getSecondaryValue(step2_lmp_known_date)"
---
name: step2_lmp_edd
description: LMP EDD
Expand Down Expand Up @@ -40,7 +40,7 @@ description: step2_ultrasound_done_date
priority: 1
condition: "step2_ultrasound_done == 'yes'"
actions:
- "calculation = helper.getDateToday()"
- "calculation = helper.getSecondaryValue(step2_ultrasound_done_date)"
---
name: step2_ultrasound_edd
description: Ultrasound edd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.json.JSONObject;
import org.smartregister.anc.library.AncLibrary;
import org.smartregister.anc.library.R;
import org.smartregister.anc.library.constants.ANCJsonFormConstants;
import org.smartregister.anc.library.contract.ContactContract;
import org.smartregister.anc.library.domain.Contact;
import org.smartregister.anc.library.model.PartialContact;
Expand Down Expand Up @@ -217,7 +218,7 @@ private void process(String[] mainContactForms) {
}
}
//Make profile always complete on second contact onwards
requiredFieldsMap.put(ConstantsUtils.JsonFormUtils.ANC_PROFILE_ENCOUNTER_TYPE, 0);
// requiredFieldsMap.put(ConstantsUtils.JsonFormUtils.ANC_PROFILE_ENCOUNTER_TYPE, 0);
requiredFieldsMap.put(ConstantsUtils.JsonFormUtils.ANC_TEST_TASKS_ENCOUNTER_TYPE, 0);

}
Expand Down Expand Up @@ -606,13 +607,21 @@ private void preProcessDefaultValues(JSONObject object) {
private void updateDefaultValues(JSONArray stepArray, int i, JSONObject fieldObject) throws JSONException {
if (defaultValueFields.contains(fieldObject.getString(JsonFormConstants.KEY))) {

String secondaryValue = null;
if (!fieldObject.has(JsonFormConstants.VALUE) ||
TextUtils.isEmpty(fieldObject.getString(JsonFormConstants.VALUE))) {

String defaultKey = fieldObject.getString(JsonFormConstants.KEY);
String mapValue = getMapValue(defaultKey);

if(fieldObject.has(ANCJsonFormConstants.KeyConstants.SECONDARY_VALUE_FIELD))
secondaryValue = getMapValue(fieldObject.getString(ANCJsonFormConstants.KeyConstants.SECONDARY_VALUE_FIELD));


if (mapValue != null) {
if(mapValue.startsWith("{"))
fieldObject.put(JsonFormConstants.VALUE, new JSONObject(mapValue));
else
fieldObject.put(JsonFormConstants.VALUE, mapValue);
fieldObject.put(JsonFormConstants.EDITABLE, editableFields.contains(defaultKey));
fieldObject.put(JsonFormConstants.READ_ONLY, editableFields.contains(defaultKey));
Expand All @@ -623,8 +632,16 @@ private void updateDefaultValues(JSONArray stepArray, int i, JSONObject fieldObj
if (fieldObject.has(JsonFormConstants.OPTIONS_FIELD_NAME)) {
boolean addDefaults = true;


for (int m = 0; m < fieldObject.getJSONArray(JsonFormConstants.OPTIONS_FIELD_NAME).length(); m++) {
String optionValue;
JSONObject optionsObject = fieldObject.getJSONArray(JsonFormConstants.OPTIONS_FIELD_NAME).getJSONObject(m);
if(optionsObject.has(JsonFormConstants.CONTENT_WIDGET) && secondaryValue != null)
{
JSONObject secondaryValueObject = ANCJsonFormUtils.populateSecondaryValues(secondaryValue,optionsObject);
optionsObject.put(JsonFormConstants.SECONDARY_VALUE,new JSONArray().put(secondaryValueObject));
}

if (fieldObject.getJSONArray(JsonFormConstants.OPTIONS_FIELD_NAME).getJSONObject(m)
.has(JsonFormConstants.VALUE)) {
optionValue = fieldObject.getJSONArray(JsonFormConstants.OPTIONS_FIELD_NAME).getJSONObject(m)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.smartregister.anc.library.constants;

import com.vijay.jsonwizard.constants.JsonFormConstants;

public class ANCJsonFormConstants extends JsonFormConstants {

public static class KeyConstants {
public static final String OPTIBP_BUTTON = "record_bp_using_optibp_button";
public static final String OPTIBP_BUTTON_SECOND = "record_bp_using_optibp_2nd_reading_button";
public static final String SECONDARY_VALUE_FIELD = "secondary_value_field";
public static final String SPECIFY_WIDGET = "specify_widget";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
public class ContactInteractor extends BaseContactInteractor implements ContactContract.Interactor {
private Utils utils = new Utils();
private String edd = null;

public ContactInteractor() {
this(new AppExecutors());
Expand Down Expand Up @@ -130,6 +131,7 @@ public HashMap<String, String> finalizeContactForm(final Map<String, String> det

addTheContactDate(baseEntityId, details);
updateWomanDetails(details, womanDetail);
edd = details.get(DBConstantsUtils.KeyUtils.EDD);
if (referral != null && !TextUtils.isEmpty(details.get(DBConstantsUtils.KeyUtils.EDD))) {
addReferralGa(baseEntityId, details);
}
Expand Down Expand Up @@ -235,12 +237,19 @@ private PreviousContact preLoadPreviousContact(String baseEntityId, Map<String,
private String getCurrentContactState(String baseEntityId) throws JSONException {
List<PreviousContact> previousContactList = getPreviousContactRepository().getPreviousContacts(baseEntityId, null);
JSONObject stateObject = null;
String lastContactNo = null;
if (previousContactList != null) {
stateObject = new JSONObject();

for (PreviousContact previousContact : previousContactList) {
if(lastContactNo == null)
lastContactNo = previousContact.getContactNo();
if(previousContact.getKey().equals(CONTACT_DATE) && stateObject.has(CONTACT_DATE))
continue;

if(!lastContactNo.equals(previousContact.getContactNo()) && !stateObject.has(GEST_AGE_OPENMRS) && !TextUtils.isEmpty(edd))
stateObject.put(GEST_AGE_OPENMRS,String.valueOf(Utils.getGestationAgeFromEDDate(edd)));

if(stateObject.has(previousContact.getKey()))
continue;
stateObject.put(previousContact.getKey(), previousContact.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,14 @@ private static void processRadioButtonsSpecialWidget(JSONObject widget, List<Str
for (int i = 0; i < jsonArray.length(); i++) {

JSONObject jsonObject = jsonArray.getJSONObject(i);

if (widget.has(JsonFormConstants.VALUE) && !TextUtils.isEmpty(widget.getString(JsonFormConstants.VALUE)) &&
jsonObject.getString(JsonFormConstants.KEY).equals(widget.getString(JsonFormConstants.VALUE))) {
String widgetValue = null;
if(widget.has(JsonFormConstants.VALUE))
widgetValue = widget.getString(JsonFormConstants.VALUE);
if(StringUtils.isNotBlank(widgetValue) && widgetValue.startsWith("{"))
widgetValue = new JSONObject(widgetValue).getString(JsonFormConstants.VALUE);

if (StringUtils.isNotBlank(widgetValue) && !TextUtils.isEmpty(widgetValue) &&
jsonObject.getString(JsonFormConstants.KEY).equals(widgetValue)) {

if (jsonObject.has(JsonFormConstants.SECONDARY_VALUE) &&
!TextUtils.isEmpty(jsonObject.getString(JsonFormConstants.SECONDARY_VALUE))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,4 +1024,22 @@ public class Template {
public String title = "";
public String detail = "";
}

public static JSONObject populateSecondaryValues(String secondaryValue , JSONObject optionsObject)
{
JSONObject secondaryValueObject = new JSONObject();
JSONArray secondaryValueArray = new JSONArray();
try {
secondaryValueArray.put(secondaryValue);
secondaryValueObject.put(JsonFormConstants.KEY, optionsObject.getString(JsonFormConstants.KEY));
secondaryValueObject.put(JsonFormConstants.VALUES, secondaryValueArray);
secondaryValueObject.put(JsonFormConstants.TYPE, optionsObject.getString(JsonFormConstants.CONTENT_WIDGET));
}
catch (JSONException e)
{
Timber.e(e);
}
return secondaryValueObject;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ protected void populateDescendants(JSONObject jsonObject) {
try {
JSONObject parentField = JsonFormUtils.getFieldJSONObject(getFormStep().getJSONArray(FIELDS),
parents.get(jsonObject.getString(KEY)));
String parentId = parentField.getString(VALUE);

String parentId = "";
if (!StringUtils.isNotBlank(parentField.optString(VALUE)) && parentField.optString(VALUE).charAt(0) == '{') {
parentId = new JSONObject(parentField.optString(VALUE)).optString(VALUE);
} else {
parentId = parentField.getString(VALUE);
}
populateLocationSpinner(jsonObject, parentId, jsonObject.getString(KEY), descendants.get(jsonObject.getString(KEY)));
} catch (JSONException e) {
Timber.e(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public void setUp() {
// It was transferred from the app module to the lib module
// The fix might to add an actual OpenSRP application but again UserService testing should be in the
// opensrp-client-core library which provides this data
@Ignore

@Test
public void testIsUserLoggedOutShouldReturnTrue() {
Assert.assertTrue(model.isUserLoggedOut());
}

@Ignore

@Test
public void testGetOpenSRPContextShouldReturnValidValue() {
Assert.assertNotNull(model.getOpenSRPContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,25 @@ public void testInitializeFirstContactValuesForIsFirstContactStrategy() throws E
Assert.assertNotNull(result);
}


@Test
public void testPopulateSecondaryValues() throws JSONException
{
JSONObject optionsObject = new JSONObject();
String secondaryValue = "[{\\\"id\\\":\\\"101\\\",\\\"name\\\":\\\"test\\\",\\\"value\\\":\\\"test value\\\"},\n" +
"{\\\"id\\\":\\\"102\\\",\\\"name\\\":\\\"test 1\\\",\\\"value\\\":\\\"test_value\\\"},\n" +
"{\\\"id\\\":\\\"103\\\",\\\"name\\\":\\\"test 2\\\",\\\"value \\\":\\\"test_value1\\\"}]";

optionsObject.put(JsonFormConstants.KEY, "KEY");
optionsObject.put(JsonFormConstants.CONTENT_WIDGET, "test_widget");

JSONObject returnedObject = ANCJsonFormUtils.populateSecondaryValues(secondaryValue, optionsObject);
Assert.assertNotNull(returnedObject);
Assert.assertNotNull(returnedObject.getJSONArray(JsonFormConstants.VALUES));
Assert.assertNotNull(returnedObject.getString(JsonFormConstants.KEY));
Assert.assertNotNull(returnedObject.getString(JsonFormConstants.TYPE));


}

}