org.openmrs.Program - java examples

Here are the examples of the java api org.openmrs.Program taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

51 Examples 7

19 View Complete Implementation : ProgramEditor.java
Copyright Apache License 2.0
Author : isstac
@Override
public String getAsText() {
    Program p = (Program) getValue();
    if (p == null) {
        return "";
    } else {
        return p.getProgramId().toString();
    }
}

18 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getProgramByName_shouldReturnProgramWhenNameMatches() {
    Program p = pws.getProgramByName("program name");
    replacedertNotNull(p);
}

18 View Complete Implementation : ProgramWorkflowDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldSaveProgram() {
    Program program = createProgram();
    dao.saveProgram(program);
}

18 View Complete Implementation : WorkflowCollectionEditor.java
Copyright Apache License 2.0
Author : isstac
/**
 * Clreplaced to convert the "programid: workflowoneid workflow2id" strings to actual workflows on a
 * program
 */
public clreplaced WorkflowCollectionEditor extends PropertyEditorSupport {

    private static final Logger log = LoggerFactory.getLogger(WorkflowCollectionEditor.clreplaced);

    public WorkflowCollectionEditor() {
    }

    private Program program = null;

    /**
     * @param program
     */
    public WorkflowCollectionEditor(Program program) {
        this.program = program;
    }

    /**
     * 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 clreplaced 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 preplaceded 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);
        }
    }

    /**
     * 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();
        }
    }
}

18 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getProgramByName_shouldReturnNullWhenNoProgramForGivenName() {
    Program p = pws.getProgramByName("unexisting program");
    replacedertNull(p);
}

17 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.ProgramWorkflowService#purgeProgram(org.openmrs.Program)
 */
@Override
public void purgeProgram(Program program) throws APIException {
    Context.getProgramWorkflowService().purgeProgram(program, false);
}

17 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
private void ensureProgramIsSet(ProgramWorkflow workflow, Program program) {
    if (workflow.getProgram() == null) {
        workflow.setProgram(program);
    } else if (!workflow.getProgram().equals(program)) {
        throw new APIException("Program.error.contains.ProgramWorkflow", new Object[] { workflow.getProgram() });
    }
}

17 View Complete Implementation : ProgramEditor.java
Copyright Apache License 2.0
Author : isstac
/**
 * @should set using concept id
 * @should set using concept uuid
 * @should set using program id
 * @should set using program uuid
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (StringUtils.hasText(text)) {
        try {
            if (text.startsWith("concept.")) {
                Integer conceptId = Integer.valueOf(text.substring(text.indexOf('.') + 1));
                Concept c = Context.getConceptService().getConcept(conceptId);
                setValue(Context.getProgramWorkflowService().getProgramByName(c.getName().getName()));
            } else {
                Integer programId = Integer.valueOf(text);
                setValue(Context.getProgramWorkflowService().getProgram(programId));
            }
        } catch (Exception ex) {
            Program p;
            if (text.startsWith("concept.")) {
                Concept c = Context.getConceptService().getConceptByUuid(text.substring(text.indexOf('.') + 1));
                p = Context.getProgramWorkflowService().getProgramByName(c.getName().getName());
            } else {
                p = Context.getProgramWorkflowService().getProgramByUuid(text);
            }
            setValue(p);
            if (p == null) {
                log.error("Error setting text: " + text, ex);
                throw new IllegalArgumentException("Program not found: " + text, ex);
            }
        }
    } else {
        setValue(null);
    }
}

17 View Complete Implementation : ProgramWorkflowDAOTest.java
Copyright Apache License 2.0
Author : isstac
private Program createProgram() {
    Program program = new Program();
    program.setName("OpenMRS");
    program.setDescription("An opensource medical record system");
    program.setDateCreated(new Date());
    return program;
}

17 View Complete Implementation : SerializedObjectDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getObjectByUuid_shouldReturnTheSavedObject() {
    Program data = dao.getObjectByUuid(Program.clreplaced, "83b452ca-a4c8-4bf2-9e0b-8bbddf2f9901");
    replacedertEquals(data.getId().intValue(), 2);
    replacedertEquals(data.getName(), "TestProgram2");
}

17 View Complete Implementation : SerializedObjectDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test(expected = DAOException.clreplaced)
public void saveObject_shouldThrowAnExceptionIfObjectNotSupported() {
    dao.unregisterSupportedType(Program.clreplaced);
    Program data = new Program();
    data.setName("NewProgram");
    data.setDescription("This is to test saving a Program");
    dao.saveObject(data);
}

17 View Complete Implementation : SerializedObjectDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getObject_shouldReturnTheSavedObject() {
    Program data = dao.getObject(Program.clreplaced, 1);
    replacedertEquals(data.getId().intValue(), 1);
    replacedertEquals(data.getName(), "TestProgram");
}

17 View Complete Implementation : HibernateProgramWorkflowDAO.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.db.ProgramWorkflowDAO#deleteProgram(org.openmrs.Program)
 */
@Override
public void deleteProgram(Program program) throws DAOException {
    sessionFactory.getCurrentSession().delete(program);
}

17 View Complete Implementation : HibernateProgramWorkflowDAO.java
Copyright Apache License 2.0
Author : isstac
// **************************
// PROGRAM
// **************************
/**
 * @see org.openmrs.api.db.ProgramWorkflowDAO#saveProgram(org.openmrs.Program)
 */
@Override
public Program saveProgram(Program program) throws DAOException {
    sessionFactory.getCurrentSession().saveOrUpdate(program);
    return program;
}

16 View Complete Implementation : ProgramValidator.java
Copyright Apache License 2.0
Author : isstac
/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should preplaced validation if description is null or empty or whitespace
 * @should fail validation if program name already in use
 * @should fail validation if concept is null or empty or whitespace
 * @should preplaced validation if all required fields have proper values
 * @should preplaced validation and save edited program
 * @should preplaced validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    Program p = (Program) obj;
    if (p == null) {
        errors.rejectValue("program", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "concept", "error.concept");
        Program existingProgram = Context.getProgramWorkflowService().getProgramByName(p.getName());
        if (existingProgram != null && !existingProgram.getUuid().equals(p.getUuid())) {
            errors.rejectValue("name", "general.error.nameAlreadyInUse");
        }
        if (existingProgram != null && existingProgram.getUuid().equals(p.getUuid())) {
            Context.evictFromSession(existingProgram);
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClreplaced(), "name");
    }
}

