com.acc.validator.ReviewDataValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.acc.validator.ReviewDataValidator.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.acc.validator;

import static de.hybris.platform.customerreview.model.CustomerReviewModel.*;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import de.hybris.platform.commercefacades.product.data.ReviewData;

@Component("reviewValidator")
public class ReviewDataValidator implements Validator {
    private final static double RATING_MIN = 1.0d;
    private final static double RATING_MAX = 5.0d;

    @Override
    public boolean supports(final Class clazz) {
        return ReviewData.class.equals(clazz);
    }

    @Override
    public void validate(final Object target, final Errors errors) {
        ValidationUtils.rejectIfEmpty(errors, HEADLINE, "field.required");
        ValidationUtils.rejectIfEmpty(errors, COMMENT, "field.required");
        validateRating(errors);
    }

    protected void validateRating(final Errors errors) {
        Assert.notNull(errors, "Errors object must not be null");
        final Double rating = (Double) errors.getFieldValue(RATING);

        if (rating == null) {
            errors.rejectValue(RATING, "field.required");
        } else {
            if (rating.doubleValue() < RATING_MIN || rating.doubleValue() > RATING_MAX) {
                errors.rejectValue(RATING, "review.rating.invalid");
            }
        }
    }

}