Skip to content

Commit

Permalink
RESTWS-955: Add support for person attribute of type Location (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Sep 18, 2024
1 parent 4a54c44 commit deb522d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 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 @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand All @@ -310,8 +320,7 @@ public Object getValue(PersonAttribute pa) {
if (value == null) {
return null;
}

return ConversionUtil.convertToRepresentation(value, Representation.REF);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit deb522d

Please sign in to comment.