|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.grails.plugins.domain |
| 21 | + |
| 22 | +import grails.core.GrailsApplication |
| 23 | +import grails.validation.ConstraintsEvaluator |
| 24 | +import groovy.transform.CompileStatic |
| 25 | +import org.grails.datastore.gorm.validation.constraints.factory.ConstraintFactory |
| 26 | +import org.grails.datastore.mapping.model.MappingContext |
| 27 | +import org.grails.plugins.domain.support.ConstraintEvaluatorAdapter |
| 28 | +import org.grails.plugins.domain.support.DefaultConstraintEvaluatorFactoryBean |
| 29 | +import org.grails.plugins.domain.support.DefaultMappingContextFactoryBean |
| 30 | +import org.grails.plugins.domain.support.ValidatorRegistryFactoryBean |
| 31 | +import org.grails.plugins.i18n.I18nAutoConfiguration |
| 32 | +import org.springframework.beans.factory.annotation.Autowired |
| 33 | +import org.springframework.beans.factory.annotation.Qualifier |
| 34 | +import org.springframework.boot.autoconfigure.AutoConfiguration |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication |
| 36 | +import org.springframework.context.ApplicationContext |
| 37 | +import org.springframework.context.MessageSource |
| 38 | +import org.springframework.context.annotation.Bean |
| 39 | +import org.springframework.context.annotation.Lazy |
| 40 | + |
| 41 | +@CompileStatic |
| 42 | +// TODO: datasource plugin is supposed to always load after this (currently will because this is a configuration) |
| 43 | +@AutoConfiguration(after = [I18nAutoConfiguration]) |
| 44 | +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
| 45 | +class GrailsDomainClassAutoConfiguration { |
| 46 | + |
| 47 | + @Autowired |
| 48 | + GrailsApplication grailsApplication |
| 49 | + |
| 50 | + @Autowired |
| 51 | + ApplicationContext applicationContext |
| 52 | + |
| 53 | + @Autowired |
| 54 | + List<MessageSource> messageSources |
| 55 | + |
| 56 | + @Lazy |
| 57 | + @Bean('grailsDomainClassMappingContext') |
| 58 | + DefaultMappingContextFactoryBean grailsDomainClassMappingContext( |
| 59 | + List<ConstraintFactory> constraintFactories |
| 60 | + ) { |
| 61 | + def bean = new DefaultMappingContextFactoryBean(grailsApplication, messageSources) |
| 62 | + bean.constraintFactories = constraintFactories ?: [] |
| 63 | + bean |
| 64 | + } |
| 65 | + |
| 66 | + @Lazy |
| 67 | + @Bean |
| 68 | + DefaultConstraintEvaluatorFactoryBean validateableConstraintsEvaluator(@Qualifier('grailsDomainClassMappingContext') MappingContext grailsDomainClassMappingContext) { |
| 69 | + |
| 70 | + new DefaultConstraintEvaluatorFactoryBean(messageSources, grailsDomainClassMappingContext, grailsApplication) |
| 71 | + } |
| 72 | + |
| 73 | + @Lazy |
| 74 | + @Bean(name = ConstraintsEvaluator.BEAN_NAME) |
| 75 | + ConstraintEvaluatorAdapter constraintsEvaluator(DefaultConstraintEvaluatorFactoryBean validateableConstraintsEvaluator) { |
| 76 | + def bean = new ConstraintEvaluatorAdapter(validateableConstraintsEvaluator.getObject()) |
| 77 | + bean |
| 78 | + } |
| 79 | + |
| 80 | + @Lazy |
| 81 | + @Bean |
| 82 | + ValidatorRegistryFactoryBean gormValidatorRegistry(@Qualifier('grailsDomainClassMappingContext') MappingContext grailsDomainClassMappingContext) { |
| 83 | + def bean = new ValidatorRegistryFactoryBean() |
| 84 | + bean.mappingContext = grailsDomainClassMappingContext |
| 85 | + bean |
| 86 | + } |
| 87 | +} |
0 commit comments