use of org.openmrs.Person in project openmrs-core by openmrs.
the class UserValidator method validate.
/**
* Checks the form object for any inconsistencies/errors
*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail validation if retired and retireReason is null
* @should fail validation if retired and retireReason is empty
* @should fail validation if retired and retireReason is whitespace
* @should pass validation if all required fields have proper values
* @should fail validation if email as username enabled and email invalid
* @should fail validation if email as username disabled and email provided
* @should not throw NPE when user is null
* @should pass validation if field lengths are correct
* @should fail validation if field lengths are not correct
*/
@Override
public void validate(Object obj, Errors errors) {
User user = (User) obj;
if (user == null) {
errors.reject("error.general");
} else {
if (user.getRetired() && StringUtils.isBlank(user.getRetireReason())) {
errors.rejectValue("retireReason", "error.null");
}
if (user.getPerson() == null) {
errors.rejectValue("person", "error.null");
} else {
// check that required person details are filled out
Person person = user.getPerson();
if (person.getGender() == null) {
errors.rejectValue("person.gender", "error.null");
}
if (person.getDead() == null) {
errors.rejectValue("person.dead", "error.null");
}
if (person.getVoided() == null) {
errors.rejectValue("person.voided", "error.null");
}
if (person.getPersonName() == null || StringUtils.isEmpty(person.getPersonName().getFullName())) {
errors.rejectValue("person", "Person.names.length");
}
errors.pushNestedPath("person");
try {
personValidator.validate(person, errors);
} finally {
errors.popNestedPath();
}
}
AdministrationService as = Context.getAdministrationService();
boolean emailAsUsername;
try {
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
emailAsUsername = Boolean.parseBoolean(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false"));
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}
if (emailAsUsername) {
boolean isValidUserName = isUserNameAsEmailValid(user.getUsername());
if (!isValidUserName) {
errors.rejectValue("username", "error.username.email");
}
} else {
boolean isValidUserName = isUserNameValid(user.getUsername());
if (!isValidUserName) {
errors.rejectValue("username", "error.username.pattern");
}
}
ValidateUtil.validateFieldLengths(errors, obj.getClass(), "username", "systemId", "retireReason");
}
}
use of org.openmrs.Person 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.Person 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.Person in project openmrs-core by openmrs.
the class ObsServiceImpl method getObservations.
/**
* This implementation queries the obs table comparing the given <code>searchString</code> with
* the patient's identifier, encounterId, and obsId
*
* @see org.openmrs.api.ObsService#getObservations(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public List<Obs> getObservations(String searchString) {
// search on patient identifier
PatientService ps = Context.getPatientService();
List<Patient> patients = ps.getPatients(searchString);
List<Person> persons = new ArrayList<>(patients);
// try to search on encounterId
EncounterService es = Context.getEncounterService();
List<Encounter> encounters = new ArrayList<>();
try {
Encounter e = es.getEncounter(Integer.valueOf(searchString));
if (e != null) {
encounters.add(e);
}
} catch (NumberFormatException e) {
// pass
}
List<Obs> returnList = new ArrayList<>();
if (!encounters.isEmpty() || !persons.isEmpty()) {
returnList = Context.getObsService().getObservations(persons, encounters, null, null, null, null, null, null, null, null, null, false);
}
// try to search on obsId
try {
Obs o = getObs(Integer.valueOf(searchString));
if (o != null) {
returnList.add(o);
}
} catch (NumberFormatException e) {
// pass
}
return returnList;
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class PersonMergeLogValidatorTest method validate_shouldPassValidationIfAllFieldsAreCorrect.
/**
* @see PersonMergeLogValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldPassValidationIfAllFieldsAreCorrect() {
PersonMergeLog personMergeLog = new PersonMergeLog();
personMergeLog.setWinner(new Person());
personMergeLog.setLoser(new Person());
personMergeLog.setPersonMergeLogData(new PersonMergeLogData());
PersonMergeLogValidator validator = new PersonMergeLogValidator();
Errors errors = new BindException(personMergeLog, "personMergeLog");
validator.validate(personMergeLog, errors);
Assert.assertFalse(errors.hasFieldErrors());
}
Aggregations