Search in sources :

Example 31 with Program

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

the class ProgramValidatorTest method validate_shouldFailValidationIfConceptIsNullOrEmptyOrWhitespace.

/**
 * @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);
    Assert.assertTrue(errors.hasFieldErrors("concept"));
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 32 with Program

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

the class ProgramValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @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);
    Assert.assertTrue(errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 33 with Program

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

the class ProgramValidatorTest method validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace.

/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() {
    Program prog = new Program();
    prog.setDescription(null);
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    prog.setDescription("");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    prog.setDescription(" ");
    errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 34 with Program

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

Example 35 with Program

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

the class ProgramWorkflowServiceUnitTest method saveProgram_shouldFailIfProgramWorkFlowStateTerminalIsNull.

@Test
public void saveProgram_shouldFailIfProgramWorkFlowStateTerminalIsNull() {
    exception.expect(APIException.class);
    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);
}
Also used : Concept(org.openmrs.Concept) ProgramWorkflow(org.openmrs.ProgramWorkflow) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ProgramWorkflowState(org.openmrs.ProgramWorkflowState) Test(org.junit.Test)

Aggregations

Program (org.openmrs.Program)43 Test (org.junit.Test)35 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 PatientProgram (org.openmrs.PatientProgram)22 ProgramWorkflow (org.openmrs.ProgramWorkflow)8 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 Concept (org.openmrs.Concept)7 ArrayList (java.util.ArrayList)6 ProgramWorkflowState (org.openmrs.ProgramWorkflowState)6 Date (java.util.Date)4 Patient (org.openmrs.Patient)4 ProgramWorkflowDAO (org.openmrs.api.db.ProgramWorkflowDAO)2 MissingPropertyException (groovy.lang.MissingPropertyException)1 HashMap (java.util.HashMap)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1 Criteria (org.hibernate.Criteria)1 DateTime (org.joda.time.DateTime)1 Ignore (org.junit.Ignore)1