KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > util > BaseFilter


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.grid.util;
17
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.apache.commons.lang.builder.ToStringStyle;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.util.Collections JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * <p>Base class for all grid filter types<br />
29  * Supports maximum of two clauses. <br />
30  * </p>
31  * <p><a HREF="BaseFilter.java.htm"><i>View Source</i></a></p>
32  *
33  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
34  * @version $Revision: 1.8 $ $Date: 2005/08/05 17:48:33 $
35  */

36 public abstract class BaseFilter implements GridFilter {
37
38     // ~ Instance variables
39

40     protected final Log log = LogFactory.getLog(BaseFilter.class);
41
42     /**
43      * Name of field this filter is associated with
44      */

45     protected String JavaDoc fieldName;
46
47     /**
48      * List of identifiers of rowIterators this filter is applicable to
49      */

50     protected List JavaDoc rowIterators = Collections.synchronizedList(new LinkedList JavaDoc());
51
52     /**
53      * First filter clause
54      */

55     private String JavaDoc firstClause = new String JavaDoc();
56
57     /**
58      * Second filter clause
59      */

60     private String JavaDoc secondClause = new String JavaDoc();
61
62     /**
63      * String with first and second clauses combined
64      */

65     private String JavaDoc clause = new String JavaDoc();
66
67     // ~ Business methods
68

69     /**
70      * Creates new instance of BaseFilter
71      *
72      * @param fieldName Name of field to associate filter with
73      */

74     public BaseFilter(String JavaDoc fieldName) {
75         this.fieldName = fieldName;
76     }
77
78     /**
79      * Returns name of field filter is associated with
80      *
81      * @return name of field
82      */

83     public String JavaDoc getFieldName() {
84         return fieldName;
85     }
86
87     /**
88      * Returns list of identifiers of rowIterators this filter is applicable to
89      *
90      * @return list of identifiers of rowIterators
91      */

92     public List JavaDoc getRowIterators() {
93         return rowIterators;
94     }
95
96     /**
97      * Sets list of identifiers of rowIterators this filter is applicable to
98      *
99      * @param rowIterators list of identifiers of rowIterators to set
100      */

101     public void setRowIterators(List JavaDoc rowIterators) {
102         this.rowIterators = rowIterators;
103     }
104
105     /**
106      * Adds row iterator ID to list of identifiers of rowIterators this filter is applicable to
107      *
108      * @param rowIteratorId row iterator ID
109      */

110     public void addRowIterator(String JavaDoc rowIteratorId) {
111         this.rowIterators.add(rowIteratorId);
112     }
113
114     /**
115      * Creates comparison with <code>null</code>
116      *
117      * @param condition Condition to set (is not null or is not null)
118      * @return String contains comparison with null
119      */

120     protected String JavaDoc createNullComparison(String JavaDoc condition) {
121         return fieldName + " " + condition;
122     }
123
124     /**
125      * Sets first clause
126      *
127      * @param firstClause Clause to set
128      */

129     protected void setFirstClause(String JavaDoc firstClause) {
130         this.firstClause = firstClause;
131     }
132
133     /**
134      * Sets second clause
135      *
136      * @param secondClause Clause to set
137      * @param logicCopula Logic copula between first and second condition ("and" or "or").
138      */

139     protected void setSecondClause(String JavaDoc secondClause, String JavaDoc logicCopula) {
140         this.secondClause = " " + logicCopula + " " + secondClause;
141     }
142
143     /**
144      * Returns clause constructed from two conditions, if
145      * this filter is enabled for iterator with specified ID. Otherwise
146      * <code>null is returned.</code>
147      *
148      * @return String with first and second clauses combined
149      */

150     public String JavaDoc getClause(String JavaDoc rowIteratorId) {
151
152         if ( (rowIteratorId == null) || (rowIterators.size() == 0) || (rowIterators.contains(rowIteratorId)) ) {
153             if ( firstClause.length() != 0 ) {
154                 clause = "(" + firstClause + secondClause + ")";
155             } else {
156                 clause = null;
157             }
158             return clause;
159         } else {
160             return null;
161         }
162     }
163
164     /**
165      * Returns clause constructed from two conditions
166      *
167      * @return clause constructed from two conditions
168      */

169     public String JavaDoc getClause() {
170         return getClause(null);
171     }
172
173     public String JavaDoc toString() {
174         return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
175     }
176 }
177
Popular Tags