16 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramWorkflowService#getProgramByUuid(String)
 */
@Test
public void getProgramByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "eae98b4c-e195-403b-b34a-82d94103b2c0";
    Program program = Context.getProgramWorkflowService().getProgramByUuid(uuid);
    replacedert.replacedertEquals(1, (int) program.getProgramId());
}

16 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldFailIfProgramConceptIsNull() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("Program concept is required");
    Program program1 = new Program(1);
    pws.saveProgram(program1);
}

16 View Complete Implementation : WorkflowCollectionEditorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see WorkflowCollectionEditor#setAsText(String)
 */
@Test
public void setAsText_shouldUpdateWorkflowsInProgram() {
    Program program = Context.getProgramWorkflowService().getProgram(1);
    WorkflowCollectionEditor editor = new WorkflowCollectionEditor();
    replacedert.replacedertEquals(2, program.getWorkflows().size());
    editor.setAsText("1:3");
    replacedert.replacedertEquals(1, program.getWorkflows().size());
    replacedert.replacedertEquals(3, program.getWorkflows().iterator().next().getConcept().getConceptId().intValue());
    replacedert.replacedertEquals(3, program.getAllWorkflows().size());
}

16 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.ProgramWorkflowService#purgeProgram(org.openmrs.Program, boolean)
 */
@Override
public void purgeProgram(Program program, boolean cascade) throws APIException {
    if (cascade && !program.getAllWorkflows().isEmpty()) {
        throw new APIException("Program.cascade.purging.not.implemented", (Object[]) null);
    }
    for (PatientProgram patientProgram : Context.getProgramWorkflowService().getPatientPrograms(null, program, null, null, null, null, true)) {
        purgePatientProgram(patientProgram);
    }
    dao.deleteProgram(program);
}

