Search in sources :

Example 6 with PatientService

use of org.openmrs.api.PatientService 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 7 with PatientService

use of org.openmrs.api.PatientService in project openmrs-core by openmrs.

the class PatientIdentifierValidatorTest method validateIdentifier_shouldPassIfInUseAndIdTypeUniquenessIsSetToNonUnique.

/**
 * @see PatientIdentifierValidator#validateIdentifier(PatientIdentifier)
 */
@Test
public void validateIdentifier_shouldPassIfInUseAndIdTypeUniquenessIsSetToNonUnique() {
    PatientService patientService = Context.getPatientService();
    PatientIdentifier duplicateId = patientService.getPatientIdentifier(1);
    Assert.assertNotNull(duplicateId.getLocation());
    PatientIdentifierType idType = duplicateId.getIdentifierType();
    idType.setUniquenessBehavior(UniquenessBehavior.NON_UNIQUE);
    patientService.savePatientIdentifierType(idType);
    PatientIdentifier pi = new PatientIdentifier(duplicateId.getIdentifier(), idType, duplicateId.getLocation());
    PatientIdentifierValidator.validateIdentifier(pi);
}
Also used : PatientService(org.openmrs.api.PatientService) PatientIdentifier(org.openmrs.PatientIdentifier) PatientIdentifierType(org.openmrs.PatientIdentifierType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with PatientService

use of org.openmrs.api.PatientService in project openmrs-core by openmrs.

the class MigrationHelper method importProgramsAndStatuses.

public static int importProgramsAndStatuses(List<String> programWorkflow) throws ParseException {
    ProgramWorkflowService pws = Context.getProgramWorkflowService();
    PatientService ps = Context.getPatientService();
    List<PatientProgram> patientPrograms = new ArrayList<>();
    Map<String, PatientProgram> knownPatientPrograms = new HashMap<>();
    Map<String, Program> programsByName = new HashMap<>();
    for (Program program : pws.getAllPrograms()) {
        programsByName.put(program.getConcept().getName(Context.getLocale(), false).getName(), program);
    }
    for (String s : programWorkflow) {
        // ENROLLMENT:HIVEMR-V1,9266,IMB HIV PROGRAM,2005-08-25,
        log.debug(s);
        if (s.startsWith("ENROLLMENT:")) {
            s = s.substring(s.indexOf(":") + 1);
            String[] temp = s.split(",");
            PatientIdentifierType pit = ps.getPatientIdentifierTypeByName(temp[0]);
            String identifier = temp[1];
            List<PatientIdentifier> pis = ps.getPatientIdentifiers(identifier, Collections.singletonList(pit), null, null, null);
            if (pis.size() != 1) {
                throw new IllegalArgumentException("Found " + pis.size() + " instances of identifier " + identifier + " of type " + pit);
            }
            Patient p = pis.get(0).getPatient();
            Program program = programsByName.get(temp[2]);
            if (program == null) {
                throw new RuntimeException("Couldn't find program \"" + temp[2] + "\" in " + programsByName);
            }
            Date enrollmentDate = temp.length < 4 ? null : parseDate(temp[3]);
            Date completionDate = temp.length < 5 ? null : parseDate(temp[4]);
            PatientProgram pp = new PatientProgram();
            pp.setPatient(p);
            pp.setProgram(program);
            pp.setDateEnrolled(enrollmentDate);
            pp.setDateCompleted(completionDate);
            patientPrograms.add(pp);
            // "HIVEMR-V1,9266,IMB HIV PROGRAM"
            knownPatientPrograms.put(temp[0] + "," + temp[1] + "," + temp[2], pp);
        } else if (s.startsWith("STATUS:")) {
            // STATUS:HIVEMR-V1,9266,IMB HIV PROGRAM,TREATMENT STATUS,ACTIVE,2005-08-25,,
            s = s.substring(s.indexOf(":") + 1);
            String[] temp = s.split(",");
            Program program = programsByName.get(temp[2]);
            if (program == null) {
                throw new RuntimeException("Couldn't find program \"" + temp[2] + "\" in " + programsByName);
            }
            ProgramWorkflow wf = program.getWorkflowByName(temp[3]);
            if (wf == null) {
                throw new RuntimeException("Couldn't find workflow \"" + temp[3] + "\" for program " + program + " (in " + program.getAllWorkflows() + ")");
            }
            ProgramWorkflowState st = wf.getStateByName(temp[4]);
            if (st == null) {
                throw new RuntimeException("Couldn't find state \"" + temp[4] + "\" for workflow " + wf + " (in " + wf.getStates() + ")");
            }
            Date startDate = temp.length < 6 ? null : parseDate(temp[5]);
            Date endDate = temp.length < 7 ? null : parseDate(temp[6]);
            PatientState state = new PatientState();
            PatientProgram pp = knownPatientPrograms.get(temp[0] + "," + temp[1] + "," + temp[2]);
            state.setPatientProgram(pp);
            state.setState(st);
            state.setStartDate(startDate);
            state.setEndDate(endDate);
            pp.getStates().add(state);
        }
    }
    int numAdded = 0;
    for (PatientProgram pp : knownPatientPrograms.values()) {
        pws.savePatientProgram(pp);
        ++numAdded;
    }
    return numAdded;
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) HashMap(java.util.HashMap) ProgramWorkflowService(org.openmrs.api.ProgramWorkflowService) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) PatientProgram(org.openmrs.PatientProgram) PatientIdentifier(org.openmrs.PatientIdentifier) Date(java.util.Date) PatientState(org.openmrs.PatientState) PatientService(org.openmrs.api.PatientService) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) PatientIdentifierType(org.openmrs.PatientIdentifierType)

Aggregations

PatientService (org.openmrs.api.PatientService)8 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PatientIdentifier (org.openmrs.PatientIdentifier)3 PatientIdentifierType (org.openmrs.PatientIdentifierType)3 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)3 Patient (org.openmrs.Patient)2 Person (org.openmrs.Person)2 EncounterService (org.openmrs.api.EncounterService)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Random (java.util.Random)1 Before (org.junit.Before)1 Cohort (org.openmrs.Cohort)1 CohortMembership (org.openmrs.CohortMembership)1 Encounter (org.openmrs.Encounter)1 Obs (org.openmrs.Obs)1 PatientProgram (org.openmrs.PatientProgram)1 PatientState (org.openmrs.PatientState)1