com.salesmanager.shop.validation.FieldMatchValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sm-shop-model Show documentation
Show all versions of sm-shop-model Show documentation
sm-shop-model contains Shopizer model objects for api
The newest version!
package com.salesmanager.shop.validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class FieldMatchValidator implements ConstraintValidator
{
private static final Logger LOG=LoggerFactory.getLogger(FieldMatchValidator.class);
private String firstFieldName;
private String secondFieldName;
private BeanUtils beanUtils;
public void initialize(final FieldMatch constraintAnnotation)
{
this.firstFieldName = constraintAnnotation.first();
this.secondFieldName = constraintAnnotation.second();
this.beanUtils = BeanUtils.newInstance();
}
@SuppressWarnings( "nls" )
public boolean isValid(final Object value, final ConstraintValidatorContext context)
{
try
{
final Object firstObj = this.beanUtils.getPropertyValue(value, this.firstFieldName);
final Object secondObj = this.beanUtils.getPropertyValue(value, this.secondFieldName);
return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
}
catch (final Exception ex)
{
LOG.info( "Error while getting values from object", ex );
return false;
}
}
}