15 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.ProgramWorkflowService#getPatientPrograms(Patient, Program, Date, Date,
 *      Date, Date, boolean)
 */
@Override
@Transactional(readOnly = true)
public List<PatientProgram> getPatientPrograms(Patient patient, Program program, Date minEnrollmentDate, Date maxEnrollmentDate, Date minCompletionDate, Date maxCompletionDate, boolean includeVoided) throws APIException {
    return dao.getPatientPrograms(patient, program, minEnrollmentDate, maxEnrollmentDate, minCompletionDate, maxCompletionDate, includeVoided);
}

15 View Complete Implementation : ProgramWorkflowDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getProgramsByName_whenThereAreNoProgramsWithTheGivenName_shouldReturnAnEmptyList() {
    Program program = createProgram();
    program.setName("wrongProgramName");
    dao.saveProgram(program);
    clearHibernateCache();
    List<Program> programs = dao.getProgramsByName("testProgramName", true);
    replacedert.replacedertNotNull(programs);
    replacedert.replacedertEquals(0, programs.size());
}

15 View Complete Implementation : ProgramWorkflowDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void getProgramsByName_whenThereAreProgramsWithTheGivenName_shouldReturnAllProgramsWithTheGivenName() {
    Program program1 = createProgram();
    program1.setName("testProgramName");
    dao.saveProgram(program1);
    Program program2 = createProgram();
    program2.setName("testProgramName");
    dao.saveProgram(program2);
    Program program3 = createProgram();
    program3.setName("wrongProgramName");
    dao.saveProgram(program3);
    clearHibernateCache();
    List<Program> programs = dao.getProgramsByName("testProgramName", true);
    replacedert.replacedertEquals(2, programs.size());
    replacedert.replacedertEquals(program1, programs.get(0));
    replacedert.replacedertEquals(program2, programs.get(1));
}

15 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void purgeProgram_shouldFailGivenNonEmptyWorkFlowsAndTrueCascade() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("Cascade purging of Programs is not implemented yet");
    Program program = new Program(1);
    ProgramWorkflow workflow = new ProgramWorkflow(1);
    program.addWorkflow(workflow);
    pws.purgeProgram(program, true);
}

15 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfNameIsNullOrEmptyOrWhitespace() {
    Program prog = new Program();
    prog.setName(null);
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("name"));
    prog.setName("");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("name"));
    prog.setName(" ");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("name"));
}

15 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptIsNullOrEmptyOrWhitespace() {
    Program prog = new Program();
    prog.setName("Hypochondriasis program");
    prog.setConcept(null);
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("concept"));
}

15 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    Program prog = new Program();
    prog.setName("too long text too long text too long text too long text too long text too long text too long text too long text");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("name"));
}

15 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfProgramNameAlreadyInUse() {
    Program prog = new Program();
    prog.setName("MDR-TB PROGRAM");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertTrue(errors.hasFieldErrors("name"));
}

15 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPreplacedValidationIfFieldLengthsAreCorrect() {
    Program prog = new Program();
    prog.setName("Hypochondriasis program");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertFalse(errors.hasErrors());
}

14 View Complete Implementation : SerializedObjectDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveObject_shouldSaveThePreplacededObjectIfSupported() {
    Program data = new Program();
    data.setName("NewProgram");
    data.setDescription("This is to test saving a Program");
    data.setCreator(new User(1));
    data.setDateCreated(new Date());
    data = dao.saveObject(data);
    replacedert.replacedertNotNull(data.getId());
    Program newData = dao.getObject(Program.clreplaced, data.getId());
    replacedertEquals("NewProgram", newData.getName());
}

14 View Complete Implementation : SerializedObjectDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveObject_shouldSetAuditableFieldsBeforeSerializing() {
    Program data = new Program();
    data.setName("NewProgram");
    data.setDescription("This is to test saving a Program");
    data = dao.saveObject(data);
    replacedert.replacedertNotNull(data.getId());
    Program newData = dao.getObject(Program.clreplaced, data.getId());
    replacedertEquals("NewProgram", newData.getName());
    replacedertNotNull(newData.getCreator());
    replacedertNotNull(newData.getDateCreated());
}

