001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.hibernate3;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.hibernate.engine.FilterDefinition;
023import org.hibernate.type.Type;
024import org.hibernate.type.TypeResolver;
025
026import org.springframework.beans.factory.BeanNameAware;
027import org.springframework.beans.factory.FactoryBean;
028import org.springframework.beans.factory.InitializingBean;
029
030/**
031 * Convenient FactoryBean for defining Hibernate FilterDefinitions.
032 * Exposes a corresponding Hibernate FilterDefinition object.
033 *
034 * <p>Typically defined as an inner bean within a LocalSessionFactoryBean
035 * definition, as the list element for the "filterDefinitions" bean property.
036 * For example:
037 *
038 * <pre class="code">
039 * &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
040 *   ...
041 *   &lt;property name="filterDefinitions"&gt;
042 *     &lt;list&gt;
043 *       &lt;bean class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean"&gt;
044 *         &lt;property name="filterName" value="myFilter"/&gt;
045 *         &lt;property name="parameterTypes"&gt;
046 *           &lt;map&gt;
047 *             &lt;entry key="myParam" value="string"/&gt;
048 *             &lt;entry key="myOtherParam" value="long"/&gt;
049 *           &lt;/map&gt;
050 *         &lt;/property&gt;
051 *       &lt;/bean&gt;
052 *     &lt;/list&gt;
053 *   &lt;/property&gt;
054 *   ...
055 * &lt;/bean&gt;</pre>
056 *
057 * Alternatively, specify a bean id (or name) attribute for the inner bean,
058 * instead of the "filterName" property.
059 *
060 * @author Juergen Hoeller
061 * @since 1.2
062 * @see org.hibernate.engine.FilterDefinition
063 * @see LocalSessionFactoryBean#setFilterDefinitions
064 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
065 */
066@Deprecated
067public class FilterDefinitionFactoryBean implements FactoryBean<FilterDefinition>, BeanNameAware, InitializingBean {
068
069        private final TypeResolver typeResolver = new TypeResolver();
070
071        private String filterName;
072
073        private Map<String, Type> parameterTypeMap = new HashMap<String, Type>();
074
075        private String defaultFilterCondition;
076
077        private FilterDefinition filterDefinition;
078
079
080        /**
081         * Set the name of the filter.
082         */
083        public void setFilterName(String filterName) {
084                this.filterName = filterName;
085        }
086
087        /**
088         * Set the parameter types for the filter,
089         * with parameter names as keys and type names as values.
090         * See {@code org.hibernate.type.TypeResolver#heuristicType(String)}.
091         */
092        public void setParameterTypes(Map<String, String> parameterTypes) {
093                if (parameterTypes != null) {
094                        this.parameterTypeMap = new HashMap<String, Type>(parameterTypes.size());
095                        for (Map.Entry<String, String> entry : parameterTypes.entrySet()) {
096                                this.parameterTypeMap.put(entry.getKey(), this.typeResolver.heuristicType(entry.getValue()));
097                        }
098                }
099                else {
100                        this.parameterTypeMap = new HashMap<String, Type>();
101                }
102        }
103
104        /**
105         * Specify a default filter condition for the filter, if any.
106         */
107        public void setDefaultFilterCondition(String defaultFilterCondition) {
108                this.defaultFilterCondition = defaultFilterCondition;
109        }
110
111        /**
112         * If no explicit filter name has been specified, the bean name of
113         * the FilterDefinitionFactoryBean will be used.
114         * @see #setFilterName
115         */
116        @Override
117        public void setBeanName(String name) {
118                if (this.filterName == null) {
119                        this.filterName = name;
120                }
121        }
122
123        @Override
124        public void afterPropertiesSet() {
125                this.filterDefinition =
126                                new FilterDefinition(this.filterName, this.defaultFilterCondition, this.parameterTypeMap);
127        }
128
129
130        @Override
131        public FilterDefinition getObject() {
132                return this.filterDefinition;
133        }
134
135        @Override
136        public Class<FilterDefinition> getObjectType() {
137                return FilterDefinition.class;
138        }
139
140        @Override
141        public boolean isSingleton() {
142                return true;
143        }
144
145}