/** * Builds a coded result from an observation * * @param obs */ public Result(Obs obs) { this( obs.getObsDatetime(), null, obs.getValueAsBoolean(), obs.getValueCoded(), obs.getValueDatetime(), obs.getValueNumeric(), obs.getValueText(), obs); Concept concept = obs.getConcept(); ConceptDatatype conceptDatatype = null; if (concept != null) { conceptDatatype = concept.getDatatype(); if (conceptDatatype == null) { return; } if (conceptDatatype.isCoded()) { this.datatype = Datatype.CODED; } else if (conceptDatatype.isNumeric()) { this.datatype = Datatype.NUMERIC; } else if (conceptDatatype.isDate()) { this.datatype = Datatype.DATETIME; } else if (conceptDatatype.isText()) { this.datatype = Datatype.TEXT; } else if (conceptDatatype.isBoolean()) { this.datatype = Datatype.BOOLEAN; } } }
/** * Creates an OpenMRS Obs instance * * @param concept concept associated with the Obs * @param value value associated with the Obs * @param datetime date/time associated with the Obs (may be null) * @param accessionNumber accession number associatd with the Obs (may be null) * @return the created Obs instance */ public static Obs createObs( Concept concept, Object value, Date datetime, String accessionNumber) { Obs obs = new Obs(); obs.setConcept(concept); ConceptDatatype dt = obs.getConcept().getDatatype(); if (dt.isNumeric()) { obs.setValueNumeric(Double.parseDouble(value.toString())); } else if (dt.isText()) { if (value instanceof Location) { obs.setValueText(((Location) value).getLocationId().toString()); } else { obs.setValueText(value.toString()); } } else if (dt.isCoded()) { if (value instanceof Concept) obs.setValueCoded((Concept) value); else obs.setValueCoded((Concept) convertToType(value.toString(), Concept.class)); } else if (dt.isBoolean()) { boolean booleanValue = value != null && !Boolean.FALSE.equals(value) && !"false".equals(value); obs.setValueNumeric(booleanValue ? 1.0 : 0.0); } else if (dt.isDate()) { Date date = (Date) value; obs.setValueDatetime(date); } else if ("ZZ".equals(dt.getHl7Abbreviation())) { // don't set a value } else { throw new IllegalArgumentException( "concept datatype not yet implemented: " + dt.getName() + " with Hl7 Abbreviation: " + dt.getHl7Abbreviation()); } if (datetime != null) obs.setObsDatetime(datetime); if (accessionNumber != null) obs.setAccessionNumber(accessionNumber); return obs; }