14 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void purgeProgram_shouldPurgeProgramWithPatientsEnrolled() {
    Program program = Context.getProgramWorkflowService().getProgram(2);
    // program has at least one patient enrolled
    List<PatientProgram> patientPrograms = Context.getProgramWorkflowService().getPatientPrograms(null, program, null, null, null, null, true);
    replacedertTrue(patientPrograms.size() > 0);
    Context.getProgramWorkflowService().purgeProgram(program);
    // should cascade to patient programs
    for (PatientProgram patientProgram : patientPrograms) {
        replacedertNull(Context.getProgramWorkflowService().getPatientProgram(patientProgram.getId()));
    }
    // make sure that the program was deleted properly
    replacedertNull(Context.getProgramWorkflowService().getProgram(2));
}

14 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPreplacedValidationIfDescriptionIsNullOrEmptyOrWhitespace() {
    Program prog = new Program();
    prog.setDescription(null);
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertFalse(errors.hasFieldErrors("description"));
    prog.setDescription("");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertFalse(errors.hasFieldErrors("description"));
    prog.setDescription(" ");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertFalse(errors.hasFieldErrors("description"));
}

14 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPreplacedValidationIfAllRequiredFieldsHaveProperValues() {
    Program prog = new Program();
    prog.setName("Hypochondriasis program");
    prog.setDescription("This is Hypochondriasis program");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    replacedert.replacedertFalse(errors.hasErrors());
}

14 View Complete Implementation : ProgramValidatorTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPreplacedValidationAndSaveEditedProgram() {
    Program program = Context.getProgramWorkflowService().getProgram(3);
    program.setDescription("Edited description");
    Errors errors = new BindException(program, "program");
    programValidator.validate(program, errors);
    replacedert.replacedertFalse(errors.hasErrors());
    Context.getProgramWorkflowService().saveProgram(program);
    program = Context.getProgramWorkflowService().getProgram(3);
    replacedert.replacedertTrue(program.getDescription().equals("Edited description"));
}

14 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
// **************************
// PROGRAM
// **************************
/**
 * @see org.openmrs.api.ProgramWorkflowService#saveProgram(org.openmrs.Program)
 */
@Override
public Program saveProgram(Program program) throws APIException {
    // Program
    if (program.getConcept() == null) {
        throw new APIException("Program.concept.required", (Object[]) null);
    }
    for (ProgramWorkflow workflow : program.getAllWorkflows()) {
        if (workflow.getConcept() == null) {
            throw new APIException("ProgramWorkflow.concept.required", (Object[]) null);
        }
        ensureProgramIsSet(workflow, program);
        for (ProgramWorkflowState state : workflow.getStates()) {
            if (state.getConcept() == null || state.getInitial() == null || state.getTerminal() == null) {
                throw new APIException("ProgramWorkflowState.requires", (Object[]) null);
            }
            ensureProgramWorkflowIsSet(state, workflow);
        }
    }
    return dao.saveProgram(program);
}

14 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.ProgramWorkflowService#retireProgram(org.openmrs.Program)
 */
@Override
public Program retireProgram(Program program, String reason) throws APIException {
    // program.setRetired(true); - Note the BaseRetireHandler aspect is already setting the retired flag and reason
    for (ProgramWorkflow workflow : program.getWorkflows()) {
        workflow.setRetired(true);
        for (ProgramWorkflowState state : workflow.getStates()) {
            state.setRetired(true);
        }
    }
    return saveProgram(program);
}

14 View Complete Implementation : ProgramWorkflowDAOTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldAlsoSaveOutcomesConcept() {
    Concept outcomesConcept = Context.getConceptService().getConcept(3);
    Program program = createProgram();
    program.setOutcomesConcept(outcomesConcept);
    int id = dao.saveProgram(program).getId();
    clearHibernateCache();
    Program savedProgram = dao.getProgram(id);
    replacedert.replacedertEquals(3, savedProgram.getOutcomesConcept().getId().intValue());
}

13 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see ProgramWorkflowService#saveProgram(Program)
 */
