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.
014package org.apache.tapestry5.internal.beanvalidator;
015
016import java.util.Arrays;
017import java.util.Locale;
018
019import javax.validation.ValidatorFactory;
020
021import org.apache.tapestry5.ComponentResources;
022import org.apache.tapestry5.Field;
023import org.apache.tapestry5.FieldValidator;
024import org.apache.tapestry5.beanvalidator.BeanValidatorGroupSource;
025import org.apache.tapestry5.beanvalidator.ClientConstraintDescriptorSource;
026import org.apache.tapestry5.commons.AnnotationProvider;
027import org.apache.tapestry5.commons.Messages;
028import org.apache.tapestry5.internal.services.CompositeFieldValidator;
029import org.apache.tapestry5.services.Core;
030import org.apache.tapestry5.services.Environment;
031import org.apache.tapestry5.services.FieldValidatorDefaultSource;
032import org.apache.tapestry5.services.FormSupport;
033
034public 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        @Override
060        public FieldValidator createDefaultValidator(final Field field,
061                        final String overrideId, final Messages overrideMessages,
062                        final Locale locale, final Class propertyType,
063                        final AnnotationProvider propertyAnnotations) 
064        {
065                
066                FieldValidator validator = fieldValidatorDefaultSource.createDefaultValidator(
067                                field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
068
069                
070                FieldValidator beanValidator 
071                        = new BeanFieldValidator(field, validatorFactory, beanValidationGroupSource, 
072                                        clientValidatorSource, formSupport, environment);
073                
074                return new CompositeFieldValidator(Arrays.asList(validator, beanValidator));
075        }
076
077        @Override
078        public FieldValidator createDefaultValidator(
079                        final ComponentResources resources, final String parameterName) 
080        {
081                final Class propertyType = resources.getBoundType(parameterName);
082                
083                if (propertyType == null)
084                        return null;
085
086                final Field field = (Field) resources.getComponent();
087
088                return createDefaultValidator(field, resources.getId(), resources
089                                .getContainerMessages(), resources.getLocale(), propertyType,
090                                resources.getAnnotationProvider(parameterName));
091        }
092}