Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-953:Support setting values of type "Location" #617

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openmrs.ConceptNumeric;
import org.openmrs.Drug;
import org.openmrs.Encounter;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.api.APIException;
Expand All @@ -35,6 +36,7 @@
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
Expand Down Expand Up @@ -315,6 +317,7 @@ public Object getValue(Obs obs) throws ConversionException {
return Context.getLocationService().getLocation(new Integer(obs.getValueText()));
}
catch (NumberFormatException e) {
// TDOO; we really shouldn't be supporting two ways of storing a location obs, should only use the location id
return Context.getLocationService().getLocationByUuid(obs.getValueText());
}
} else {
Expand Down Expand Up @@ -394,23 +397,28 @@ public static void setConcept(Obs obs, Object value) {
@PropertySetter("value")
public static void setValue(Obs obs, Object value) throws ParseException, ConversionException, IOException {
if (value != null) {

// special case for complex obs
if (obs.isComplex()) {
byte[] bytes = DatatypeConverter.parseBase64Binary(value.toString());

ComplexData complexData = new ComplexData(obs.getUuid() + ".raw", new ByteArrayInputStream(bytes));
obs.setComplexData(complexData);
} else if (obs.getConcept().getDatatype().isCoded()) {
// setValueAsString is not implemented for coded obs (in core)

//We want clients to be able to fetch a coded value in one rest call
//and set the returned payload as the obs value
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was reluctant to refactor this too much because I wasn't sure how good the test coverage was, but the nested if/else block were confusing and this change was going to make them even more confusing, so I removed the top-level if/else and had each block just return instead.

}

// special case for data type coded (setValueAsString is not implemented for coded obs (in core))
if (obs.getConcept().getDatatype().isCoded()) {
// We want clients to be able to fetch a coded value in one rest call
// and set the returned payload as the obs value
// (ie support setting based on posting the entire REST rep or just the concept uuid)
if (value instanceof Map) {
Object uuid = ((Map) value).get(RestConstants.PROPERTY_UUID);
if (uuid != null) {
value = uuid.toString();
}
}

Concept valueCoded = (Concept) ConversionUtil.convert(value, Concept.class);
if (valueCoded == null) {
//try checking if this this is value drug
Expand All @@ -421,58 +429,86 @@ public static void setValue(Obs obs, Object value) throws ParseException, Conver
} else {
throw new ObjectNotFoundException(obs.getConcept().getName().getName() + ":" + value.toString());
}

} else {
obs.setValueCoded(valueCoded);
}

} else {
if (obs.getConcept().isNumeric()) {
//get the actual persistent object rather than the hibernate proxy
ConceptNumeric concept = Context.getConceptService().getConceptNumeric(obs.getConcept().getId());
String units = concept.getUnits();
if (StringUtils.isNotBlank(units)) {
String originalValue = value.toString().trim();
if (originalValue.endsWith(units))
value = originalValue.substring(0, originalValue.indexOf(units)).trim();
else {
//check that that this value has no invalid units
try {
Double.parseDouble(originalValue);
}
catch (NumberFormatException e) {
throw new APIException(originalValue + " has invalid units", e);
}
return;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the start of the stuff I added to handle location

// special case for Location
String potentialLocationUuid = null;

// if this is a representation of an object, get the uuid property as potential location uuid
if (value instanceof Map) {
Object uuid = ((Map) value).get(RestConstants.PROPERTY_UUID);
if (uuid != null) {
potentialLocationUuid = uuid.toString();
}
}
else {
// otherwise, we will test if the value itself is a location uuid
potentialLocationUuid = value.toString();
}

// if there is a potential uuid, see if there is a matching location, and,if so, set the value text as the primary key
if (RestUtil.isUuid(potentialLocationUuid)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't strictly need to do a uuid check, but otherwise we'd we calling a getLocation.getLocationByUuid for any value text passed it, which seemed like it could be a performance hit.

Location location = Context.getLocationService().getLocationByUuid(potentialLocationUuid);
if (location != null) {
obs.setValueText(location.getLocationId().toString());
obs.setComment("org.openmrs.Location");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, but could also do "Location.class.getName()"

return;
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End of the stuff I added. The stuff below should be the same, just with one less if/then block.

// handle all other types using obs.setValueAsString after special conversions for numeric and boolean
if (obs.getConcept().isNumeric()) {
//get the actual persistent object rather than the hibernate proxy
ConceptNumeric concept = Context.getConceptService().getConceptNumeric(obs.getConcept().getId());
String units = concept.getUnits();
if (StringUtils.isNotBlank(units)) {
String originalValue = value.toString().trim();
if (originalValue.endsWith(units))
value = originalValue.substring(0, originalValue.indexOf(units)).trim();
else {
//check that this value has no invalid units
try {
Double.parseDouble(originalValue);
}
catch (NumberFormatException e) {
throw new APIException(originalValue + " has invalid units", e);
}
}
} else if (obs.getConcept().getDatatype().isBoolean()) {
if (value instanceof Concept) {
value = ((Concept) value).getUuid();
}
} else if (obs.getConcept().getDatatype().isBoolean()) {
if (value instanceof Concept) {
value = ((Concept) value).getUuid();
}
if (value.equals(Context.getConceptService().getTrueConcept().getUuid())) {
value = true;
} else if (value.equals(Context.getConceptService().getFalseConcept().getUuid())) {
value = false;
} else if (!value.getClass().isAssignableFrom(Boolean.class)) {
List<String> trueValues = Arrays.asList("true", "1", "on", "yes");
List<String> falseValues = Arrays.asList("false", "0", "off", "no");

String val = value.toString().trim().toLowerCase();
if (trueValues.contains(val)) {
value = Boolean.TRUE;
} else if (falseValues.contains(val)) {
value = Boolean.FALSE;
}
if (value.equals(Context.getConceptService().getTrueConcept().getUuid())) {
value = true;
} else if (value.equals(Context.getConceptService().getFalseConcept().getUuid())) {
value = false;
} else if (!value.getClass().isAssignableFrom(Boolean.class)) {
List<String> trueValues = Arrays.asList("true", "1", "on", "yes");
List<String> falseValues = Arrays.asList("false", "0", "off", "no");

String val = value.toString().trim().toLowerCase();
if (trueValues.contains(val)) {
value = Boolean.TRUE;
} else if (falseValues.contains(val)) {
value = Boolean.FALSE;
}

if (!(Boolean.TRUE.equals(value) || Boolean.FALSE.equals(value))) {
throw new ConversionException("Unexpected value: " + value + " set as the value of boolean. "
+ trueValues + falseValues + ", ConceptService.getTrueConcept or "
+ ", ConceptService.getFalseConcept expected");
}

if (!(Boolean.TRUE.equals(value) || Boolean.FALSE.equals(value))) {
throw new ConversionException("Unexpected value: " + value + " set as the value of boolean. "
+ trueValues + falseValues + ", ConceptService.getTrueConcept or "
+ ", ConceptService.getFalseConcept expected");
}
}
obs.setValueAsString(value.toString());
}
obs.setValueAsString(value.toString());


} else {
throw new APIException("The value for an observation cannot be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,46 @@ public void setValue_shouldReturnDrug() throws Exception {
ObsResource1_8.setValue(obs, drugUuid);
assertEquals(drug, new ObsResource1_8().getValue(obs));
}


@Test
public void setValue_shouldSupportPassingInFullConceptRep() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid("89ca642a-dab6-4f20-b712-e12ca4fc6d36"));
Concept valueCoded = Context.getConceptService().getConceptByUuid("32d3611a-6699-4d52-823f-b4b788bac3e3");
ObsResource1_8.setValue(obs, ConversionUtil.convertToRepresentation(valueCoded, Representation.FULL));
assertEquals(valueCoded, new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldReturnLocation() throws Exception {
Obs obs = new Obs();
Concept concept = Context.getConceptService().getConceptByUuid("96408258-000b-424e-af1a-403919332938");
obs.setConcept(concept);
String locationUuid = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f";
Location location = Context.getLocationService().getLocationByUuid(locationUuid);
ObsResource1_8.setValue(obs, locationUuid);
assertEquals(location, new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldSupportPassingInFullLocationRep() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid("96408258-000b-424e-af1a-403919332938"));
String locationUuid = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f";
Location location = Context.getLocationService().getLocationByUuid(locationUuid);
ObsResource1_8.setValue(obs, ConversionUtil.convertToRepresentation(location, Representation.FULL));
assertEquals(location, new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldReturnValueText() throws Exception {
Obs obs = new Obs();
Concept concept = Context.getConceptService().getConceptByUuid("96408258-000b-424e-af1a-403919332938");
obs.setConcept(concept);
ObsResource1_8.setValue(obs, "cheesecake");
assertEquals("cheesecake", new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldReturnUuidForConceptFalse() throws Exception {
Obs obs = new Obs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class RestUtil implements GlobalPropertyListener {
private static Log log = LogFactory.getLog(RestUtil.class);

private static boolean contextEnabled = true;

private static final Pattern UUID_REGEX =
Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");

/**
* Looks up the admin defined global property for the system limit
Expand Down Expand Up @@ -936,4 +940,8 @@ public static Class<?> getSupportedClass(Resource resource) {
.supportedClass();
}
}

public static boolean isUuid(String str) {
return StringUtils.isNotBlank(str) && UUID_REGEX.matcher(str).matches();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any existing standard util methods for this (besides doing a UUID.toString() in a try/catch block, which was ugly) so I just grabbed a regex I found online.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've definitely seen other implementations of this in OpenMRS code, can't remember where off-hand...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few lying around, but they often run up against the issue that we've been allowing invalid UUIDs for years (e.g., every single "uuid" in CIEL is, in fact, not a "valid UUID"). Usually these implementations hang around until someone complains and we basically end up with a check that says "is this string 36 characters long"? Here is one version and a slight variant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, @ibacher regarding the invalid uuid formats for concepts. Hopefully shouldn't matter for this case, since it is regarding to locations, but makes sense to standardized on a single util method, will fix.

}
}
Loading