@Test
public void saveProgram_shouldUpdateDetachedProgram() {
    Program program = Context.getProgramWorkflowService().getProgramByUuid("eae98b4c-e195-403b-b34a-82d94103b2c0");
    program.setDescription("new description");
    Context.evictFromSession(program);
    program = Context.getProgramWorkflowService().saveProgram(program);
    replacedert.replacedertEquals("new description", program.getDescription());
}

13 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldFailIfProgramWorkFlowConceptIsNull() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("ProgramWorkflow concept is required");
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(new Concept(1));
    program.addWorkflow(new ProgramWorkflow());
    pws.saveProgram(program);
}

12 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test(expected = org.openmrs.api.ProgramNameDuplicatedException.clreplaced)
public void getProgramByName_shouldFailWhenTwoProgramsFoundWithSameName() {
    ProgramWorkflowDAO mockDao = Mockito.mock(ProgramWorkflowDAO.clreplaced);
    List<Program> programsWithGivenName = new ArrayList<>();
    Program program1 = new Program("A name");
    Program program2 = new Program("A name");
    programsWithGivenName.add(program1);
    programsWithGivenName.add(program2);
    Mockito.stub(mockDao.getProgramsByName("A name", false)).toReturn(programsWithGivenName);
    Mockito.stub(mockDao.getProgramsByName("A name", true)).toReturn(programsWithGivenName);
    pws.setProgramWorkflowDAO(mockDao);
    pws.getProgramByName("A name");
}

10 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.ProgramWorkflowService#getPossibleOutcomes(Integer)
 */
@Override
@Transactional(readOnly = true)
public List<Concept> getPossibleOutcomes(Integer programId) {
    List<Concept> possibleOutcomes = new ArrayList<>();
    Program program = Context.getProgramWorkflowService().getProgram(programId);
    if (program == null) {
        return possibleOutcomes;
    }
    Concept outcomesConcept = program.getOutcomesConcept();
    if (outcomesConcept == null) {
        return possibleOutcomes;
    }
    if (!outcomesConcept.getAnswers().isEmpty()) {
        for (ConceptAnswer conceptAnswer : outcomesConcept.getAnswers()) {
            possibleOutcomes.add(conceptAnswer.getAnswerConcept());
        }
        return possibleOutcomes;
    }
    if (!outcomesConcept.getSetMembers().isEmpty()) {
        return outcomesConcept.getSetMembers();
    }
    return possibleOutcomes;
}

10 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
@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);
    replacedertTrue(retireProgram.getRetired());
    replacedertEquals(reason, retireProgram.getRetireReason());
    for (ProgramWorkflow programWorkflow : program.getAllWorkflows()) {
        replacedertTrue(programWorkflow.getRetired());
        for (ProgramWorkflowState programWorkflowState : programWorkflow.getStates()) {
            replacedertTrue(programWorkflowState.getRetired());
        }
    }
}

9 View Complete Implementation : ProgramWorkflowServiceImpl.java
Copyright Apache License 2.0
Author : isstac
/**
 * @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);
}

7 View Complete Implementation : HibernateProgramWorkflowDAO.java
Copyright Apache License 2.0
Author : isstac
/**
 * @see org.openmrs.api.db.ProgramWorkflowDAO#getPatientPrograms(Patient, Program, Date, Date,
 *      Date, Date, boolean)
 */
@Override
@SuppressWarnings("unchecked")
public List<PatientProgram> getPatientPrograms(Patient patient, Program program, Date minEnrollmentDate, Date maxEnrollmentDate, Date minCompletionDate, Date maxCompletionDate, boolean includeVoided) throws DAOException {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(PatientProgram.clreplaced);
    if (patient != null) {
        crit.add(Restrictions.eq("patient", patient));
    }
    if (program != null) {
        crit.add(Restrictions.eq("program", program));
    }
    if (minEnrollmentDate != null) {
        crit.add(Restrictions.ge("dateEnrolled", minEnrollmentDate));
    }
    if (maxEnrollmentDate != null) {
        crit.add(Restrictions.le("dateEnrolled", maxEnrollmentDate));
    }
    if (minCompletionDate != null) {
        crit.add(Restrictions.or(Restrictions.isNull("dateCompleted"), Restrictions.ge("dateCompleted", minCompletionDate)));
    }
    if (maxCompletionDate != null) {
        crit.add(Restrictions.le("dateCompleted", maxCompletionDate));
    }
    if (!includeVoided) {
        crit.add(Restrictions.eq("voided", false));
    }
    return crit.list();
}

