use of org.openmrs.api.EncounterService in project openmrs-module-pihcore by PIH.
the class RedirectToDeathNoteIfMissing method hasDeathCertificateEncounter.
private boolean hasDeathCertificateEncounter(Patient patient) {
EncounterService encounterService = Context.getEncounterService();
EncounterType deathCertificateEncounterType = Context.getEncounterService().getEncounterTypeByUuid(PihCoreConstants.DEATH_CERTIFICATE_ENCOUNTER_TYPE_UUID);
List<Encounter> deathCertificateEncounters = encounterService.getEncounters(patient, null, null, null, null, Arrays.asList(deathCertificateEncounterType), null, null, null, false);
return deathCertificateEncounters.size() > 0;
}
use of org.openmrs.api.EncounterService in project openmrs-core by openmrs.
the class ObsServiceImpl method getObservations.
/**
* This implementation queries the obs table comparing the given <code>searchString</code> with
* the patient's identifier, encounterId, and obsId
*
* @see org.openmrs.api.ObsService#getObservations(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public List<Obs> getObservations(String searchString) {
// search on patient identifier
PatientService ps = Context.getPatientService();
List<Patient> patients = ps.getPatients(searchString);
List<Person> persons = new ArrayList<>(patients);
// try to search on encounterId
EncounterService es = Context.getEncounterService();
List<Encounter> encounters = new ArrayList<>();
try {
Encounter e = es.getEncounter(Integer.valueOf(searchString));
if (e != null) {
encounters.add(e);
}
} catch (NumberFormatException e) {
// pass
}
List<Obs> returnList = new ArrayList<>();
if (!encounters.isEmpty() || !persons.isEmpty()) {
returnList = Context.getObsService().getObservations(persons, encounters, null, null, null, null, null, null, null, null, null, false);
}
// try to search on obsId
try {
Obs o = getObs(Integer.valueOf(searchString));
if (o != null) {
returnList.add(o);
}
} catch (NumberFormatException e) {
// pass
}
return returnList;
}
use of org.openmrs.api.EncounterService in project openmrs-core by openmrs.
the class PatientDataUnvoidHandler method handle.
@Override
public void handle(Patient patient, User originalVoidingUser, Date origParentVoidedDate, String unused) {
// can't be unvoiding a patient that doesn't exist in the database
if (patient.getId() != null) {
// unvoid all the encounter that got voided as a result of the patient getting voided
EncounterService es = Context.getEncounterService();
EncounterSearchCriteria encounterSearchCriteria = new EncounterSearchCriteriaBuilder().setPatient(patient).setIncludeVoided(true).createEncounterSearchCriteria();
List<Encounter> encounters = es.getEncounters(encounterSearchCriteria);
if (CollectionUtils.isNotEmpty(encounters)) {
for (Encounter encounter : encounters) {
if (encounter.getVoided() && encounter.getDateVoided().equals(origParentVoidedDate) && encounter.getVoidedBy().equals(originalVoidingUser)) {
es.unvoidEncounter(encounter);
}
}
}
// unvoid all the orders that got voided as a result of the patient getting voided
OrderService os = Context.getOrderService();
List<Order> orders = os.getAllOrdersByPatient(patient);
if (CollectionUtils.isNotEmpty(orders)) {
for (Order order : orders) {
if (order.getVoided() && order.getDateVoided().equals(origParentVoidedDate) && order.getVoidedBy().equals(originalVoidingUser)) {
os.unvoidOrder(order);
}
}
}
CohortService cs = Context.getCohortService();
cs.notifyPatientUnvoided(patient, originalVoidingUser, origParentVoidedDate);
}
}
use of org.openmrs.api.EncounterService in project openmrs-core by openmrs.
the class PatientDataVoidHandler method handle.
/**
* @see org.openmrs.api.handler.VoidHandler#handle(org.openmrs.Voidable, org.openmrs.User,
* java.util.Date, java.lang.String)
* @should void the orders encounters and observations associated with the patient
*/
@Override
public void handle(Patient patient, User voidingUser, Date voidedDate, String voidReason) {
// void all the encounters associated with this patient
EncounterService es = Context.getEncounterService();
List<Encounter> encounters = es.getEncountersByPatient(patient);
if (CollectionUtils.isNotEmpty(encounters)) {
for (Encounter encounter : encounters) {
if (!encounter.getVoided()) {
// EncounterServiceImpl.voidEncounter and the requiredDataAdvice will set dateVoided to current date
// if it is null, we need to set it now to match the patient's date voided so that the unvoid
// handler's logic doesn't fail when comparing dates while unvoiding encounters that were voided
// with the patient
encounter.setDateVoided(patient.getDateVoided());
es.voidEncounter(encounter, voidReason);
}
}
}
Context.addProxyPrivilege(PrivilegeConstants.EDIT_COHORTS);
try {
Context.getCohortService().notifyPatientVoided(patient);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.EDIT_COHORTS);
}
}
use of org.openmrs.api.EncounterService in project openmrs-core by openmrs.
the class PatientDataUnvoidHandlerTest method handle_shouldNotUnvoidTheOrdersAndEncountersThatNeverGotVoidedWithThePatient.
/**
* @see PatientDataUnvoidHandler#handle(Patient,User,Date,String)
*/
@Test
public void handle_shouldNotUnvoidTheOrdersAndEncountersThatNeverGotVoidedWithThePatient() {
executeDataSet("org/openmrs/api/include/OrderServiceTest-otherEncounters.xml");
Patient patient = Context.getPatientService().getPatient(7);
EncounterService es = Context.getEncounterService();
OrderService os = Context.getOrderService();
Encounter testEncounter = es.getEncountersByPatient(patient).get(0);
// santy checks
assertFalse(testEncounter.getVoided());
assertNull(testEncounter.getDateVoided());
assertNull(testEncounter.getVoidedBy());
assertNull(testEncounter.getVoidReason());
// void one of the encounter orders be voided at a different time for testing purposes
assertFalse(testEncounter.getOrders().isEmpty());
Order testOrder = testEncounter.getOrders().iterator().next();
assertFalse(testOrder.getVoided());
Context.getOrderService().voidOrder(testOrder, "testing");
assertTrue(testOrder.getVoided());
TestUtil.waitForClockTick();
// void one of the unvoided encounters for testing purposes
es.voidEncounter(testEncounter, "random reason");
assertTrue(testEncounter.getVoided());
assertTrue(testOrder.getVoided());
List<Patient> patients = new ArrayList<>();
patients.add(patient);
// wait a bit so that the patient isn't voided on the same millisecond
TestUtil.waitForClockTick();
// now void the patient for testing purposes
patient = Context.getPatientService().voidPatient(patient, "Void Reason");
assertTrue(patient.getVoided());
new PatientDataUnvoidHandler().handle(patient, patient.getVoidedBy(), patient.getDateVoided(), null);
// the encounter that was initially voided separately should still be voided
testEncounter = es.getEncounter(testEncounter.getId());
assertTrue(testEncounter.getVoided());
assertNotNull(testEncounter.getDateVoided());
assertNotNull(testEncounter.getVoidedBy());
assertNotNull(testEncounter.getVoidReason());
// the order that was initially voided separately should still be voided
assertTrue(testOrder.getVoided());
assertNotNull(testOrder.getDateVoided());
assertNotNull(testOrder.getVoidedBy());
assertNotNull(testOrder.getVoidReason());
}
Aggregations