Search in sources :

Example 16 with PersonName

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

the class PersonValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if birthdate makes patient older that 120 years old
 * @should fail validation if birthdate is a future date
 * @should fail validation if deathdate is a future date
 * @should fail validation if birthdate is after death date
 * @should fail validation if voidReason is blank when patient is voided
 * @should fail validation if causeOfDeath is blank when patient is dead
 * @should pass validation if gender is blank for Persons
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object target, Errors errors) {
    log.debug("{}.validate...", this.getClass().getName());
    if (target == null) {
        return;
    }
    Person person = (Person) target;
    int index = 0;
    boolean atLeastOneNonVoidPersonNameLeft = false;
    for (PersonName personName : person.getNames()) {
        errors.pushNestedPath("names[" + index + "]");
        personNameValidator.validate(personName, errors);
        if (!personName.getVoided()) {
            atLeastOneNonVoidPersonNameLeft = true;
        }
        errors.popNestedPath();
        index++;
    }
    if (!person.getVoided() && !atLeastOneNonVoidPersonNameLeft) {
        errors.rejectValue("names", "Person.shouldHaveAtleastOneNonVoidedName");
    }
    // validate the personAddress
    index = 0;
    for (PersonAddress address : person.getAddresses()) {
        try {
            errors.pushNestedPath("addresses[" + index + "]");
            ValidationUtils.invokeValidator(personAddressValidator, address, errors);
        } finally {
            errors.popNestedPath();
            index++;
        }
    }
    validateBirthDate(errors, person.getBirthdate());
    validateDeathDate(errors, person.getDeathDate(), person.getBirthdate());
    if (person.getVoided()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
    }
    if (person.getDead()) {
        ValidationUtils.rejectIfEmpty(errors, "causeOfDeath", "Person.dead.causeOfDeathNull");
    }
    ValidateUtil.validateFieldLengths(errors, Person.class, "gender", "personVoidReason");
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Person(org.openmrs.Person)

Example 17 with PersonName

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

the class PatientValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see PatientValidator#validate(Object,Errors)
 */
@Override
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    PatientIdentifierType patientIdentifierType = Context.getPatientService().getAllPatientIdentifierTypes(false).get(0);
    Patient patient = new Patient();
    PersonName pName = new PersonName();
    pName.setGivenName("Tom");
    pName.setMiddleName("E.");
    pName.setFamilyName("Patient");
    patient.addName(pName);
    patient.setGender("male");
    PersonAddress pAddress = new PersonAddress();
    pAddress.setAddress1("123 My street");
    pAddress.setAddress2("Apt 402");
    pAddress.setCityVillage("Anywhere city");
    pAddress.setCountry("Some Country");
    Set<PersonAddress> pAddressList = patient.getAddresses();
    pAddressList.add(pAddress);
    patient.setAddresses(pAddressList);
    patient.addAddress(pAddress);
    PatientIdentifier patientIdentifier1 = new PatientIdentifier();
    patientIdentifier1.setLocation(new Location(1));
    patientIdentifier1.setIdentifier("012345678");
    patientIdentifier1.setDateCreated(new Date());
    patientIdentifier1.setIdentifierType(patientIdentifierType);
    patient.addIdentifier(patientIdentifier1);
    patient.setVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(patient, "patient");
    validator.validate(patient, errors);
    Assert.assertTrue(errors.hasFieldErrors("voidReason"));
}
Also used : Errors(org.springframework.validation.Errors) PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Date(java.util.Date) Location(org.openmrs.Location) Test(org.junit.Test)

Example 18 with PersonName

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

the class PatientValidatorTest method validate_shouldNotFailWhenPatientHasOnlyOneIdentifierAndItsNotPreferred.

@Test
public void validate_shouldNotFailWhenPatientHasOnlyOneIdentifierAndItsNotPreferred() {
    PatientIdentifierType patientIdentifierType = Context.getPatientService().getAllPatientIdentifierTypes(false).get(0);
    Patient patient = new Patient();
    PersonName pName = new PersonName();
    pName.setGivenName("Tom");
    pName.setMiddleName("E.");
    pName.setFamilyName("Patient");
    patient.addName(pName);
    patient.setGender("male");
    PersonAddress pAddress = new PersonAddress();
    pAddress.setAddress1("123 My street");
    pAddress.setAddress2("Apt 402");
    pAddress.setCityVillage("Anywhere city");
    pAddress.setCountry("Some Country");
    Set<PersonAddress> pAddressList = patient.getAddresses();
    pAddressList.add(pAddress);
    patient.setAddresses(pAddressList);
    patient.addAddress(pAddress);
    PatientIdentifier patientIdentifier1 = new PatientIdentifier();
    patientIdentifier1.setLocation(new Location(1));
    patientIdentifier1.setIdentifier("012345678");
    patientIdentifier1.setDateCreated(new Date());
    patientIdentifier1.setIdentifierType(patientIdentifierType);
    patient.addIdentifier(patientIdentifier1);
    Errors errors = new BindException(patient, "patient");
    validator.validate(patient, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Date(java.util.Date) Location(org.openmrs.Location) Test(org.junit.Test)

Example 19 with PersonName

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

the class PersonNameValidatorTest method validate_shouldReportErrorsOnCorrectFieldNames.

/**
 * @see PersonNameValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldReportErrorsOnCorrectFieldNames() {
    PersonName personName = new PersonName("", "reb", "feb");
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "personName");
    validator.validate(personName, errors);
    assertThat(errors, hasFieldErrors("givenName"));
}
Also used : PersonName(org.openmrs.PersonName) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with PersonName

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

the class PersonNameValidatorTest method validatePersonName_shouldReportErrorsWithNonStandardPrefixWhenCalledInHistoricWay.

/**
 * @see PersonNameValidator#validatePersonName(PersonName, Errors, Boolean, Boolean)
 */
@Test
public void validatePersonName_shouldReportErrorsWithNonStandardPrefixWhenCalledInHistoricWay() {
    PersonName personName = new PersonName("", "reb", "feb");
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "personName");
    validator.validatePersonName(personName, errors, true, false);
    assertThat(errors, hasFieldErrors("names[0]." + "givenName"));
}
Also used : PersonName(org.openmrs.PersonName) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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