KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > FilterHelper


1 // $Id: FilterHelper.java,v 1.1 2005/04/25 16:47:31 steveebersole Exp $
2
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 JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * Implementation of FilterHelper.
13  *
14  * @author Steve Ebersole
15  */

16 public class FilterHelper {
17
18     private final String JavaDoc[] filterNames;
19     private final String JavaDoc[] filterConditions;
20
21     /**
22      * The map of defined filters. This is expected to be in format
23      * where the filter names are the map keys, and the defined
24      * conditions are the values.
25      *
26      * @param filters The map of defined filters.
27      * @param dialect The sql dialect
28      */

29     public FilterHelper(Map JavaDoc filters, Dialect dialect) {
30         int filterCount = filters.size();
31         filterNames = new String JavaDoc[filterCount];
32         filterConditions = new String JavaDoc[filterCount];
33         Iterator JavaDoc iter = filters.entrySet().iterator();
34         filterCount = 0;
35         while ( iter.hasNext() ) {
36             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
37             filterNames[filterCount] = (String JavaDoc) entry.getKey();
38             filterConditions[filterCount] = Template.renderWhereStringTemplate(
39                     (String JavaDoc) 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 JavaDoc 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 JavaDoc render(String JavaDoc alias, Map JavaDoc enabledFilters) {
60         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
61         render( buffer, alias, enabledFilters );
62         return buffer.toString();
63     }
64
65     public void render(StringBuffer JavaDoc buffer, String JavaDoc alias, Map JavaDoc 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 JavaDoc 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