use of org.openmrs.ConceptAnswer in project openmrs-core by openmrs.
the class ORUR01Handler method parseObs.
/**
* Creates the Obs pojo from the OBX message
*
* @param encounter The Encounter object this Obs is a member of
* @param obx The hl7 obx message
* @param obr The parent hl7 or message
* @param uid unique string for this message for any error reporting purposes
* @return Obs pojo with all values filled in
* @throws HL7Exception if there is a parsing exception
* @throws ProposingConceptException if the answer to this obs is a proposed concept
* @should add comments to an observation from NTE segments
* @should add multiple comments for an observation as one comment
* @should add comments to an observation group
*/
private Obs parseObs(Encounter encounter, OBX obx, OBR obr, String uid) throws HL7Exception, ProposingConceptException {
if (log.isDebugEnabled()) {
log.debug("parsing observation: " + obx);
}
Varies[] values = obx.getObservationValue();
// bail out if no values were found
if (values == null || values.length < 1) {
return null;
}
String hl7Datatype = values[0].getName();
if (log.isDebugEnabled()) {
log.debug(" datatype = " + hl7Datatype);
}
Concept concept = getConcept(obx.getObservationIdentifier(), uid);
if (log.isDebugEnabled()) {
log.debug(" concept = " + concept.getConceptId());
}
ConceptName conceptName = getConceptName(obx.getObservationIdentifier());
if (log.isDebugEnabled()) {
log.debug(" concept-name = " + conceptName);
}
Date datetime = getDatetime(obx);
if (log.isDebugEnabled()) {
log.debug(" timestamp = " + datetime);
}
if (datetime == null) {
datetime = encounter.getEncounterDatetime();
}
Obs obs = new Obs();
obs.setPerson(encounter.getPatient());
obs.setConcept(concept);
obs.setEncounter(encounter);
obs.setObsDatetime(datetime);
obs.setLocation(encounter.getLocation());
obs.setCreator(encounter.getCreator());
obs.setDateCreated(encounter.getDateCreated());
// set comments if there are any
StringBuilder comments = new StringBuilder();
ORU_R01_OBSERVATION parent = (ORU_R01_OBSERVATION) obx.getParent();
// iterate over all OBX NTEs
for (int i = 0; i < parent.getNTEReps(); i++) {
for (FT obxComment : parent.getNTE(i).getComment()) {
if (comments.length() > 0) {
comments.append(" ");
}
comments = comments.append(obxComment.getValue());
}
}
// only set comments if there are any
if (StringUtils.hasText(comments.toString())) {
obs.setComment(comments.toString());
}
Type obx5 = values[0].getData();
if ("NM".equals(hl7Datatype)) {
String value = ((NM) obx5).getValue();
if (value == null || value.length() == 0) {
log.warn("Not creating null valued obs for concept " + concept);
return null;
} else if ("0".equals(value) || "1".equals(value)) {
concept = concept.hydrate(concept.getConceptId().toString());
obs.setConcept(concept);
if (concept.getDatatype().isBoolean()) {
obs.setValueBoolean("1".equals(value));
} else if (concept.getDatatype().isNumeric()) {
try {
obs.setValueNumeric(Double.valueOf(value));
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.notnumericConcept", new Object[] { value, concept.getConceptId(), conceptName.getName(), uid }, null), e);
}
} else if (concept.getDatatype().isCoded()) {
Concept answer = "1".equals(value) ? Context.getConceptService().getTrueConcept() : Context.getConceptService().getFalseConcept();
boolean isValidAnswer = false;
Collection<ConceptAnswer> conceptAnswers = concept.getAnswers();
if (conceptAnswers != null && !conceptAnswers.isEmpty()) {
for (ConceptAnswer conceptAnswer : conceptAnswers) {
if (conceptAnswer.getAnswerConcept().getId().equals(answer.getId())) {
obs.setValueCoded(answer);
isValidAnswer = true;
break;
}
}
}
// answer the boolean answer concept was't found
if (!isValidAnswer) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.invalidAnswer", new Object[] { answer.toString(), uid }, null));
}
} else {
// throw this exception to make sure that the handler doesn't silently ignore bad hl7 message
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.CannotSetBoolean", new Object[] { obs.getConcept().getConceptId() }, null));
}
} else {
try {
obs.setValueNumeric(Double.valueOf(value));
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.notnumericConcept", new Object[] { value, concept.getConceptId(), conceptName.getName(), uid }, null), e);
}
}
} else if ("CWE".equals(hl7Datatype)) {
log.debug(" CWE observation");
CWE value = (CWE) obx5;
String valueIdentifier = value.getIdentifier().getValue();
log.debug(" value id = " + valueIdentifier);
String valueName = value.getText().getValue();
log.debug(" value name = " + valueName);
if (isConceptProposal(valueIdentifier)) {
if (log.isDebugEnabled()) {
log.debug("Proposing concept");
}
throw new ProposingConceptException(concept, valueName);
} else {
log.debug(" not proposal");
try {
Concept valueConcept = getConcept(value, uid);
obs.setValueCoded(valueConcept);
if (HL7Constants.HL7_LOCAL_DRUG.equals(value.getNameOfAlternateCodingSystem().getValue())) {
Drug valueDrug = new Drug();
valueDrug.setDrugId(Integer.valueOf(value.getAlternateIdentifier().getValue()));
obs.setValueDrug(valueDrug);
} else {
ConceptName valueConceptName = getConceptName(value);
if (valueConceptName != null) {
if (log.isDebugEnabled()) {
log.debug(" value concept-name-id = " + valueConceptName.getConceptNameId());
log.debug(" value concept-name = " + valueConceptName.getName());
}
obs.setValueCodedName(valueConceptName);
}
}
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.InvalidConceptId", new Object[] { valueIdentifier, valueName }, null));
}
}
if (log.isDebugEnabled()) {
log.debug(" Done with CWE");
}
} else if ("CE".equals(hl7Datatype)) {
CE value = (CE) obx5;
String valueIdentifier = value.getIdentifier().getValue();
String valueName = value.getText().getValue();
if (isConceptProposal(valueIdentifier)) {
throw new ProposingConceptException(concept, valueName);
} else {
try {
obs.setValueCoded(getConcept(value, uid));
obs.setValueCodedName(getConceptName(value));
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.InvalidConceptId", new Object[] { valueIdentifier, valueName }, null));
}
}
} else if ("DT".equals(hl7Datatype)) {
DT value = (DT) obx5;
if (value != null) {
Date valueDate = getDate(value.getYear(), value.getMonth(), value.getDay(), 0, 0, 0);
obs.setValueDatetime(valueDate);
} else {
log.warn("Not creating null valued obs for concept " + concept);
return null;
}
} else if ("TS".equals(hl7Datatype)) {
DTM value = ((TS) obx5).getTime();
if (value != null) {
Date valueDate = getDate(value.getYear(), value.getMonth(), value.getDay(), value.getHour(), value.getMinute(), value.getSecond());
obs.setValueDatetime(valueDate);
} else {
log.warn("Not creating null valued obs for concept " + concept);
return null;
}
} else if ("TM".equals(hl7Datatype)) {
TM value = (TM) obx5;
if (value != null) {
Date valueTime = getDate(0, 0, 0, value.getHour(), value.getMinute(), value.getSecond());
obs.setValueDatetime(valueTime);
} else {
log.warn("Not creating null valued obs for concept " + concept);
return null;
}
} else if ("ST".equals(hl7Datatype)) {
ST value = (ST) obx5;
if (value == null || value.getValue() == null || value.getValue().trim().length() == 0) {
log.warn("Not creating null valued obs for concept " + concept);
return null;
}
obs.setValueText(value.getValue());
} else if ("ED".equals(hl7Datatype)) {
ED value = (ED) obx5;
if (value == null || value.getData() == null || !StringUtils.hasText(value.getData().getValue())) {
log.warn("Not creating null valued obs for concept " + concept);
return null;
}
// we need to hydrate the concept so that the EncounterSaveHandler
// doesn't fail since it needs to check if it is a concept numeric
Concept c = Context.getConceptService().getConcept(obs.getConcept().getConceptId());
obs.setConcept(c);
String title = null;
if (obs.getValueCodedName() != null) {
title = obs.getValueCodedName().getName();
}
if (!StringUtils.hasText(title)) {
title = c.getName().getName();
}
obs.setComplexData(new ComplexData(title, value.getData().getValue()));
} else {
// do we need to support BIT just in case it slips thru?
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.UpsupportedObsType", new Object[] { hl7Datatype }, null));
}
return obs;
}
use of org.openmrs.ConceptAnswer in project openmrs-module-pihcore by PIH.
the class ConceptDictionaryExportTest method exportData.
/**
* Intentional issues currently with this, in order to isolate away known issues. These should be removed as appropriate:
* - Normalizes concept source name, due to known issue needed in OCL to facilitate source matching
* - Excludes voided concept names (which are really retired names that may have references), as these are not provided by ocl
* - Converts answer sort weight from null -> 1.0 if only one answer exists for the concept
*/
@Test
public void exportData() throws Exception {
// Only run this test if it is being run alone. This ensures this test will not run in normal build.
if (props == null) {
return;
}
if (!Context.isSessionOpen()) {
Context.openSession();
}
authenticate();
List<Map<String, Object>> concepts = new ArrayList<>();
System.out.println("Pulling concepts");
int i = 0;
List<Concept> conceptList = Context.getConceptService().getAllConcepts();
conceptList.sort(Comparator.comparing(t -> t.getUuid().toUpperCase()));
List<Concept> conceptsToIgnore = getConceptsToIgnore();
conceptList.removeAll(conceptsToIgnore);
for (Concept concept : conceptList) {
System.out.println(i++ + ": Pulling concept: " + concept.getUuid());
Map<String, Object> c = new LinkedHashMap<>();
concepts.add(c);
c.put("uuid", concept.getUuid());
c.put("concept_class", concept.getConceptClass().getName());
c.put("concept_datatype", concept.getDatatype().getName());
c.put("is_set", BooleanUtils.isTrue(concept.getSet()) ? "true" : "false");
c.put("version", concept.getVersion() == null ? "" : concept.getVersion());
c.put("retired", BooleanUtils.isTrue(concept.getRetired()) ? "true" : "false");
c.put("retireReason", concept.getRetireReason() == null ? "" : concept.getRetireReason());
if (concept instanceof ConceptNumeric) {
ConceptNumeric cn = (ConceptNumeric) concept;
c.put("hi_absolute", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
c.put("hi_critical", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
c.put("hi_normal", cn.getHiNormal() == null ? "" : cn.getHiNormal().toString());
c.put("low_absolute", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
c.put("low_critical", cn.getLowCritical() == null ? "" : cn.getLowCritical().toString());
c.put("low_normal", cn.getLowNormal() == null ? "" : cn.getLowNormal().toString());
c.put("units", cn.getUnits() == null ? "" : cn.getUnits());
c.put("allow_decimal", BooleanUtils.isTrue(cn.getAllowDecimal()) ? "true" : "false");
c.put("display_precision", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
}
if (concept instanceof ConceptComplex) {
ConceptComplex cc = (ConceptComplex) concept;
c.put("handler", cc.getHandler());
}
List<ConceptName> names = new ArrayList<>(concept.getNames(true));
names.sort(Comparator.comparing(ConceptName::getName).thenComparing(cn -> cn.getConceptNameType() == null ? "" : cn.getConceptNameType().name()).thenComparing(cn -> cn.getLocale().toString()).thenComparing(ConceptName::getLocalePreferred));
List<Map<String, Object>> nameList = new ArrayList<>();
for (ConceptName name : names) {
if (BooleanUtils.isNotTrue(name.getVoided())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("name", name.getName());
n.put("locale", name.getLocale().toString());
n.put("locale_preferred", BooleanUtils.isTrue(name.getLocalePreferred()) ? "true" : "false");
n.put("type", name.getConceptNameType() == null ? "" : name.getConceptNameType().name());
n.put("voided", BooleanUtils.isTrue(name.getVoided()) ? "true" : "false");
nameList.add(n);
}
}
c.put("names", nameList);
List<ConceptDescription> descriptions = new ArrayList<>(concept.getDescriptions());
descriptions.sort(Comparator.comparing(ConceptDescription::getDescription));
List<Map<String, Object>> descriptionList = new ArrayList<>();
for (ConceptDescription description : descriptions) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("description", description.getDescription());
n.put("locale", description.getLocale().toString());
descriptionList.add(n);
}
c.put("descriptions", descriptionList);
List<ConceptAnswer> answers = new ArrayList<>(concept.getAnswers(true));
answers.sort(Comparator.comparingDouble(ConceptAnswer::getSortWeight).thenComparing(ca -> ca.getAnswerConcept().getUuid()));
List<Map<String, Object>> answerList = new ArrayList<>();
for (ConceptAnswer ca : answers) {
if (!conceptsToIgnore.contains(ca.getAnswerConcept())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("answerConcept", ca.getAnswerConcept().getUuid());
n.put("sortWeight", ca.getSortWeight() == null ? (answers.size() == 1 ? "1.0" : "") : ca.getSortWeight().toString());
answerList.add(n);
}
}
c.put("answers", answerList);
List<ConceptSet> setMembers = new ArrayList<>(concept.getConceptSets());
setMembers.sort(Comparator.comparingDouble(ConceptSet::getSortWeight).thenComparing(cs -> cs.getConcept().getUuid()));
List<Map<String, Object>> setMemberList = new ArrayList<>();
for (ConceptSet cs : setMembers) {
if (!conceptsToIgnore.contains(cs.getConcept())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("concept", cs.getConcept().getUuid());
n.put("sortWeight", cs.getSortWeight() == null ? "" : cs.getSortWeight().toString());
setMemberList.add(n);
}
}
c.put("setMembers", setMemberList);
List<ConceptMap> mappings = new ArrayList<>(concept.getConceptMappings());
mappings.sort(Comparator.comparing((ConceptMap m) -> m.getConceptReferenceTerm().getConceptSource().getName()).thenComparing(m -> m.getConceptMapType().getName()).thenComparing(m -> m.getConceptReferenceTerm().getCode()));
List<Map<String, Object>> mappingList = new ArrayList<>();
for (ConceptMap cm : mappings) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("source", normalizeSourceName(cm.getConceptReferenceTerm().getConceptSource().getName()));
n.put("type", cm.getConceptMapType().getName());
n.put("code", cm.getConceptReferenceTerm().getCode());
mappingList.add(n);
}
c.put("mappings", mappingList);
}
File exportFile = new File(EXPORT_DIR, EXPORT_FILENAME);
System.out.println("Writing concepts to: " + exportFile);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithDefaultPrettyPrinter().writeValue(exportFile, concepts);
System.out.println("Concepts exported successfully");
}
use of org.openmrs.ConceptAnswer in project openmrs-module-mirebalais by PIH.
the class PatientRegistrationApp method populateDropdownWithConceptAnswers.
protected void populateDropdownWithConceptAnswers(DropdownWidget d, Concept questionConcept) {
List<ConceptAnswer> conceptAnswers = new ArrayList<ConceptAnswer>(questionConcept.getAnswers());
Collections.sort(conceptAnswers, new Comparator<ConceptAnswer>() {
@Override
public int compare(ConceptAnswer answer1, ConceptAnswer answer2) {
return answer1.getAnswerConcept().getName().toString().compareTo(answer2.getAnswerConcept().getName().toString());
}
});
for (ConceptAnswer conceptAnswer : conceptAnswers) {
d.getConfig().addOption(conceptAnswer.getAnswerConcept().getId().toString(), conceptAnswer.getAnswerConcept().getName().toString());
}
}
use of org.openmrs.ConceptAnswer in project openmrs-module-coreapps by openmrs.
the class EncounterDispositionTagHandlerComponentTest method testFormOnlyShowsDispositionsInDescriptor.
@Test
public void testFormOnlyShowsDispositionsInDescriptor() throws Exception {
ConceptClass misc = conceptService.getConceptClassByName("Misc");
ConceptDatatype naDatatype = conceptService.getConceptDatatypeByName("N/A");
Concept dispositionConcept = dispositionService.getDispositionDescriptor().getDispositionConcept();
dispositionConcept.addAnswer(new ConceptAnswer(new ConceptBuilder(conceptService, naDatatype, misc).addName("Should not be in option list").saveAndGet()));
new RegressionTestHelper() {
@Override
public void testBlankFormHtml(String html) {
assertThat(html, containsString("disposition.admission"));
assertThat(html, containsString("disposition.transfer"));
assertThat(html, containsString("disposition.discharge"));
assertThat(html, not(containsString("Should not be in option list")));
}
@Override
public String getXmlDatasetPath() {
return "";
}
@Override
public String getFormName() {
return "encounterDispositionSimpleForm";
}
@Override
public Map<String, Object> getFormEntrySessionAttributes() {
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("uiUtils", new TestUiUtils());
return attributes;
}
@Override
public String[] widgetLabels() {
return new String[] { "Date:", "Location:", "Provider:", "Disposition:", "Encounter Type:" };
}
}.run();
}
use of org.openmrs.ConceptAnswer in project openmrs-module-coreapps by openmrs.
the class QuickVisitFragmentController method getConceptAnswers.
public SimpleObject getConceptAnswers(@RequestParam("conceptId") Concept concept) {
SimpleObject results = new SimpleObject();
List<SimpleObject> values = new LinkedList<SimpleObject>();
for (ConceptAnswer conceptAnswer : concept.getAnswers()) {
SimpleObject conceptAnswerMap = new SimpleObject();
conceptAnswerMap.put("uuid", conceptAnswer.getUuid());
conceptAnswerMap.put("name", conceptAnswer.getAnswerConcept().getName().getName());
values.add(conceptAnswerMap);
}
results.put("results", values);
return results;
}
Aggregations