Search in sources :

Example 1 with FormEntryContext

use of org.openmrs.module.htmlformentry.FormEntryContext in project openmrs-module-pihcore by PIH.

the class PastMedicalHistoryCheckboxTagHandler method getSubstitution.

@Override
protected String getSubstitution(FormEntrySession session, FormSubmissionController controller, Map<String, String> attributes) throws BadFormDesignException {
    Concept concept = HtmlFormEntryUtil.getConcept(attributes.get("concept"));
    if (concept == null) {
        throw new IllegalArgumentException("Concept not found: " + attributes.get("concept"));
    }
    boolean includeCommentField = parseBooleanAttribute(attributes.get("specify"), false);
    String label = attributes.get("label");
    // TODO translate label
    if (label == null) {
        label = concept.getName().getName();
    }
    FormEntryContext context = session.getContext();
    Obs existingObs = findExistingObs(context, HtmlFormEntryUtil.getConcept(PihCoreConstants.PAST_MEDICAL_HISTORY_CONSTRUCT), HtmlFormEntryUtil.getConcept(PihCoreConstants.PAST_MEDICAL_HISTORY_FINDING), concept);
    CheckboxWidget checkboxWidget = new CheckboxWidget(label, PRESENT);
    checkboxWidget.setInitialValue(existingObs);
    context.registerWidget(checkboxWidget);
    TextFieldWidget textFieldWidget = null;
    ErrorWidget errorWidget = new ErrorWidget();
    if (includeCommentField) {
        textFieldWidget = new TextFieldWidget();
        textFieldWidget.setPlaceholder(messageSourceService.getMessage("zl.pastMedicalHistory.specifyLabel"));
        if (existingObs != null) {
            Obs candidate = findMember(existingObs, HtmlFormEntryUtil.getConcept(PihCoreConstants.PAST_MEDICAL_HISTORY_FINDING_TEXT));
            if (candidate != null) {
                String comments = candidate.getValueText();
                if (StringUtils.isNotBlank(comments)) {
                    textFieldWidget.setInitialValue(comments);
                }
            }
        }
        context.registerWidget(textFieldWidget);
        context.registerErrorWidget(textFieldWidget, errorWidget);
    }
    String clazz = attributes.get("class");
    String elementId = attributes.get("id");
    StringBuilder html = new StringBuilder();
    html.append("<div ");
    if (StringUtils.isNotBlank(elementId)) {
        html.append("id=\"").append(elementId).append("\" ");
    }
    html.append("class=\"past-medical-history-item");
    if (StringUtils.isNotBlank(clazz)) {
        html.append(" ").append(clazz);
    }
    html.append("\">");
    html.append(checkboxWidget.generateHtml(context));
    if (includeCommentField) {
        html.append(textFieldWidget.generateHtml(context));
        html.append(errorWidget.generateHtml(context));
    }
    html.append("</div>");
    session.getSubmissionController().addAction(new Action(concept, checkboxWidget, textFieldWidget, existingObs));
    return html.toString();
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) FormSubmissionControllerAction(org.openmrs.module.htmlformentry.action.FormSubmissionControllerAction) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext) CheckboxWidget(org.openmrs.module.htmlformentry.widget.CheckboxWidget) TextFieldWidget(org.openmrs.module.htmlformentry.widget.TextFieldWidget) ErrorWidget(org.openmrs.module.htmlformentry.widget.ErrorWidget)

Example 2 with FormEntryContext

use of org.openmrs.module.htmlformentry.FormEntryContext in project openmrs-module-coreapps by openmrs.

the class EncounterDiagnosesElement method getExistingDiagnosisObs.

private Map<Integer, Obs> getExistingDiagnosisObs(FormEntryContext context, DiagnosisMetadata diagnosisMetadata) {
    Map<Integer, Obs> existingDiagnosisObs = null;
    FormEntryContext.Mode mode = context.getMode();
    if (mode == FormEntryContext.Mode.EDIT || mode == FormEntryContext.Mode.VIEW) {
        existingDiagnosisObs = new HashMap<Integer, Obs>();
        Encounter encounter = context.getExistingEncounter();
        if (encounter == null) {
            // real use case though.
            return null;
        }
        for (Obs candidate : encounter.getObsAtTopLevel(false)) {
            if (diagnosisMetadata.isDiagnosis(candidate)) {
                existingDiagnosisObs.put(candidate.getObsId(), candidate);
            }
        }
        // TODO do we need to remove from existingObs as well?
        for (Obs existingDiagnosis : existingDiagnosisObs.values()) {
            context.getExistingObsInGroups().remove(existingDiagnosis);
        }
    }
    return existingDiagnosisObs;
}
Also used : Obs(org.openmrs.Obs) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext) Encounter(org.openmrs.Encounter)

