Search in sources :

Example 16 with CohortMembership

use of org.openmrs.CohortMembership in project openmrs-core by openmrs.

the class CohortServiceImpl method removePatientFromCohort.

/**
 * @see org.openmrs.api.CohortService#removePatientFromCohort(org.openmrs.Cohort,
 *      org.openmrs.Patient)
 */
@Override
public Cohort removePatientFromCohort(Cohort cohort, Patient patient) {
    List<CohortMembership> memberships = getCohortMemberships(patient.getPatientId(), null, false);
    List<CohortMembership> toVoid = memberships.stream().filter(m -> m.getCohort().equals(cohort)).collect(Collectors.toList());
    for (CohortMembership membership : toVoid) {
        Context.getCohortService().voidCohortMembership(membership, "removePatientFromCohort");
    }
    return cohort;
}
Also used : OpenmrsUtil(org.openmrs.util.OpenmrsUtil) Logger(org.slf4j.Logger) APIException(org.openmrs.api.APIException) Date(java.util.Date) Cohort(org.openmrs.Cohort) LoggerFactory(org.slf4j.LoggerFactory) PrivilegeConstants(org.openmrs.util.PrivilegeConstants) Collectors(java.util.stream.Collectors) CohortMembership(org.openmrs.CohortMembership) CohortService(org.openmrs.api.CohortService) List(java.util.List) DateUtil.truncateToSeconds(org.openmrs.util.DateUtil.truncateToSeconds) User(org.openmrs.User) CohortDAO(org.openmrs.api.db.CohortDAO) Patient(org.openmrs.Patient) Context(org.openmrs.api.context.Context) Transactional(org.springframework.transaction.annotation.Transactional) CohortMembership(org.openmrs.CohortMembership)

Example 17 with CohortMembership

use of org.openmrs.CohortMembership in project openmrs-core by openmrs.

the class CohortServiceImpl method notifyPatientUnvoided.

/**
 * @see org.openmrs.api.CohortService#notifyPatientUnvoided(Patient, User, Date)
 */
@Override
public void notifyPatientUnvoided(Patient patient, User originallyVoidedBy, Date originalDateVoided) throws APIException {
    List<CohortMembership> memberships = getCohortMemberships(patient.getPatientId(), null, true);
    List<CohortMembership> toUnvoid = memberships.stream().filter(m -> m.getVoided() && m.getVoidedBy().equals(originallyVoidedBy) && OpenmrsUtil.compare(truncateToSeconds(m.getDateVoided()), truncateToSeconds(originalDateVoided)) == 0).collect(Collectors.toList());
    for (CohortMembership member : toUnvoid) {
        member.setVoided(false);
        member.setDateVoided(null);
        member.setVoidedBy(null);
        member.setVoidReason(null);
        dao.saveCohortMembership(member);
    }
}
Also used : OpenmrsUtil(org.openmrs.util.OpenmrsUtil) Logger(org.slf4j.Logger) APIException(org.openmrs.api.APIException) Date(java.util.Date) Cohort(org.openmrs.Cohort) LoggerFactory(org.slf4j.LoggerFactory) PrivilegeConstants(org.openmrs.util.PrivilegeConstants) Collectors(java.util.stream.Collectors) CohortMembership(org.openmrs.CohortMembership) CohortService(org.openmrs.api.CohortService) List(java.util.List) DateUtil.truncateToSeconds(org.openmrs.util.DateUtil.truncateToSeconds) User(org.openmrs.User) CohortDAO(org.openmrs.api.db.CohortDAO) Patient(org.openmrs.Patient) Context(org.openmrs.api.context.Context) Transactional(org.springframework.transaction.annotation.Transactional) CohortMembership(org.openmrs.CohortMembership)

Example 18 with CohortMembership

use of org.openmrs.CohortMembership in project openmrs-core by openmrs.

the class PatientDataVoidHandlerTest method handle_shouldVoidCohortMemberships.

@Test
public void handle_shouldVoidCohortMemberships() throws Exception {
    // test a corner case by letting the same patient belong to the cohort for two separate periods
    CohortMembership membership1 = new CohortMembership(7, parseDate("2001-01-01", "yyyy-MM-dd"));
    membership1.setEndDate(parseDate("2001-12-31", "yyyy-MM-dd"));
    CohortMembership membership2 = new CohortMembership(7, parseDate("2017-01-01", "yyyy-MM-dd"));
    CohortMembership membership3 = new CohortMembership(8);
    Cohort cohort = new Cohort();
    cohort.setName("Cohort");
    cohort.setDescription("Description");
    cohort.setUuid(COHORT_UUID);
    cohort.addMembership(membership1);
    cohort.addMembership(membership2);
    cohort.addMembership(membership3);
    cohortService.saveCohort(cohort);
    PatientService patientService = Context.getPatientService();
    patientService.voidPatient(patientService.getPatient(7), "void reason");
    Collection<CohortMembership> memberships = cohortService.getCohortByUuid(COHORT_UUID).getMemberships(false);
    assertEquals(1, memberships.size());
    // patientId 7 was voided
    assertEquals(8, (int) memberships.iterator().next().getPatientId());
}
Also used : Cohort(org.openmrs.Cohort) PatientService(org.openmrs.api.PatientService) CohortMembership(org.openmrs.CohortMembership) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 19 with CohortMembership

use of org.openmrs.CohortMembership in project openmrs-core by openmrs.

the class CohortValidatorTest method setUp.

@Before
public void setUp() {
    validator = new CohortValidator();
    executeDataSet(COHORT_XML);
    cohort = Context.getCohortService().getCohort(2);
    patient = Context.getPatientService().getPatient(7);
    cohortMembership = new CohortMembership(patient.getPatientId());
    cohort.addMembership(cohortMembership);
    errors = new BindException(cohort, "cohort");
}
Also used : BindException(org.springframework.validation.BindException) CohortMembership(org.openmrs.CohortMembership) Before(org.junit.Before)

Example 20 with CohortMembership

use of org.openmrs.CohortMembership in project openmrs-core by openmrs.

the class CohortServiceTest method patientUnvoided_shouldUnvoidMemberships.

@Test
public void patientUnvoided_shouldUnvoidMemberships() {
    executeDataSet(COHORT_XML);
    Cohort cohort = Context.getCohortService().getCohort(2);
    Patient unvoidedPatient = new Patient(7);
    User voidedBy = Context.getAuthenticatedUser();
    Date dateVoided = new Date();
    String voidReason = "Associated patient is voided";
    CohortMembership voidedMembership = new CohortMembership(unvoidedPatient.getPatientId());
    cohort.addMembership(voidedMembership);
    voidedMembership.setVoided(true);
    voidedMembership.setVoidedBy(voidedBy);
    voidedMembership.setDateVoided(dateVoided);
    voidedMembership.setVoidReason(voidReason);
    service.notifyPatientUnvoided(unvoidedPatient, voidedBy, dateVoided);
    assertFalse(voidedMembership.getVoided());
    assertNull(voidedMembership.getVoidedBy());
    assertNull(voidedMembership.getDateVoided());
    assertNull(voidedMembership.getVoidReason());
}
Also used : User(org.openmrs.User) 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)

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