Skip to content

Commit

Permalink
Merge pull request #1857 from samshuster/TRUNK-4869
Browse files Browse the repository at this point in the history
TRUNK-4869: added deathDate validation if it is set in future. BirthD…
  • Loading branch information
dkayiwa committed Oct 16, 2016
2 parents 6e98a8c + 2ffdf76 commit c907036
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
61 changes: 49 additions & 12 deletions api/src/main/java/org/openmrs/validator/PersonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public boolean supports(Class<?> clazz) {
* 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 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
Expand Down Expand Up @@ -81,7 +82,7 @@ public void validate(Object target, Errors errors) {
errors.rejectValue("names", "Person.shouldHaveAtleastOneNonVoidedName");
}

//validate the personAddress
// validate the personAddress
index = 0;
for (PersonAddress address : person.getAddresses()) {
try {
Expand All @@ -95,6 +96,7 @@ public void validate(Object target, Errors errors) {
}

validateBirthDate(errors, person.getBirthdate());
validateDeathDate(errors, person.getDeathDate());

if (person.isVoided()) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
Expand All @@ -113,17 +115,52 @@ public void validate(Object target, Errors errors) {
* @param errors Stores information about errors encountered during validation.
*/
private void validateBirthDate(Errors errors, Date birthDate) {
if (birthDate != null) {
if (birthDate.after(new Date())) {
errors.rejectValue("birthdate", "error.date.future");
} else {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -120);
if (birthDate.before(c.getTime())) {
errors.rejectValue("birthdate", "error.date.nonsensical");
}
}
if (birthDate == null) {
return;
}
rejectIfFutureDate(errors, birthDate, "birthdate");
rejectDateIfBefore120YearsAgo(errors, birthDate, "birthdate");
}

/**
* Checks if the death date is in the future.
*
* @param errors Stores information about errors encountered during validation
* @param deathDate the deathdate to validate
*/
private void validateDeathDate(Errors errors, Date deathDate) {
if (deathDate == null) {
return;
}
rejectIfFutureDate(errors, deathDate, "deathDate");
}

/**
* Rejects a date if it is in the future.
*
* @param errors the error object
* @param date the date to check
* @param dateField the name of the field
*/
private void rejectIfFutureDate(Errors errors, Date date, String dateField) {
if (date.after(new Date())) {
errors.rejectValue(dateField, "error.date.future");
}
}

/**
* Rejects a date if it is before 120 years ago.
*
* @param errors the error object
* @param date the date to check
* @param dateField the name of the field
*/
private void rejectDateIfBefore120YearsAgo(Errors errors, Date date, String dateField) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -120);
if (date.before(c.getTime())) {
errors.rejectValue(dateField, "error.date.nonsensical");
}
}

Expand Down
19 changes: 19 additions & 0 deletions api/src/test/java/org/openmrs/validator/PersonValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public void validate_shouldFailValidationIfBirthdateIsAFutureDate() throws Excep
Assert.assertTrue(errors.hasFieldErrors("birthdate"));
}

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if deathdate is a future date
*/

@Test
@Verifies(value = "should fail validation if deathdate is a future date", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfDeathDateIsAFutureDate() throws Exception {
Patient pa = new Patient(1);
Calendar death = Calendar.getInstance();
death.setTime(new Date());
death.add(Calendar.YEAR, 20);
pa.setDeathDate(death.getTime());
Errors errors = new BindException(pa, "patient");
validator.validate(pa, errors);

Assert.assertTrue(errors.hasFieldErrors("deathDate"));
}

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if birthdate makes patient older that 120 years old
Expand Down

0 comments on commit c907036

Please sign in to comment.