-
Notifications
You must be signed in to change notification settings - Fork 518
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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 { | ||
|
@@ -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; | ||
} | ||
|
||
// 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 | ||
|
@@ -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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine, but could also do "Location.class.getName()" |
||
return; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} |
There was a problem hiding this comment.
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.