瀏覽代碼

Rooms Occupied, Inhabited Houses, Uninhabited Houses to validator

Andrew Grant 6 月之前
父節點
當前提交
c0cb996f3e

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

@@ -24,7 +24,7 @@ import scot.carricksoftware.grants.constants.ViewConstants;
 import scot.carricksoftware.grants.converters.census.CensusConverter;
 import scot.carricksoftware.grants.services.census.census.CensusService;
 import scot.carricksoftware.grants.services.places.places.PlaceService;
-import scot.carricksoftware.grants.validators.census.CensusCommandValidatorImpl;
+import scot.carricksoftware.grants.validators.census.CensusCommandValidator;
 
 @SuppressWarnings("LoggingSimilarMessage")
 @Controller
@@ -32,16 +32,16 @@ public class CensusFormControllerImpl implements CensusFormController {
 
     private static final Logger logger = LogManager.getLogger(CensusFormControllerImpl.class);
     private final CensusService censusService;
-    private final CensusCommandValidatorImpl censusCommandValidatorImpl;
+    private final CensusCommandValidator censusCommandValidator;
     private final CensusConverter censusConverter;
     private final PlaceService placeService;
 
 
     public CensusFormControllerImpl(CensusService censusService,
-                                    CensusCommandValidatorImpl censusCommandValidatorImpl,
+                                    CensusCommandValidator censusCommandValidator,
                                     CensusConverter censusConverter, PlaceService placeService) {
         this.censusService = censusService;
-        this.censusCommandValidatorImpl = censusCommandValidatorImpl;
+        this.censusCommandValidator = censusCommandValidator;
         this.censusConverter = censusConverter;
         this.placeService = placeService;
     }
@@ -70,7 +70,7 @@ public class CensusFormControllerImpl implements CensusFormController {
     public String saveOrUpdate(@Valid @ModelAttribute CensusCommand censusCommand, BindingResult bindingResult, Model model) {
         logger.debug("CensusFormControllerImpl::saveOrUpdate");
 
-        censusCommandValidatorImpl.validate(censusCommand, bindingResult);
+        censusCommandValidator.validate(censusCommand, bindingResult);
 
 
         if (bindingResult.hasErrors()) {

+ 30 - 0
src/main/java/scot/carricksoftware/grants/validators/census/CensusCommandValidatorImpl.java

@@ -16,9 +16,14 @@ public class CensusCommandValidatorImpl implements CensusCommandValidator {
 
     public void validate(CensusCommand censusCommand, BindingResult bindingResult) {
         validateDate(censusCommand, bindingResult);
+
         validateRoomsInhabited(censusCommand, bindingResult);
         validateRoomsWithWindows(censusCommand, bindingResult);
         validateTotalRooms(censusCommand, bindingResult);
+        validateRoomsOccupied(censusCommand, bindingResult);
+        validateUninhabitedHouses(censusCommand, bindingResult);
+        validateInhabitedHouses(censusCommand, bindingResult);
+
         validateTotalRoomsAndInhabitedRooms(censusCommand, bindingResult);
         validateTotalRoomsAndRoomsWithWindows(censusCommand, bindingResult);
     }
@@ -75,6 +80,31 @@ public class CensusCommandValidatorImpl implements CensusCommandValidator {
         }
     }
 
+    private void validateRoomsOccupied(CensusCommand censusCommand, BindingResult bindingResult) {
+        if (notANonNegativeInteger(censusCommand.getRoomsOccupied())) {
+            bindingResult.rejectValue("roomsOccupied", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER);
+        }
+    }
+
+    private void validateUninhabitedHouses(CensusCommand censusCommand, BindingResult bindingResult) {
+        if (notANonNegativeInteger(censusCommand.getUninhabitedHouses())) {
+            bindingResult.rejectValue("uninhabitedHouses", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER);
+        }
+    }
+
+    private void validateInhabitedHouses(CensusCommand censusCommand, BindingResult bindingResult) {
+        if (notANonNegativeInteger(censusCommand.getInhabitedHouses())) {
+            bindingResult.rejectValue("inhabitedHouses", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.FIELD_NOT_NEGATIVE_INTEGER);
+        }
+    }
+
+
     private boolean notANonNegativeInteger(String field) {
         if (field == null || field.isEmpty()) {
             return false;