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 324f58ee2..76ea3282b 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; @@ -24,6 +24,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; @@ -37,6 +38,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 +98,17 @@ 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 (RestUtil.isValidUuid(value)) { + Location location = Context.getLocationService().getLocationByUuid(value); + if (location != null) { + personAttribute.setValue(location.getUuid()); + return; + } + } + PersonAttributeType attributeType = personAttribute.getAttributeType(); if (attributeType == null) { personAttribute.setValue(value); @@ -297,10 +307,10 @@ public String getDisplayString(PersonAttribute pa) { String value = pa.toString(); return value == null ? "" : value; } - + /** * Gets the hydrated object of person attribute. - * + * * @param pa the person attribute. * @return an object containing the hydrated object. */ @@ -310,8 +320,7 @@ public Object getValue(PersonAttribute pa) { if (value == null) { return 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 5ab6df5fc..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 @@ -34,10 +34,11 @@ import static org.hamcrest.Matchers.equalToIgnoringCase; public class PersonAttributeResource1_8Test extends BaseModuleWebContextSensitiveTest { - - public static final String PERSON_ATTRIBUTE_JSON = "{" + " \"value\": \"Bangalore\"," + " \"attributeType\": {" - + " \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\"" + " }" + "}"; - + + public static final String PERSON_ATTRIBUTE_JSON = "{\"value\": \"Bangalore\", \"uuid\": \"592349ed-d012-4552-a274-d5d8e73b9401\", \"attributeType\": { \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\" }}"; + + public static final String PERSON_ATTRIBUTE_JSON_WITHOUT_UUID = "{\"value\": \"Bangalore\", \"uuid\": \"\", \"attributeType\": { \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\" }}"; + private SimpleObject personAttributeSimpleObject = new SimpleObject(); private PersonAttributeResource1_8 resource; @@ -65,6 +66,14 @@ public void shouldCreatePersonAttribute() throws Exception { Assert.assertEquals("Bangalore", created.get("value")); } + @Test + public void getValue_shouldFallBackToHydratedObjectWhenUuidIsEmpty() throws Exception { + personAttributeSimpleObject.putAll(new ObjectMapper().readValue(PERSON_ATTRIBUTE_JSON_WITHOUT_UUID, HashMap.class)); + SimpleObject created = (SimpleObject) resource.create("da7f524f-27ce-4bb2-86d6-6d1d05312bd5", + personAttributeSimpleObject, new RequestContext()); + Assert.assertEquals("Bangalore", created.get("value")); + } + @Test public void getDisplayString_shouldGetDisplayStringForConcept() { // arrange @@ -138,11 +147,9 @@ public void setValue_shouldSetProperAttributableIdIfFound() { PersonAttribute attribute = new PersonAttribute(type, null); attribute.setAttributeType(type); - Assert.assertNull(attribute.getValue()); resource.setValue(attribute, location.getUuid()); - Assert.assertEquals(location.getUuid(), attribute.getValue()); }