Search in sources :

Example 16 with Program

use of org.openmrs.Program in project openmrs-module-coreapps by openmrs.

the class ProgramHistoryFragmentController method controller.

public void controller(FragmentConfiguration config, @FragmentParam("app") AppDescriptor app, @InjectBeans PatientDomainWrapper patientWrapper, @SpringBean("programWorkflowService") ProgramWorkflowService programWorkflowService) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Object patient = null;
    patient = config.get("patient");
    if (patient == null) {
        patient = config.get("patientId");
    }
    if (patient == null) {
        config.require("patient");
    }
    if (patient instanceof Patient) {
        patientWrapper.setPatient((Patient) patient);
    } else if (patient instanceof PatientDomainWrapper) {
        patientWrapper = (PatientDomainWrapper) patient;
    } else if (patient instanceof Integer) {
        // assume we have patientId
        patientWrapper.setPatient(Context.getPatientService().getPatient((Integer) patient));
    } else {
        throw new IllegalArgumentException("Patient must be of type Patient or PatientDomainWrapper");
    }
    ObjectNode appConfig = app.getConfig();
    appConfig.put("patientUuid", patientWrapper.getPatient().getUuid());
    if (appConfig.get("program") == null || StringUtils.isEmpty(appConfig.get("program").getTextValue())) {
        throw new MissingPropertyException("Program must be specified");
    }
    Program program = programWorkflowService.getProgramByUuid(appConfig.get("program").getTextValue());
    if (program == null) {
        throw new MissingPropertyException("Invalid program");
    }
    List<PatientProgram> patientPrograms = programWorkflowService.getPatientPrograms(patientWrapper.getPatient(), program, null, null, null, null, false);
    // TODO: assumption here is that "getPatientPrograms" returns results in chronological order--need to confirm?
    Collections.reverse(patientPrograms);
    List<String> programJson = new ArrayList<String>();
    Boolean includeActive = appConfig.get("includeActive") != null ? appConfig.get("includeActive").getBooleanValue() : true;
    Boolean includeInactive = appConfig.get("includeInactive") != null ? appConfig.get("includeInactive").getBooleanValue() : true;
    for (PatientProgram patientProgram : patientPrograms) {
        if ((patientProgram.getDateCompleted() == null && includeActive) || (patientProgram.getDateCompleted() != null && includeInactive)) {
            appConfig.put("patientProgram", patientProgram.getUuid());
            programJson.add(appConfig.toString().replace('\"', '\''));
        }
    }
    Map<String, Object> appConfigMap = mapper.convertValue(appConfig, Map.class);
    config.merge(appConfigMap);
    config.addAttribute("programJson", programJson);
}
Also used : PatientDomainWrapper(org.openmrs.module.emrapi.patient.PatientDomainWrapper) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) MissingPropertyException(groovy.lang.MissingPropertyException) PatientProgram(org.openmrs.PatientProgram) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 17 with Program

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

the class ProgramWorkflowServiceImpl method getPossibleOutcomes.

/**
 * @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;
}
Also used : Concept(org.openmrs.Concept) PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) ConceptAnswer(org.openmrs.ConceptAnswer) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with Program

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

the class OpenmrsServiceTest method shouldCheckThatAMethodIsNotRolledBackInCaseOfAnErrorInAnotherInvokedInsideIt.

/**
 * Tests that if two service methods are called (one from inside the other) the first one will
 * be rolled back if an exception is thrown inside the second one. <pre>
 * We are testing with the merge patient method since it is transactional and calls multiple other
 * transactional methods
 * </pre>
 *
 * @throws SerializationException
 */
@Test
@Ignore
public void shouldCheckThatAMethodIsNotRolledBackInCaseOfAnErrorInAnotherInvokedInsideIt() throws SerializationException {
    // TODO FIx why this test fails when run with other tests
    PatientService patientService = Context.getPatientService();
    EncounterService encounterService = Context.getEncounterService();
    ProgramWorkflowService programService = Context.getProgramWorkflowService();
    Patient prefPatient = patientService.getPatient(6);
    Patient notPrefPatient = patientService.getPatient(7);
    Collection<Program> programs = programService.getAllPrograms(false);
    int originalPrefEncounterCount = encounterService.getEncountersByPatient(prefPatient).size();
    int originalNotPrefEncounterCount = encounterService.getEncountersByPatient(notPrefPatient).size();
    Assert.assertTrue(originalNotPrefEncounterCount > 0);
    Cohort notPreferredCohort = new Cohort(notPrefPatient.getPatientId().toString());
    List<PatientProgram> notPrefPrograms = programService.getPatientPrograms(notPreferredCohort, programs);
    Assert.assertTrue(notPrefPrograms.size() > 0);
    // Set the program to null so that the patient program is rejected on validation with
    // an APIException, since it is a RuntimeException, all transactions should be rolled back
    notPrefPrograms.get(0).setProgram(null);
    boolean failed = false;
    try {
        patientService.mergePatients(prefPatient, notPrefPatient);
    } catch (APIException e) {
        // should have failed to force a rollback
        failed = true;
    }
    Assert.assertTrue(failed);
    // Since the encounters are moved first, that logic should have been rolled back
    Assert.assertEquals(originalPrefEncounterCount, encounterService.getEncountersByPatient(prefPatient).size());
}
Also used : PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) Cohort(org.openmrs.Cohort) Patient(org.openmrs.Patient) PatientProgram(org.openmrs.PatientProgram) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 19 with Program

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

the class ProgramWorkflowServiceTest method getProgramByName_shouldReturnProgramWhenNameMatches.

@Test
public void getProgramByName_shouldReturnProgramWhenNameMatches() {
    Program p = pws.getProgramByName("program name");
    assertNotNull(p);
}
Also used : PatientProgram(org.openmrs.PatientProgram) Program(org.openmrs.Program) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with Program

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

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