Browse Source

Five attributes added to CensusEntry validator

Andrew Grant 7 months ago
parent
commit
0e798df3d7

+ 92 - 75
src/main/java/scot/carricksoftware/grants/validators/census/CensusEntryCommandValidatorImpl.java

@@ -22,34 +22,41 @@ public class CensusEntryCommandValidatorImpl implements CensusEntryCommandValida
     public void validate(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
         logger.debug("censusEntryCommandValidator::validate");
         validateCensus(censusEntryCommand, bindingResult);
-        validateNameAndPerson(censusEntryCommand, bindingResult);
+        validateCoExistences(censusEntryCommand, bindingResult);
+        validateIntegers(censusEntryCommand, bindingResult);
         if (censusEntryCommand.getBirthDay() != null && !censusEntryCommand.getBirthDay().isEmpty()) {
             validateBirthDay(censusEntryCommand, bindingResult);
         }
-        if (censusEntryCommand.getBirthYear() != null  && !censusEntryCommand.getBirthYear().isEmpty()) {
-            validateBirthYear(censusEntryCommand, bindingResult);
-        }
+    }
+
+    private void validateCoExistences(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+        validateNameAndPerson(censusEntryCommand, bindingResult);
         validateAgeAndBirthYear(censusEntryCommand, bindingResult);
         validateAgeAndBirthDay(censusEntryCommand, bindingResult);
     }
 
