KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbFilterConditionTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbFilterConditionTag.java,v 1.22 2004/10/20 10:51:55 hkollmann Exp $
3  * $Revision: 1.22 $
4  * $Date: 2004/10/20 10:51:55 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.taglib;
25
26 import org.dbforms.config.FieldValue;
27
28 import org.dbforms.util.ParseUtil;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
37
38
39
40 /**
41  * Holds an sql condition that has to be nested inside a DbFilterTag. A
42  * condition is specified as sql code in the body of the tag. The character ?
43  * is a placeholder for user's input substitution. Every char ? found in sql
44  * code is replaced with value evalutated from corresponding filterValue tag
45  * nested. So there must be as ? as filterValue tags.
46  *
47  * @author Sergio Moretti
48  * @version $Revision: 1.22 $
49  */

50 public class DbFilterConditionTag extends TagSupportWithScriptHandler
51    implements TryCatchFinally JavaDoc {
52
53    /** object containing tag's state */
54    private transient State state;
55
56    /**
57                                                                                         *
58                                                                                         */

59    public DbFilterConditionTag() {
60       super();
61       state = new State();
62    }
63
64    /**
65     * DOCUMENT ME!
66     *
67     * @param string
68     */

69    public void setLabel(String JavaDoc string) {
70       state.label = string;
71    }
72
73
74    /**
75     * DOCUMENT ME!
76     *
77     * @return
78     */

79    public String JavaDoc getLabel() {
80       return state.label;
81    }
82
83
84    /**
85     * read filterCondition from body
86     *
87     * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
88     */

89    public int doAfterBody() throws JspException JavaDoc {
90       if (this.bodyContent != null) {
91          state.filterCondition = bodyContent.getString()
92                                             .trim();
93       }
94
95       return SKIP_BODY;
96    }
97
98
99    /**
100     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
101     */

102    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
103       throw t;
104    }
105
106
107    /**
108     * reset tag state
109     *
110     * @see javax.servlet.jsp.tagext.TryCatchFinally#doFinally()
111     */

112    public void doFinally() {
113       state = new State();
114    }
115
116
117    /**
118     * initialize environment and process body only if this condition is the
119     * currently selected.
120     *
121     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
122     */

123    public int doStartTag() throws JspException JavaDoc {
124       init();
125
126       String JavaDoc sel = ParseUtil.getParameter((HttpServletRequest JavaDoc) pageContext
127                                           .getRequest(),
128                                           ((DbFilterTag) getParent())
129                                           .getFilterName()
130                                           + DbFilterTag.FLT_SEL);
131
132       // process body only if this condition is active
133
if (sel != null) {
134          int selId = Integer.parseInt(sel);
135
136          if (selId == state.conditionId) {
137             return EVAL_BODY_BUFFERED;
138          }
139       }
140
141       return SKIP_BODY;
142    }
143
144
145    /**
146     * comparison using conditionId field
147     *
148     * @see java.lang.Object#equals(java.lang.Object)
149     */

150    public boolean equals(Object JavaDoc obj) {
151       return (obj instanceof DbFilterConditionTag)
152              && (state.conditionId == ((DbFilterConditionTag) obj).state.conditionId);
153    }
154
155
156    /**
157     * DOCUMENT ME!
158     *
159     * @return DOCUMENT ME!
160     */

161    public int hashCode() {
162       return state.conditionId;
163    }
164
165
166    /**
167     * condition prefix for request parameter
168     *
169     * @param tableId
170     * @param conditionId
171     *
172     * @return
173     */

174    protected static String JavaDoc getConditionName(int tableId,
175                                             int conditionId) {
176       return DbFilterTag.getFilterName(tableId) + DbFilterTag.FLT_COND
177              + conditionId;
178    }
179
180
181    /**
182     * generate condition from request. Called from nested DbFilterTag object
183     *
184     * @param request
185     * @param tableId
186     * @param conditionId
187     *
188     * @return string containing sql condition code
189     */

