From cc168a87a949af48be6db2fff20257aba0988de1 Mon Sep 17 00:00:00 2001 From: mherman22 Date: Tue, 17 Sep 2024 23:19:40 +0300 Subject: [PATCH] add logic to setter --- .../PersonAttributeResource1_8.java | 49 +++++++++++++------ .../PersonAttributeResource1_8Test.java | 2 +- 2 files changed, 36 insertions(+), 15 deletions(-) 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..11e8a5895 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 @@ -14,11 +14,7 @@ import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; -import org.openmrs.Attributable; -import org.openmrs.Concept; -import org.openmrs.Person; -import org.openmrs.PersonAttribute; -import org.openmrs.PersonAttributeType; +import org.openmrs.*; import org.openmrs.api.APIException; import org.openmrs.api.context.Context; import org.openmrs.module.webservices.rest.web.ConversionUtil; @@ -37,6 +33,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 +93,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 +135,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 +320,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