7 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldFailIfProgramWorkFlowStateInitialIsNull() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("ProgramWorkflowState concept, initial, terminal are required");
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(new Concept(1));
    ProgramWorkflow workflow = new ProgramWorkflow();
    workflow.setConcept(new Concept(2));
    ProgramWorkflowState state1 = new ProgramWorkflowState();
    state1.setConcept(new Concept(3));
    state1.setTerminal(false);
    workflow.addState(state1);
    program.addWorkflow(workflow);
    pws.saveProgram(program);
}

7 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldFailIfProgramWorkFlowStateTerminalIsNull() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("ProgramWorkflowState concept, initial, terminal are required");
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(new Concept(1));
    ProgramWorkflow workflow = new ProgramWorkflow();
    workflow.setConcept(new Concept(2));
    ProgramWorkflowState state1 = new ProgramWorkflowState();
    state1.setConcept(new Concept(3));
    state1.setInitial(true);
    workflow.addState(state1);
    program.addWorkflow(workflow);
    pws.saveProgram(program);
}

7 View Complete Implementation : ProgramWorkflowServiceUnitTest.java
Copyright Apache License 2.0
Author : isstac
@Test
public void saveProgram_shouldFailIfProgramWorkFlowStateConceptIsNull() {
    exception.expect(APIException.clreplaced);
    exception.expectMessage("ProgramWorkflowState concept, initial, terminal are required");
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(new Concept(1));
    ProgramWorkflow workflow = new ProgramWorkflow();
    workflow.setConcept(new Concept(2));
    ProgramWorkflowState state1 = new ProgramWorkflowState();
    state1.setInitial(true);
    state1.setTerminal(false);
    workflow.addState(state1);
    program.addWorkflow(workflow);
    pws.saveProgram(program);
}

0 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * 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);
    replacedertEquals("Failed to create program", numBefore + 1, Context.getProgramWorkflowService().getAllPrograms().size());
    Program p = Context.getProgramWorkflowService().getProgramByName("TEST PROGRAM");
    replacedertNotNull("Program is null", p);
    replacedertNotNull("Workflows is null", p.getWorkflows());
    replacedertEquals("Wrong number of workflows", 1, p.getWorkflows().size());
    ProgramWorkflow wf = p.getWorkflowByName("CIVIL STATUS");
    replacedertNotNull(wf);
    List<String> names = new ArrayList<>();
    for (ProgramWorkflowState s : wf.getStates()) {
        names.add(s.getConcept().getName().getName());
    }
    TestUtil.replacedertCollectionContentsEquals(Arrays.asList("SINGLE", "MARRIED"), names);
}

0 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * Tests if the savePatientProgram(PatientProgram) sets the EndDate of recent state of each workflow
 * on calling the setDateCompleted(Date date) method.
 *
 * @see ProgramWorkflowService#savePatientProgram(PatientProgram)
 */
