001    // Copyright 2009, 2010 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry5.internal.beanvalidator;
015    
016    import java.util.Arrays;
017    import java.util.Locale;
018    
019    import javax.validation.ValidatorFactory;
020    
021    import org.apache.tapestry5.ComponentResources;
022    import org.apache.tapestry5.Field;
023    import org.apache.tapestry5.FieldValidator;
024    import org.apache.tapestry5.beanvalidator.BeanValidatorGroupSource;
025    import org.apache.tapestry5.beanvalidator.ClientConstraintDescriptorSource;
026    import org.apache.tapestry5.internal.services.CompositeFieldValidator;
027    import org.apache.tapestry5.ioc.AnnotationProvider;
028    import org.apache.tapestry5.ioc.Messages;
029    import org.apache.tapestry5.services.Core;
030    import org.apache.tapestry5.services.Environment;
031    import org.apache.tapestry5.services.FieldValidatorDefaultSource;
032    import org.apache.tapestry5.services.FormSupport;
033    
034    public class BeanFieldValidatorDefaultSource implements FieldValidatorDefaultSource 
035    {
036            private final FieldValidatorDefaultSource fieldValidatorDefaultSource;
037            private final ValidatorFactory validatorFactory;
038            private final BeanValidatorGroupSource beanValidationGroupSource;
039            private final ClientConstraintDescriptorSource clientValidatorSource;
040            private final FormSupport formSupport;
041            private final Environment environment;
042    
043            public BeanFieldValidatorDefaultSource(
044                            @Core FieldValidatorDefaultSource fieldValidatorDefaultSource,
045                            final ValidatorFactory validatorFactory,
046                            final BeanValidatorGroupSource beanValidationGroupSource,
047                            final ClientConstraintDescriptorSource clientValidatorSource,
048                            final FormSupport formSupport,
049                            final Environment environment) 
050            {
051                    this.fieldValidatorDefaultSource = fieldValidatorDefaultSource;
052                    this.validatorFactory = validatorFactory;
053                    this.beanValidationGroupSource = beanValidationGroupSource;
054                    this.clientValidatorSource = clientValidatorSource;
055                    this.formSupport = formSupport;
056                    this.environment = environment;
057            }
058    
059            public FieldValidator createDefaultValidator(final Field field,
060                            final String overrideId, final Messages overrideMessages,
061                            final Locale locale, final Class propertyType,
062                            final AnnotationProvider propertyAnnotations) 
063            {
064                    
065                    FieldValidator validator = fieldValidatorDefaultSource.createDefaultValidator(
066                                    field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
067    
068                    
069                    FieldValidator beanValidator 
070                            = new BeanFieldValidator(field, validatorFactory, beanValidationGroupSource, 
071                                            clientValidatorSource, formSupport, environment);
072                    
073                    return new CompositeFieldValidator(Arrays.asList(validator, beanValidator));
074            }
075    
076            public FieldValidator createDefaultValidator(
077                            final ComponentResources resources, final String parameterName) 
078            {
079                    final Class propertyType = resources.getBoundType(parameterName);
080                    
081                    if (propertyType == null)
082                            return null;
083    
084                    final Field field = (Field) resources.getComponent();
085    
086                    return createDefaultValidator(field, resources.getId(), resources
087                                    .getContainerMessages(), resources.getLocale(), propertyType,
088                                    resources.getAnnotationProvider(parameterName));
089            }
090    }