Search in sources :

Example 1 with CheckboxWidget

use of org.openmrs.module.htmlformentry.widget.CheckboxWidget 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 CheckboxWidget

use of org.openmrs.module.htmlformentry.widget.CheckboxWidget 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

Concept (org.openmrs.Concept)2 Obs (org.openmrs.Obs)2 FormEntryContext (org.openmrs.module.htmlformentry.FormEntryContext)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