@Test
public void savePatientProgram_shouldSetEndDateOfAllRecentStatesWhenCompletingTheProgram() throws Exception {
    Date day3 = new Date();
    Date day2_5 = new Date(day3.getTime() - 12 * 3600 * 1000);
    Date day2 = new Date(day3.getTime() - 24 * 3600 * 1000);
    Date day1 = new Date(day2.getTime() - 24 * 3600 * 1000);
    // Program Architecture
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(cs.getConcept(3));
    ProgramWorkflow workflow1 = new ProgramWorkflow();
    workflow1.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow1);
    ProgramWorkflow workflow2 = new ProgramWorkflow();
    workflow2.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow2);
    // workflow1
    ProgramWorkflowState state1_w1 = new ProgramWorkflowState();
    state1_w1.setConcept(cs.getConcept(5));
    state1_w1.setInitial(true);
    state1_w1.setTerminal(false);
    workflow1.addState(state1_w1);
    ProgramWorkflowState state2_w1 = new ProgramWorkflowState();
    state2_w1.setConcept(cs.getConcept(6));
    state2_w1.setInitial(false);
    state2_w1.setTerminal(true);
    workflow1.addState(state2_w1);
    // workflow2
    ProgramWorkflowState state1_w2 = new ProgramWorkflowState();
    state1_w2.setConcept(cs.getConcept(5));
    state1_w2.setInitial(true);
    state1_w2.setTerminal(false);
    workflow2.addState(state1_w2);
    ProgramWorkflowState state2_w2 = new ProgramWorkflowState();
    state2_w2.setConcept(cs.getConcept(6));
    state2_w2.setInitial(false);
    state2_w2.setTerminal(true);
    workflow2.addState(state2_w2);
    Context.getProgramWorkflowService().saveProgram(program);
    // Patient Program Architecture
    PatientProgram patientprogram = new PatientProgram();
    patientprogram.setProgram(program);
    patientprogram.setPatient(new Patient());
    patientprogram.setDateEnrolled(day1);
    patientprogram.setDateCompleted(null);
    PatientState patientstate1_w1 = new PatientState();
    patientstate1_w1.setStartDate(day1);
    patientstate1_w1.setEndDate(day2);
    patientstate1_w1.setState(state1_w1);
    PatientState patientstate2_w1 = new PatientState();
    patientstate2_w1.setStartDate(day2);
    // Forcefully setEndDate to simulate suspended state
    patientstate2_w1.setEndDate(day2_5);
    patientstate2_w1.setState(state2_w1);
    PatientState patientstate1_w2 = new PatientState();
    patientstate1_w2.setStartDate(day1);
    patientstate1_w2.setEndDate(day2);
    patientstate1_w2.setState(state1_w2);
    PatientState patientstate2_w2 = new PatientState();
    patientstate2_w2.setStartDate(day2);
    patientstate2_w2.setEndDate(null);
    patientstate2_w2.setState(state2_w2);
    patientprogram.getStates().add(patientstate1_w1);
    patientprogram.getStates().add(patientstate2_w1);
    patientprogram.getStates().add(patientstate1_w2);
    patientprogram.getStates().add(patientstate2_w2);
    patientstate1_w1.setPatientProgram(patientprogram);
    patientstate2_w1.setPatientProgram(patientprogram);
    patientstate1_w2.setPatientProgram(patientprogram);
    patientstate2_w2.setPatientProgram(patientprogram);
    // when
    Date terminal_date = day3;
    patientprogram.setDateCompleted(terminal_date);
    Context.getProgramWorkflowService().savePatientProgram(patientprogram);
    // then
    // End date of recent active states should be set
    replacedertTrue((patientstate2_w2.getEndDate().toString()).equals(terminal_date.toString()));
    replacedertTrue((patientprogram.getDateCompleted()).equals(patientstate2_w2.getEndDate()));
    // End Date of suspended state should not change
    replacedertTrue((patientstate2_w1.getEndDate().toString()).equals(day2_5.toString()));
    // End date of past states should not change
    replacedertTrue((patientstate1_w1.getEndDate().toString()).equals(day2.toString()));
    replacedertTrue((patientstate1_w2.getEndDate().toString()).equals(day2.toString()));
}

0 View Complete Implementation : ProgramWorkflowServiceTest.java
Copyright Apache License 2.0
Author : isstac
/**
 * Tests if the savePatientProgram(PatientProgram) sets the EndDate of recent state of each workflow
 * when a patient transitions to a terminal state.
 *
 * @see ProgramWorkflowService#savePatientProgram(PatientProgram)
 */
