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