Skip to content
Open
50 changes: 36 additions & 14 deletions api/src/main/java/org/openmrs/Cohort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
package org.openmrs;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.envers.Audited;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -22,18 +31,31 @@
/**
* This class represents a list of patientIds.
*/
@Entity
@Table(name = "cohort")
@Audited
public class Cohort extends BaseChangeableOpenmrsData {

public static final long serialVersionUID = 0L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "cohort_id_seq")
@GenericGenerator(
name = "cohort_id_seq",
strategy = "native",
parameters = @Parameter(name = "sequence", value = "cohort_cohort_id_seq")
)
@Column(name = "cohort_id", nullable = false)
private Integer cohortId;

@Column(name = "name", nullable = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you just forget to specify the length as was in the xml mapping file?

private String name;

@Column(name = "description", nullable = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the length?

private String description;

private Collection<CohortMembership> memberships;
@OneToMany(mappedBy = "cohort", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<CohortMembership> memberships;

public Cohort() {
memberships = new TreeSet<>();
Expand Down Expand Up @@ -89,7 +111,7 @@ public Cohort(String name, String description, Patient[] patients) {
* @param patientsOrIds optional collection which may contain Patients, or patientIds which may
* be Integers, Strings, or anything whose toString() can be parsed to an Integer.
*/
public Cohort(Collection<?> patientsOrIds) {
public Cohort(Set<?> patientsOrIds) {
this(null, null, patientsOrIds);
}

Expand All @@ -102,7 +124,7 @@ public Cohort(Collection<?> patientsOrIds) {
* @param patientsOrIds optional collection which may contain Patients, or patientIds which may
* be Integers, Strings, or anything whose toString() can be parsed to an Integer.
*/
public Cohort(String name, String description, Collection<?> patientsOrIds) {
public Cohort(String name, String description, Set<?> patientsOrIds) {
this(name, description, (Integer[]) null);
if (patientsOrIds != null) {
for (Object o : patientsOrIds) {
Expand Down Expand Up @@ -181,17 +203,17 @@ public boolean removeMembership(CohortMembership cohortMembership) {
* @param includeVoided boolean true/false to include/exclude voided memberships
* @return Collection of cohort memberships
*/
public Collection<CohortMembership> getMemberships(boolean includeVoided) {
public Set<CohortMembership> getMemberships(boolean includeVoided) {
if (includeVoided) {
return getMemberships();
}
return getMemberships().stream().filter(m -> m.getVoided() == includeVoided).collect(Collectors.toList());
return getMemberships().stream().filter(m -> m.getVoided() == includeVoided).collect(Collectors.toSet());
}

/**
* @since 2.1.0
*/
public Collection<CohortMembership> getMemberships() {
public Set<CohortMembership> getMemberships() {
if (memberships == null) {
memberships = new TreeSet<>();
}
Expand All @@ -203,11 +225,11 @@ public Collection<CohortMembership> getMemberships() {
* @param asOfDate date used to return active memberships
* @return Collection of cohort memberships
*/
public Collection<CohortMembership> getActiveMemberships(Date asOfDate) {
return getMemberships().stream().filter(m -> m.isActive(asOfDate)).collect(Collectors.toList());
public Set<CohortMembership> getActiveMemberships(Date asOfDate) {
return getMemberships().stream().filter(m -> m.isActive(asOfDate)).collect(Collectors.toSet());
}

public Collection<CohortMembership> getActiveMemberships() {
public Set<CohortMembership> getActiveMemberships() {
return getActiveMemberships(new Date());
}

Expand All @@ -219,7 +241,7 @@ public CohortMembership getActiveMembership(Patient patient) {
}

public int size() {
return getMemberships().stream().filter(m -> !m.getVoided()).collect(Collectors.toList())
return getMemberships().stream().filter(m -> !m.getVoided()).collect(Collectors.toSet())
.size();
}

Expand Down Expand Up @@ -348,13 +370,13 @@ public void setMemberIds(Set<Integer> memberIds) {
}
}

public void setMemberships(Collection<CohortMembership> members) {
public void setMemberships(Set<CohortMembership> members) {
this.memberships = members;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something minor. Do we need the space here :) ?

}

/**
* @since 1.5
* @see org.openmrs.OpenmrsObject#getId()
* @see OpenmrsObject#getId()
*/
@Override
public Integer getId() {
Expand All @@ -364,7 +386,7 @@ public Integer getId() {

/**
* @since 1.5
* @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
* @see OpenmrsObject#setId(Integer)
*/
@Override
public void setId(Integer id) {
Expand Down
1 change: 0 additions & 1 deletion api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
<mapping resource="org/openmrs/api/db/hibernate/Program.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/ProgramWorkflow.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/ProgramWorkflowState.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Cohort.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/CohortMembership.hbm.xml"/>
<mapping resource="org/openmrs/api/db/hibernate/OrderFrequency.hbm.xml" />

Expand Down
55 changes: 0 additions & 55 deletions api/src/main/resources/org/openmrs/api/db/hibernate/Cohort.hbm.xml

This file was deleted.

13 changes: 5 additions & 8 deletions api/src/test/java/org/openmrs/CohortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
Expand All @@ -46,7 +43,7 @@ public void constructorWithIntegers_shouldAddMembersToCohort() {
@Test
public void constructorWithPatients_shouldAddMembersToCohort() {

List<Patient> patients = new ArrayList<>();
Set<Patient> patients = new HashSet<>();
Arrays.stream(ids).forEach(id -> patients.add(new Patient(id)));

Cohort cohort = new Cohort("name", "description", patients);
Expand All @@ -65,7 +62,7 @@ public void constructorWithCommaSeparatedIntegers_shouldAddMembersToCohort() {
@Test
public void getCommaSeparatedPatientIds_shouldReturnCommaSeparatedListOfPatients() {

List<Patient> patients = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change required as part of the ticket?

Set<Patient> patients = new HashSet<>();
Arrays.stream(ids).forEach(id -> patients.add(new Patient(id)));

Cohort cohort = new Cohort("name", "description", patients);
Expand Down Expand Up @@ -94,7 +91,7 @@ public void union_shouldContainVoidedAndExpiredMemberships() throws Exception {
cohortTwo.addMembership(membershipTwo);

Cohort cohortUnion = Cohort.union(cohortOne, cohortTwo);
Collection<CohortMembership> unionOfMemberships = cohortUnion.getMemberships();
Set<CohortMembership> unionOfMemberships = cohortUnion.getMemberships();
unionOfMemberships.forEach(m -> {
assertTrue(m.getPatientId().equals(7) || m.getPatientId().equals(8));
assertTrue(m.getVoided() && m.getEndDate() != null);
Expand All @@ -120,7 +117,7 @@ public void subtract_shouldContainVoidedAndExpiredMemberships() throws Exception
cohortTwo.addMembership(membershipTwo);

Cohort cohortSubtract = Cohort.subtract(cohortOne, cohortTwo);
Collection<CohortMembership> subtractOfMemberships = cohortSubtract.getMemberships();
Set<CohortMembership> subtractOfMemberships = cohortSubtract.getMemberships();
subtractOfMemberships.forEach(m -> {
assertTrue(m.getPatientId().equals(7));
assertTrue(m.getVoided() && m.getEndDate() != null);
Expand All @@ -147,7 +144,7 @@ public void intersect_shouldContainVoidedAndExpiredMemberships() throws Exceptio
cohortTwo.addMembership(membershipTwo);

Cohort cohortIntersect = Cohort.intersect(cohortOne, cohortTwo);
Collection<CohortMembership> intersectOfMemberships = cohortIntersect.getMemberships();
Set<CohortMembership> intersectOfMemberships = cohortIntersect.getMemberships();
assertTrue(intersectOfMemberships.stream().anyMatch(m -> m.getVoided() || m.getEndDate() != null));
intersectOfMemberships.forEach(m -> {
assertTrue(m.getPatientId().equals(7));
Expand Down
11 changes: 6 additions & 5 deletions api/src/test/java/org/openmrs/api/CohortServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
//import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.time.DateUtils;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -677,7 +678,7 @@ public void getMemberships_shouldGetMembershipsAsOfADate() throws ParseException
cohort.addMembership(newMember);
service.saveCohort(cohort);

Collection<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest);
Set<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest);
assertFalse(membersAsOfDate.isEmpty());
assertTrue(membersAsOfDate.stream().anyMatch(m -> m.getStartDate().equals(dateToTest)));
}
Expand All @@ -700,7 +701,7 @@ public void getMemberships_shouldNotGetMatchingMembershipsAsOfADate() throws Exc
service.saveCohort(cohort);

Date dateToTest = dateFormat.parse("2016-11-01 00:00:00");
Collection<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest);
Set<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest);
assertFalse(membersAsOfDate.stream().anyMatch(m -> m.getStartDate().equals(dateToTest)));
}

Expand All @@ -724,7 +725,7 @@ public void getMemberships_shouldReturnVoidedMemberships() throws Exception {
cohort.addMembership(voidedMembership);

Context.getCohortService().saveCohort(cohort);
Collection<CohortMembership> allMemberships = cohort.getMemberships(true);
Set<CohortMembership> allMemberships = cohort.getMemberships(true);
assertEquals(3, allMemberships.size());
}

Expand All @@ -749,7 +750,7 @@ public void getMemberships_shouldReturnUnvoidedMemberships() throws Exception {
cohort.addMembership(voidedMembership);

Context.getCohortService().saveCohort(cohort);
Collection<CohortMembership> unvoidedMemberships = cohort.getMemberships(false);
Set<CohortMembership> unvoidedMemberships = cohort.getMemberships(false);
assertEquals(2, unvoidedMemberships.size());
}

Expand Down
2 changes: 2 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.openmrs.Allergy;
import org.openmrs.CareSetting;
import org.openmrs.Cohort;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptDatatype;
Expand Down Expand Up @@ -2740,6 +2741,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th
.addAnnotatedClass(ProgramAttributeType.class)
.addAnnotatedClass(HL7InError.class)
.addAnnotatedClass(OrderType.class)
.addAnnotatedClass(Cohort.class)
.getMetadataBuilder().build();


Expand Down