Search in sources :

Example 26 with Person

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");
    }
}
Also used : User(org.openmrs.User) AdministrationService(org.openmrs.api.AdministrationService) Person(org.openmrs.Person)

Example 27 with Person

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);
}
Also used : PersonName(org.openmrs.PersonName) Person(org.openmrs.Person) Test(org.junit.Test)

Example 28 with Person

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;
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) PersonAttribute(org.openmrs.PersonAttribute) Person(org.openmrs.Person)

Example 29 with Person

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;
}
Also used : Obs(org.openmrs.Obs) PatientService(org.openmrs.api.PatientService) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) Person(org.openmrs.Person) EncounterService(org.openmrs.api.EncounterService) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with Person

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());
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) PersonMergeLog(org.openmrs.person.PersonMergeLog) Person(org.openmrs.Person) PersonMergeLogData(org.openmrs.person.PersonMergeLogData) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Person (org.openmrs.Person)167 Test (org.junit.Test)140 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)107 PersonName (org.openmrs.PersonName)39 Date (java.util.Date)33 User (org.openmrs.User)33 Relationship (org.openmrs.Relationship)19 Obs (org.openmrs.Obs)16 Patient (org.openmrs.Patient)15 BindException (org.springframework.validation.BindException)15 Message (ca.uhn.hl7v2.model.Message)14 Concept (org.openmrs.Concept)14 Voidable (org.openmrs.Voidable)14 Errors (org.springframework.validation.Errors)14 ArrayList (java.util.ArrayList)10 Provider (org.openmrs.Provider)10 PersonMergeLog (org.openmrs.person.PersonMergeLog)9 RelationshipType (org.openmrs.RelationshipType)8 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)7 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)7