1 package org.hibernate.util; 3 4 import org.hibernate.sql.Template; 5 import org.hibernate.impl.FilterImpl; 6 import org.hibernate.dialect.Dialect; 7 8 import java.util.Map ; 9 import java.util.Iterator ; 10 11 16 public class FilterHelper { 17 18 private final String [] filterNames; 19 private final String [] filterConditions; 20 21 29 public FilterHelper(Map filters, Dialect dialect) { 30 int filterCount = filters.size(); 31 filterNames = new String [filterCount]; 32 filterConditions = new String [filterCount]; 33 Iterator iter = filters.entrySet().iterator(); 34 filterCount = 0; 35 while ( iter.hasNext() ) { 36 final Map.Entry entry = (Map.Entry ) iter.next(); 37 filterNames[filterCount] = (String ) entry.getKey(); 38 filterConditions[filterCount] = Template.renderWhereStringTemplate( 39 (String ) entry.getValue(), 40 FilterImpl.MARKER, 41 dialect 42 ); 43 filterConditions[filterCount] = StringHelper.replace( filterConditions[filterCount], 44 ":", 45 ":" + filterNames[filterCount] + "." ); 46 filterCount++; 47 } 48 } 49 50 public boolean isAffectedBy(Map enabledFilters) { 51 for ( int i = 0, max = filterNames.length; i < max; i++ ) { 52 if ( enabledFilters.containsKey( filterNames[i] ) ) { 53 return true; 54 } 55 } 56 return false; 57 } 58 59 public String render(String alias, Map enabledFilters) { 60 StringBuffer buffer = new StringBuffer (); 61 render( buffer, alias, enabledFilters ); 62 return buffer.toString(); 63 } 64 65 public void render(StringBuffer buffer, String alias, Map enabledFilters) { 66 if ( filterNames != null && filterNames.length > 0 ) { 67 for ( int i = 0, max = filterNames.length; i < max; i++ ) { 68 if ( enabledFilters.containsKey( filterNames[i] ) ) { 69 final String condition = filterConditions[i]; 70 if ( StringHelper.isNotEmpty( condition ) ) { 71 buffer.append( " and " ) 72 .append( StringHelper.replace( condition, FilterImpl.MARKER, alias ) ); 73 } 74 } 75 } 76 } 77 } 78 } 79 | Popular Tags |