
nz.co.senanque.validationengine.fieldvalidators.RangeValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of madura-objects Show documentation
Show all versions of madura-objects Show documentation
This is essentially a JAXB/XJC plugin that adds automatic validation to the generated Java classes.
You define business objects in an XSD file, pass it through XJC and the plugin will add the validation code.
It uses information in the XSD to pick up validation, and you can specify extensions to that in the XSD. The resulting
Java classes check for validity when the setter is called and they reject attempts to set invalid values (this is a difference from other validation frameworks). The Java classes also expose a metadata interface to make it easy for UIs to generate, say, lists of options for a select box.
The validation framework handles single field validation but you can inject a rule engine (or several) to handle cross field validation.
But to any Java code the objects still look like ordinary Java beans. The surrounding application is unaware that they are anything different until they throw an exception. This makes it easy to use with frameworks that expect Java beans, and most of them do.
/*******************************************************************************
* Copyright (c)2014 Prometheus Consulting
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package nz.co.senanque.validationengine.fieldvalidators;
import nz.co.senanque.validationengine.ConvertUtils;
import nz.co.senanque.validationengine.ValidationException;
import nz.co.senanque.validationengine.annotations.Range;
import nz.co.senanque.validationengine.metadata.PropertyMetadata;
/**
* Used to validate a range. The field can be anything that implements the Comparable interface.
* Ranges are specified as min/max with an inclusive or exclusive option.
* Each can have a message associated with it.
*
* @author Roger Parkinson
* @version $Revision: 1.6 $
*/
public class RangeValidator implements FieldValidator
{
private String m_maxExclusive;
private String m_minExclusive;
private String m_maxInclusive;
private String m_minInclusive;
private String m_maxExclusiveMessage;
private String m_minExclusiveMessage;
private String m_maxInclusiveMessage;
private String m_minInclusiveMessage;
private PropertyMetadata m_propertyMetadata;
/* (non-Javadoc)
* @see nz.co.senanque.validationengine.annotations1.FieldValidator#init(java.lang.annotation.Annotation)
*/
public void init(Range annotation, PropertyMetadata propertyMetadata)
{
m_maxExclusive = getValue(annotation.maxExclusive());
m_minExclusive = getValue(annotation.minExclusive());
m_maxInclusive = getValue(annotation.maxInclusive());
m_minInclusive = getValue(annotation.minInclusive());
m_maxExclusiveMessage = annotation.maxExclusiveMessage();
m_minExclusiveMessage = annotation.minExclusiveMessage();
m_maxInclusiveMessage = annotation.maxInclusiveMessage();
m_minInclusiveMessage = annotation.minInclusiveMessage();
m_propertyMetadata = propertyMetadata;
}
public void initTest(RangeDTO annotation, PropertyMetadata propertyMetadata)
{
m_maxExclusive = getValue(annotation.maxExclusive());
m_minExclusive = getValue(annotation.minExclusive());
m_maxInclusive = getValue(annotation.maxInclusive());
m_minInclusive = getValue(annotation.minInclusive());
m_maxExclusiveMessage = annotation.maxExclusiveMessage();
m_minExclusiveMessage = annotation.minExclusiveMessage();
m_maxInclusiveMessage = annotation.maxInclusiveMessage();
m_minInclusiveMessage = annotation.minInclusiveMessage();
m_propertyMetadata = propertyMetadata;
}
private String getValue(String v)
{
if (v == null) return null;
if ("none".equals(v)) return null;
return v;
}
/* (non-Javadoc)
* @see nz.co.senanque.validationengine.annotations1.FieldValidator#validate(java.lang.Object)
*/
public void validate(Object o)
{
if (o != null && o instanceof Comparable)
{
compareMinExclusive((Comparable)o);
compareMinInclusive((Comparable)o);
compareMaxExclusive((Comparable)o);
compareMaxInclusive((Comparable)o);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareMinExclusive(Comparable o)
{
if (m_minExclusive == null) return;
if (o.compareTo((T) ConvertUtils.convertToComparable((Class)o.getClass(), m_minExclusive, m_propertyMetadata.getMessageSourceAccessor())) <= 0)
{
String message = m_propertyMetadata.getMessageSourceAccessor().getMessage(m_minExclusiveMessage, new Object[]{ m_propertyMetadata.getLabelName(),m_minExclusive, String.valueOf(o) });
throw new ValidationException(message);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareMinInclusive(Comparable o)
{
if (m_minInclusive == null)
{
return;
}
if (o.compareTo((T) ConvertUtils.convertToComparable((Class)o.getClass(), m_minInclusive, m_propertyMetadata.getMessageSourceAccessor())) < 0)
{
String message = m_propertyMetadata.getMessageSourceAccessor().getMessage(m_minInclusiveMessage, new Object[]{ m_propertyMetadata.getLabelName(),m_minInclusive, String.valueOf(o) });
throw new ValidationException(message);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareMaxExclusive(Comparable o)
{
if (m_maxExclusive == null)
{
return;
}
if (o.compareTo((T) ConvertUtils.convertToComparable((Class)o.getClass(), m_maxExclusive, m_propertyMetadata.getMessageSourceAccessor())) >= 0)
{
String message = m_propertyMetadata.getMessageSourceAccessor().getMessage(m_maxExclusiveMessage, new Object[]{ m_propertyMetadata.getLabelName(),m_maxExclusive, String.valueOf(o) });
throw new ValidationException(message);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareMaxInclusive(Comparable o)
{
if (m_maxInclusive == null)
{
return;
}
if (o.compareTo((T) ConvertUtils.convertToComparable( (Class)o.getClass(), m_maxInclusive, m_propertyMetadata.getMessageSourceAccessor())) > 0)
{
String message = m_propertyMetadata.getMessageSourceAccessor().getMessage(m_maxInclusiveMessage, new Object[]{ m_propertyMetadata.getLabelName(),m_maxInclusive, String.valueOf(o) });
throw new ValidationException(message);
}
}
}