Search in sources :

Example 6 with CodedOrFreeTextAnswer

use of org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer in project openmrs-module-pihcore by PIH.

the class CauseOfDeathListTagHandler method initialValue.

private List<CodedOrFreeTextAnswer> initialValue(List<Obs> existingList) {
    Concept codedCauseConcept = HtmlFormEntryUtil.getConcept(CODED_CAUSE_OF_DEATH_CONCEPT);
    Concept nonCodedCauseConcept = HtmlFormEntryUtil.getConcept(NON_CODED_CAUSE_OF_DEATH_CONCEPT);
    final Concept orderConcept = HtmlFormEntryUtil.getConcept(DIAGNOSIS_SEQUENCE_CONCEPT);
    List<CodedOrFreeTextAnswer> list = new ArrayList<CodedOrFreeTextAnswer>();
    Collections.sort(existingList, new Comparator<Obs>() {

        @Override
        public int compare(Obs left, Obs right) {
            return OpenmrsUtil.compareWithNullAsGreatest(getMemberValueNumeric(orderConcept, left), getMemberValueNumeric(orderConcept, right));
        }
    });
    for (Obs existing : existingList) {
        Obs coded = findMember(existing, codedCauseConcept);
        if (coded != null) {
            list.add(new CodedOrFreeTextAnswer(coded));
        } else {
            Obs nonCoded = findMember(existing, nonCodedCauseConcept);
            list.add(new CodedOrFreeTextAnswer(nonCoded));
        }
    }
    return list;
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) ArrayList(java.util.ArrayList) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)

Example 7 with CodedOrFreeTextAnswer

use of org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerListWidget method getValue.

@Override
public Object getValue(FormEntryContext context, HttpServletRequest request) {
    String fieldName = context.getFieldName(this);
    String[] submitted = request.getParameterValues(fieldName);
    if (submitted != null && submitted.length > 1) {
        throw new IllegalArgumentException("Expected one submitted parameter value for " + fieldName + " but got " + submitted.length);
    }
    try {
        List<CodedOrFreeTextAnswer> results = new ArrayList<CodedOrFreeTextAnswer>();
        if (submitted != null && StringUtils.isNotEmpty(submitted[0])) {
            ArrayNode array = new ObjectMapper().readValue(submitted[0], ArrayNode.class);
            ConceptService conceptService = Context.getConceptService();
            for (JsonNode node : array) {
                String conceptNameUuid = node.path("ConceptName").getTextValue();
                String conceptUuid = node.path("Concept").getTextValue();
                String nonCodedValue = node.path("NonCodedValue").getTextValue();
                if (conceptNameUuid != null) {
                    results.add(new CodedOrFreeTextAnswer(conceptService.getConceptNameByUuid(conceptNameUuid)));
                } else if (conceptUuid != null) {
                    results.add(new CodedOrFreeTextAnswer(conceptService.getConceptByUuid(conceptUuid)));
                } else {
                    results.add(new CodedOrFreeTextAnswer(nonCodedValue));
                }
            }
        }
        return results;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer) ArrayNode(org.codehaus.jackson.node.ArrayNode) IOException(java.io.IOException) ConceptService(org.openmrs.api.ConceptService) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 8 with CodedOrFreeTextAnswer

use of org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerListWidget method generateHtml.

@Override
public String generateHtml(FormEntryContext context) {
    Translator translator = context.getTranslator();
    String localeStr = locale.toString();
    String title = translator.translate(localeStr, titleCode);
    String betweenElements = translator.translate(localeStr, betweenElementsCode);
    String placeholder = translator.translate(localeStr, placeholderCode);
    StringBuilder ret = new StringBuilder();
    ret.append("<div class=\"coded-or-free-text-list-widget\">");
    if (context.getMode().equals(FormEntryContext.Mode.VIEW)) {
        ret.append("<span class=\"coded-or-free-text-title\">" + title + "</span>\n");
        if (initialValue != null) {
            boolean first = true;
            for (CodedOrFreeTextAnswer answer : initialValue) {
                if (!first && StringUtils.isNotEmpty(betweenElements)) {
                    ret.append("<br/>");
                    ret.append("<span class=\"coded-or-free-text-between\">").append(betweenElements).append("</span>");
                }
                ret.append("<br/>");
                ret.append("<span class=\"value\">").append(answer.format(locale)).append("</span>");
                first = false;
            }
        }
    } else {
        String hiddenInputName = context.getFieldName(this);
        Map<String, Object> fragmentConfig = new HashMap<String, Object>();
        fragmentConfig.put("formFieldName", hiddenInputName);
        fragmentConfig.put("title", title);
        fragmentConfig.put("placeholder", placeholder);
        fragmentConfig.put("betweenElements", betweenElements);
        fragmentConfig.put("containerClasses", containerClasses);
        fragmentConfig.put("initialValue", initialValueAsJson(initialValue));
        try {
            ret.append(uiUtils.includeFragment("coreapps", "htmlformentry/codedOrFreeTextAnswerList", fragmentConfig));
        } catch (PageAction pageAction) {
            throw new IllegalStateException("fragment threw PageAction", pageAction);
        } catch (NullPointerException ex) {
            // (This is hacky, but I don't see a better way to do it.)
            return "<input type=\"hidden\" name=\"" + context.getFieldName(this) + "\"/> (Submitting the form, so we don't generate HTML)";
        }
    }
    ret.append("</div>");
    return ret.toString();
}
Also used : PageAction(org.openmrs.ui.framework.page.PageAction) Translator(org.openmrs.module.htmlformentry.Translator) HashMap(java.util.HashMap) SimpleObject(org.openmrs.ui.framework.SimpleObject) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)

