Search in sources :

Example 1 with ProgramWorkflow

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

the class WorkflowCollectionEditor method getAsText.

/**
 * Convert this program's workflows into "id: wkflowid wkflowid wkflowid"
 *
 * @see java.beans.PropertyEditorSupport#getAsText()
 */
@Override
@SuppressWarnings("unchecked")
public String getAsText() {
    Collection<ProgramWorkflow> pws = (Collection<ProgramWorkflow>) getValue();
    if (pws == null || pws.isEmpty()) {
        return ":";
    } else {
        Integer progId = null;
        for (ProgramWorkflow pw : pws) {
            if (pw.getProgram() != null && pw.getProgram().getProgramId() != null) {
                progId = pw.getProgram().getProgramId();
                break;
            }
        }
        StringBuilder ret = new StringBuilder();
        if (progId != null) {
            ret.append(progId);
        }
        ret.append(":");
        for (ProgramWorkflow pw : pws) {
            ret.append(pw.getConcept().getConceptId()).append(" ");
        }
        return ret.toString().trim();
    }
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) Collection(java.util.Collection)

Example 2 with ProgramWorkflow

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

use of org.openmrs.ProgramWorkflow 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 4 with ProgramWorkflow

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

the class PatientProgramValidatorTest method validate_shouldNotFailIfPatientStateIsInRetiredWorkflow.

/**
 * @see PatientProgramValidator#validate(Object,Errors)
 * this test is to specifically validate fix for https://tickets.openmrs.org/browse/TRUNK-3670
 */
@Test
public void validate_shouldNotFailIfPatientStateIsInRetiredWorkflow() {
    ProgramWorkflowService pws = Context.getProgramWorkflowService();
    Patient patient = Context.getPatientService().getPatient(6);
    // create a patient program
    PatientProgram pp = new PatientProgram();
    pp.setPatient(patient);
    pp.setProgram(pws.getProgram(1));
    // add a test workflow, and put the patient in a state in that workflow
    ProgramWorkflow testWorkflow = pp.getProgram().getWorkflow(1);
    PatientState newPatientState = new PatientState();
    newPatientState.setState(testWorkflow.getState(1));
    pp.getStates().add(newPatientState);
    // now retire the workflow
    testWorkflow.setRetired(true);
    testWorkflow.setRetiredBy(Context.getAuthenticatedUser());
    testWorkflow.setRetireReason("test");
    BindException errors = new BindException(pp, "");
    new PatientProgramValidator().validate(pp, errors);
    Assert.assertEquals(false, errors.hasFieldErrors("states"));
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) ProgramWorkflowService(org.openmrs.api.ProgramWorkflowService) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) PatientProgram(org.openmrs.PatientProgram) PatientState(org.openmrs.PatientState) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with ProgramWorkflow

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

the class StateConversionValidatorTest method validate_shouldFailValidationIfProgramWorkflowIsNullOrEmptyOrWhitespace.

/**
 * @see StateConversionValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfProgramWorkflowIsNullOrEmptyOrWhitespace() {
    ConceptStateConversion csc = new ConceptStateConversion();
    csc.setProgramWorkflow(null);
    ProgramWorkflow workflow = Context.getProgramWorkflowService().getProgram(1).getAllWorkflows().iterator().next();
    csc.setConcept(Context.getConceptService().getConcept(3));
    csc.setProgramWorkflowState(workflow.getState(1));
    Errors errors = new BindException(csc, "csc");
    new StateConversionValidator().validate(csc, errors);
    Assert.assertTrue(errors.hasFieldErrors("programWorkflow"));
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) ConceptStateConversion(org.openmrs.ConceptStateConversion) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

ProgramWorkflow (org.openmrs.ProgramWorkflow)22 Test (org.junit.Test)16 PatientProgram (org.openmrs.PatientProgram)12 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 ProgramWorkflowState (org.openmrs.ProgramWorkflowState)9 Program (org.openmrs.Program)8 BindException (org.springframework.validation.BindException)6 Concept (org.openmrs.Concept)5 PatientState (org.openmrs.PatientState)5 ConceptStateConversion (org.openmrs.ConceptStateConversion)4 ProgramWorkflowService (org.openmrs.api.ProgramWorkflowService)4 Errors (org.springframework.validation.Errors)4 Date (java.util.Date)3 Patient (org.openmrs.Patient)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 ConceptName (org.openmrs.ConceptName)1 PatientIdentifier (org.openmrs.PatientIdentifier)1