Java tutorial
package com.ccserver.digital.validator; import java.time.LocalDateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import com.ccserver.digital.common.ErrorCodes; import com.ccserver.digital.domain.type.TypeOfEmployment; import com.ccserver.digital.dto.CreditCardApplicationDTO; import com.ccserver.digital.dto.PhoneDTO; import com.ccserver.digital.dto.SocialAccountDTO; import com.ccserver.digital.service.CreditCardApplicationService; @Service public class CreditCardApplicationValidator implements Validator { private static Long DELIVERY_CARD_VP_BRANCH = 45L; @Autowired private CreditCardApplicationService creditCardApplicationService; @Override public boolean supports(Class<?> clazz) { return CreditCardApplicationDTO.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { CreditCardApplicationDTO creditCardApplicationDTO = (CreditCardApplicationDTO) target; switch (creditCardApplicationDTO.getProcessStep()) { case Other: validateOtherTab(creditCardApplicationDTO, errors); case Employment: validateEmploymentTab(creditCardApplicationDTO, errors); case Personal: validatePersonalTab(creditCardApplicationDTO, errors); case General: validateGeneralTab(creditCardApplicationDTO, errors); break; case BeginStep: validateBasicInformation(creditCardApplicationDTO, errors); break; } } private void validateGeneralTab(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { // Validate date of issue ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dob", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "typeOfID", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passportNumber", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateOfIssue", ErrorCodes.FIELD_REQUIRED); if (creditCardApplicationDTO.getCitizenship() != null && "+84".equals(creditCardApplicationDTO.getCitizenship().getCode())) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "placeOfIssue", ErrorCodes.FIELD_REQUIRED); } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passportNumber", ErrorCodes.FIELD_REQUIRED); LocalDateTime dateOfIssue = creditCardApplicationDTO.getDateOfIssue(); if (dateOfIssue != null) { if (dateOfIssue.isAfter(LocalDateTime.now())) { errors.rejectValue("dateOfIssue", ErrorCodes.FIELD_NOT_FUTURE); } } validateMobilephone(creditCardApplicationDTO, errors); } private void validatePersonalTab(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "education", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "maritalStatus", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "permanentAddress", ErrorCodes.FIELD_REQUIRED); if (!creditCardApplicationDTO.isCurrentIsPermanent()) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "currentAddress", ErrorCodes.FIELD_REQUIRED); } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "monthsOfLiving", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "yearsOfLiving", ErrorCodes.FIELD_REQUIRED); } private void validateMobilephone(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { // validate empty ValidationUtils.rejectIfEmpty(errors, "mobile", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmpty(errors, "mobile.phoneNumber", ErrorCodes.FIELD_REQUIRED); PhoneDTO mobile = creditCardApplicationDTO.getMobile(); if (mobile != null) { // validate existing CreditCardApplicationDTO ccAppExisting = creditCardApplicationService.findByMobile(mobile); if (ccAppExisting != null && ccAppExisting.getId() != creditCardApplicationDTO.getId()) { errors.rejectValue("mobile", ErrorCodes.FIELD_EXISTING); } } } private void validateSocialAccount(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { SocialAccountDTO socialAccount = creditCardApplicationDTO.getSocialAccount(); if (socialAccount != null) { CreditCardApplicationDTO ccAppExisting = creditCardApplicationService .findBySocialAccount(socialAccount); if (ccAppExisting != null && ccAppExisting.getId() != creditCardApplicationDTO.getId()) { errors.rejectValue("socialAccount", ErrorCodes.FIELD_EXISTING); } } } private void validateEmploymentTab(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { if (creditCardApplicationDTO.getTypeOfEmployement() == TypeOfEmployment.Business) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "licenseNumber", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "businessStartDate", ErrorCodes.FIELD_REQUIRED); if (creditCardApplicationDTO.getBusinessStartDate() != null) { if (creditCardApplicationDTO.getBusinessStartDate().isAfter(LocalDateTime.now())) { errors.rejectValue("businessStartDate", ErrorCodes.FIELD_NOT_FUTURE); } } ValidationUtils.rejectIfEmpty(errors, "businessPhone", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "businessAddress", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "companyName", ErrorCodes.FIELD_REQUIRED); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "nameOfEmployer", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "occupation", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "industry", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "employerAddress", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "businessTelephone", ErrorCodes.FIELD_REQUIRED); if ((creditCardApplicationDTO.getMonthsOfWorking() == null || creditCardApplicationDTO.getMonthsOfWorking() == 0) && (creditCardApplicationDTO.getYearsOfWorking() == null || creditCardApplicationDTO.getYearsOfWorking() == 0)) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "monthsOfWorking", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "yearsOfWorking", ErrorCodes.FIELD_REQUIRED); } } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "monthlyIncome", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "monthlyExpenses", ErrorCodes.FIELD_REQUIRED); if (creditCardApplicationDTO.getMonthlyExpenses() != null && creditCardApplicationDTO.getMonthlyExpenses() .compareTo(creditCardApplicationDTO.getMonthlyIncome()) > 0) { errors.rejectValue("monthlyExpenses", ErrorCodes.FIELD_INVALID); } } private void validateOtherTab(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "securityQuestion", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "securityAnswer", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "referenceName", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "relationshipReference", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phoneOfReference", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "deliveryCard", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creditLimit", ErrorCodes.FIELD_REQUIRED); if (creditCardApplicationDTO.getDeliveryCard() != null && creditCardApplicationDTO.getDeliveryCard().getId() == DELIVERY_CARD_VP_BRANCH) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "branchAddress", ErrorCodes.FIELD_REQUIRED); } // validate referencePhone PhoneDTO appPhone = creditCardApplicationDTO.getMobile(); PhoneDTO refPhone = creditCardApplicationDTO.getPhoneOfReference(); if (appPhone != null && appPhone.getPhoneNumber() != null && refPhone != null && refPhone.getPhoneNumber() != null && appPhone.getCountryCode() != null && refPhone.getCountryCode() != null) { String appPhoneNum = appPhone.getPhoneNumber(); if (appPhoneNum.startsWith("0")) { appPhoneNum = appPhoneNum.substring(1); } String refPhoneNum = refPhone.getPhoneNumber(); if (refPhoneNum.startsWith("0")) { refPhoneNum = refPhoneNum.substring(1); } if (appPhoneNum.equals(refPhoneNum) && appPhone.getCountryCode().equals(refPhone.getCountryCode())) { errors.rejectValue("phoneOfReference", ErrorCodes.FIELD_INVALID); } } } private void validateBasicInformation(CreditCardApplicationDTO creditCardApplicationDTO, Errors errors) { // Validate name, phone, email ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", ErrorCodes.FIELD_REQUIRED); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", ErrorCodes.FIELD_REQUIRED); validateMobilephone(creditCardApplicationDTO, errors); validateSocialAccount(creditCardApplicationDTO, errors); } }