de.hybris.platform.acceleratorservices.web.payment.validation.PaymentDetailsValidator.java Source code

Java tutorial

Introduction

Here is the source code for de.hybris.platform.acceleratorservices.web.payment.validation.PaymentDetailsValidator.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2014 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 de.hybris.platform.acceleratorservices.web.payment.validation;

import de.hybris.platform.acceleratorservices.web.payment.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;

        final Calendar start = parseDate(form.getStartMonth(), form.getStartYear());
        final Calendar expiration = parseDate(form.getExpiryMonth(), form.getExpiryYear());
        final Calendar current = Calendar.getInstance();

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

        if (expiration.before(current)) {
            errors.rejectValue("expiryMonth", "payment.expiryDate.future.invalid");
        }

        if (start.after(expiration)) {
            errors.rejectValue("startMonth", "payment.startDate.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.city.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.postcode", "address.postcode.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.countryIso", "address.country.invalid");
    }

    protected Calendar parseDate(final String month, final String year) {
        if (StringUtils.isNotBlank(month) && StringUtils.isNotBlank(year)) {
            final Integer yearInt = getIntegerForString(year);
            final Integer monthInt = getIntegerForString(month);

            if (yearInt != null && monthInt != null) {
                final Calendar date = getCalendarResetTime();
                date.set(Calendar.YEAR, yearInt.intValue());
                date.set(Calendar.MONTH, monthInt.intValue() - 1);
                date.set(Calendar.DAY_OF_MONTH, 1);
                return date;
            }
        }
        return null;
    }

    protected Calendar getCalendarResetTime() {
        final Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    /**
     * Common method to convert a String to an Integer.
     * 
     * @param value
     *           - the String value to be converted.
     * @return - an Integer object.
     */
    protected Integer getIntegerForString(final String value) {
        if (value != null && !value.isEmpty()) {
            try {
                return Integer.valueOf(value);
            } catch (final Exception ignore) {
                // Ignore
            }
        }

        return null;
    }
}