Skip to content

Commit

Permalink
RESTWS-953:Support setting values of type "Location" (#618)
Browse files Browse the repository at this point in the history
(standardize UUID method)
  • Loading branch information
mogoodrich committed Aug 13, 2024
1 parent 4f3e8ec commit 8764f60
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public static void setValue(Obs obs, Object value) throws ParseException, Conver
}

// 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)) {
if (RestUtil.isValidUuid(potentialLocationUuid)) {
Location location = Context.getLocationService().getLocationByUuid(potentialLocationUuid);
if (location != null) {
obs.setValueText(location.getLocationId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openmrs.api.ConceptService;
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.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig;
import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler;
Expand Down Expand Up @@ -89,7 +90,7 @@ public PageableResult search(RequestContext context) throws ResponseException {
continue;
}
// handle UUIDs
if (isValidUuid(conceptReference)) {
if (RestUtil.isValidUuid(conceptReference)) {
Concept concept = conceptService.getConceptByUuid(conceptReference);
if (concept != null) {
concepts.add(concept);
Expand Down Expand Up @@ -185,8 +186,4 @@ public PageableResult search(RequestContext context) throws ResponseException {
return new NeedsPaging<Concept>(conceptsByMapping, context);
}
}

private static boolean isValidUuid(String uuid) {
return uuid != null && (uuid.length() == 36 || uuid.length() == 38 || uuid.indexOf(' ') < 0 || uuid.indexOf('.') < 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Object search(HttpServletRequest request, HttpServletResponse response) {
continue;
}
// handle UUIDs
if (isValidUuid(conceptReference)) {
if (RestUtil.isValidUuid(conceptReference)) {
Concept concept = conceptService.getConceptByUuid(conceptReference);
if (concept != null) {
addResult(results, conceptReference, concept, requestContext.getRepresentation());
Expand Down Expand Up @@ -95,8 +95,5 @@ private void addResult(SimpleObject results, String conceptReference, Concept co
ConversionUtil.convertToRepresentation(concept, rep == null ? new DefaultRepresentation() : rep));
}

private static boolean isValidUuid(String uuid) {
return uuid != null && (uuid.length() == 36 || uuid.length() == 38 || uuid.indexOf(' ') < 0
|| uuid.indexOf('.') < 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ 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 @@ -941,7 +938,7 @@ public static Class<?> getSupportedClass(Resource resource) {
}
}

public static boolean isUuid(String str) {
return StringUtils.isNotBlank(str) && UUID_REGEX.matcher(str).matches();
public static boolean isValidUuid(String uuid) {
return uuid != null && (uuid.length() == 36 || uuid.length() == 38 || uuid.indexOf(' ') < 0 || uuid.indexOf('.') < 0);
}
}

0 comments on commit 8764f60

Please sign in to comment.