Преглед изворни кода

Divorce certificate Validate two parties

Andrew Grant пре 3 месеци
родитељ
комит
ed02fd6f9e

+ 34 - 0
src/main/java/scot/carricksoftware/grants/validators/certificates/divorcecertificate/DivorceCertificateCommandPeopleValidatorImpl.java

@@ -10,6 +10,9 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.commands.certificates.divorcecertificates.DivorceCertificateCommand;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+import scot.carricksoftware.grants.validators.helpers.ValidateTwoFieldTypes;
+import scot.carricksoftware.grants.validators.helpers.ValidateTypesImpl;
 
 
 @Component
@@ -17,10 +20,41 @@ public class DivorceCertificateCommandPeopleValidatorImpl implements DivorceCert
 
     private static final Logger logger = LogManager.getLogger(DivorceCertificateCommandPeopleValidatorImpl.class);
 
+    private final ValidateTypesImpl validateTypes;
+    private final ValidateTwoFieldTypes validateTwoFieldTypes;
+
+    public DivorceCertificateCommandPeopleValidatorImpl(ValidateTypesImpl validateTypes,
+                                                        ValidateTwoFieldTypes validateTwoFieldTypes) {
+        this.validateTypes = validateTypes;
+        this.validateTwoFieldTypes = validateTwoFieldTypes;
+    }
+
     @Override
     public void validate(DivorceCertificateCommand divorceCertificateCommand, BindingResult bindingResult) {
         logger.info("DivorceCertificateCommandPeopleValidator::validate");
+        validateFirstParty(divorceCertificateCommand, bindingResult);
+        validateSecondParty(divorceCertificateCommand, bindingResult);
+        validateDifferentParties(divorceCertificateCommand, bindingResult);
+    }
+
+    private void validateFirstParty(DivorceCertificateCommand divorceCertificateCommand, BindingResult bindingResult) {
+        logger.debug("DivorceCertificatePeopleValidator::validateFirstParty");
+        validateTypes.validatePerson(divorceCertificateCommand.getFirstParty(), "firstParty", ValidationConstants.FIRST_PARTY_IS_NULL, bindingResult);
+    }
+
+    private void validateSecondParty(DivorceCertificateCommand divorceCertificateCommand, BindingResult bindingResult) {
+        logger.debug("DivorceCertificatePeopleValidator::validateSecondParty");
+        validateTypes.validatePerson(divorceCertificateCommand.getSecondParty(), "secondParty", ValidationConstants.SECOND_PARTY_IS_NULL, bindingResult);
+    }
 
+    private void validateDifferentParties(DivorceCertificateCommand divorceCertificateCommand, BindingResult bindingResult) {
+        logger.debug("DivorceCertificatePeopleValidator::validateDifferentParties");
+        validateTwoFieldTypes.validateNotSamePerson(
+                divorceCertificateCommand.getFirstParty(),
+                divorceCertificateCommand.getSecondParty(),
+                "firstParty",
+                "secondParty",
+                ValidationConstants.SAME_TWO_PARTIES, bindingResult);
     }
 }
 

+ 2 - 2
src/main/java/scot/carricksoftware/grants/validators/helpers/ValidateTwoFieldTypes.java

@@ -5,12 +5,10 @@
 
 package scot.carricksoftware.grants.validators.helpers;
 
-import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.domains.people.Person;
 import scot.carricksoftware.grants.domains.places.Place;
 
-@Component
 public interface ValidateTwoFieldTypes {
 
     void validatePersonAndUntrackedPerson(Person person, String untrackedPerson, String personFieldName, String untrackedFieldName, String message,  BindingResult bindingResult);
@@ -20,4 +18,6 @@ public interface ValidateTwoFieldTypes {
     void validatePlaceAndUntrackedPlace(Place place, String untrackedPlace, String placeFieldName, String untrackedFieldName, String message, BindingResult bindingResult);
 
     void validateOptionalPlaceAndUntrackedPlace(Place place, String untrackedPlace, String placeFieldName, String untrackedFieldName, String message, BindingResult bindingResult);
+
+    void validateNotSamePerson(Person firstPerson, Person secondPerson, String firstPersonFieldName, String secondPersonFieldName, String message, BindingResult bindingResult);
 }

+ 8 - 0
src/main/java/scot/carricksoftware/grants/validators/helpers/ValidateTwoFieldTypesImpl.java

@@ -44,6 +44,14 @@ public class ValidateTwoFieldTypesImpl implements ValidateTwoFieldTypes {
       }
     }
 
+    @Override
+    public void validateNotSamePerson(Person firstPerson, Person secondPerson, String firstPersonFieldName, String secondPersonFieldName, String message, BindingResult bindingResult) {
+        if (secondPerson != null && firstPerson == secondPerson) {
+            bindingResult.rejectValue(firstPersonFieldName, ApplicationConstants.EMPTY_STRING, null, message);
+            bindingResult.rejectValue(secondPersonFieldName, ApplicationConstants.EMPTY_STRING, null, message);
+        }
+    }
+
     private void reportError(String personFieldName, String untrackedFieldName, String message, BindingResult bindingResult) {
         bindingResult.rejectValue(personFieldName, ApplicationConstants.EMPTY_STRING, null, message);
         bindingResult.rejectValue(untrackedFieldName, ApplicationConstants.EMPTY_STRING, null, message);

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

@@ -49,7 +49,7 @@
                     </select>
                     <div th:if="${#fields.hasErrors('firstParty')}">
                         <ul class="text-danger">
-                            <li th:each="err : ${#fields.errors('bride')}" th:text="${err}"/>
+                            <li th:each="err : ${#fields.errors('firstParty')}" th:text="${err}"/>
                         </ul>
                     </div>
                 </td>