Search in sources :

Example 1 with PatientProgram

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

the class ProgramWorkflowServiceUnitTest method purgePatientProgram_shouldFailGivenNonEmptyStatesAndTrueCascade.

@Test
public void purgePatientProgram_shouldFailGivenNonEmptyStatesAndTrueCascade() {
    exception.expect(APIException.class);
    exception.expectMessage("Cascade purging of PatientPrograms is not implemented yet");
    PatientProgram patientProgram = new PatientProgram();
    PatientState patientState = new PatientState();
    patientProgram.getStates().add(patientState);
    pws.purgePatientProgram(patientProgram, true);
}
Also used : PatientProgram(org.openmrs.PatientProgram) PatientState(org.openmrs.PatientState) Test(org.junit.Test)

Example 2 with PatientProgram

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

the class ProgramWorkflowServiceUnitTest method savePatientProgram_shouldFailForNullPatient.

@Test
public void savePatientProgram_shouldFailForNullPatient() {
    exception.expect(APIException.class);
    exception.expectMessage("PatientProgram requires a Patient and a Program");
    PatientProgram patientProgram = new PatientProgram(1);
    patientProgram.setProgram(new Program(1));
    pws.savePatientProgram(patientProgram);
}
Also used : PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) PatientProgram(org.openmrs.PatientProgram) Test(org.junit.Test)

Example 3 with PatientProgram

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

the class PatientServiceImpl method mergeProgramEnrolments.

private void mergeProgramEnrolments(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) {
    // copy all program enrollments
    ProgramWorkflowService programService = Context.getProgramWorkflowService();
    for (PatientProgram pp : programService.getPatientPrograms(notPreferred, null, null, null, null, null, false)) {
        if (!pp.getVoided()) {
            PatientProgram enroll = pp.copy();
            enroll.setPatient(preferred);
            log.debug("Copying patientProgram " + pp.getPatientProgramId() + " to " + preferred.getPatientId());
            PatientProgram persisted = programService.savePatientProgram(enroll);
            mergedData.addCreatedProgram(persisted.getUuid());
        }
    }
}
Also used : ProgramWorkflowService(org.openmrs.api.ProgramWorkflowService) PatientProgram(org.openmrs.PatientProgram)

Example 4 with PatientProgram

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

the class ProgramWorkflowServiceImpl method triggerStateConversion.

/**
 * @see org.openmrs.api.ProgramWorkflowService#triggerStateConversion(org.openmrs.Patient,
 *      org.openmrs.Concept, java.util.Date)
 */
public void triggerStateConversion(Patient patient, Concept trigger, Date dateConverted) {
    // Check input parameters
    if (patient == null) {
        throw new APIException("convert.state.invalid.patient", (Object[]) null);
    }
    if (trigger == null) {
        throw new APIException("convert.state.patient.without.valid.trigger", (Object[]) null);
    }
    if (dateConverted == null) {
        throw new APIException("convert.state.invalid.date", (Object[]) null);
    }
    for (PatientProgram patientProgram : getPatientPrograms(patient, null, null, null, null, null, false)) {
        // skip past patient programs that already completed
        if (patientProgram.getDateCompleted() == null) {
            Set<ProgramWorkflow> workflows = patientProgram.getProgram().getWorkflows();
            for (ProgramWorkflow workflow : workflows) {
                // (getWorkflows() is only returning over nonretired workflows)
                PatientState patientState = patientProgram.getCurrentState(workflow);
                // #1080 cannot exit patient from care
                // Should allow a transition from a null state to a terminal state
                // Or we should require a user to ALWAYS add an initial workflow/state when a patient is added to a program
                ProgramWorkflowState currentState = (patientState != null) ? patientState.getState() : null;
                ProgramWorkflowState transitionState = workflow.getState(trigger);
                log.debug("Transitioning from current state [" + currentState + "]");
                log.debug("|---> Transitioning to final state [" + transitionState + "]");
                if (transitionState != null && workflow.isLegalTransition(currentState, transitionState)) {
                    patientProgram.transitionToState(transitionState, dateConverted);
                    log.debug("State Conversion Triggered: patientProgram=" + patientProgram + " transition from " + currentState + " to " + transitionState + " on " + dateConverted);
                }
            }
            // #1068 - Exiting a patient from care causes "not-null property references
            // a null or transient value: org.openmrs.PatientState.dateCreated". Explicitly
            // calling the savePatientProgram() method will populate the metadata properties.
            // 
            // #1067 - We should explicitly save the patient program rather than let
            // Hibernate do so when it flushes the session.
            Context.getProgramWorkflowService().savePatientProgram(patientProgram);
        }
    }
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) APIException(org.openmrs.api.APIException) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) PatientProgram(org.openmrs.PatientProgram) PatientState(org.openmrs.PatientState)

Example 5 with PatientProgram

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

the class PatientProgramValidatorTest method validate_shouldFailIfThereIsMoreThanOnePatientStateWithTheSameStatesAndStartDates.

/**
 * @see PatientProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfThereIsMoreThanOnePatientStateWithTheSameStatesAndStartDates() {
    PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1);
    Set<PatientState> states = program.getStates();
    Assert.assertNotNull(states);
    PatientState patientState = states.iterator().next();
    PatientState duplicate = patientState.copy();
    states.add(duplicate);
    BindException errors = new BindException(program, "");
    new PatientProgramValidator().validate(program, errors);
    Assert.assertTrue(errors.hasFieldErrors("states"));
}
Also used : BindException(org.springframework.validation.BindException) PatientProgram(org.openmrs.PatientProgram) PatientState(org.openmrs.PatientState) 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