com.epam.cme.storefront.forms.validation.PaymentDetailsValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.cme.storefront.forms.validation.PaymentDetailsValidator.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2013 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 * 
 *  
 */
package com.epam.cme.storefront.forms.validation;

import com.epam.cme.storefront.forms.PaymentDetailsForm;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component("paymentDetailsValidator")
public class PaymentDetailsValidator implements Validator {
    @Override
    public boolean supports(final Class<?> aClass) {
        return PaymentDetailsForm.class.equals(aClass);
    }

    @Override
    public void validate(final Object object, final Errors errors) {
        final PaymentDetailsForm form = (PaymentDetailsForm) object;

        if (StringUtils.isNotBlank(form.getStartMonth()) && StringUtils.isNotBlank(form.getStartYear())
                && StringUtils.isNotBlank(form.getExpiryMonth()) && StringUtils.isNotBlank(form.getExpiryYear())) {
            final Calendar start = Calendar.getInstance();
            start.set(Calendar.DAY_OF_MONTH, 0);
            start.set(Calendar.MONTH, Integer.parseInt(form.getStartMonth()) - 1);
            start.set(Calendar.YEAR, Integer.parseInt(form.getStartYear()) - 1);

            final Calendar expiration = Calendar.getInstance();
            expiration.set(Calendar.DAY_OF_MONTH, 0);
            expiration.set(Calendar.MONTH, Integer.parseInt(form.getExpiryMonth()) - 1);
            expiration.set(Calendar.YEAR, Integer.parseInt(form.getExpiryYear()) - 1);

            if (start.after(expiration)) {
                errors.rejectValue("startMonth", "payment.startDate.invalid");
            }
        }

        final boolean editMode = StringUtils.isNotBlank(form.getPaymentId());
        if (editMode || Boolean.TRUE.equals(form.getNewBillingAddress())) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.titleCode", "address.title.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.firstName",
                    "address.firstName.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.lastName",
                    "address.lastName.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.line1", "address.line1.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.townCity",
                    "address.townCity.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.postcode",
                    "address.postcode.invalid");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.countryIso",
                    "address.country.invalid");
            // ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.line2",
            // "address.line2.invalid"); // for some addresses this field is required by cybersource
        }
    }
}