Search in sources :

Example 86 with Patient

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));
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) User(org.openmrs.User) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) Date(java.util.Date) DateUtils.parseDate(org.apache.commons.lang3.time.DateUtils.parseDate) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 87 with Patient

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());
}
Also used : Concept(org.openmrs.Concept) Errors(org.springframework.validation.Errors) TestOrder(org.openmrs.TestOrder) Patient(org.openmrs.Patient) CareSetting(org.openmrs.CareSetting) Encounter(org.openmrs.Encounter) BindException(org.springframework.validation.BindException) OrderService(org.openmrs.api.OrderService) ConceptService(org.openmrs.api.ConceptService) Date(java.util.Date) Provider(org.openmrs.Provider) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 88 with Patient

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());
}
Also used : Concept(org.openmrs.Concept) Errors(org.springframework.validation.Errors) TestOrder(org.openmrs.TestOrder) Patient(org.openmrs.Patient) CareSetting(org.openmrs.CareSetting) Encounter(org.openmrs.Encounter) BindException(org.springframework.validation.BindException) OrderService(org.openmrs.api.OrderService) ConceptService(org.openmrs.api.ConceptService) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 89 with Patient

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);
}
Also used : Errors(org.springframework.validation.Errors) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) ValidationException(org.openmrs.api.ValidationException) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 90 with Patient

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;
}
Also used : CX(ca.uhn.hl7v2.model.v25.datatype.CX) HL7Exception(ca.uhn.hl7v2.HL7Exception) Patient(org.openmrs.Patient) Person(org.openmrs.Person) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) URISyntaxException(java.net.URISyntaxException) DAOException(org.openmrs.api.db.DAOException) FileNotFoundException(java.io.FileNotFoundException) APIException(org.openmrs.api.APIException) HL7Exception(ca.uhn.hl7v2.HL7Exception) EncodingNotSupportedException(ca.uhn.hl7v2.parser.EncodingNotSupportedException) IOException(java.io.IOException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Patient (org.openmrs.Patient)389 Test (org.junit.Test)345 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)284 Date (java.util.Date)106 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)91 Encounter (org.openmrs.Encounter)76 PatientIdentifier (org.openmrs.PatientIdentifier)60 Location (org.openmrs.Location)57 OrderUtilTest (org.openmrs.order.OrderUtilTest)47 PersonName (org.openmrs.PersonName)43 DrugOrder (org.openmrs.DrugOrder)42 BindException (org.springframework.validation.BindException)38 Concept (org.openmrs.Concept)37 Order (org.openmrs.Order)36 PatientIdentifierType (org.openmrs.PatientIdentifierType)33 Errors (org.springframework.validation.Errors)33 Visit (org.openmrs.Visit)29 ArrayList (java.util.ArrayList)28 Obs (org.openmrs.Obs)28 TestOrder (org.openmrs.TestOrder)28