Search in sources :

Example 21 with PersonName

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

the class PersonByNameComparatorTest method comparePersonsByName_shouldReturnZeroIfTheGivenNameMiddleNameAndFamilyNameMatch.

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

Example 22 with PersonName

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

the class PatientServiceImpl method setPreferredPatientName.

private void setPreferredPatientName(Patient patient) {
    PersonName preferredName = null;
    PersonName possiblePreferredName = patient.getPersonName();
    if (possiblePreferredName != null && possiblePreferredName.getPreferred() && !possiblePreferredName.getVoided()) {
        preferredName = possiblePreferredName;
    }
    for (PersonName name : patient.getNames()) {
        if (preferredName == null && !name.getVoided()) {
            name.setPreferred(true);
            preferredName = name;
            continue;
        }
        if (!name.equals(preferredName)) {
            name.setPreferred(false);
        }
    }
}
Also used : PersonName(org.openmrs.PersonName)

Example 23 with PersonName

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

the class HibernatePersonDAO method getPeople.

/**
 * @see org.openmrs.api.db.PersonDAO#getPeople(java.lang.String, java.lang.Boolean)
 * @should get no one by null
 * @should get every one by empty string
 * @should get no one by non-existing attribute
 * @should get no one by non-searchable attribute
 * @should get no one by voided attribute
 * @should get one person by attribute
 * @should get one person by random case attribute
 * @should get one person by searching for a mix of attribute and voided attribute
 * @should get multiple people by single attribute
 * @should get multiple people by multiple attributes
 * @should get no one by non-existing name
 * @should get one person by name
 * @should get one person by random case name
 * @should get multiple people by single name
 * @should get multiple people by multiple names
 * @should get no one by non-existing name and non-existing attribute
 * @should get no one by non-existing name and non-searchable attribute
 * @should get no one by non-existing name and voided attribute
 * @should get one person by name and attribute
 * @should get one person by name and voided attribute
 * @should get multiple people by name and attribute
 * @should get one person by given name
 * @should get multiple people by given name
 * @should get one person by middle name
 * @should get multiple people by middle name
 * @should get one person by family name
 * @should get multiple people by family name
 * @should get one person by family name2
 * @should get multiple people by family name2
 * @should get one person by multiple name parts
 * @should get multiple people by multiple name parts
 * @should get no one by voided name
 * @should not get voided person
 * @should not get dead person
 * @should get single dead person
 * @should get multiple dead people
 */
@Override
@SuppressWarnings("unchecked")
public List<Person> getPeople(String searchString, Boolean dead, Boolean voided) {
    if (searchString == null) {
        return new ArrayList<>();
    }
    int maxResults = HibernatePersonDAO.getMaximumSearchResults();
    boolean includeVoided = (voided != null) ? voided : false;
    if (StringUtils.isBlank(searchString)) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
        if (dead != null) {
            criteria.add(Restrictions.eq("dead", dead));
        }
        if (!includeVoided) {
            criteria.add(Restrictions.eq("personVoided", false));
        }
        criteria.setMaxResults(maxResults);
        return criteria.list();
    }
    String query = LuceneQuery.escapeQuery(searchString);
    PersonLuceneQuery personLuceneQuery = new PersonLuceneQuery(sessionFactory);
    LuceneQuery<PersonName> nameQuery = personLuceneQuery.getPersonNameQueryWithOrParser(query, includeVoided);
    if (dead != null) {
        nameQuery.include("person.dead", dead);
    }
    List<Person> people = new ArrayList<>();
    ListPart<Object[]> names = nameQuery.listPartProjection(0, maxResults, "person.personId");
    names.getList().forEach(name -> people.add(getPerson((Integer) name[0])));
    LuceneQuery<PersonAttribute> attributeQuery = personLuceneQuery.getPersonAttributeQueryWithOrParser(query, includeVoided, nameQuery);
    ListPart<Object[]> attributes = attributeQuery.listPartProjection(0, maxResults, "person.personId");
    attributes.getList().forEach(attribute -> people.add(getPerson((Integer) attribute[0])));
    return people;
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) PersonAttribute(org.openmrs.PersonAttribute) Person(org.openmrs.Person)

Example 24 with PersonName

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

the class HibernatePersonDAO method deletePersonAndAttributes.

/**
 * Used by deletePerson, deletePatient, and deleteUser to remove all properties of a person
 * before deleting them.
 *
 * @param sessionFactory the session factory from which to pull the current session
 * @param person the person to delete
 */
public static void deletePersonAndAttributes(SessionFactory sessionFactory, Person person) {
    // delete properties and fields so hibernate can't complain
    for (PersonAddress address : person.getAddresses()) {
        if (address.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(address);
        } else {
            sessionFactory.getCurrentSession().delete(address);
        }
    }
    person.setAddresses(null);
    for (PersonAttribute attribute : person.getAttributes()) {
        if (attribute.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(attribute);
        } else {
            sessionFactory.getCurrentSession().delete(attribute);
        }
    }
    person.setAttributes(null);
    for (PersonName name : person.getNames()) {
        if (name.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(name);
        } else {
            sessionFactory.getCurrentSession().delete(name);
        }
    }
    person.setNames(null);
    // finally, just tell hibernate to delete our object
    sessionFactory.getCurrentSession().delete(person);
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

Example 25 with PersonName

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

the class PersonSaveHandler method handle.

/**
 * @see org.openmrs.api.handler.SaveHandler#handle(org.openmrs.OpenmrsObject, org.openmrs.User,
 *      java.util.Date, java.lang.String)
 */
@Override
public void handle(Person person, User creator, Date dateCreated, String other) {
    // address collection
    if (person.getAddresses() != null && !person.getAddresses().isEmpty()) {
        for (PersonAddress pAddress : person.getAddresses()) {
            if (pAddress.isBlank()) {
                person.removeAddress(pAddress);
                continue;
            }
            pAddress.setPerson(person);
        }
    }
    // name collection
    if (person.getNames() != null && !person.getNames().isEmpty()) {
        for (PersonName pName : person.getNames()) {
            pName.setPerson(person);
        }
    }
    // attribute collection
    if (person.getAttributes() != null && !person.getAttributes().isEmpty()) {
        for (PersonAttribute pAttr : person.getAttributes()) {
            pAttr.setPerson(person);
        }
    }
    // if the patient was marked as dead and reversed, drop the cause of death
    if (!person.getDead() && person.getCauseOfDeath() != null) {
        person.setCauseOfDeath(null);
    }
    // do the checks for voided attributes (also in PersonVoidHandler)
    if (person.getPersonVoided()) {
        if (!StringUtils.hasLength(person.getPersonVoidReason())) {
            throw new APIException("Person.voided.bit", new Object[] { person });
        }
        if (person.getPersonVoidedBy() == null) {
            person.setPersonVoidedBy(creator);
        }
        if (person.getPersonDateVoided() == null) {
            person.setPersonDateVoided(dateCreated);
        }
    } else {
        // voided is set to false
        person.setPersonVoidedBy(null);
        person.setPersonDateVoided(null);
        person.setPersonVoidReason(null);
    }
}
Also used : PersonName(org.openmrs.PersonName) APIException(org.openmrs.api.APIException) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

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