Example 3 with FormEntryContext

use of org.openmrs.module.htmlformentry.FormEntryContext in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerListWidgetTest method setUpEnter.

public void setUpEnter() throws Exception {
    context = new FormEntryContext(FormEntryContext.Mode.ENTER) {

        @Override
        public Translator getTranslator() {
            return new Translator() {

                @Override
                public String translate(String localeStr, String key) {
                    return key;
                }
            };
        }
    };
    context.registerWidget(widget);
}
Also used : Translator(org.openmrs.module.htmlformentry.Translator) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext)

Example 4 with FormEntryContext

use of org.openmrs.module.htmlformentry.FormEntryContext in project openmrs-module-coreapps by openmrs.

the class EncounterDiagnosesElementTest method testGenerateHtml.

@Test
public void testGenerateHtml() throws Exception {
    EmrApiProperties emrApiProperties = mock(EmrApiProperties.class);
    FormEntryContext context = mock(FormEntryContext.class);
    when(context.getMode()).thenReturn(FormEntryContext.Mode.VIEW);
    EncounterDiagnosesElement element = new EncounterDiagnosesElement() {

        @Override
        List<Diagnosis> getExistingDiagnoses(FormEntryContext context, DiagnosisMetadata diagnosisMetadata) {
            return Arrays.asList(new Diagnosis(new CodedOrFreeTextAnswer("Some disease")));
        }
    };
    element.setEmrApiProperties(emrApiProperties);
    String html = element.generateHtml(context);
    assertTrue(html.indexOf("Translated") >= 0);
}
Also used : DiagnosisMetadata(org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata) EmrApiProperties(org.openmrs.module.emrapi.EmrApiProperties) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext) Diagnosis(org.openmrs.module.emrapi.diagnosis.Diagnosis) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer) Matchers.anyString(org.mockito.Matchers.anyString) BaseContextMockTest(org.openmrs.test.BaseContextMockTest) Test(org.junit.Test)

Example 5 with FormEntryContext

use of org.openmrs.module.htmlformentry.FormEntryContext in project openmrs-module-pihcore by PIH.

the class FamilyHistoryRelativeCheckboxesTagHandler method getSubstitution.

