nz.co.senanque.validationengine.Binder 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;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import nz.co.senanque.validationengine.annotations.Ignore;
import nz.co.senanque.validationengine.metadata.ClassMetadata;
import nz.co.senanque.validationengine.metadata.PropertyMetadata;
import nz.co.senanque.validationengine.metadata.PropertyMetadataImpl;
import org.springframework.context.support.MessageSourceAccessor;
/**
*
* This class is responsible for binding the graphed objects into the validator
* session The arrays being used must be converted to listener arrays.
*
* @author Roger Parkinson
* @version $Revision: 1.11 $
*/
public class Binder
{
private final transient ValidationEngineImpl m_validationEngine;
public Binder(final ValidationEngineImpl validationEngine)
{
m_validationEngine = validationEngine;
}
public Map bind(final ValidationObject object,
final ValidationSession session, ValidationObject owner)
{
return bind(object, session, null, null,owner);
}
public Map bind(final ValidationObject object,
final ValidationSession session, final ProxyField parent, final Integer index, ValidationObject owner)
{
MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(m_validationEngine.getMessageSource());
final Map ret = new IdentityHashMap();
if (object == null)
{
return ret;
}
Class> clazz = object.getClass();
final ClassMetadata classMetadata = m_validationEngine
.getClassMetadata(clazz);
if (classMetadata == null)
{
throw m_validationEngine.getLocaleAwareExceptionFactory().getRuntimeException("nz.co.senanque.validationengine.class.not.recognised", new Object[]{clazz.getName()});
}
object.setValidationSession(session);
final ProxyObject proxyObject = new ProxyObject(object, parent, index,session);
ret.put(object, proxyObject);
final ObjectMetadata objectMetadata = (ObjectMetadata)object.getMetadata();
objectMetadata.setClassMetadata(proxyObject, classMetadata);
Map propertyMap = ValidationUtils.getProperties(object.getClass());
for (Property property: propertyMap.values()) {
String fieldName = property.getFieldName();
final PropertyMetadataImpl propertyMetadata = (PropertyMetadataImpl)classMetadata
.getField(fieldName);
if (propertyMetadata == null)
{
continue;
}
final ProxyFieldImpl proxyField = new ProxyFieldImpl(fieldName,
proxyObject, parent, propertyMetadata, messageSourceAccessor);
proxyObject.put(fieldName, proxyField);
Method method = property.getGetter();
if (property.getGetter().getReturnType().isAssignableFrom(List.class))
{
try
{
@SuppressWarnings("unchecked")
final List validationObjects = (List) method.invoke(object,new Object[]{});
final ListeningArray
© 2015 - 2025 Weber Informatics LLC | Privacy Policy