* @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 (HtmlFormEntryConstants.COMPLEX_UUID.equals(dt.getUuid())) {
obs.setComplexData((ComplexData) value);
obs.setValueComplex(obs.getComplexData().getTitle());
} else if (dt.isText()) {
if (value instanceof Location) {
Location location = (Location) value;
obs.setValueText(location.getId().toString() + " - " + location.getName());
} else if (value instanceof Person) {
Person person = (Person) value;
obs.setValueText(person.getId().toString() + " - " + person.getPersonName().toString());
} else {
obs.setValueText(value.toString());
}
} else if (dt.isCoded()) {
if (value instanceof Drug) {
obs.setValueDrug((Drug) value);
obs.setValueCoded(((Drug) value).getConcept());
} else if (value instanceof ConceptName) {
obs.setValueCodedName((ConceptName) value);
obs.setValueCoded(obs.getValueCodedName().getConcept());
} else if (value instanceof Concept) {
obs.setValueCoded((Concept) value);
} else {
obs.setValueCoded((Concept) convertToType(value.toString(), Concept.class));
}
} else if (dt.isBoolean()) {
if (value != null) {
try {
obs.setValueAsString(value.toString());
}
catch (ParseException e) {
throw new IllegalArgumentException("Unable to convert " + value + " to a Boolean Obs value", e);
}
}
} else if (ConceptDatatype.DATE.equals(dt.getHl7Abbreviation())
|| ConceptDatatype.TIME.equals(dt.getHl7Abbreviation())
|| ConceptDatatype.DATETIME.equals(dt.getHl7Abbreviation())) {
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);