-
Notifications
You must be signed in to change notification settings - Fork 4.1k
TRUNK-5837: Migrate Cohort Domain from Hibernate Mapping XML to JPA annotations. #4892
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
2ac6eab
7e782ca
a382fb6
f984bdd
28e3962
797cb9f
4b477fd
5209a9f
f0acb37
bd0d5ce
3182742
8e4412c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) | ||
| private String name; | ||
|
|
||
| @Column(name = "description", nullable = false) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
dilankavishka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public Cohort() { | ||
| memberships = new TreeSet<>(); | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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) { | ||
|
|
@@ -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<>(); | ||
| } | ||
|
|
@@ -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()); | ||
| } | ||
|
|
||
|
|
@@ -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(); | ||
| } | ||
|
|
||
|
|
@@ -348,13 +370,13 @@ public void setMemberIds(Set<Integer> memberIds) { | |
| } | ||
| } | ||
|
|
||
| public void setMemberships(Collection<CohortMembership> members) { | ||
| public void setMemberships(Set<CohortMembership> members) { | ||
dilankavishka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.memberships = members; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
dilankavishka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| @Override | ||
| public Integer getId() { | ||
|
|
@@ -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) { | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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); | ||
|
|
@@ -65,7 +62,7 @@ public void constructorWithCommaSeparatedIntegers_shouldAddMembersToCohort() { | |
| @Test | ||
| public void getCommaSeparatedPatientIds_shouldReturnCommaSeparatedListOfPatients() { | ||
|
|
||
| List<Patient> patients = new ArrayList<>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
@@ -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)); | ||
|
|
||
There was a problem hiding this comment.
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?