Search in sources :

Example 1 with ProgramWorkflowState

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

the class ProgramWorkflowServiceImpl method unretireProgram.

/**
 * @see org.openmrs.api.ProgramWorkflowService#retireProgram(org.openmrs.Program)
 */
@Override
public Program unretireProgram(Program program) throws APIException {
    Date lastModifiedDate = program.getDateChanged();
    program.setRetired(false);
    for (ProgramWorkflow workflow : program.getAllWorkflows()) {
        if (lastModifiedDate != null && lastModifiedDate.equals(workflow.getDateChanged())) {
            workflow.setRetired(false);
            for (ProgramWorkflowState state : workflow.getStates()) {
                if (lastModifiedDate.equals(state.getDateChanged())) {
                    state.setRetired(false);
                }
            }
        }
    }
    return saveProgram(program);
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) Date(java.util.Date)

Example 2 with ProgramWorkflowState

use of org.openmrs.ProgramWorkflowState 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 3 with ProgramWorkflowState

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

the class ProgramWorkflowServiceTest method getStateByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see ProgramWorkflowService#getStateByUuid(String)
 */
@Test
public void getStateByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "92584cdc-6a20-4c84-a659-e035e45d36b0";
    ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(uuid);
    Assert.assertEquals(1, (int) state.getProgramWorkflowStateId());
}
Also used : ProgramWorkflowState(org.openmrs.ProgramWorkflowState) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with ProgramWorkflowState

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

the class ProgramWorkflowServiceTest method retireProgram_shouldSaveTheRetiredProgramWithReason.

@Test
public void retireProgram_shouldSaveTheRetiredProgramWithReason() throws APIException {
    String reason = "Feeling well.";
    String uuid = "eae98b4c-e195-403b-b34a-82d94103b2c0";
    Program program = Context.getProgramWorkflowService().getProgramByUuid(uuid);
    Program retireProgram = pws.retireProgram(program, reason);
    assertTrue(retireProgram.getRetired());
    assertEquals(reason, retireProgram.getRetireReason());
    for (ProgramWorkflow programWorkflow : program.getAllWorkflows()) {
        assertTrue(programWorkflow.getRetired());
        for (ProgramWorkflowState programWorkflowState : programWorkflow.getStates()) {
            assertTrue(programWorkflowState.getRetired());
        }
    }
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with ProgramWorkflowState

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

the class ProgramWorkflowServiceTest method getSortedStates_shouldSortNamesContainingNumbersIntelligently.

/**
 * THIS TEST SHOULD BE IN THE CLASS 'PROGRAMWORKFLOWTEST.JAVA' BUT IT REQUIRES ACCESS TO THE DAO
 * LAYER
 *
 * @see ProgramWorkflow#getSortedStates()
 */
@Test
public void getSortedStates_shouldSortNamesContainingNumbersIntelligently() {
    ProgramWorkflow program = new ProgramWorkflow();
    ConceptName state1ConceptName = new ConceptName("Group 10", Context.getLocale());
    Concept state1Concept = new Concept();
    state1Concept.addName(state1ConceptName);
    ProgramWorkflowState state1 = new ProgramWorkflowState();
    state1.setConcept(state1Concept);
    program.addState(state1);
    ConceptName state2ConceptName = new ConceptName("Group 2", Context.getLocale());
    Concept state2Concept = new Concept();
    state2Concept.addName(state2ConceptName);
    ProgramWorkflowState state2 = new ProgramWorkflowState();
    state2.setConcept(state2Concept);
    program.addState(state2);
    Set<ProgramWorkflowState> sortedStates = program.getSortedStates();
    int x = 1;
    for (ProgramWorkflowState state : sortedStates) {
        if (x == 1) {
            Assert.assertEquals("Group 2", state.getConcept().getName(Context.getLocale()).getName());
        } else if (x == 2) {
            Assert.assertEquals("Group 10", state.getConcept().getName(Context.getLocale()).getName());
        } else {
            Assert.fail("Wha?!");
        }
        x++;
    }
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) Concept(org.openmrs.Concept) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) ConceptName(org.openmrs.ConceptName) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ProgramWorkflowState (org.openmrs.ProgramWorkflowState)12 Test (org.junit.Test)9 ProgramWorkflow (org.openmrs.ProgramWorkflow)9 PatientProgram (org.openmrs.PatientProgram)8 Program (org.openmrs.Program)6 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)6 Concept (org.openmrs.Concept)4 PatientState (org.openmrs.PatientState)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)1 ConceptName (org.openmrs.ConceptName)1 Patient (org.openmrs.Patient)1 PatientIdentifier (org.openmrs.PatientIdentifier)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1 APIException (org.openmrs.api.APIException)1 PatientService (org.openmrs.api.PatientService)1 ProgramWorkflowService (org.openmrs.api.ProgramWorkflowService)1 BindException (org.springframework.validation.BindException)1