Skip to content

Commit

Permalink
add logic to setter
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Sep 17, 2024
1 parent ab1332c commit dbd41e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dbd41e7

Please sign in to comment.