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);
}
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);
}
}
}
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;
}
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);
}
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);
}
}
Aggregations