001: // $Id: FilterImpl.java 8754 2005-12-05 23:36:59Z steveebersole $
002: package org.hibernate.impl;
003:
004: import java.io.Serializable;
005: import java.util.Arrays;
006: import java.util.Collection;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: import org.hibernate.Filter;
012: import org.hibernate.HibernateException;
013: import org.hibernate.engine.FilterDefinition;
014: import org.hibernate.type.Type;
015:
016: /**
017: * Implementation of FilterImpl. FilterImpl implements the user's
018: * view into enabled dynamic filters, allowing them to set filter parameter values.
019: *
020: * @author Steve Ebersole
021: */
022: public class FilterImpl implements Filter, Serializable {
023: public static final String MARKER = "$FILTER_PLACEHOLDER$";
024:
025: private transient FilterDefinition definition;
026: private String filterName;
027: private Map parameters = new HashMap();
028:
029: void afterDeserialize(SessionFactoryImpl factory) {
030: definition = factory.getFilterDefinition(filterName);
031: }
032:
033: /**
034: * Constructs a new FilterImpl.
035: *
036: * @param configuration The filter's global configuration.
037: */
038: public FilterImpl(FilterDefinition configuration) {
039: this .definition = configuration;
040: filterName = definition.getFilterName();
041: }
042:
043: public FilterDefinition getFilterDefinition() {
044: return definition;
045: }
046:
047: /**
048: * Get the name of this filter.
049: *
050: * @return This filter's name.
051: */
052: public String getName() {
053: return definition.getFilterName();
054: }
055:
056: public Map getParameters() {
057: return parameters;
058: }
059:
060: /**
061: * Set the named parameter's value for this filter.
062: *
063: * @param name The parameter's name.
064: * @param value The value to be applied.
065: * @return This FilterImpl instance (for method chaining).
066: * @throws IllegalArgumentException Indicates that either the parameter was undefined or that the type
067: * of the passed value did not match the configured type.
068: */
069: public Filter setParameter(String name, Object value)
070: throws IllegalArgumentException {
071: // Make sure this is a defined parameter and check the incoming value type
072: // TODO: what should be the actual exception type here?
073: Type type = definition.getParameterType(name);
074: if (type == null) {
075: throw new IllegalArgumentException(
076: "Undefined filter parameter [" + name + "]");
077: }
078: if (value != null
079: && !type.getReturnedClass().isAssignableFrom(
080: value.getClass())) {
081: throw new IllegalArgumentException(
082: "Incorrect type for parameter [" + name + "]");
083: }
084: parameters.put(name, value);
085: return this ;
086: }
087:
088: /**
089: * Set the named parameter's value list for this filter. Used
090: * in conjunction with IN-style filter criteria.
091: *
092: * @param name The parameter's name.
093: * @param values The values to be expanded into an SQL IN list.
094: * @return This FilterImpl instance (for method chaining).
095: */
096: public Filter setParameterList(String name, Collection values)
097: throws HibernateException {
098: // Make sure this is a defined parameter and check the incoming value type
099: if (values == null) {
100: throw new IllegalArgumentException(
101: "Collection must be not null!");
102: }
103: Type type = definition.getParameterType(name);
104: if (type == null) {
105: throw new HibernateException("Undefined filter parameter ["
106: + name + "]");
107: }
108: if (values.size() > 0) {
109: Class elementClass = values.iterator().next().getClass();
110: if (!type.getReturnedClass().isAssignableFrom(elementClass)) {
111: throw new HibernateException(
112: "Incorrect type for parameter [" + name + "]");
113: }
114: }
115: parameters.put(name, values);
116: return this ;
117: }
118:
119: /**
120: * Set the named parameter's value list for this filter. Used
121: * in conjunction with IN-style filter criteria.
122: *
123: * @param name The parameter's name.
124: * @param values The values to be expanded into an SQL IN list.
125: * @return This FilterImpl instance (for method chaining).
126: */
127: public Filter setParameterList(String name, Object[] values)
128: throws IllegalArgumentException {
129: return setParameterList(name, Arrays.asList(values));
130: }
131:
132: /**
133: * Get the value of the named parameter for the current filter.
134: *
135: * @param name The name of the parameter for which to return the value.
136: * @return The value of the named parameter.
137: */
138: public Object getParameter(String name) {
139: return parameters.get(name);
140: }
141:
142: /**
143: * Perform validation of the filter state. This is used to verify the
144: * state of the filter after its enablement and before its use.
145: *
146: * @throws HibernateException If the state is not currently valid.
147: */
148: public void validate() throws HibernateException {
149: // for each of the defined parameters, make sure its value
150: // has been set
151: Iterator itr = definition.getParameterNames().iterator();
152: while (itr.hasNext()) {
153: final String parameterName = (String) itr.next();
154: if (parameters.get(parameterName) == null) {
155: throw new HibernateException("Filter [" + getName()
156: + "] parameter [" + parameterName
157: + "] value not set");
158: }
159: }
160: }
161: }
|