@Test
public void savePatientProgram_shouldSetEndDateOfAllRecentStatesOnTransitionToTerminalState() throws Exception {
    Date day3 = new Date();
    Date day2_5 = new Date(day3.getTime() - 12 * 3600 * 1000);
    Date day2 = new Date(day3.getTime() - 24 * 3600 * 1000);
    Date day1 = new Date(day2.getTime() - 24 * 3600 * 1000);
    // Program Architecture
    Program program = new Program();
    program.setName("TEST PROGRAM");
    program.setDescription("TEST PROGRAM DESCRIPTION");
    program.setConcept(cs.getConcept(3));
    ProgramWorkflow workflow1 = new ProgramWorkflow();
    workflow1.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow1);
    ProgramWorkflow workflow2 = new ProgramWorkflow();
    workflow2.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow2);
    ProgramWorkflow workflow3 = new ProgramWorkflow();
    workflow3.setConcept(cs.getConcept(4));
    program.addWorkflow(workflow3);
    // workflow1
    ProgramWorkflowState state1_w1 = new ProgramWorkflowState();
    state1_w1.setConcept(cs.getConcept(5));
    state1_w1.setInitial(true);
    state1_w1.setTerminal(false);
    workflow1.addState(state1_w1);
    ProgramWorkflowState state2_w1 = new ProgramWorkflowState();
    state2_w1.setConcept(cs.getConcept(6));
    state2_w1.setInitial(false);
    state2_w1.setTerminal(true);
    workflow1.addState(state2_w1);
    // workflow2
    ProgramWorkflowState state1_w2 = new ProgramWorkflowState();
    state1_w2.setConcept(cs.getConcept(5));
    state1_w2.setInitial(true);
    state1_w2.setTerminal(false);
    workflow2.addState(state1_w2);
    ProgramWorkflowState state2_w2 = new ProgramWorkflowState();
    state2_w2.setConcept(cs.getConcept(6));
    state2_w2.setInitial(false);
    state2_w2.setTerminal(true);
    workflow2.addState(state2_w2);
    // workflow3
    ProgramWorkflowState state1_w3 = new ProgramWorkflowState();
    state1_w3.setConcept(cs.getConcept(5));
    state1_w3.setInitial(true);
    state1_w3.setTerminal(false);
    workflow3.addState(state1_w3);
    ProgramWorkflowState state2_w3 = new ProgramWorkflowState();
    state2_w3.setConcept(cs.getConcept(6));
    state2_w3.setInitial(false);
    state2_w3.setTerminal(true);
    workflow3.addState(state2_w3);
    Context.getProgramWorkflowService().saveProgram(program);
    // Patient Program Architecture
    PatientProgram patientprogram = new PatientProgram();
    patientprogram.setProgram(program);
    patientprogram.setPatient(new Patient());
    patientprogram.setDateEnrolled(day1);
    patientprogram.setDateCompleted(null);
    PatientState patientstate1_w1 = new PatientState();
    patientstate1_w1.setStartDate(day1);
    patientstate1_w1.setState(state1_w1);
    PatientState patientstate1_w2 = new PatientState();
    patientstate1_w2.setStartDate(day1);
    patientstate1_w2.setState(state1_w2);
    PatientState patientstate1_w3 = new PatientState();
    patientstate1_w3.setStartDate(day1);
    patientstate1_w3.setState(state1_w3);
    // Forcefully setEndDate to simulate suspended state
    patientstate1_w3.setEndDate(day2_5);
    patientprogram.getStates().add(patientstate1_w1);
    patientprogram.getStates().add(patientstate1_w2);
    patientprogram.getStates().add(patientstate1_w3);
    patientstate1_w1.setPatientProgram(patientprogram);
    patientstate1_w2.setPatientProgram(patientprogram);
    patientstate1_w3.setPatientProgram(patientprogram);
    // when
    patientprogram.transitionToState(state2_w1, day3);
    pws.savePatientProgram(patientprogram);
    // then
    for (PatientState state : patientprogram.getMostRecentStateInEachWorkflow()) {
        if (!(state.equals(patientstate1_w3))) {
            replacedertTrue(state.getEndDate().toString().equals(patientprogram.getDateCompleted().toString()));
        }
    }
    // End date of recent states should be set
    replacedertTrue((patientstate1_w2.getEndDate().toString()).equals(patientprogram.getDateCompleted().toString()));
    replacedertTrue((patientprogram.getDateCompleted().toString()).equals(day3.toString()));
    // End date of suspended state should not change
    replacedertTrue(patientstate1_w3.getEndDate().toString().equals(day2_5.toString()));
    // End date of past states should not change
    replacedertTrue((patientstate1_w1.getEndDate().toString()).equals(day3.toString()));
}