Search in sources :

Example 16 with PatientProgram

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

the class ProgramWorkflowServiceTest method triggerStateConversion_shouldSkipPastPatientProgramsThatAreAlreadyCompleted.

/**
 * @throws InterruptedException
 * @see ProgramWorkflowService#triggerStateConversion(Patient,Concept,Date)
 */
@Test
public void triggerStateConversion_shouldSkipPastPatientProgramsThatAreAlreadyCompleted() throws InterruptedException {
    Integer patientProgramId = 1;
    PatientProgram pp = pws.getPatientProgram(patientProgramId);
    Date originalDateCompleted = new Date();
    pp.setDateCompleted(originalDateCompleted);
    pp = pws.savePatientProgram(pp);
    Concept diedConcept = cs.getConcept(16);
    // sanity check to ensure the patient died is a possible state in one of the work flows
    Assert.assertNotNull(pp.getProgram().getWorkflow(1).getState(diedConcept));
    // delay so that we have a time difference
    Thread.sleep(10);
    pp = pws.getPatientProgram(patientProgramId);
    Assert.assertEquals(originalDateCompleted, pp.getDateCompleted());
}
Also used : Concept(org.openmrs.Concept) PatientProgram(org.openmrs.PatientProgram) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 17 with PatientProgram

use of org.openmrs.PatientProgram in project openmrs-module-pihcore by PIH.

the class ProgramDashboardPageController method controller.

public Redirect controller(@RequestParam(value = "patientId") Patient patient) {
    String providerName = "coreapps";
    String pageName = "clinicianfacing/patient";
    String queryString = "patientId=" + patient.getUuid();
    List<PatientProgram> activeEnrollments = new ArrayList<>();
    ProgramWorkflowService pws = Context.getProgramWorkflowService();
    for (PatientProgram pp : pws.getPatientPrograms(patient, null, null, null, null, null, false)) {
        if (pp.getActive()) {
            activeEnrollments.add(pp);
        }
    }
    if (activeEnrollments.size() == 1) {
        // TODO: Once we have legitimate dashboards for all programs, remove this constraint on HIV program?
        String programUuid = activeEnrollments.get(0).getProgram().getUuid();
        if (programUuid.equals(PihEmrConfigConstants.PROGRAM_HIV_UUID)) {
            queryString += "&dashboard=" + programUuid;
        }
    }
    return new Redirect(providerName, pageName, queryString);
}
Also used : ProgramWorkflowService(org.openmrs.api.ProgramWorkflowService) ArrayList(java.util.ArrayList) Redirect(org.openmrs.ui.framework.page.Redirect) PatientProgram(org.openmrs.PatientProgram)

Example 18 with PatientProgram

use of org.openmrs.PatientProgram in project openmrs-module-coreapps by openmrs.

the class ProgramHistoryFragmentController method controller.

public void controller(FragmentConfiguration config, @FragmentParam("app") AppDescriptor app, @InjectBeans PatientDomainWrapper patientWrapper, @SpringBean("programWorkflowService") ProgramWorkflowService programWorkflowService) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Object patient = null;
    patient = config.get("patient");
    if (patient == null) {
        patient = config.get("patientId");
    }
    if (patient == null) {
        config.require("patient");
    }
    if (patient instanceof Patient) {
        patientWrapper.setPatient((Patient) patient);
    } else if (patient instanceof PatientDomainWrapper) {
        patientWrapper = (PatientDomainWrapper) patient;
    } else if (patient instanceof Integer) {
        // assume we have patientId
        patientWrapper.setPatient(Context.getPatientService().getPatient((Integer) patient));
    } else {
        throw new IllegalArgumentException("Patient must be of type Patient or PatientDomainWrapper");
    }
    ObjectNode appConfig = app.getConfig();
    appConfig.put("patientUuid", patientWrapper.getPatient().getUuid());
    if (appConfig.get("program") == null || StringUtils.isEmpty(appConfig.get("program").getTextValue())) {
        throw new MissingPropertyException("Program must be specified");
    }
    Program program = programWorkflowService.getProgramByUuid(appConfig.get("program").getTextValue());
    if (program == null) {
        throw new MissingPropertyException("Invalid program");
    }
    List<PatientProgram> patientPrograms = programWorkflowService.getPatientPrograms(patientWrapper.getPatient(), program, null, null, null, null, false);
    // TODO: assumption here is that "getPatientPrograms" returns results in chronological order--need to confirm?
    Collections.reverse(patientPrograms);
    List<String> programJson = new ArrayList<String>();
    Boolean includeActive = appConfig.get("includeActive") != null ? appConfig.get("includeActive").getBooleanValue() : true;
    Boolean includeInactive = appConfig.get("includeInactive") != null ? appConfig.get("includeInactive").getBooleanValue() : true;
    for (PatientProgram patientProgram : patientPrograms) {
        if ((patientProgram.getDateCompleted() == null && includeActive) || (patientProgram.getDateCompleted() != null && includeInactive)) {
            appConfig.put("patientProgram", patientProgram.getUuid());
            programJson.add(appConfig.toString().replace('\"', '\''));
        }
    }
    Map<String, Object> appConfigMap = mapper.convertValue(appConfig, Map.class);
    config.merge(appConfigMap);
    config.addAttribute("programJson", programJson);
}
Also used : PatientDomainWrapper(org.openmrs.module.emrapi.patient.PatientDomainWrapper) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) MissingPropertyException(groovy.lang.MissingPropertyException) PatientProgram(org.openmrs.PatientProgram) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 19 with PatientProgram

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

