use of org.openmrs.Patient in project openmrs-core by openmrs.
the class PatientDataVoidHandlerTest method handle_shouldVoidTheOrdersEncountersAndObservationsAssociatedWithThePatient.
/**
* @see PatientDataVoidHandler#handle(Patient,User,Date,String)
*/
@Test
public void handle_shouldVoidTheOrdersEncountersAndObservationsAssociatedWithThePatient() {
Patient patient = Context.getPatientService().getPatient(7);
Assert.assertFalse(patient.getVoided());
List<Encounter> encounters = Context.getEncounterService().getEncountersByPatient(patient);
List<Obs> observations = Context.getObsService().getObservationsByPerson(patient);
List<Order> orders = Context.getOrderService().getAllOrdersByPatient(patient);
// we should have some unvoided encounters, obs and orders for the test to be concrete
assertTrue(CollectionUtils.isNotEmpty(encounters));
assertTrue(CollectionUtils.isNotEmpty(observations));
assertTrue(CollectionUtils.isNotEmpty(orders));
// check that fields to be set by the handler are initially null
for (Encounter encounter : encounters) {
assertNull(encounter.getDateVoided());
assertNull(encounter.getVoidedBy());
assertNull(encounter.getVoidReason());
}
for (Obs obs : observations) {
assertNull(obs.getDateVoided());
assertNull(obs.getVoidedBy());
assertNull(obs.getVoidReason());
}
for (Order order : orders) {
assertNull(order.getDateVoided());
assertNull(order.getVoidedBy());
assertNull(order.getVoidReason());
}
new PatientDataVoidHandler().handle(patient, new User(1), new Date(), "voidReason");
// all encounters void related fields should have been set
for (Encounter encounter : encounters) {
assertTrue(encounter.getVoided());
assertNotNull(encounter.getDateVoided());
assertNotNull(encounter.getVoidedBy());
assertNotNull(encounter.getVoidReason());
}
// all obs void related fields should have been set
for (Obs obs : observations) {
assertTrue(obs.getVoided());
assertNotNull(obs.getDateVoided());
assertNotNull(obs.getVoidedBy());
assertNotNull(obs.getVoidReason());
}
// all order void related fields should have been set
for (Order order : orders) {
assertTrue(order.getVoided());
assertNotNull(order.getDateVoided());
assertNotNull(order.getVoidedBy());
assertNotNull(order.getVoidReason());
}
// refresh the lists and check that all encounters, obs and orders were voided
encounters = Context.getEncounterService().getEncountersByPatient(patient);
observations = Context.getObsService().getObservationsByPerson(patient);
assertTrue(CollectionUtils.isEmpty(encounters));
assertTrue(CollectionUtils.isEmpty(observations));
}
use of org.openmrs.Patient in project openmrs-core by openmrs.
the class TestOrderValidatorTest method validate_shouldFailValidationIfTheSpecimenSourceIsInvalid.
/**
* @see TestOrderValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldFailValidationIfTheSpecimenSourceIsInvalid() {
ConceptService conceptService = Context.getConceptService();
Concept specimenSource = conceptService.getConcept(3);
OrderService orderService = Context.getOrderService();
assertThat(specimenSource, not(isIn(orderService.getDrugRoutes())));
TestOrder order = new TestOrder();
Patient patient = new Patient(8);
order.setPatient(patient);
order.setOrderType(orderService.getOrderTypeByName("Test order"));
order.setConcept(conceptService.getConcept(5497));
order.setOrderer(new Provider());
order.setCareSetting(new CareSetting());
Encounter encounter = new Encounter();
encounter.setPatient(patient);
order.setEncounter(encounter);
order.setDateActivated(new Date());
order.setSpecimenSource(specimenSource);
Errors errors = new BindException(order, "order");
new TestOrderValidator().validate(order, errors);
Assert.assertTrue(errors.hasFieldErrors("specimenSource"));
Assert.assertEquals("TestOrder.error.specimenSourceNotAmongAllowedConcepts", errors.getFieldError("specimenSource").getCode());
}
use of org.openmrs.Patient in project openmrs-core by openmrs.
the class TestOrderValidatorTest method validate_shouldPassValidationIfTheSpecimenSourceIsValid.
/**
* @see TestOrderValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldPassValidationIfTheSpecimenSourceIsValid() {
ConceptService conceptService = Context.getConceptService();
Concept specimenSource = conceptService.getConcept(22);
OrderService orderService = Context.getOrderService();
assertThat(specimenSource, isIn(orderService.getDrugRoutes()));
TestOrder order = new TestOrder();
Patient patient = new Patient(8);
order.setPatient(patient);
order.setOrderType(orderService.getOrderTypeByName("Test order"));
order.setConcept(conceptService.getConcept(5497));
order.setOrderer(Context.getProviderService().getProvider(1));
order.setCareSetting(new CareSetting());
Encounter encounter = new Encounter();
encounter.setPatient(patient);
order.setEncounter(encounter);
order.setDateActivated(new Date());
order.setSpecimenSource(specimenSource);
Errors errors = new BindException(order, "order");
new TestOrderValidator().validate(order, errors);
Assert.assertFalse(errors.hasFieldErrors());
}
use of org.openmrs.Patient in project openmrs-core by openmrs.
the class ValidateUtilTest method validate_shouldReturnImmediatelyIfValidationIsDisabledAndHaveNoErrors.
/**
* @see ValidateUtil#validate(Object,Errors)
*/
@Test
public void validate_shouldReturnImmediatelyIfValidationIsDisabledAndHaveNoErrors() {
Boolean prevVal = ValidateUtil.getDisableValidation();
ValidateUtil.setDisableValidation(true);
try {
Patient patient = new Patient();
Errors errors = new BindException(patient, "patient");
ValidateUtil.validate(patient, errors);
assertFalse(errors.hasErrors());
} catch (Exception e) {
ValidateUtil.setDisableValidation(prevVal);
e.printStackTrace();
fail("An unexpected exception occurred");
}
ValidateUtil.setDisableValidation(prevVal);
}
use of org.openmrs.Patient in project openmrs-core by openmrs.
the class HL7ServiceImpl method resolvePersonFromIdentifiers.
/**
* @param identifiers CX identifier list from an identifier (either PID or NK1)
* @return The internal id number of the Patient based on one of the given identifiers, or null
* if the patient is not found
* @throws HL7Exception
*/
@Override
@Transactional(readOnly = true)
public Person resolvePersonFromIdentifiers(CX[] identifiers) throws HL7Exception {
// give up if no identifiers exist
if (identifiers.length < 1) {
throw new HL7Exception("Missing patient identifier in PID segment");
}
// Take the first uniquely matching identifier
for (CX identifier : identifiers) {
String hl7PersonId = identifier.getIDNumber().getValue();
// TODO if 1st component is blank, check 2nd and 3rd of assigning
// authority
String assigningAuthority = identifier.getAssigningAuthority().getNamespaceID().getValue();
if (StringUtils.isNotBlank(assigningAuthority)) {
// Assigning authority defined
try {
PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierTypeByName(assigningAuthority);
if (pit == null) {
// there is no matching PatientIdentifierType
if (assigningAuthority.equals(HL7Constants.HL7_AUTHORITY_UUID)) {
// the identifier is a UUID
Person p = Context.getPersonService().getPersonByUuid(hl7PersonId);
if (p != null) {
return p;
}
log.warn("Can't find person for UUID '" + hl7PersonId + "'");
// skip identifiers with unknown type
continue;
} else if (assigningAuthority.equals(HL7Constants.HL7_AUTHORITY_LOCAL)) {
// the ID is internal (local)
String idType = identifier.getIdentifierTypeCode().getValue();
try {
if (idType.equals(HL7Constants.HL7_ID_PERSON)) {
Integer pid = Integer.parseInt(hl7PersonId);
// patient_id == person_id, so just look for
// the person
Person p = Context.getPersonService().getPerson(pid);
if (p != null) {
return p;
}
} else if (idType.equals(HL7Constants.HL7_ID_PATIENT)) {
Integer pid = Integer.parseInt(hl7PersonId);
// patient_id == person_id, so just look for
// the person
Patient p = Context.getPatientService().getPatient(pid);
if (p != null) {
return p;
}
}
} catch (NumberFormatException e) {
}
log.warn("Can't find Local identifier of '" + hl7PersonId + "'");
// skip identifiers with unknown type
continue;
}
log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
// skip identifiers with unknown type
continue;
}
List<PatientIdentifier> matchingIds = Context.getPatientService().getPatientIdentifiers(hl7PersonId, Collections.singletonList(pit), null, null, null);
if (matchingIds == null || matchingIds.isEmpty()) {
// no matches
log.warn("NO matches found for " + hl7PersonId);
// try next identifier
continue;
} else if (matchingIds.size() == 1) {
// unique match -- we're done
return matchingIds.get(0).getPatient();
} else {
// ambiguous identifier
log.debug("Ambiguous identifier in PID. " + matchingIds.size() + " matches for identifier '" + hl7PersonId + "' of type '" + pit + "'");
// try next identifier
continue;
}
} catch (Exception e) {
log.error("Error resolving patient identifier '" + hl7PersonId + "' for assigning authority '" + assigningAuthority + "'", e);
continue;
}
} else {
try {
log.debug("CX contains ID '" + hl7PersonId + "' without assigning authority -- assuming patient.patient_id");
return Context.getPatientService().getPatient(Integer.parseInt(hl7PersonId));
} catch (NumberFormatException e) {
log.warn("Invalid patient ID '" + hl7PersonId + "'");
}
}
}
return null;
}
Aggregations