Search in sources :

Example 31 with PersonName

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

the class PersonDAOTest method getPersonName_shouldNotGetPersonNameGivenInvalidId.

@Test
public void getPersonName_shouldNotGetPersonNameGivenInvalidId() {
    PersonName personName = dao.getPersonName(-1);
    assertNull(personName);
}
Also used : PersonName(org.openmrs.PersonName) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 32 with PersonName

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

the class DropMillisecondsHibernateInterceptorTest method shouldClearMillisecondsWhenSavingANewObject.

@Test
public void shouldClearMillisecondsWhenSavingANewObject() {
    Date dateWithMillisecond = new Date(567L);
    Date dateWithoutMillisecond = new Date(0L);
    Person person = new Person();
    person.addName(new PersonName("Alice", null, "Paul"));
    person.setGender("F");
    person.setBirthdate(dateWithMillisecond);
    personService.savePerson(person);
    Context.flushSession();
    assertThat(person.getBirthdate(), is(dateWithoutMillisecond));
}
Also used : PersonName(org.openmrs.PersonName) Person(org.openmrs.Person) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 33 with PersonName

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

the class RequiredDataAdviceTest method setUp.

@Before
public void setUp() {
    Context.setUserContext(userContext);
    context.setServiceContext(serviceContext);
    Context.setContext(serviceContext);
    serviceContext.setApplicationContext(applicationContext);
    User user = new User();
    user.setUuid("1010d442-e134-11de-babe-001e378eb67e");
    user.setUserId(1);
    user.setUsername("admin");
    user.addRole(new Role(RoleConstants.SUPERUSER));
    Person person = new Person();
    person.setUuid("6adb7c42-cfd2-4301-b53b-ff17c5654ff7");
    person.setId(1);
    person.addName(new PersonName("Bob", "", "Smith"));
    Calendar calendar = Calendar.getInstance();
    calendar.set(1980, 01, 01);
    person.setBirthdate(calendar.getTime());
    person.setGender("M");
    user.setPerson(person);
    when(userContext.getAuthenticatedUser()).thenReturn(user);
    when(userContext.isAuthenticated()).thenReturn(true);
    Map<String, SaveHandler> saveHandlers = new HashMap<>();
    saveHandlers.put("saveHandler", saveHandler);
    when(applicationContext.getBeansOfType(SaveHandler.class)).thenReturn(saveHandlers);
    Map<String, VoidHandler> voidHandlers = new HashMap<>();
    voidHandlers.put("voidHandler", voidHandler);
    when(applicationContext.getBeansOfType(VoidHandler.class)).thenReturn(voidHandlers);
    // Clear cache since handlers are updated
    HandlerUtil.clearCachedHandlers();
}
Also used : Role(org.openmrs.Role) BaseVoidHandler(org.openmrs.api.handler.BaseVoidHandler) VoidHandler(org.openmrs.api.handler.VoidHandler) PersonName(org.openmrs.PersonName) SaveHandler(org.openmrs.api.handler.SaveHandler) OpenmrsObjectSaveHandler(org.openmrs.api.handler.OpenmrsObjectSaveHandler) User(org.openmrs.User) HashMap(java.util.HashMap) Calendar(java.util.Calendar) Matchers.anyString(org.mockito.Matchers.anyString) Person(org.openmrs.Person) Before(org.junit.Before)

Example 34 with PersonName

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

the class PersonByNameComparator method comparePersonsByName.

/**
 * Compares two person objects by name
 *
 * @should return negative if personName for person1 comes before that of person2
 * @should return positive if personName for person1 comes after that of person2
 * @should return zero if the givenName middleName and familyName match
 * @should be case insensitive
 * @since 1.8
 */
public static int comparePersonsByName(Person person1, Person person2) {
    // test for null cases (sorting them to be last in a list)
    if (person1 == null || person1.getPersonName() == null) {
        return 1;
    } else if (person2 == null || person2.getPersonName() == null) {
        return -1;
    }
    // if neither are null, do the actual comparison
    PersonName name1 = person1.getPersonName();
    PersonName name2 = person2.getPersonName();
    int ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName() != null ? name1.getFamilyName().toLowerCase() : null, name2.getFamilyName() != null ? name2.getFamilyName().toLowerCase() : null);
    if (ret == 0) {
        ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName2() != null ? name1.getFamilyName().toLowerCase() : null, name2.getFamilyName2() != null ? name2.getFamilyName2().toLowerCase() : null);
    }
    if (ret == 0) {
        ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getGivenName() != null ? name1.getGivenName().toLowerCase() : null, name2.getGivenName() != null ? name2.getGivenName().toLowerCase() : null);
    }
    if (ret == 0) {
        ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getMiddleName() != null ? name1.getMiddleName().toLowerCase() : null, name2.getMiddleName() != null ? name2.getMiddleName().toLowerCase() : null);
    }
    if (ret == 0) {
        ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNamePrefix() != null ? name1.getFamilyNamePrefix().toLowerCase() : null, name2.getFamilyNamePrefix() != null ? name2.getFamilyNamePrefix().toLowerCase() : null);
    }
    if (ret == 0) {
        ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNameSuffix() != null ? name1.getFamilyNameSuffix().toLowerCase() : null, name2.getFamilyNameSuffix() != null ? name2.getFamilyNameSuffix().toLowerCase() : null);
    }
    return ret;
}
Also used : PersonName(org.openmrs.PersonName)

