Browse Source

Five attributes added to CensusEntry validator (2)

Andrew Grant 7 months ago
parent
commit
f79717e6a2

+ 5 - 5
src/main/java/scot/carricksoftware/grants/controllers/census/censusentry/CensusEntryFormControllerImpl.java

@@ -27,7 +27,7 @@ import scot.carricksoftware.grants.services.census.censusentry.CensusEntryServic
 import scot.carricksoftware.grants.services.census.census.CensusService;
 import scot.carricksoftware.grants.services.census.censusentry.UpdateRecordedYearOfBirth;
 import scot.carricksoftware.grants.services.people.PersonService;
-import scot.carricksoftware.grants.validators.census.CensusEntryCommandValidatorImpl;
+import scot.carricksoftware.grants.validators.census.CensusEntryCommandValidator;
 
 @SuppressWarnings("LoggingSimilarMessage")
 @Controller
@@ -35,7 +35,7 @@ public class CensusEntryFormControllerImpl implements CensusEntryFormController
 
     private static final Logger logger = LogManager.getLogger(CensusEntryFormControllerImpl.class);
     private final CensusEntryService censusEntryService;
-    private final CensusEntryCommandValidatorImpl censusEntryCommandValidatorImpl;
+    private final CensusEntryCommandValidator censusEntryCommandValidator;
     private final CensusEntryConverter censusEntryConverter;
     private final Capitalisation capitalisation;
     private final PersonService personService;
@@ -45,13 +45,13 @@ public class CensusEntryFormControllerImpl implements CensusEntryFormController
 
 
     public CensusEntryFormControllerImpl(CensusEntryService censusEntryService,
-                                         CensusEntryCommandValidatorImpl censusEntryCommandValidatorImpl,
+                                         CensusEntryCommandValidator censusEntryCommandValidator,
                                          CensusEntryConverter censusEntryConverter,
                                          Capitalisation capitalisation,
                                          PersonService personService,
                                          CensusService censusService, UpdateRecordedYearOfBirth updateRecordedYearOfBirth) {
         this.censusEntryService = censusEntryService;
-        this.censusEntryCommandValidatorImpl = censusEntryCommandValidatorImpl;
+        this.censusEntryCommandValidator = censusEntryCommandValidator;
         this.censusEntryConverter = censusEntryConverter;
         this.capitalisation = capitalisation;
         this.personService = personService;
@@ -85,7 +85,7 @@ public class CensusEntryFormControllerImpl implements CensusEntryFormController
     public String saveOrUpdate(@Valid @ModelAttribute CensusEntryCommand censusEntryCommand, BindingResult bindingResult, Model model) {
         logger.debug("CensusEntryFormControllerImpl::saveOrUpdate");
 
-        censusEntryCommandValidatorImpl.validate(censusEntryCommand, bindingResult);
+        censusEntryCommandValidator.validate(censusEntryCommand, bindingResult);
         censusEntryCommand.setName(capitalisation.getCapitalisation(censusEntryCommand.getName()));
 
         if (bindingResult.hasErrors()) {

+ 84 - 71
src/main/java/scot/carricksoftware/grants/validators/census/CensusEntryCommandValidatorImpl.java

@@ -37,16 +37,16 @@ public class CensusEntryCommandValidatorImpl implements CensusEntryCommandValida
 
     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);
+            validateIntegerNoLimits(censusEntryCommand.getChildrenBornAlive(), "childrenBornAlive", bindingResult);
         }
         if (censusEntryCommand.getChildrenStillAlive() != null && !censusEntryCommand.getChildrenStillAlive().isEmpty()) {
-            validateInteger(censusEntryCommand.getChildrenStillAlive(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "childrenStillAlive", bindingResult);
+            validateIntegerNoLimits(censusEntryCommand.getChildrenStillAlive(), "childrenStillAlive", bindingResult);
         }
         if (censusEntryCommand.getChildrenWhoHaveDied() != null && !censusEntryCommand.getChildrenWhoHaveDied().isEmpty()) {
-            validateInteger(censusEntryCommand.getChildrenWhoHaveDied(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "childrenWhoHaveDied", bindingResult);
+            validateIntegerNoLimits(censusEntryCommand.getChildrenWhoHaveDied(), "childrenWhoHaveDied", bindingResult);
         }
         if (censusEntryCommand.getYearsCompletedMarriage() != null && !censusEntryCommand.getYearsCompletedMarriage().isEmpty()) {
-            validateInteger(censusEntryCommand.getYearsCompletedMarriage(), 1, 100, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "yearsCompletedMarriage", bindingResult);
+            validateIntegerNoLimits(censusEntryCommand.getYearsCompletedMarriage(), "yearsCompletedMarriage", bindingResult);
         }
         if (censusEntryCommand.getBirthYear() != null && !censusEntryCommand.getBirthYear().isEmpty()) {
             validateInteger(censusEntryCommand.getBirthYear(), 1800, 2050, ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER, "birthYear", bindingResult);
@@ -69,96 +69,109 @@ public class CensusEntryCommandValidatorImpl implements CensusEntryCommandValida
     }
 
 
+    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);
 
-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);
+            }
+        }
+    }
 
-            bindingResult.rejectValue("age", ApplicationConstants.EMPTY_STRING,
+    private void validateBirthDay(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
+        String[] parts = censusEntryCommand.getBirthDay().split("/");
+        if (parts.length != 2) {
+            bindingResult.rejectValue("birthDay", ApplicationConstants.EMPTY_STRING,
                     null,
-                    ValidationConstants.BIRTH_DAY_AND_AGE_CANNOT_COEXIST);
+                    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 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;
+            }
+        } catch (RuntimeException e) {
+            invalid = true;
+        }
+        if (invalid) {
+            bindingResult.rejectValue(field, ApplicationConstants.EMPTY_STRING,
+                    null,
+                    validationConstant);
+        }
     }
-}
 
-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) {
+    private void validateIntegerNoLimits(String part, String field, BindingResult bindingResult) {
+        boolean invalid = false;
+        try {
+            Integer.parseInt(part);
+        } catch (RuntimeException e) {
             invalid = true;
         }
-    } catch (RuntimeException e) {
-        invalid = true;
-    }
-    if (invalid) {
-        bindingResult.rejectValue(field, ApplicationConstants.EMPTY_STRING,
-                null,
-                validationConstant);
+        if (invalid) {
+            bindingResult.rejectValue(field, ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER);
+        }
     }
-}
 
 
-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);
+    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);
+            }
         } else {
-            validateUntrackedPerson(censusEntryCommand, bindingResult);
-        }
-    } 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);
+            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);
+            }
         }
     }
-}
 
-private void validateCensus(CensusEntryCommand censusEntryCommand, BindingResult bindingResult) {
-    if (censusEntryCommand.getCensus() == null) {
-        bindingResult.rejectValue("census", ApplicationConstants.EMPTY_STRING,
-                null,
-                ValidationConstants.CENSUS_IS_NULL);
+    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) {
+    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_LONG);
+                    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);
+            }
         }
     }
-}
 
 
 }