Ver código fonte

Birth Certificate more fields

Andrew Grant 6 meses atrás
pai
commit
be0aec58c1

+ 4 - 0
src/main/java/scot/carricksoftware/grants/constants/ValidationConstants.java

@@ -28,9 +28,13 @@ public class ValidationConstants {
     public static final String NEWBORN_IS_NULL = "The New Born cannot be null.";
     public static final String CERTIFICATE_NUMBER_IS_NULL = "The certificate number cannot be null.";
     public static final String SOURCE_IS_NULL = "The certificate source cannot be null.";
+    public static final String REGISTRATION_AUTHORITY_IS_NULL = "The registration authority cannot be null.";
     public static final String CERTIFICATE_DATE_IS_NULL = "The certificate date cannot be null.";
     public static final String CERTIFICATE_TYPE_IS_NULL = "The certificate type cannot be null.";
     public static final String DATE_IS_INVALID = "The certificate date is invalid or of the wrong format.";
+    public static final String NUMBER_IS_NULL = "The number cannot be null.";
+    public static final String NUMBER_IS_INVALID = "The number must be a non negative integer.";
+    public static final String REGISTRATION_VOLUME_IS_NULL = "The volume cannot be null.";
 
     public static final String PLACE_IS_NULL = "The place cannot be null.";
     public static final String COUNTRY_IS_NULL = "The country cannot be null.";

+ 106 - 0
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandPartOneValidator.java

@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 09:50. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.certificates;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+
+import java.time.LocalDate;
+
+@Component
+public class BirthCertificateCommandPartOneValidator {
+
+    private static final Logger logger = LogManager.getLogger(BirthCertificateCommandPartOneValidator.class);
+
+    @SuppressWarnings("unused")
+    public void validate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate command (part one)");
+
+        validateNewBorn(birthCertificateCommand, bindingResult);
+        validateCertificateNumber(birthCertificateCommand, bindingResult);
+        validateCertificateSource(birthCertificateCommand, bindingResult);
+        validateCertificateType(birthCertificateCommand, bindingResult);
+        validateCertificateDate(birthCertificateCommand, bindingResult);
+        if (!bindingResult.hasErrors()) {
+            validateCertificateLegitimateDate(birthCertificateCommand, bindingResult);
+        }
+    }
+
+    private void validateNewBorn(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate newBorn");
+        if (birthCertificateCommand.getNewBorn() == null ) {
+            bindingResult.rejectValue("newBorn", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.NEWBORN_IS_NULL);
+        }
+    }
+
+    private void validateCertificateNumber(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Number");
+        if (birthCertificateCommand.getCertificateNumber() == null || birthCertificateCommand.getCertificateNumber().isEmpty()) {
+            bindingResult.rejectValue("certificateNumber", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CERTIFICATE_NUMBER_IS_NULL);
+        }
+    }
+
+    private void validateCertificateSource(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Source");
+        if (birthCertificateCommand.getCertificateSource() == null ) {
+            bindingResult.rejectValue("certificateSource", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.SOURCE_IS_NULL);
+        }
+    }
+
+    private void validateCertificateType(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Type");
+        if (birthCertificateCommand.getCertificateType() == null ) {
+            bindingResult.rejectValue("certificateType", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CERTIFICATE_TYPE_IS_NULL);
+        }
+    }
+
+    private void validateCertificateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Date");
+        if (birthCertificateCommand.getCertificateDate() == null || birthCertificateCommand.getCertificateDate().isEmpty()) {
+            bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CERTIFICATE_DATE_IS_NULL);
+        } else {
+            try {
+                LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+            } catch (Exception e) {
+                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.DATE_IS_INVALID);
+            }
+        }
+    }
+
+    private void validateCertificateLegitimateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth Certificate valid date");
+        try {
+            LocalDate testDate = LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+            if (testDate.isAfter(LocalDate.now())) {
+                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.DATE_IN_FUTURE);
+            }
+        } catch (Exception e) {
+            logger.debug("Error trapped in  birth Certificate valid date");
+        }
+    }
+
+
+}
+

+ 72 - 0
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandPartTwoValidator.java

