Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Cohort API to also save attributes #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.customdatatype.CustomDatatypeUtil;
import org.openmrs.module.cohort.CohortAttribute;
import org.openmrs.module.cohort.CohortAttributeType;
import org.openmrs.module.cohort.CohortM;
Expand Down Expand Up @@ -81,6 +82,7 @@ public Collection<CohortM> findAll() {

@Override
public CohortM saveCohort(@NotNull CohortM cohortM) {
CustomDatatypeUtil.saveAttributesIfNecessary(cohortM);
return cohortDao.createOrUpdate(cohortM);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openmrs.User;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.cohort.CohortAttribute;
Expand Down Expand Up @@ -224,16 +223,8 @@ public Collection<CohortAttribute> getCohortAttributes(CohortM cohort) {
*/
@PropertySetter("attributes")
public void setAttributes(CohortM cohort, List<CohortAttribute> attributes) {
if (attributes != null) {
User authenticatedUser = Context.getAuthenticatedUser();
ibacher marked this conversation as resolved.
Show resolved Hide resolved
Set<CohortAttribute> attributeSet = new HashSet<>(attributes);
cohort.getActiveAttributes().stream().filter(a -> !attributeSet.contains(a)).forEach(a -> {
a.setVoided(true);
a.setVoidReason("Attribute voided by API");
a.setVoidedBy(authenticatedUser);
});

cohort.getActiveAttributes().addAll(attributeSet);
for (CohortAttribute attribute : attributes) {
cohort.addAttribute(attribute);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.openmrs.api.context.Context;
import org.openmrs.module.cohort.CohortAttribute;
import org.openmrs.module.cohort.CohortAttributeType;
import org.openmrs.module.cohort.CohortM;
import org.openmrs.module.cohort.api.CohortService;
import org.openmrs.module.webservices.rest.web.RequestContext;
Expand All @@ -30,6 +32,14 @@ public class CohortResourceTest extends BaseCohortResourceTest<CohortM, CohortRe

private static final String COHORT_NAME = "Test cohort attribute type";

private static final String COHORT_ATTRIBUTE_TYPE_UUID = "09790099-9190-429d-811a-aac9edb8d98e";

private static final String COHORT_ATTRIBUTE_TYPE_NAME = "Test";

private static final String COHORT_ATTRIBUTE_TYPE_DESCRIPTION = "This is a test group.";

private static final String COHORT_ATTRIBUTE_TYPE_DATATYPE = "org.openmrs.customdatatype.datatype.FreeTextDatatype";

@Mock
@Qualifier("cohort.cohortService")
private CohortService cohortService;
Expand All @@ -41,6 +51,20 @@ public void setup() {
cohort = new CohortM();
cohort.setUuid(COHORT_UUID);
cohort.setName(COHORT_NAME);
CohortAttributeType attributeType = new CohortAttributeType();
ibacher marked this conversation as resolved.
Show resolved Hide resolved
attributeType.setUuid(COHORT_ATTRIBUTE_TYPE_UUID);
attributeType.setName(COHORT_ATTRIBUTE_TYPE_NAME);
attributeType.setDescription(COHORT_ATTRIBUTE_TYPE_DESCRIPTION);
attributeType.setMinOccurs(0);
attributeType.setDatatypeClassname(COHORT_ATTRIBUTE_TYPE_DATATYPE);
CohortAttribute attribute1 = new CohortAttribute();
attribute1.setAttributeType(attributeType);
attribute1.setValue("Test group");
cohort.addAttribute(attribute1);
CohortAttribute attribute2 = new CohortAttribute();
attribute2.setAttributeType(attributeType);
attribute2.setValue("Control group");
cohort.addAttribute(attribute2);

//Mocks
this.prepareMocks();
Expand Down Expand Up @@ -86,6 +110,7 @@ public void shouldGetResourceByUniqueUuid() {
assertThat(result, notNullValue());
assertThat(result.getUuid(), is(COHORT_UUID));
assertThat(result.getName(), is(COHORT_NAME));
assertThat(result.getAttributes().size(), is(2));
}

@Test
Expand All @@ -97,6 +122,7 @@ public void shouldCreateNewResource() {
assertThat(newlyCreatedObject, notNullValue());
assertThat(newlyCreatedObject.getUuid(), is(COHORT_UUID));
assertThat(newlyCreatedObject.getName(), is(COHORT_NAME));
assertThat(newlyCreatedObject.getAttributes().size(), is(2));
}

@Test
Expand Down