the class OpenmrsServiceTest method shouldCheckThatAMethodIsNotRolledBackInCaseOfAnErrorInAnotherInvokedInsideIt.

/**
 * Tests that if two service methods are called (one from inside the other) the first one will
 * be rolled back if an exception is thrown inside the second one. <pre>
 * We are testing with the merge patient method since it is transactional and calls multiple other
 * transactional methods
 * </pre>
 *
 * @throws SerializationException
 */
@Test
@Ignore
public void shouldCheckThatAMethodIsNotRolledBackInCaseOfAnErrorInAnotherInvokedInsideIt() throws SerializationException {
    // TODO FIx why this test fails when run with other tests
    PatientService patientService = Context.getPatientService();
    EncounterService encounterService = Context.getEncounterService();
    ProgramWorkflowService programService = Context.getProgramWorkflowService();
    Patient prefPatient = patientService.getPatient(6);
    Patient notPrefPatient = patientService.getPatient(7);
    Collection<Program> programs = programService.getAllPrograms(false);
    int originalPrefEncounterCount = encounterService.getEncountersByPatient(prefPatient).size();
    int originalNotPrefEncounterCount = encounterService.getEncountersByPatient(notPrefPatient).size();
    Assert.assertTrue(originalNotPrefEncounterCount > 0);
    Cohort notPreferredCohort = new Cohort(notPrefPatient.getPatientId().toString());
    List<PatientProgram> notPrefPrograms = programService.getPatientPrograms(notPreferredCohort, programs);
    Assert.assertTrue(notPrefPrograms.size() > 0);
    // Set the program to null so that the patient program is rejected on validation with
    // an APIException, since it is a RuntimeException, all transactions should be rolled back
    notPrefPrograms.get(0).setProgram(null);
    boolean failed = false;
    try {
        patientService.mergePatients(prefPatient, notPrefPatient);
    } catch (APIException e) {
        // should have failed to force a rollback
        failed = true;
    }
    Assert.assertTrue(failed);
    // Since the encounters are moved first, that logic should have been rolled back
    Assert.assertEquals(originalPrefEncounterCount, encounterService.getEncountersByPatient(prefPatient).size());
}
Also used : PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) Cohort(org.openmrs.Cohort) Patient(org.openmrs.Patient) PatientProgram(org.openmrs.PatientProgram) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with PatientProgram

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

the class ProgramWorkflowServiceTest method getPatientProgramByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see ProgramWorkflowService#getPatientProgramByUuid(String)
 */
@Test
public void getPatientProgramByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "2edf272c-bf05-4208-9f93-2fa213ed0415";
    PatientProgram patientProgram = Context.getProgramWorkflowService().getPatientProgramByUuid(uuid);
    Assert.assertEquals(2, (int) patientProgram.getPatientProgramId());
}
Also used : PatientProgram(org.openmrs.PatientProgram) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

PatientProgram (org.openmrs.PatientProgram)37 Test (org.junit.Test)30 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)27 BindException (org.springframework.validation.BindException)18 PatientState (org.openmrs.PatientState)17 Date (java.util.Date)14 Patient (org.openmrs.Patient)11 ProgramWorkflowService (org.openmrs.api.ProgramWorkflowService)7 Program (org.openmrs.Program)6 ProgramWorkflow (org.openmrs.ProgramWorkflow)5 ArrayList (java.util.ArrayList)3 ProgramWorkflowState (org.openmrs.ProgramWorkflowState)3 MissingPropertyException (groovy.lang.MissingPropertyException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1 DateTime (org.joda.time.DateTime)1