Search in sources :

Example 91 with PersonName

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

the class PersonByNameComparatorTest method comparePersonsByName_shouldReturnNegativeIfPersonNameForPerson1ComesBeforeThatOfPerson2.

/**
 * @see PersonByNameComparator#comparePersonsByName(Person,Person)
 */
@Test
public void comparePersonsByName_shouldReturnNegativeIfPersonNameForPerson1ComesBeforeThatOfPerson2() {
    Person person1 = new Person();
    person1.addName(new PersonName("givenName", "middleName", "familyName"));
    Person person2 = new Person();
    person2.addName(new PersonName("givenName", "middleNamf", "familyName"));
    int actualValue = PersonByNameComparator.comparePersonsByName(person1, person2);
    Assert.assertTrue("Expected a negative value but it was: " + actualValue, actualValue < 0);
}
Also used : PersonName(org.openmrs.PersonName) Person(org.openmrs.Person) Test(org.junit.Test)

Example 92 with PersonName

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

the class ProviderValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see ProviderValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    Provider provider = new Provider();
    provider.setIdentifier("identifier");
    provider.setRetireReason("retireReason");
    Person person = new Person();
    Set<PersonName> personNames = new HashSet<>(1);
    PersonName personName = new PersonName();
    personName.setFamilyName("name");
    personNames.add(personName);
    person.setNames(personNames);
    provider.setPerson(person);
    providerValidator.validate(provider, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : PersonName(org.openmrs.PersonName) Person(org.openmrs.Person) Provider(org.openmrs.Provider) HashSet(java.util.HashSet) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 93 with PersonName

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

the class UserValidatorTest method validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues.

/**
 * @see UserValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() {
    User user = new User();
    user.setUsername("test");
    user.setRetired(true);
    user.setRetireReason("for the lulz");
    user.setPerson(new Person(999));
    user.getPerson().addName(new PersonName("Users", "Need", "People"));
    user.getPerson().setGender("F");
    Errors errors = new BindException(user, "user");
    validator.validate(user, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonName(org.openmrs.PersonName) User(org.openmrs.User) BindException(org.springframework.validation.BindException) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 94 with PersonName

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

the class ADTA28Handler method createPatient.

// Create a new patient when this patient doesn't exist in the database
private Patient createPatient(PID pid, String creatorName) throws HL7Exception {
    Patient patient = new Patient();
    // Try to use the specified username as the creator
    User creator = Context.getUserService().getUserByUsername(creatorName);
    if (creator != null) {
        patient.setCreator(creator);
    }
    // Create all patient identifiers specified in the message
    // Copied code from resolvePatientId() in HL7ServiceImpl.java
    CX[] idList = pid.getPatientIdentifierList();
    if (idList == null || idList.length < 1) {
        throw new HL7Exception("Missing patient identifier in PID segment");
    }
    List<PatientIdentifier> goodIdentifiers = new ArrayList<>();
    for (CX id : idList) {
        String assigningAuthority = id.getAssigningAuthority().getNamespaceID().getValue();
        String hl7PatientId = id.getIDNumber().getValue();
        log.debug("identifier has id=" + hl7PatientId + " assigningAuthority=" + assigningAuthority);
        if (assigningAuthority != null && assigningAuthority.length() > 0) {
            try {
                PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierTypeByName(assigningAuthority);
                if (pit == null) {
                    log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
                    // skip identifiers with unknown type
                    continue;
                }
                PatientIdentifier pi = new PatientIdentifier();
                if (creator != null) {
                    pi.setCreator(creator);
                }
                pi.setIdentifierType(pit);
                pi.setIdentifier(hl7PatientId);
                // Get default location
                Location location = Context.getLocationService().getDefaultLocation();
                if (location == null) {
                    throw new HL7Exception("Cannot find default location");
                }
                pi.setLocation(location);
                try {
                    PatientIdentifierValidator.validateIdentifier(pi);
                    goodIdentifiers.add(pi);
                } catch (PatientIdentifierException ex) {
                    log.warn("Patient identifier in PID is invalid: " + pi, ex);
                }
            } catch (Exception e) {
                log.error("Uncaught error parsing/creating patient identifier '" + hl7PatientId + "' for assigning authority '" + assigningAuthority + "'", e);
            }
        } else {
            log.error("PID contains identifier with no assigning authority");
            continue;
        }
    }
    if (goodIdentifiers.isEmpty()) {
        throw new HL7Exception("PID segment has no recognizable patient identifiers.");
    }
    patient.addIdentifiers(goodIdentifiers);
    // Extract patient name from the message
    XPN patientNameX = pid.getPatientName(0);
    if (patientNameX == null) {
        throw new HL7Exception("Missing patient name in the PID segment");
    }
    // Patient name
    PersonName name = new PersonName();
    name.setFamilyName(patientNameX.getFamilyName().getSurname().getValue());
    name.setGivenName(patientNameX.getGivenName().getValue());
    name.setMiddleName(patientNameX.getSecondAndFurtherGivenNamesOrInitialsThereof().getValue());
    if (creator != null) {
        name.setCreator(creator);
    }
    patient.addName(name);
    // Gender (checks for null, but not for 'M' or 'F')
    String gender = pid.getAdministrativeSex().getValue();
    if (gender == null) {
        throw new HL7Exception("Missing gender in the PID segment");
    }
    gender = gender.toUpperCase();
    if (!OpenmrsConstants.GENDER().containsKey(gender)) {
        throw new HL7Exception("Unrecognized gender: " + gender);
    }
    patient.setGender(gender);
    // Date of Birth
    TS dateOfBirth = pid.getDateTimeOfBirth();
    if (dateOfBirth == null || dateOfBirth.getTime() == null || dateOfBirth.getTime().getValue() == null) {
        throw new HL7Exception("Missing birth date in the PID segment");
    }
    patient.setBirthdate(tsToDate(dateOfBirth));
    // Estimated birthdate?
    ID precisionTemp = dateOfBirth.getDegreeOfPrecision();
    if (precisionTemp != null && precisionTemp.getValue() != null) {
        String precision = precisionTemp.getValue().toUpperCase();
        log.debug("The birthdate is estimated: " + precision);
        if ("Y".equals(precision) || "L".equals(precision)) {
            patient.setBirthdateEstimated(true);
        }
    }
    return patient;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) HL7Exception(ca.uhn.hl7v2.HL7Exception) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) CX(ca.uhn.hl7v2.model.v25.datatype.CX) XPN(ca.uhn.hl7v2.model.v25.datatype.XPN) HL7Exception(ca.uhn.hl7v2.HL7Exception) PID(ca.uhn.hl7v2.model.v25.segment.PID) ID(ca.uhn.hl7v2.model.v25.datatype.ID) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) Location(org.openmrs.Location) TS(ca.uhn.hl7v2.model.v25.datatype.TS)

Example 95 with PersonName

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

the class ProviderByPersonNameComparatorTest method compareProvidersByPersonName_shouldReturnPositiveIfPersonNameForProvider1ComesAfterThatOfProvider2.

/**
 * @see PersonByNameComparator#comparePersonsByName(Person,Person)
 */
@Test
public void compareProvidersByPersonName_shouldReturnPositiveIfPersonNameForProvider1ComesAfterThatOfProvider2() {
    Person person1 = new Person();
    person1.addName(new PersonName("givenNamf", "middleName", "familyName"));
    Provider provider1 = new Provider();
    provider1.setPerson(person1);
    Person person2 = new Person();
    person2.addName(new PersonName("givenName", "middleName", "familyName"));
    Provider provider2 = new Provider();
    provider2.setPerson(person2);
    int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2);
    Assert.assertTrue("Expected a positive value but it was: " + actualValue, actualValue > 0);
}
Also used : PersonName(org.openmrs.PersonName) Person(org.openmrs.Person) Provider(org.openmrs.Provider) Test(org.junit.Test)

Aggregations

PersonName (org.openmrs.PersonName)108 Test (org.junit.Test)81 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)57 Patient (org.openmrs.Patient)41 Person (org.openmrs.Person)39 Date (java.util.Date)26 PatientIdentifier (org.openmrs.PatientIdentifier)19 PersonAddress (org.openmrs.PersonAddress)19 User (org.openmrs.User)17 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)17 PatientIdentifierType (org.openmrs.PatientIdentifierType)13 Location (org.openmrs.Location)12 ArrayList (java.util.ArrayList)9 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)9 PersonMergeLog (org.openmrs.person.PersonMergeLog)8 PersonAttribute (org.openmrs.PersonAttribute)7 PatientAndMatchQuality (org.openmrs.module.registrationcore.api.search.PatientAndMatchQuality)7 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)6 Provider (org.openmrs.Provider)5