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