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();
}
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;
}
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);
}
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);
}
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();
}
Aggregations