com.iana.dver.controller.validators.RegistrationValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.iana.dver.controller.validators.RegistrationValidator.java

Source

/*
 * Copyright (c) 2013 IANA. All Rights Reserved. THE AUTHOR MAKES NO
 * REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE
 * AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.iana.dver.controller.validators;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.iana.dver.service.DverRegistrationService;

/**
 * @author tgbaxi
 * @date 06-Sep-2013
 * 
 */
@Component("registrationValidator")
public class RegistrationValidator implements Validator {

    @Autowired
    private DverRegistrationService dverRegistrationService;

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.validation.Validator#supports(java.lang.Class)
     */
    @Override
    public boolean supports(Class<?> arg0) {
        return RegistrationValidator.class.isAssignableFrom(arg0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.validation.Validator#validate(java.lang.Object,
     * org.springframework.validation.Errors)
     */
    @Override
    public void validate(Object regVO, Errors errors) {
        validateStep1(regVO, errors);
        validateStep2(regVO, errors);
        validateStep3(regVO, errors);
    }

    public void validateStep1(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.companyName", "required.companyName",
                "Company Name can not be blank.");

        if (Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 0) {
            errors.rejectValue("dverUserVO.userType", "required.user.type", "Please Select User Type.");
        }

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.scac", "required.scac",
                "SCAC Code can not be blank.");
        // unique SCAC validation;
        if (errors.getFieldError("dverUserVO.scac") == null
                && errors.getFieldError("dverUserVO.userType") == null) {
            String scac = errors.getFieldValue("dverUserVO.scac").toString();
            String userType = Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 2 ? "IEP"
                    : "MC";
            if (isScacExists(scac, userType)) {
                errors.rejectValue("dverUserVO.scac", "scac.nonunique",
                        "The provided SCAC code already exists for " + userType + " user.");
            }
        }

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.usDOT", "required.usDOT",
                "USDOT can not be blank.");
        // unique USDOT validation;
        if (errors.getFieldError("dverUserVO.usDOT") == null
                && errors.getFieldError("dverUserVO.userType") == null) {
            String usdot = errors.getFieldValue("dverUserVO.usDOT").toString();
            String userType = Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 2 ? "IEP"
                    : "MC";

            if (isUsDotExists(usdot, userType)) {
                errors.rejectValue("dverUserVO.usDOT", "usdot.nonunique",
                        "The provided DOT code already exists for " + userType + " user.");
            }
        }

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.firstName", "required.firstName",
                "First Name can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.lastName", "required.lastName",
                "Last Name can not be blank.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.title", "required.title",
                "Title can not be blank.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.email", "required.email",
                "Email can not be blank.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.phone", "required.phone",
                "Phone No. can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.fax", "required.fax",
                "Fax No. can not be blank.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.address1", "required.address1",
                "Address can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.city", "required.city",
                "City can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.state", "required.state",
                "State Code can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.zipCode", "required.zipCode",
                "ZIP Code can not be blank.");
    }

    public void validateStep2(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserLoginVO.userName", "required.userName",
                "User Name can not be blank");
        // unique User Name validation;
        if (errors.getFieldError("dverUserLoginVO.userName") == null
                && isUserNameExists(errors.getFieldValue("dverUserLoginVO.userName").toString())) {
            errors.rejectValue("dverUserLoginVO.userName", "username.nonunique",
                    "The provided User Name already exists");
        }
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserLoginVO.password", "required.password",
                "Password can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserLoginVO.confirmPwd", "required.confirmPwd",
                "Repeat password can not be blank.");

        // check if both the passwords are equal or not.
        verifyPwdAndConfirmPwdEqual(errors, errors.getFieldValue("dverUserLoginVO.password").toString(),
                errors.getFieldValue("dverUserLoginVO.confirmPwd").toString());
    }

    private void verifyPwdAndConfirmPwdEqual(Errors errors, String password, String confirmPassword) {
        if (errors.getFieldError("dverUserLoginVO.password") == null
                && errors.getFieldError("dverUserLoginVO.confirmPwd") == null) {
            if (!password.equals(confirmPassword)) {
                errors.rejectValue("dverUserLoginVO.confirmPwd", "password.notmatching",
                        "Password and Confirm password should be equal.");
            }
        }
    }

    public void validateStep3(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverConfigVO.firstName", "required.firstName",
                "First Name can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverConfigVO.lastName", "required.lastName",
                "Last Name can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverConfigVO.title", "required.title",
                "Title can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverConfigVO.email", "required.email",
                "Email can not be blank.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverConfigVO.phone", "required.phone",
                "Phone can not be blank.");
    }

    public Boolean isScacExists(final String scacCode, final String userType) {
        return dverRegistrationService.isScacExists(scacCode, userType);
    }

    public Boolean isUsDotExists(final String usdot, final String userType) {
        return dverRegistrationService.isUsDotExists(usdot, userType);
    }

    public Boolean isEmailExists(final String email) {
        return dverRegistrationService.isEmailExists(email);
    }

    public Boolean isUserNameExists(final String userName) {
        return dverRegistrationService.isUserNameExists(userName);
    }

    public Boolean isConfigEmailExists(final String email) {
        return dverRegistrationService.isConfigEmailExists(email);
    }

    /**
     * @return the dverRegistrationService
     */
    public DverRegistrationService getDverRegistrationService() {
        return dverRegistrationService;
    }

    /**
     * @param dverRegistrationService
     *          the dverRegistrationService to set
     */
    public void setDverRegistrationService(DverRegistrationService dverRegistrationService) {
        this.dverRegistrationService = dverRegistrationService;
    }

}