diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8.java index f3592d28a..74453ee06 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8.java @@ -15,7 +15,7 @@ import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; import org.openmrs.Attributable; -import org.openmrs.Concept; +import org.openmrs.Location; import org.openmrs.Person; import org.openmrs.PersonAttribute; import org.openmrs.PersonAttributeType; @@ -37,6 +37,7 @@ import org.openmrs.module.webservices.rest.web.response.ConversionException; import org.openmrs.module.webservices.rest.web.response.ResponseException; import org.openmrs.util.OpenmrsClassLoader; +import java.util.UUID; /** * {@link Resource} for PersonAttributes, supporting standard CRUD operations @@ -96,9 +97,13 @@ public void setHydratedObject(PersonAttribute personAttribute, String attributab throw new APIException("Could not convert value to Attributable", e); } } - + @PropertySetter("value") public void setValue(PersonAttribute personAttribute, String value) { + if (setLocationIfUUID(personAttribute, value)) { + return; + } + PersonAttributeType attributeType = personAttribute.getAttributeType(); if (attributeType == null) { personAttribute.setValue(value); @@ -134,6 +139,26 @@ public void setValue(PersonAttribute personAttribute, String value) { } } } + + private boolean setLocationIfUUID(PersonAttribute personAttribute, String value) { + if (isValidUUID(value)) { + Location location = Context.getLocationService().getLocationByUuid(value); + if (location != null) { + personAttribute.setValue(location.getUuid()); + return true; + } + } + return false; + } + + private boolean isValidUUID(String value) { + try { + UUID.fromString(value); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } /** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUpdatableProperties() @@ -299,18 +324,18 @@ public String getDisplayString(PersonAttribute pa) { } /** - * Retrieves the value of a PersonAttribute, prioritizing the UUID. - * If the UUID is unavailable, falls back to the hydrated object. + * Gets the hydrated object of person attribute. * - * @param pa the PersonAttribute from which to retrieve the value - * @return the converted representation of the UUID or hydrated object, or null if both are unavailable + * @param pa the person attribute. + * @return an object containing the hydrated object. */ @PropertyGetter("value") public Object getValue(PersonAttribute pa) { - Object uuid = pa.getUuid(); - Object value = (uuid == null) ? pa.getHydratedObject() : null; + Object value = pa.getHydratedObject(); + if (value == null) { + return null; + } - Object toConvert = (uuid != null) ? uuid : value; - return (toConvert != null) ? ConversionUtil.convertToRepresentation(toConvert, Representation.REF) : null; + return ConversionUtil.convertToRepresentation(value, Representation.REF); } } diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8Test.java index c2d3a232a..7c620f738 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8Test.java @@ -63,7 +63,7 @@ public void beforeEachTests() throws Exception { public void shouldCreatePersonAttribute() throws Exception { SimpleObject created = (SimpleObject) resource.create("da7f524f-27ce-4bb2-86d6-6d1d05312bd5", personAttributeSimpleObject, new RequestContext()); - Assert.assertEquals("592349ed-d012-4552-a274-d5d8e73b9401", created.get("value")); + Assert.assertEquals("Bangalore", created.get("value")); } @Test