Example 9 with CodedOrFreeTextAnswer

use of org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerWidget method getValue.

@Override
public Object getValue(FormEntryContext context, HttpServletRequest request) {
    String fieldName = context.getFieldName(this);
    String[] submitted = request.getParameterValues(fieldName);
    if (submitted == null || submitted.length == 0) {
        return null;
    }
    if (submitted.length != 1) {
        throw new IllegalArgumentException("Expected one submitted parameter value for " + fieldName + " but got " + submitted.length);
    }
    if (submitted[0].equals("")) {
        return null;
    }
    String prefix = submitted[0].substring(0, submitted[0].indexOf(':'));
    String value = submitted[0].substring(submitted[0].indexOf(':') + 1);
    if (prefix.equals("ConceptName")) {
        return new CodedOrFreeTextAnswer(Context.getConceptService().getConceptNameByUuid(value));
    } else if (prefix.equals("Concept")) {
        return new CodedOrFreeTextAnswer(Context.getConceptService().getConceptByUuid(value));
    } else if (prefix.equals("NonCoded")) {
        return new CodedOrFreeTextAnswer(value);
    } else {
        throw new IllegalArgumentException("Unexpected submission: " + submitted[0]);
    }
}
Also used : CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)

Example 10 with CodedOrFreeTextAnswer

use of org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer in project openmrs-module-coreapps by openmrs.

the class EncounterDiagnosesElement method parseDiagnoses.

private List<Diagnosis> parseDiagnoses(String jsonList, Map<Integer, Obs> existingDiagnosisObs) throws IOException {
    // low-priority: refactor this so that a Diagnosis can parse itself via jackson.
    // requires changing org.openmrs.module.emrapi.diagnosis.ConceptCodeDeserializer to also handle parse by id.
    List<Diagnosis> parsed = new ArrayList<Diagnosis>();
    JsonNode list = new ObjectMapper().readTree(jsonList);
    for (JsonNode node : list) {
        CodedOrFreeTextAnswer answer = new CodedOrFreeTextAnswer(node.get("diagnosis").getTextValue(), conceptService);
        Diagnosis.Order diagnosisOrder = Diagnosis.Order.valueOf(node.get("order").getTextValue());
        Diagnosis.Certainty certainty = Diagnosis.Certainty.valueOf(node.get("certainty").getTextValue());
        Obs existingObs = null;
        if (existingDiagnosisObs != null && node.path("existingObs").getNumberValue() != null) {
            existingObs = existingDiagnosisObs.get(node.get("existingObs").getNumberValue());
        }
        Diagnosis diagnosis = new Diagnosis(answer, diagnosisOrder);
        diagnosis.setCertainty(certainty);
        diagnosis.setExistingObs(existingObs);
        parsed.add(diagnosis);
    }
    return parsed;
}
Also used : Obs(org.openmrs.Obs) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) Diagnosis(org.openmrs.module.emrapi.diagnosis.Diagnosis) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

CodedOrFreeTextAnswer (org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)14 ArrayList (java.util.ArrayList)9 Diagnosis (org.openmrs.module.emrapi.diagnosis.Diagnosis)8 Test (org.junit.Test)6 Concept (org.openmrs.Concept)5 Obs (org.openmrs.Obs)5 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)4 SimpleObject (org.openmrs.ui.framework.SimpleObject)4 List (java.util.List)3 JsonNode (org.codehaus.jackson.JsonNode)3 ConceptName (org.openmrs.ConceptName)3 TestUiUtils (org.openmrs.module.appui.TestUiUtils)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ArrayNode (org.codehaus.jackson.node.ArrayNode)2 ConceptSearchResult (org.openmrs.ConceptSearchResult)2 DiagnosisMetadata (org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata)2 FragmentModel (org.openmrs.ui.framework.fragment.FragmentModel)2 Method (java.lang.reflect.Method)1 Date (java.util.Date)1