use of org.openmrs.CohortMembership in project openmrs-core by openmrs.
the class CohortServiceTest method patientVoided_shouldVoidMemberships.
@Test
public void patientVoided_shouldVoidMemberships() {
executeDataSet(COHORT_XML);
Cohort cohort = Context.getCohortService().getCohort(2);
Patient voidedPatient = new Patient(7);
voidedPatient.setVoided(true);
voidedPatient.setDateVoided(new Date());
voidedPatient.setVoidedBy(Context.getAuthenticatedUser());
voidedPatient.setVoidReason("Voided as a result of the associated patient getting voided");
CohortMembership newMemberContainingVoidedPatient = new CohortMembership(voidedPatient.getPatientId());
cohort.addMembership(newMemberContainingVoidedPatient);
assertTrue(cohort.contains(voidedPatient.getPatientId()));
assertEquals(1, service.getCohortsContainingPatientId(voidedPatient.getId()).size());
service.notifyPatientVoided(voidedPatient);
assertTrue(newMemberContainingVoidedPatient.getVoided());
assertEquals(newMemberContainingVoidedPatient.getDateVoided(), voidedPatient.getDateVoided());
assertEquals(newMemberContainingVoidedPatient.getVoidedBy(), voidedPatient.getVoidedBy());
assertEquals(newMemberContainingVoidedPatient.getVoidReason(), voidedPatient.getVoidReason());
}
use of org.openmrs.CohortMembership in project openmrs-core by openmrs.
the class CohortValidator method validate.
@Override
public void validate(Object obj, Errors errors) {
if (obj == null || !(obj instanceof Cohort)) {
throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + Cohort.class);
}
Cohort cohort = (Cohort) obj;
if (!cohort.getVoided()) {
Collection<CohortMembership> members = cohort.getMemberships();
if (!CollectionUtils.isEmpty(members)) {
for (CohortMembership member : members) {
Patient p = Context.getPatientService().getPatient(member.getPatientId());
int dateCompare = OpenmrsUtil.compareWithNullAsLatest(member.getStartDate(), member.getEndDate());
if (p != null && p.getVoided() && !member.getVoided()) {
String message = "Patient " + p.getPatientId() + " is voided, cannot add voided members to a cohort";
errors.rejectValue("memberships", "Cohort.patientAndMemberShouldBeVoided", message);
}
if (dateCompare == 1) {
String message = "Start date is null or end date is before start date";
errors.rejectValue("memberships", "Cohort.startDateShouldNotBeNullOrBeforeEndDate", message);
}
}
}
}
}
use of org.openmrs.CohortMembership in project openmrs-core by openmrs.
the class CohortValidatorTest method validate_shouldPassIfMembershipStartDateIsAfterEndDate.
@Test
public void validate_shouldPassIfMembershipStartDateIsAfterEndDate() throws Exception {
Cohort cohort = new Cohort(2);
CohortMembership membership = new CohortMembership(patient.getPatientId());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = dateFormat.parse("2016-11-01 00:00:00");
Date endDate = dateFormat.parse("2015-01-01 00:00:00");
membership.setStartDate(startDate);
membership.setEndDate(endDate);
Errors errors = new BindException(cohort, "cohort");
new CohortValidator().validate(cohort, errors);
Assert.assertFalse(errors.hasFieldErrors("memberships"));
}
use of org.openmrs.CohortMembership in project openmrs-core by openmrs.
the class CohortServiceImpl method addPatientToCohort.
/**
* @see org.openmrs.api.CohortService#addPatientToCohort(org.openmrs.Cohort,
* org.openmrs.Patient)
*/
@Override
public Cohort addPatientToCohort(Cohort cohort, Patient patient) {
if (!cohort.contains(patient.getPatientId())) {
CohortMembership cohortMembership = new CohortMembership(patient.getPatientId());
cohort.addMembership(cohortMembership);
Context.getCohortService().saveCohort(cohort);
}
return cohort;
}
use of org.openmrs.CohortMembership in project openmrs-core by openmrs.
the class PatientDataUnvoidHandlerTest method handle_shouldUnvoidMembersAssociatedWithThePatient.
/**
* @verifies unvoid the members associated with the patient
* @see PatientDataUnvoidHandler#handle(Patient,User,Date,String)
*/
@Test
public void handle_shouldUnvoidMembersAssociatedWithThePatient() throws Exception {
executeDataSet(COHORT_XML);
CohortService cs = Context.getCohortService();
Cohort cohort = cs.getCohort(2);
CohortMembership otherMembership = cohort.getMemberships().iterator().next();
Patient patient = Context.getPatientService().getPatient(7);
CohortMembership membership = new CohortMembership(patient.getPatientId());
membership.setStartDate(parseDate("2001-01-01", "yyyy-MM-dd"));
membership.setEndDate(parseDate("2001-12-31", "yyyy-MM-dd"));
cohort.addMembership(membership);
cs.saveCohort(cohort);
patient = Context.getPatientService().voidPatient(patient, "Void Reason");
assertTrue(membership.getVoided());
assertFalse(otherMembership.getVoided());
Context.getPatientService().unvoidPatient(patient);
assertFalse(membership.getVoided());
assertNull(membership.getDateVoided());
assertNull(membership.getVoidedBy());
assertNull(membership.getVoidReason());
}
Aggregations