1 package org.hibernate.impl; 3 4 import java.io.Serializable ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 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 22 public class FilterImpl implements Filter, Serializable { 23 public static final String MARKER = "$FILTER_PLACEHOLDER$"; 24 25 private transient FilterDefinition definition; 26 private String filterName; 27 private Map parameters = new HashMap (); 28 29 void afterDeserialize(SessionFactoryImpl factory) { 30 definition = factory.getFilterDefinition(filterName); 31 } 32 33 38 public FilterImpl(FilterDefinition configuration) { 39 this.definition = configuration; 40 } 41 42 public Map getParameters() { 43 return parameters; 44 } 45 46 public Map getParameterTypes() { 47 return definition.getParameterTypes(); 48 } 49 50 59 public Filter setParameter(String name, Object value) throws IllegalArgumentException { 60 Type type = definition.getParameterType( name ); 63 if ( type == null ) { 64 throw new IllegalArgumentException ( "Undefined filter parameter [" + name + "]" ); 65 } 66 if ( value != null && !type.getReturnedClass().isAssignableFrom( value.getClass() ) ) { 67 throw new IllegalArgumentException ( "Incorrect type for parameter [" + name + "]" ); 68 } 69 parameters.put( name, value ); 70 return this; 71 } 72 73 81 public Filter setParameterList(String name, Collection values) throws IllegalArgumentException { 82 if ( values == null ) { 85 throw new IllegalArgumentException ( "Collection must be not null!" ); 86 } 87 Type type = definition.getParameterType( name ); 88 if ( type == null ) { 89 throw new IllegalArgumentException ( "Undefined filter parameter [" + name + "]" ); 90 } 91 if ( values != null && values.size() > 0 ) { 92 Class elementClass = values.iterator().next().getClass(); 93 if ( !type.getReturnedClass().isAssignableFrom( elementClass ) ) { 94 throw new IllegalArgumentException ("Incorrect type for parameter [" + name + "]"); 95 } 96 } 97 parameters.put( name, values ); 98 return this; 99 } 100 101 109 public Filter setParameterList(String name, Object [] values) throws IllegalArgumentException { 110 return setParameterList( name, Arrays.asList( values ) ); 111 } 112 113 119 public Object getParameter(String name) { 120 return parameters.get( name ); 121 } 122 123 128 public String getName() { 129 return definition.getFilterName(); 130 } 131 132 138 public void validate() throws HibernateException { 139 Iterator itr = definition.getParameterNames().iterator(); 142 while ( itr.hasNext() ) { 143 final String parameterName = (String ) itr.next(); 144 if ( parameters.get(parameterName) == null ) { 145 throw new IllegalArgumentException ("Filter [" + getName() + "] defined parameter [" + parameterName + "] whose value was not set"); 146 } 147 } 148 } 149 } 150 | Popular Tags |