Search in sources :

Example 11 with ProgramWorkflow

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

the class ProgramWorkflowServiceTest method saveProgram_shouldCreateProgramWorkflows.

/**
 * Tests creating a new program containing workflows and states
 *
 * @see ProgramWorkflowService#saveProgram(Program)
 */
@Test
public void saveProgram_shouldCreateProgramWorkflows() {
    int numBefore = Context.getProgramWorkflowService().getAllPrograms().size();
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(cs.getConcept(3));
    ProgramWorkflow workflow = new ProgramWorkflow();
    workflow.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow);
    ProgramWorkflowState state1 = new ProgramWorkflowState();
    state1.setConcept(cs.getConcept(5));
    state1.setInitial(true);
    state1.setTerminal(false);
    workflow.addState(state1);
    ProgramWorkflowState state2 = new ProgramWorkflowState();
    state2.setConcept(cs.getConcept(6));
    state2.setInitial(false);
    state2.setTerminal(true);
    workflow.addState(state2);
    Context.getProgramWorkflowService().saveProgram(program);
    assertEquals("Failed to create program", numBefore + 1, Context.getProgramWorkflowService().getAllPrograms().size());
    Program p = Context.getProgramWorkflowService().getProgramByName("TEST PROGRAM");
    assertNotNull("Program is null", p);
    assertNotNull("Workflows is null", p.getWorkflows());
    assertEquals("Wrong number of workflows", 1, p.getWorkflows().size());
    ProgramWorkflow wf = p.getWorkflowByName("CIVIL STATUS");
    assertNotNull(wf);
    List<String> names = new ArrayList<>();
    for (ProgramWorkflowState s : wf.getStates()) {
        names.add(s.getConcept().getName().getName());
    }
    TestUtil.assertCollectionContentsEquals(Arrays.asList("SINGLE", "MARRIED"), names);
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) ArrayList(java.util.ArrayList) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with ProgramWorkflow

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

the class WorkflowCollectionEditor method setAsText.

/**
 * Takes a "program_id:list" where program_id is the id of the program that this collection is
 * for (or not present, if it's a new program) and list is a space-separated list of concept
 * ids. This class is a bit of a hack, because I don't know a better way to do this. -DJ The
 * purpose is to retire and un-retire workflows where possible rather than deleting and creating
 * them.
 *
 * @should update workflows in program
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (StringUtils.hasText(text)) {
        ConceptService cs = Context.getConceptService();
        ProgramWorkflowService pws = Context.getProgramWorkflowService();
        try {
            int ind = text.indexOf(":");
            String progIdStr = text.substring(0, ind);
            text = text.substring(ind + 1);
            if (program == null) {
                // if a program wasn't passed in, try to look it up now
                program = pws.getProgram(Integer.valueOf(progIdStr));
            }
        } catch (Exception ex) {
        }
        String[] conceptIds = text.split(" ");
        Set<ProgramWorkflow> oldSet = program == null ? new HashSet<>() : program.getAllWorkflows();
        Set<Integer> newConceptIds = new HashSet<>();
        for (String id : conceptIds) {
            if (id.trim().length() == 0) {
                continue;
            }
            log.debug("trying " + id);
            newConceptIds.add(Integer.valueOf(id.trim()));
        }
        // go through oldSet and see what we need to keep and what we need to unvoid
        Set<Integer> alreadyDone = new HashSet<>();
        for (ProgramWorkflow pw : oldSet) {
            if (!newConceptIds.contains(pw.getConcept().getConceptId())) {
                pw.setRetired(true);
            } else if (newConceptIds.contains(pw.getConcept().getConceptId()) && pw.getRetired()) {
                pw.setRetired(false);
            }
            alreadyDone.add(pw.getConcept().getConceptId());
        }
        // now add any new ones
        newConceptIds.removeAll(alreadyDone);
        for (Integer conceptId : newConceptIds) {
            ProgramWorkflow pw = new ProgramWorkflow();
            pw.setProgram(program);
            pw.setConcept(cs.getConcept(conceptId));
            oldSet.add(pw);
        }
        setValue(oldSet);
    } else {
        setValue(null);
    }
}
Also used : ProgramWorkflow(org.openmrs.ProgramWorkflow) ProgramWorkflowService(org.openmrs.api.ProgramWorkflowService) ConceptService(org.openmrs.api.ConceptService) HashSet(java.util.HashSet)

Example 13 with ProgramWorkflow

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

the class StateConversionValidatorTest method validate_shouldFailValidationIfConceptIsNullOrEmptyOrWhitespace.

/**
 * @see StateConversionValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptIsNullOrEmptyOrWhitespace() {
    ConceptStateConversion csc = new ConceptStateConversion();
    ProgramWorkflow workflow = Context.getProgramWorkflowService().getProgram(1).getAllWorkflows().iterator().next();
    csc.setProgramWorkflow(workflow);
    csc.setProgramWorkflowState(workflow.getState(1));
    Errors errors = new BindException(csc, "csc");
    new StateConversionValidator().validate(csc, errors);
    Assert.assertTrue(errors.hasFieldErrors("concept"));
}
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)

Example 14 with ProgramWorkflow

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

the class StateConversionValidatorTest method validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues.

/**
 * @see StateConversionValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() {
    ConceptStateConversion csc = new ConceptStateConversion();
    ProgramWorkflow workflow = Context.getProgramWorkflowService().getProgram(1).getAllWorkflows().iterator().next();
    csc.setConcept(Context.getConceptService().getConcept(3));
    csc.setProgramWorkflow(workflow);
    csc.setProgramWorkflowState(workflow.getState(1));
    Errors errors = new BindException(csc, "csc");
    new StateConversionValidator().validate(csc, errors);
    Assert.assertFalse(errors.hasErrors());
}
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)

Example 15 with ProgramWorkflow

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

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