@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 09:50. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.certificates;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+
+@Component
+public class BirthCertificateCommandPartTwoValidator {
+
+    private static final Logger logger = LogManager.getLogger(BirthCertificateCommandPartTwoValidator.class);
+
+    public void validate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate command (part two)");
+
+        validateNumber(birthCertificateCommand, bindingResult);
+        validateVolume(birthCertificateCommand, bindingResult);
+        validateRegistrationAuthority(birthCertificateCommand, bindingResult);
+    }
+
+
+    private void validateNumber(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating registration number");
+        if (birthCertificateCommand.getNumber() == null || birthCertificateCommand.getNumber().isEmpty()) {
+            bindingResult.rejectValue("number", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.NUMBER_IS_NULL);
+        } else {
+            try {
+                int number = Integer.parseInt(birthCertificateCommand.getNumber());
+                if (number < 0) {
+                    bindingResult.rejectValue("number", ApplicationConstants.EMPTY_STRING,
+                            null,
+                            ValidationConstants.NUMBER_IS_INVALID);
+                }
+            } catch (NumberFormatException e) {
+                bindingResult.rejectValue("number", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.NUMBER_IS_INVALID);
+            }
+        }
+    }
+
+
+    private void validateVolume(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating number Registration Volume");
+        if (birthCertificateCommand.getVolume() == null || birthCertificateCommand.getVolume().isEmpty()) {
+            bindingResult.rejectValue("volume", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.REGISTRATION_VOLUME_IS_NULL);
+        }
+    }
+
+    private void validateRegistrationAuthority(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Registration Authority");
+        if (birthCertificateCommand.getRegistrationAuthority() == null) {
+            bindingResult.rejectValue("registrationAuthority", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.REGISTRATION_AUTHORITY_IS_NULL);
+        }
+    }
+
+}
+

+ 10 - 77
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidator.java

@@ -10,95 +10,28 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
-import scot.carricksoftware.grants.constants.ApplicationConstants;
-import scot.carricksoftware.grants.constants.ValidationConstants;
-
-import java.time.LocalDate;
 
 @Component
 public class BirthCertificateCommandValidator {
     private static final Logger logger = LogManager.getLogger(BirthCertificateCommandValidator.class);
 
-    @SuppressWarnings("unused")
-    public void validate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate command");
-
-        validateNewBorn(birthCertificateCommand, bindingResult);
-        validateCertificateNumber(birthCertificateCommand, bindingResult);
-        validateCertificateSource(birthCertificateCommand, bindingResult);
-        validateCertificateType(birthCertificateCommand, bindingResult);
-        validateCertificateDate(birthCertificateCommand, bindingResult);
-        if (!bindingResult.hasErrors()) {
-            validateCertificateLegitimateDate(birthCertificateCommand, bindingResult);
-        }
-    }
-
-    private void validateNewBorn(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate newBorn");
-        if (birthCertificateCommand.getNewBorn() == null ) {
-            bindingResult.rejectValue("newBorn", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.NEWBORN_IS_NULL);
-        }
-    }
+    private final BirthCertificateCommandPartOneValidator partOneValidator;
+    private final BirthCertificateCommandPartTwoValidator partTwoValidator;
 
-    private void validateCertificateNumber(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate Certificate Number");
-        if (birthCertificateCommand.getCertificateNumber() == null || birthCertificateCommand.getCertificateNumber().isEmpty()) {
-            bindingResult.rejectValue("certificateNumber", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.CERTIFICATE_NUMBER_IS_NULL);
-        }
+    public BirthCertificateCommandValidator(BirthCertificateCommandPartOneValidator partOneValidator,
+                                            BirthCertificateCommandPartTwoValidator partTwoValidator) {
+        this.partOneValidator = partOneValidator;
+        this.partTwoValidator = partTwoValidator;
     }
 
-    private void validateCertificateSource(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate Certificate Source");
-        if (birthCertificateCommand.getCertificateSource() == null ) {
-            bindingResult.rejectValue("certificateSource", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.SOURCE_IS_NULL);
-        }
-    }
 
-    private void validateCertificateType(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate Certificate Type");
-        if (birthCertificateCommand.getCertificateType() == null ) {
-            bindingResult.rejectValue("certificateType", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.CERTIFICATE_TYPE_IS_NULL);
-        }
-    }
+    public void validate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate command");
 
-    private void validateCertificateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth certificate Certificate Date");
-        if (birthCertificateCommand.getCertificateDate() == null || birthCertificateCommand.getCertificateDate().isEmpty()) {
-            bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.CERTIFICATE_DATE_IS_NULL);
-        } else {
-            try {
-                LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
-            } catch (Exception e) {
-                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.DATE_IS_INVALID);
-            }
-        }
+        partOneValidator.validate(birthCertificateCommand, bindingResult);
+        partTwoValidator.validate(birthCertificateCommand, bindingResult);
     }
 
-    private void validateCertificateLegitimateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
-        logger.debug("Validating birth Certificate valid date");
-        try {
-            LocalDate testDate = LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
-            if (testDate.isAfter(LocalDate.now())) {
-                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
-                        null,
-                        ValidationConstants.DATE_IN_FUTURE);
-            }
-        } catch (Exception e) {
-            logger.debug("Error trapped in  birth Certificate valid date");
-        }
-    }
 
 
 }

+ 1 - 13
src/main/resources/templates/certificates/birthCertificate/form.html

@@ -139,12 +139,6 @@
                                 <li th:each="err : ${#fields.errors('volume')}" th:text="${err}"/>
                             </ul>
                         </div>
