/** * @since 1.5 * @see org.openmrs.OpenmrsObject#getId() */ @Override public Integer getId() { return getPatientId(); }
@Override @Transactional(readOnly = true) public List<Cohort> getCohortsContainingPatient(Patient patient) { return getCohortsContainingPatientId(patient.getPatientId()); }
/** * @since 2.1.0 */ public CohortMembership getActiveMembership(Patient patient) { return getMemberships().stream().filter(m -> m.isActive() && m.getPatientId().equals(patient.getPatientId())).findFirst().get(); }
/** * This constructor does not check whether the database contains patients with the given ids, * but {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will. * * @param name * @param description optional description * @param patients optional array of patients */ public Cohort(String name, String description, Patient[] patients) { this(name, description, (Integer[]) null); if (patients != null) { Arrays.stream(patients).forEach(p -> addMembership(new CohortMembership(p.getPatientId()))); } }
/** * @see org.openmrs.api.db.PatientDAO#getAllergyStatus(org.openmrs.Patient) */ //@Override @Override public String getAllergyStatus(Patient patient) { return (String) sessionFactory.getCurrentSession().createSQLQuery( "select allergy_status from patient where patient_id = :patientId").setInteger("patientId", patient.getPatientId()).uniqueResult(); }
/** * @see org.openmrs.api.CohortService#addPatientToCohort(org.openmrs.Cohort, * org.openmrs.Patient) */ @Override public Cohort addPatientToCohort(Cohort cohort, Patient patient) { if (!cohort.contains(patient.getPatientId())) { CohortMembership cohortMembership = new CohortMembership(patient.getPatientId()); cohort.addMembership(cohortMembership); Context.getCohortService().saveCohort(cohort); } return cohort; }
private void requireAppropriatePatientModificationPrivilege(Patient patient) { if (patient.getPatientId() == null) { Context.requirePrivilege(PrivilegeConstants.ADD_PATIENTS); } else { Context.requirePrivilege(PrivilegeConstants.EDIT_PATIENTS); } if (patient.getVoided()) { Context.requirePrivilege(PrivilegeConstants.DELETE_PATIENTS); } }
private void mergeVisits(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) { // move all visits, including voided ones (encounters will be handled below) //TODO: this should be a copy, not a move VisitService visitService = Context.getVisitService(); for (Visit visit : visitService.getVisitsByPatient(notPreferred, true, true)) { if (log.isDebugEnabled()) { log.debug("Merging visit " + visit.getVisitId() + " to " + preferred.getPatientId()); } visit.setPatient(preferred); Visit persisted = visitService.saveVisit(visit); mergedData.addMovedVisit(persisted.getUuid()); } }
private void requireNoActiveOrderOfSameType(Patient patient1, Patient patient2) { String messageKey = "Patient.merge.cannotHaveSameTypeActiveOrders"; List<Order> ordersByPatient1 = Context.getOrderService().getAllOrdersByPatient(patient1); List<Order> ordersByPatient2 = Context.getOrderService().getAllOrdersByPatient(patient2); ordersByPatient1.forEach((Order order1) -> ordersByPatient2.forEach((Order order2) -> { if (order1.isActive() && order2.isActive() && order1.getOrderType().equals(order2.getOrderType())) { Object[] parameters = { patient1.getPatientId(), patient2.getPatientId(), order1.getOrderType() }; String message = Context.getMessageSourceService().getMessage(messageKey, parameters, Context.getLocale()); log.debug(message); throw new APIException(message); } })); }
private void mergeProgramEnrolments(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) { // copy all program enrollments ProgramWorkflowService programService = Context.getProgramWorkflowService(); for (PatientProgram pp : programService.getPatientPrograms(notPreferred, null, null, null, null, null, false)) { if (!pp.getVoided()) { PatientProgram enroll = pp.copy(); enroll.setPatient(preferred); log.debug("Copying patientProgram " + pp.getPatientProgramId() + " to " + preferred.getPatientId()); PatientProgram persisted = programService.savePatientProgram(enroll); mergedData.addCreatedProgram(persisted.getUuid()); } } }
/** * @see DiagnosisService#getUniqueDiagnoses(Patient, Date) */ @Test public void getUniqueDiagnoses_shouldGetUniqueDiagnosesOfPatient(){ Patient patient = patientService.getPatient(2); List<Diagnosis> diagnoses = diagnosisService.getUniqueDiagnoses(patient, new Date(0)); Assert.assertEquals("68802cce-6880-17e4-6880-a68804d22fb7", diagnoses.get(0).getUuid()); Assert.assertEquals(ConditionVerificationStatus.CONFIRMED, diagnoses.get(0).getCertainty()); Assert.assertEquals(new Integer(1), diagnoses.get(0).getDiagnosisId()); Assert.assertEquals(new Integer(2), diagnoses.get(0).getPatient().getPatientId()); Assert.assertEquals(1, diagnoses.size()); }
private void mergeObservationsNotContainedInEncounters(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) { // move all obs that weren't contained in encounters // TODO: this should be a copy, not a move ObsService obsService = Context.getObsService(); for (Obs obs : obsService.getObservationsByPerson(notPreferred)) { if (obs.getEncounter() == null && !obs.getVoided()) { obs.setPerson(preferred); Obs persisted = obsService.saveObs(obs, "Merged from patient #" + notPreferred.getPatientId()); mergedData.addMovedIndependentObservation(persisted.getUuid()); } } }
/** * @see PatientService#getPatientByUuid(String) */ @Test public void getPatientByUuid_shouldFetchPatientWithGivenUuid() throws Exception { String uuid = "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"; Patient patient = Context.getPatientService().getPatientByUuid(uuid); Assert.assertEquals(2, (int) patient.getPatientId()); }
/** * @see PatientService#getPatientByUuid(String) */ @Test public void getPatientByUuid_shouldFindObjectGivenValidUuid() throws Exception { String uuid = "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"; Patient patient = Context.getPatientService().getPatientByUuid(uuid); Assert.assertEquals(2, (int) patient.getPatientId()); }
/** * @see PatientService#getPatientByExample(Patient) */ @Test public void getPatientByExample_shouldFetchPatientMatchingPatientIdOfGivenPatient() throws Exception { Patient examplePatient = Context.getPatientService().getPatient(6); examplePatient.setId(2); Patient patient = Context.getPatientService().getPatientByExample(examplePatient); Assert.assertNotNull(patient); Assert.assertTrue(patient.getClass().isAssignableFrom(Patient.class)); Assert.assertEquals(Integer.valueOf(2), patient.getPatientId()); }
@Test public void validate_shouldPassIfMembershipStartDateIsAfterEndDate() throws Exception { Cohort cohort = new Cohort(2); CohortMembership membership = new CohortMembership(patient.getPatientId()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormat.parse("2016-11-01 00:00:00"); Date endDate = dateFormat.parse("2015-01-01 00:00:00"); membership.setStartDate(startDate); membership.setEndDate(endDate); Errors errors = new BindException(cohort, "cohort"); new CohortValidator().validate(cohort, errors); Assert.assertFalse(errors.hasFieldErrors("memberships")); } }
@Test public void savePatient_shouldUpdateAnExistingPatient() throws Exception { Patient patient = patientService.getPatient(2); // just some sanity checks assertNotNull("There should be a patient with patient_id of 2", patient); assertTrue("The patient should be listed as male", patient.getGender().equals("M")); patient.setGender("F"); patientService.savePatient(patient); Patient patient2 = patientService.getPatient(patient.getPatientId()); assertTrue("The updated patient and the orig patient should still be equal", patient.equals(patient2)); assertTrue("The gender should be new", patient2.getGender().equals("F")); }
/** * @see CohortValidator#validate(Object, Errors) */ @Test public void validate_shouldFailIfPatientIsVoided() { patient.setVoided(true); validator.validate(cohort, errors); Assert.assertTrue(errors.hasFieldErrors("memberships")); String eMessage = "Patient " + patient.getPatientId() + " is voided, cannot add voided members to a cohort"; Assert.assertEquals(eMessage, errors.getFieldError("memberships").getDefaultMessage()); }
/** * @see CohortService#getCohortsContainingPatient(Patient) */ @Test public void getCohortsContainingPatient_shouldReturnCohortsThatHaveGivenPatient() { executeDataSet(COHORT_XML); Patient patientToAdd = new Patient(7); service.addPatientToCohort(service.getCohort(2), patientToAdd); assertTrue(service.getCohort(2).contains(patientToAdd.getPatientId())); List<Cohort> cohortsWithGivenPatient = service.getCohortsContainingPatientId(patientToAdd.getId()); assertTrue(cohortsWithGivenPatient.contains(service.getCohort(2))); }
@Before public void setUp() { validator = new CohortValidator(); executeDataSet(COHORT_XML); cohort = Context.getCohortService().getCohort(2); patient = Context.getPatientService().getPatient(7); cohortMembership = new CohortMembership(patient.getPatientId()); cohort.addMembership(cohortMembership); errors = new BindException(cohort, "cohort"); }