KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > impl > FilterImpl


1 // $Id: FilterImpl.java,v 1.7 2005/07/04 02:40:29 oneovthafew Exp $
2
package org.hibernate.impl;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.hibernate.Filter;
12 import org.hibernate.HibernateException;
13 import org.hibernate.engine.FilterDefinition;
14 import org.hibernate.type.Type;
15
16 /**
17  * Implementation of FilterImpl. FilterImpl implements the user's
18  * view into enabled dynamic filters, allowing them to set filter parameter values.
19  *
20  * @author Steve Ebersole
21  */

22 public class FilterImpl implements Filter, Serializable JavaDoc {
23     public static final String JavaDoc MARKER = "$FILTER_PLACEHOLDER$";
24
25     private transient FilterDefinition definition;
26     private String JavaDoc filterName;
27     private Map JavaDoc parameters = new HashMap JavaDoc();
28     
29     void afterDeserialize(SessionFactoryImpl factory) {
30         definition = factory.getFilterDefinition(filterName);
31     }
32
33     /**
34      * Constructs a new FilterImpl.
35      *
36      * @param configuration The filter's global configuration.
37      */

38     public FilterImpl(FilterDefinition configuration) {
39         this.definition = configuration;
40     }
41     
42     public Map JavaDoc getParameters() {
43         return parameters;
44     }
45     
46     public Map JavaDoc getParameterTypes() {
47         return definition.getParameterTypes();
48     }
49
50     /**
51      * Set the named parameter's value for this filter.
52      *
53      * @param name The parameter's name.
54      * @param value The value to be applied.
55      * @return This FilterImpl instance (for method chaining).
56      * @throws IllegalArgumentException Indicates that either the parameter was undefined or that the type
57      * of the passed value did not match the configured type.
58      */

59     public Filter setParameter(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc {
60         // Make sure this is a defined parameter and check the incoming value type
61
// TODO: what should be the actual exception type here?
62
Type type = definition.getParameterType( name );
63         if ( type == null ) {
64             throw new IllegalArgumentException JavaDoc( "Undefined filter parameter [" + name + "]" );
65         }
66         if ( value != null && !type.getReturnedClass().isAssignableFrom( value.getClass() ) ) {
67             throw new IllegalArgumentException JavaDoc( "Incorrect type for parameter [" + name + "]" );
68         }
69         parameters.put( name, value );
70         return this;
71     }
72
73     /**
74      * Set the named parameter's value list for this filter. Used
75      * in conjunction with IN-style filter criteria.
76      *
77      * @param name The parameter's name.
78      * @param values The values to be expanded into an SQL IN list.
79      * @return This FilterImpl instance (for method chaining).
80      */

81     public Filter setParameterList(String JavaDoc name, Collection JavaDoc values) throws IllegalArgumentException JavaDoc {
82         // Make sure this is a defined parameter and check the incoming value type
83
// TODO: what should be the actual exception types here?
84
if ( values == null ) {
85             throw new IllegalArgumentException JavaDoc( "Collection must be not null!" );
86         }
87         Type type = definition.getParameterType( name );
88         if ( type == null ) {
89             throw new IllegalArgumentException JavaDoc( "Undefined filter parameter [" + name + "]" );
90         }
91         if ( values != null && values.size() > 0 ) {
92             Class JavaDoc elementClass = values.iterator().next().getClass();
93             if ( !type.getReturnedClass().isAssignableFrom( elementClass ) ) {
94                 throw new IllegalArgumentException JavaDoc("Incorrect type for parameter [" + name + "]");
95             }
96         }
97         parameters.put( name, values );
98         return this;
99     }
100
101     /**
102      * Set the named parameter's value list for this filter. Used
103      * in conjunction with IN-style filter criteria.
104      *
105      * @param name The parameter's name.
106      * @param values The values to be expanded into an SQL IN list.
107      * @return This FilterImpl instance (for method chaining).
108      */

109     public Filter setParameterList(String JavaDoc name, Object JavaDoc[] values) throws IllegalArgumentException JavaDoc {
110         return setParameterList( name, Arrays.asList( values ) );
111     }
112
113     /**
114      * Get the value of the named parameter for the current filter.
115      *
116      * @param name The name of the parameter for which to return the value.
117      * @return The value of the named parameter.
118      */

119     public Object JavaDoc getParameter(String JavaDoc name) {
120         return parameters.get( name );
121     }
122
123     /**
124      * Get the name of this filter.
125      *
126      * @return This filter's name.
127      */

128     public String JavaDoc getName() {
129         return definition.getFilterName();
130     }
131
132     /**
133      * Perform validation of the filter state. This is used to verify the
134      * state of the filter after its enablement and before its use.
135      *
136      * @throws HibernateException If the state is not currently valid.
137      */

138     public void validate() throws HibernateException {
139         // for each of the defined parameters, make sure its value
140
// has been set
141
Iterator JavaDoc itr = definition.getParameterNames().iterator();
142         while ( itr.hasNext() ) {
143             final String JavaDoc parameterName = (String JavaDoc) itr.next();
144             if ( parameters.get(parameterName) == null ) {
145                 throw new IllegalArgumentException JavaDoc("Filter [" + getName() + "] defined parameter [" + parameterName + "] whose value was not set");
146             }
147         }
148     }
149 }
150
Popular Tags