190    protected static String JavaDoc getSqlFilter(HttpServletRequest JavaDoc request,
191                                         int tableId,
192                                         int conditionId) {
193       return ParseUtil.getParameter(request,
194                                     DbFilterTag.getFilterName(tableId)
195                                     + DbFilterTag.FLT_COND + conditionId);
196    }
197
198
199    /**
200     * generate condition from request. Called from nested DbFilterTag object
201     *
202     * @param request
203     * @param tableId
204     * @param conditionId
205     *
206     * @return string containing sql condition code
207     */

208    protected static FieldValue[] getSqlFilterParams(HttpServletRequest JavaDoc request,
209                                                     int tableId,
210                                                     int conditionId) {
211       return DbFilterValueTag.readValuesFromRequest(request, tableId,
212                                                     conditionId);
213    }
214
215
216
217    /**
218     * condition prefix for request parameters
219     *
220     * @return
221     */

222    protected String JavaDoc getConditionName() {
223       return getConditionName(((DbFilterTag) getParent()).getTableId(),
224                               state.conditionId);
225    }
226
227
228    /**
229     * DOCUMENT ME!
230     *
231     * @param pg DOCUMENT ME!
232     * @param parent DOCUMENT ME!
233     * @param state
234     */

235    protected void setState(PageContext JavaDoc pg,
236                            DbFilterTag parent,
237                            State state) {
238       setParent(parent);
239       setPageContext(pg);
240       this.state = state;
241    }
242
243
244    /**
245     * DOCUMENT ME!
246     *
247     * @return
248     */

249    protected State getState() {
250       return state;
251    }
252
253
254    /**
255     * add a value to the value's list. called from nested DbFilterValueTag
256     * objs.
257     *
258     * @param value
259     *
260     * @return index of the newly added object
261     */

262    protected int addValue(DbFilterValueTag value) {
263       state.values.add(value.getState());
264
265       return state.values.size() - 1;
266    }
267
268
269    /**
270     * render output, called from parent DbFilterCondition obj.
271     *
272     * @return string containing html code for this obj
273     */

274    protected StringBuffer JavaDoc render() throws JspException JavaDoc {
275       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
276       buf.append("<input type=\"hidden\" name=\""
277                  + ((DbFilterTag) getParent()).getFilterName()
278                  + DbFilterTag.FLT_COND + state.conditionId + "\" value=\""
279                  + state.filterCondition + "\" />\n");
280
281       DbFilterValueTag value = new DbFilterValueTag();
282
283       for (Iterator JavaDoc i = state.values.iterator(); i.hasNext();) {
284          value.setState(this.pageContext, this,
285                         (DbFilterValueTag.State) i.next());
286          buf.append(value.render());
287       }
288
289       return buf;
290    }
291
292
293    /**
294     * initialize class's attributes
295     */

296    private void init() {
297       // state object is createad in constructor (for the first use) and in doEndTag next times
298
state.values = new ArrayList JavaDoc();
299
300       //state.parentFilter = (DbFilterTag) getParent();
301
state.conditionId = ((DbFilterTag) getParent()).addCondition(this);
302       state.filterCondition = null;
303
304       if (state.label == null) {
305          state.label = Integer.toString(state.conditionId);
306       }
307    }
308
309    /**
310     * tag's state holder. Used a separate class to hold tag's state to
311     * workaround to Tag pooling, in which an tag object is reused, but we have
312     * the need to store informations about all child tags in the parent, so
313     * we store the state, and apply it to a dummy tag when needed.
314     *
315     * @author Sergio Moretti
316     */

317    protected static class State {
318       /** list of value object's state contained in this condition */
319       protected ArrayList JavaDoc values = null;
320
321       /** raw filter condition */
322       protected String JavaDoc filterCondition = null;
323
324       /** condition's label, appear as an option in html select element */
325       protected String JavaDoc label = null;
326
327       /** identifier of condition */
328       protected int conditionId = -1;
329    }
330 }
331
Popular Tags