Example 35 with PersonName

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

the class HL7ServiceImpl method createPersonFromNK1.

/**
 * @see org.openmrs.hl7.HL7Service#createPersonFromNK1(ca.uhn.hl7v2.model.v25.segment.NK1)
 */
@Override
public Person createPersonFromNK1(NK1 nk1) throws HL7Exception {
    // NOTE: following block (with minor modifications) stolen from
    // ADTA28Handler
    // TODO: generalize this for use with both PID and NK1 segments
    Person person = new Person();
    // UUID
    CX[] identifiers = nk1.getNextOfKinAssociatedPartySIdentifiers();
    String uuid = getUuidFromIdentifiers(identifiers);
    if (Context.getPersonService().getPersonByUuid(uuid) != null) {
        throw new HL7Exception("Non-unique UUID '" + uuid + "' for new person");
    }
    person.setUuid(uuid);
    // Patient Identifiers
    List<PatientIdentifier> goodIdentifiers = new ArrayList<>();
    for (CX id : identifiers) {
        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) {
                    if (!"UUID".equals(assigningAuthority)) {
                        log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
                    }
                    // skip identifiers with unknown type
                    continue;
                }
                PatientIdentifier pi = new PatientIdentifier();
                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 NK1 is invalid: " + pi, ex);
                }
            } catch (Exception e) {
                log.error("Uncaught error parsing/creating patient identifier '" + hl7PatientId + "' for assigning authority '" + assigningAuthority + "'", e);
            }
        } else {
            log.debug("NK1 contains identifier with no assigning authority");
            continue;
        }
    }
    if (!goodIdentifiers.isEmpty()) {
        // If we have one identifier, set it as the preferred to make the validator happy.
        if (goodIdentifiers.size() == 1) {
            goodIdentifiers.get(0).setPreferred(true);
        }
        // cast the person as a Patient and add identifiers
        person = new Patient(person);
        ((Patient) person).addIdentifiers(goodIdentifiers);
    }
    // Person names
    for (XPN patientNameX : nk1.getNKName()) {
        PersonName name = new PersonName();
        name.setFamilyName(patientNameX.getFamilyName().getSurname().getValue());
        name.setGivenName(patientNameX.getGivenName().getValue());
        name.setMiddleName(patientNameX.getSecondAndFurtherGivenNamesOrInitialsThereof().getValue());
        person.addName(name);
    }
    // Gender (checks for null, but not for 'M' or 'F')
    String gender = nk1.getAdministrativeSex().getValue();
    if (gender == null) {
        throw new HL7Exception("Missing gender in an NK1 segment");
    }
    gender = gender.toUpperCase();
    if (!OpenmrsConstants.GENDER().containsKey(gender)) {
        throw new HL7Exception("Unrecognized gender: " + gender);
    }
    person.setGender(gender);
    // Date of Birth
    TS dateOfBirth = nk1.getDateTimeOfBirth();
    if (dateOfBirth == null || dateOfBirth.getTime() == null || dateOfBirth.getTime().getValue() == null) {
        throw new HL7Exception("Missing birth date in an NK1 segment");
    }
    person.setBirthdate(HL7Util.parseHL7Timestamp(dateOfBirth.getTime().getValue()));
    // 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)) {
            person.setBirthdateEstimated(true);
        }
    }
    // save the new person or patient
    if (person instanceof Patient) {
        Context.getPatientService().savePatient((Patient) person);
    } else {
        Context.getPersonService().savePerson(person);
    }
    return person;
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) 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) CX(ca.uhn.hl7v2.model.v25.datatype.CX) XPN(ca.uhn.hl7v2.model.v25.datatype.XPN) HL7Exception(ca.uhn.hl7v2.HL7Exception) ID(ca.uhn.hl7v2.model.v25.datatype.ID) PID(ca.uhn.hl7v2.model.v25.segment.PID) Person(org.openmrs.Person) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) Location(org.openmrs.Location) TS(ca.uhn.hl7v2.model.v25.datatype.TS)

Aggregations

PersonName (org.openmrs.PersonName)115 Test (org.junit.Test)86 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)57 Patient (org.openmrs.Patient)46 Person (org.openmrs.Person)41 Date (java.util.Date)29 PatientIdentifier (org.openmrs.PatientIdentifier)24 PersonAddress (org.openmrs.PersonAddress)20 User (org.openmrs.User)19 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)17 Location (org.openmrs.Location)13 PatientIdentifierType (org.openmrs.PatientIdentifierType)13 ArrayList (java.util.ArrayList)9 PihCoreContextSensitiveTest (org.openmrs.module.pihcore.PihCoreContextSensitiveTest)8 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 Provider (org.openmrs.Provider)6 Errors (org.springframework.validation.Errors)6