-
-                    </div>
-                    <div th:if="${#fields.hasErrors('volume')}">
-                        <ul class="text-danger">
-                            <li th:each="err : ${#fields.errors('volume')}" th:text="${err}"/>
-                        </ul>
                     </div>
                 </td>
             </tr>
@@ -161,12 +155,6 @@
                                 <li th:each="err : ${#fields.errors('number')}" th:text="${err}"/>
                             </ul>
                         </div>
-
-                    </div>
-                    <div th:if="${#fields.hasErrors('number')}">
-                        <ul class="text-danger">
-                            <li th:each="err : ${#fields.errors('number')}" th:text="${err}"/>
-                        </ul>
                     </div>
                 </td>
 
@@ -175,7 +163,7 @@
                     <label for="newBorn">NewBorn</label>
                 </td>
                 <td style="text-align: left;">
-                    <select id="newBorn" name="place" th:field="*{newBorn}">
+                    <select id="newBorn" name="newBorn" th:field="*{newBorn}">
                         <option th:value="${''}" th:text="${''}"></option>
                         <option th:each="person : ${people}"
                                 th:value="${person.id}" th:text="${person.toString()}"></option>

+ 3 - 3
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorDateTest.java → src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandPartOneValidatorDateTest.java

@@ -25,9 +25,9 @@ import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPe
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
 
 @ExtendWith(MockitoExtension.class)
-class BirthCertificateCommandValidatorDateTest {
+class BirthCertificateCommandPartOneValidatorDateTest {
 
-    private BirthCertificateCommandValidator commandValidator;
+    private BirthCertificateCommandPartOneValidator commandValidator;
 
     private ArgumentCaptor<String> stringArgumentCaptor;
     private ArgumentCaptor<String> stringArgumentCaptor2;
@@ -41,7 +41,7 @@ class BirthCertificateCommandValidatorDateTest {
 
     @BeforeEach
     void setUp() {
-        commandValidator = new BirthCertificateCommandValidator();
+        commandValidator = new BirthCertificateCommandPartOneValidator();
         stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);

+ 3 - 3
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorPersonTest.java → src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandPartOneValidatorPersonTest.java

@@ -26,9 +26,9 @@ import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPe
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
 
 @ExtendWith(MockitoExtension.class)
-class BirthCertificateCommandValidatorPersonTest {
+class BirthCertificateCommandPartOneValidatorPersonTest {
 
-    private BirthCertificateCommandValidator commandValidator;
+    private BirthCertificateCommandPartOneValidator commandValidator;
 
     private ArgumentCaptor<String> stringArgumentCaptor;
     private ArgumentCaptor<String> stringArgumentCaptor2;
@@ -42,7 +42,7 @@ class BirthCertificateCommandValidatorPersonTest {
 
     @BeforeEach
     void setUp() {
-        commandValidator = new BirthCertificateCommandValidator();
+        commandValidator = new BirthCertificateCommandPartOneValidator();
         stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);

+ 3 - 3
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorCertificateTest.java → src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorCertificatePartOneTest.java

@@ -25,9 +25,9 @@ import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPe
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
 
 @ExtendWith(MockitoExtension.class)
-class BirthCertificateCommandValidatorCertificateTest {
+class BirthCertificateCommandValidatorCertificatePartOneTest {
 
-    private BirthCertificateCommandValidator commandValidator;
+    private BirthCertificateCommandPartOneValidator commandValidator;
 
     private ArgumentCaptor<String> stringArgumentCaptor;
     private ArgumentCaptor<String> stringArgumentCaptor2;
@@ -41,7 +41,7 @@ class BirthCertificateCommandValidatorCertificateTest {
 
     @BeforeEach
     void setUp() {
-        commandValidator = new BirthCertificateCommandValidator();
+        commandValidator = new BirthCertificateCommandPartOneValidator();
         stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);

+ 3 - 3
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorCertificateTypeTest.java → src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorCertificateTypePartTwoTest.java

@@ -23,9 +23,9 @@ import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPe
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
 
 @ExtendWith(MockitoExtension.class)
-class BirthCertificateCommandValidatorCertificateTypeTest {
+class BirthCertificateCommandValidatorCertificateTypePartTwoTest {
 
-    private BirthCertificateCommandValidator commandValidator;
+    private BirthCertificateCommandPartOneValidator commandValidator;
 
     private ArgumentCaptor<String> stringArgumentCaptor;
     private ArgumentCaptor<String> stringArgumentCaptor2;
@@ -39,7 +39,7 @@ class BirthCertificateCommandValidatorCertificateTypeTest {
 
     @BeforeEach
     void setUp() {
-        commandValidator = new BirthCertificateCommandValidator();
+        commandValidator = new BirthCertificateCommandPartOneValidator();
         stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
         stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);