Search in sources :

Example 11 with CohortMembership

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());
}
Also used : Cohort(org.openmrs.Cohort) Patient(org.openmrs.Patient) Date(java.util.Date) CohortMembership(org.openmrs.CohortMembership) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with CohortMembership

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);
                }
            }
        }
    }
}
Also used : Cohort(org.openmrs.Cohort) Patient(org.openmrs.Patient) CohortMembership(org.openmrs.CohortMembership)

Example 13 with CohortMembership

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"));
}
Also used : Errors(org.springframework.validation.Errors) Cohort(org.openmrs.Cohort) BindException(org.springframework.validation.BindException) SimpleDateFormat(java.text.SimpleDateFormat) CohortMembership(org.openmrs.CohortMembership) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with CohortMembership

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;
}
Also used : CohortMembership(org.openmrs.CohortMembership)

Example 15 with CohortMembership

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());
}
Also used : CohortService(org.openmrs.api.CohortService) Cohort(org.openmrs.Cohort) Patient(org.openmrs.Patient) CohortMembership(org.openmrs.CohortMembership) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

CohortMembership (org.openmrs.CohortMembership)21 Test (org.junit.Test)16 Cohort (org.openmrs.Cohort)16 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)16 Date (java.util.Date)13 Patient (org.openmrs.Patient)9 User (org.openmrs.User)5 SimpleDateFormat (java.text.SimpleDateFormat)4 List (java.util.List)4 Context (org.openmrs.api.context.Context)4 Before (org.junit.Before)3 CohortService (org.openmrs.api.CohortService)3 ParseException (java.text.ParseException)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2 DateUtils (org.apache.commons.lang3.time.DateUtils)2 Is.is (org.hamcrest.core.Is.is)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2