@Override
protected String getSubstitution(FormEntrySession session, FormSubmissionController controller, Map<String, String> attributes) throws BadFormDesignException {
    Concept concept = HtmlFormEntryUtil.getConcept(attributes.get("concept"));
    if (concept == null) {
        throw new IllegalArgumentException("Concept not found: " + attributes.get("concept"));
    }
    List<Concept> relatives = new ArrayList<Concept>();
    for (String id : attributes.get("relatives").split(",")) {
        Concept c = HtmlFormEntryUtil.getConcept(id);
        if (c == null) {
            throw new IllegalArgumentException("Relative type not found: " + id);
        }
        relatives.add(c);
    }
    if (relatives.isEmpty()) {
        throw new IllegalArgumentException(PihCoreConstants.HTMLFORMENTRY_FAMILY_HISTORY_RELATIVE_CHECKBOXES_TAG_NAME + " tag must have at least one relative specified");
    }
    boolean includeCommentField = parseBooleanAttribute(attributes.get("specify"), false);
    String label = attributes.get("label");
    // TODO translate label
    if (label == null) {
        label = concept.getName().getName();
    }
    Concept familyHistoryConstructConcept = HtmlFormEntryUtil.getConcept(PihCoreConstants.PATIENT_FAMILY_HISTORY_LIST_CONSTRUCT);
    Concept relationShipConcept = HtmlFormEntryUtil.getConcept(PihCoreConstants.RELATIONSHIP_OF_RELATIVE_TO_PATIENT);
    Concept diagnosisConcept = HtmlFormEntryUtil.getConcept(PihCoreConstants.FAMILY_HISTORY_DIAGNOSIS);
    Concept commentsConcept = HtmlFormEntryUtil.getConcept(PihCoreConstants.FAMILY_HISTORY_COMMENT);
    FormEntryContext context = session.getContext();
    TextFieldWidget textFieldWidget = null;
    ErrorWidget errorWidget = new ErrorWidget();
    // maps from wiget to existing obs for this widget
    Map<CheckboxWidget, Obs> relativeWidgets = new LinkedHashMap<CheckboxWidget, Obs>();
    for (Concept relative : relatives) {
        // group: family hx construct, member = which relative, member value = $relative
        // note: this assumes that all instances of this obs group have "is present = true"
        Obs existingObs = findObsGroupForDiagnosisAndRelativeInExistingObs(context, familyHistoryConstructConcept, diagnosisConcept, concept, relationShipConcept, relative);
        CheckboxWidget widget = new CheckboxWidget(relative.getName().getName(), relative.getUuid());
        widget.setInitialValue(existingObs);
        relativeWidgets.put(widget, existingObs);
        context.registerWidget(widget);
    }
    if (includeCommentField) {
        textFieldWidget = new TextFieldWidget();
        textFieldWidget.setPlaceholder(messageSourceService.getMessage("zl.pastMedicalHistory.specifyLabel"));
        Obs existingComments = findDiagnosisCommentInExistingDiagnosisObsGroups(relativeWidgets, commentsConcept);
        if (existingComments != null) {
            String valueText = existingComments.getValueText();
            if (StringUtils.isNotBlank(valueText)) {
                textFieldWidget.setInitialValue(valueText);
            }
        }
        context.registerWidget(textFieldWidget);
        context.registerErrorWidget(textFieldWidget, errorWidget);
    }
    String clazz = attributes.get("class");
    String elementId = attributes.get("id");
    StringBuilder html = new StringBuilder();
    html.append("<div ");
    if (StringUtils.isNotBlank(elementId)) {
        html.append("id=\"").append(elementId).append("\" ");
    }
    html.append("class=\"family-history-item");
    if (StringUtils.isNotBlank(clazz)) {
        html.append(" ").append(clazz);
    }
    html.append("\">");
    html.append("<div class=\"label\">");
    html.append(label);
    if (includeCommentField) {
        html.append(messageSourceService.getMessage("zl.familyHistoryRelativeCheckboxes.specifyLabel"));
        html.append(textFieldWidget.generateHtml(context));
        html.append(errorWidget.generateHtml(context));
    }
    html.append("</div>");
    for (Map.Entry<CheckboxWidget, Obs> entry : relativeWidgets.entrySet()) {
        html.append("<div class=\"relative\">");
        CheckboxWidget widget = entry.getKey();
        html.append(widget.generateHtml(context));
        html.append("</div>");
    }
    html.append("</div>");
    session.getSubmissionController().addAction(new Action(concept, relativeWidgets, textFieldWidget));
    return html.toString();
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) FormSubmissionControllerAction(org.openmrs.module.htmlformentry.action.FormSubmissionControllerAction) ArrayList(java.util.ArrayList) CheckboxWidget(org.openmrs.module.htmlformentry.widget.CheckboxWidget) ErrorWidget(org.openmrs.module.htmlformentry.widget.ErrorWidget) LinkedHashMap(java.util.LinkedHashMap) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext) TextFieldWidget(org.openmrs.module.htmlformentry.widget.TextFieldWidget) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

FormEntryContext (org.openmrs.module.htmlformentry.FormEntryContext)5 Obs (org.openmrs.Obs)3 Concept (org.openmrs.Concept)2 FormSubmissionControllerAction (org.openmrs.module.htmlformentry.action.FormSubmissionControllerAction)2 CheckboxWidget (org.openmrs.module.htmlformentry.widget.CheckboxWidget)2 ErrorWidget (org.openmrs.module.htmlformentry.widget.ErrorWidget)2 TextFieldWidget (org.openmrs.module.htmlformentry.widget.TextFieldWidget)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Encounter (org.openmrs.Encounter)1 EmrApiProperties (org.openmrs.module.emrapi.EmrApiProperties)1 CodedOrFreeTextAnswer (org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)1 Diagnosis (org.openmrs.module.emrapi.diagnosis.Diagnosis)1 DiagnosisMetadata (org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata)1 Translator (org.openmrs.module.htmlformentry.Translator)1 BaseContextMockTest (org.openmrs.test.BaseContextMockTest)1