-    private void validateAgeAndBirthDay(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        if (censusEntryCommand.getBirthDay() != null && !censusEntryCommand.getBirthDay().isEmpty()) {
-            if (censusEntryCommand.getAge() != null && !censusEntryCommand.getAge().isEmpty() ) {
-                bindingResult.rejectValue("birthDay", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.BIRTH_DAY_AND_AGE_CANNOT_COEXIST);
-
-                bindingResult.rejectValue("age", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.BIRTH_DAY_AND_AGE_CANNOT_COEXIST);
-            }
+    private void validateIntegers(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+        if (censusEntryCommand.getChildrenBornAlive() != null && !censusEntryCommand.getChildrenBornAlive().isEmpty()) {
+            validateInteger(censusEntryCommand.getChildrenBornAlive(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "childrenBornAlive", bindingResult);
+        }
+        if (censusEntryCommand.getChildrenStillAlive() != null && !censusEntryCommand.getChildrenStillAlive().isEmpty()) {
+            validateInteger(censusEntryCommand.getChildrenStillAlive(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "childrenStillAlive", bindingResult);
+        }
+        if (censusEntryCommand.getChildrenWhoHaveDied() != null && !censusEntryCommand.getChildrenWhoHaveDied().isEmpty()) {
+            validateInteger(censusEntryCommand.getChildrenWhoHaveDied(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "childrenWhoHaveDied", bindingResult);
+        }
+        if (censusEntryCommand.getYearsCompletedMarriage() != null && !censusEntryCommand.getYearsCompletedMarriage().isEmpty()) {
+            validateInteger(censusEntryCommand.getYearsCompletedMarriage(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "yearsCompletedMarriage", bindingResult);
+        }
+        if (censusEntryCommand.getBirthYear() != null && !censusEntryCommand.getBirthYear().isEmpty()) {
+            validateInteger(censusEntryCommand.getBirthYear(), 1800, 2050, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "birthYear", bindingResult);
         }
     }
 
+
     private void validateAgeAndBirthYear(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
         if (censusEntryCommand.getBirthYear() != null && !censusEntryCommand.getBirthYear().isEmpty()) {
-            if (censusEntryCommand.getAge() != null && !censusEntryCommand.getAge().isEmpty() ) {
+            if (censusEntryCommand.getAge() != null && !censusEntryCommand.getAge().isEmpty()) {
                 bindingResult.rejectValue("birthYear", ApplicationConstants.EMPTY_STRING,
                         null,
                         ValidationConstants.BIRTH_YEAR_AND_AGE_CANNOT_COEXIST);
@@ -61,87 +68,97 @@ public class CensusEntryCommandValidatorImpl implements CensusEntryCommandValida
         }
     }
 
-    private void validateBirthDay(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        String[] parts = censusEntryCommand.getBirthDay().split("/");
-        if (parts.length != 2) {
+
+
+private void validateAgeAndBirthDay(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+    if (censusEntryCommand.getBirthDay() != null && !censusEntryCommand.getBirthDay().isEmpty()) {
+        if (censusEntryCommand.getAge() != null && !censusEntryCommand.getAge().isEmpty()) {
             bindingResult.rejectValue("birthDay", ApplicationConstants.EMPTY_STRING,
                     null,
-                    ValidationConstants.BIRTHDAY_INCORRECT_FORMAT);
-        } else {
-            validateInteger(parts[0], 1, 31, ValidationConstants.BIRTHDAY_INCORRECT_FORMAT, "birthDay", bindingResult);
-            validateInteger(parts[1], 1, 12, ValidationConstants.BIRTHDAY_INCORRECT_FORMAT, "birthDay", bindingResult);
+                    ValidationConstants.BIRTH_DAY_AND_AGE_CANNOT_COEXIST);
 
+            bindingResult.rejectValue("age", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.BIRTH_DAY_AND_AGE_CANNOT_COEXIST);
         }
     }
+}
 
-    private void validateInteger(String part, int low, int high, String validationConstant, String field, BindingResult bindingResult) {
-        boolean invalid = false;
-        try {
-            int value = Integer.parseInt(part);
-            if (value < low || value > high) {
-                invalid = true;
-            }
-        } catch (RuntimeException e) {
+private void validateBirthDay(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+    String[] parts = censusEntryCommand.getBirthDay().split("/");
+    if (parts.length != 2) {
+        bindingResult.rejectValue("birthDay", ApplicationConstants.EMPTY_STRING,
+                null,
+                ValidationConstants.BIRTHDAY_INCORRECT_FORMAT);
+    } else {
+        validateInteger(parts[0], 1, 31, ValidationConstants.BIRTHDAY_INCORRECT_FORMAT, "birthDay", bindingResult);
+        validateInteger(parts[1], 1, 12, ValidationConstants.BIRTHDAY_INCORRECT_FORMAT, "birthDay", bindingResult);
+    }
+}
+
+private void validateInteger(String part, int low, int high, String validationConstant, String field, BindingResult bindingResult) {
+    boolean invalid = false;
+    try {
+        int value = Integer.parseInt(part);
+        if (value < low || value > high) {
             invalid = true;
         }
-        if (invalid) {
-            bindingResult.rejectValue(field, ApplicationConstants.EMPTY_STRING,
-                    null,
-                    validationConstant);
-        }
+    } catch (RuntimeException e) {
+        invalid = true;
     }
-
-    private void validateBirthYear(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        validateInteger(censusEntryCommand.getBirthYear(), 1800, 2050, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "birthYear", bindingResult);
+    if (invalid) {
+        bindingResult.rejectValue(field, ApplicationConstants.EMPTY_STRING,
+                null,
+                validationConstant);
     }
+}
 
 
-    private void validateNameAndPerson(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        if (censusEntryCommand.getPerson() == null) {
-            if (censusEntryCommand.getName() == null || censusEntryCommand.getName().isEmpty()) {
-                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.CENSUS_NAME_IS_NULL);
-                bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.CENSUS_NAME_IS_NULL);
-            } else {
-                validateUntrackedPerson(censusEntryCommand, bindingResult);
-            }
+private void validateNameAndPerson(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+    if (censusEntryCommand.getPerson() == null) {
+        if (censusEntryCommand.getName() == null || censusEntryCommand.getName().isEmpty()) {
+            bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CENSUS_NAME_IS_NULL);
+            bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CENSUS_NAME_IS_NULL);
         } else {
-            if (censusEntryCommand.getName() != null && !censusEntryCommand.getName().isEmpty()) {
-                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.CENSUS_NAME_IS_NOT_NULL);
-                bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.CENSUS_NAME_IS_NOT_NULL);
-            }
+            validateUntrackedPerson(censusEntryCommand, bindingResult);
         }
-    }
-
-    private void validateCensus(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        if (censusEntryCommand.getCensus() == null) {
-            bindingResult.rejectValue("census", ApplicationConstants.EMPTY_STRING,
+    } else {
+        if (censusEntryCommand.getName() != null && !censusEntryCommand.getName().isEmpty()) {
+            bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CENSUS_NAME_IS_NOT_NULL);
+            bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
                     null,
-                    ValidationConstants.CENSUS_IS_NULL);
+                    ValidationConstants.CENSUS_NAME_IS_NOT_NULL);
         }
     }
+}
 
-    private void validateUntrackedPerson(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-        if (censusEntryCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
+private void validateCensus(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+    if (censusEntryCommand.getCensus() == null) {
+        bindingResult.rejectValue("census", ApplicationConstants.EMPTY_STRING,
+                null,
+                ValidationConstants.CENSUS_IS_NULL);
+    }
+}
+
+private void validateUntrackedPerson(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+    if (censusEntryCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
+        bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                null,
+                ValidationConstants.NAME_IS_TOO_SHORT);
+    } else {
+        if (censusEntryCommand.getName().length() > ApplicationConstants.MAXIMUM_NAME_LENGTH) {
             bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
                     null,
-                    ValidationConstants.NAME_IS_TOO_SHORT);
-        } else {
-            if (censusEntryCommand.getName().length() > ApplicationConstants.MAXIMUM_NAME_LENGTH) {
-                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.NAME_IS_TOO_LONG);
-            }
+                    ValidationConstants.NAME_IS_TOO_LONG);
         }
     }
-
+}
 
 
 }