Search in sources :

Example 11 with PatientIdentifierType

use of org.openmrs.PatientIdentifierType in project openmrs-core by openmrs.

the class PatientSearchCriteriaTest method getSearchMode_shouldIdentifySearchByIdentifierAndIdentifierTypeList.

/**
 * @see PatientSearchCriteria#getSearchMode(String, String, java.util.List, boolean)
 */
@Test
public void getSearchMode_shouldIdentifySearchByIdentifierAndIdentifierTypeList() {
    List<PatientIdentifierType> patientIdentifierTypeList = new ArrayList<>();
    patientIdentifierTypeList.add(new PatientIdentifierType());
    Assert.assertEquals(PatientSearchMode.PATIENT_SEARCH_BY_IDENTIFIER, patientSearchCriteria.getSearchMode(null, "identifier", patientIdentifierTypeList, false));
    Assert.assertEquals(PatientSearchMode.PATIENT_SEARCH_BY_IDENTIFIER, patientSearchCriteria.getSearchMode("", "identifier", patientIdentifierTypeList, false));
    Assert.assertEquals(PatientSearchMode.PATIENT_SEARCH_BY_IDENTIFIER, patientSearchCriteria.getSearchMode("  \n\t", "identifier", patientIdentifierTypeList, false));
}
Also used : ArrayList(java.util.ArrayList) PatientIdentifierType(org.openmrs.PatientIdentifierType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with PatientIdentifierType

use of org.openmrs.PatientIdentifierType in project openmrs-core by openmrs.

the class PatientServiceImplTest method checkPatientIdentifiers_shouldNotThrowMissingRequiredIdentifierGivenRequiredIdentifierTypesArePresent.

@Test
public void checkPatientIdentifiers_shouldNotThrowMissingRequiredIdentifierGivenRequiredIdentifierTypesArePresent() throws Exception {
    // given
    final String typeUuid = "equal type uuid";
    final PatientIdentifierType requiredIdentifierType = new PatientIdentifierType(12345);
    requiredIdentifierType.setUuid(typeUuid);
    final PatientIdentifierType patientIdentifierType = new PatientIdentifierType(12345);
    patientIdentifierType.setUuid(typeUuid);
    final List<PatientIdentifierType> requiredTypes = new ArrayList<>();
    requiredTypes.add(requiredIdentifierType);
    when(patientDaoMock.getPatientIdentifierTypes(any(), any(), any(), any())).thenReturn(requiredTypes);
    final Patient patientWithIdentifiers = new Patient();
    patientWithIdentifiers.addIdentifier(new PatientIdentifier("some identifier", patientIdentifierType, mock(Location.class)));
    final PatientIdentifierType anotherPatientIdentifier = new PatientIdentifierType(2345);
    anotherPatientIdentifier.setUuid("another type uuid");
    patientWithIdentifiers.addIdentifier(new PatientIdentifier("some identifier", anotherPatientIdentifier, mock(Location.class)));
    // when
    patientService.checkPatientIdentifiers(patientWithIdentifiers);
// then no exception
}
Also used : ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Matchers.anyString(org.mockito.Matchers.anyString) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PatientServiceTest(org.openmrs.api.PatientServiceTest)

Example 13 with PatientIdentifierType

use of org.openmrs.PatientIdentifierType in project openmrs-core by openmrs.

the class HibernatePatientDAOTest method getPatientIdentifiers_shouldGetByIdentifierType.

@Test
public void getPatientIdentifiers_shouldGetByIdentifierType() {
    List<PatientIdentifierType> identifierTypes = singletonList(new PatientIdentifierType(2));
    List<PatientIdentifier> identifiers = hibernatePatientDao.getPatientIdentifiers(null, identifierTypes, emptyList(), emptyList(), null);
    List<Integer> identifierIds = identifiers.stream().map(PatientIdentifier::getId).collect(Collectors.toList());
    Assert.assertEquals(2, identifiers.size());
    Assert.assertThat(identifierIds, hasItems(1, 3));
}
Also used : PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with PatientIdentifierType

use of org.openmrs.PatientIdentifierType in project openmrs-core by openmrs.

the class ValidateUtilTest method validateFieldLength_shouldReturnImmediatelyIfValidationIsDisabledAndHaveNoErrors.

/**
 * @see ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...)
 */
@Test
public void validateFieldLength_shouldReturnImmediatelyIfValidationIsDisabledAndHaveNoErrors() {
    Boolean prevVal = ValidateUtil.getDisableValidation();
    ValidateUtil.setDisableValidation(true);
    PatientIdentifierType patientIdentifierType = new PatientIdentifierType();
    patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl +1");
    BindException errors = new BindException(patientIdentifierType, "patientIdentifierType");
    ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name");
    assertFalse(errors.hasFieldErrors("name"));
    ValidateUtil.setDisableValidation(prevVal);
}
Also used : BindException(org.springframework.validation.BindException) PatientIdentifierType(org.openmrs.PatientIdentifierType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 15 with PatientIdentifierType

use of org.openmrs.PatientIdentifierType 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

PatientIdentifierType (org.openmrs.PatientIdentifierType)131 Test (org.junit.Test)99 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)88 PatientIdentifier (org.openmrs.PatientIdentifier)56 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)53 Patient (org.openmrs.Patient)37 Location (org.openmrs.Location)29 ArrayList (java.util.ArrayList)20 BindException (org.springframework.validation.BindException)17 Errors (org.springframework.validation.Errors)14 Date (java.util.Date)13 PersonName (org.openmrs.PersonName)13 User (org.openmrs.User)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Concept (org.openmrs.Concept)6 Person (org.openmrs.Person)5 HashMap (java.util.HashMap)4 PersonAddress (org.openmrs.PersonAddress)4 PatientServiceTest (org.openmrs.api.PatientServiceTest)4 HL7Exception (ca.uhn.hl7v2.HL7Exception)3