View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbSearchTag.java,v 1.18 2006/03/25 15:37:44 nicparise Exp $
3    * $Revision: 1.18 $
4    * $Date: 2006/03/25 15:37:44 $
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.*;
27  
28  import org.dbforms.util.*;
29  
30  import javax.servlet.http.*;
31  import javax.servlet.jsp.*;
32  
33  /***
34   * <p>
35   * renders a input field for searching with special default search modes.
36   * </p>
37   * 
38   * <p>
39   * example:
40   * </p>
41   * &lt;input type="hidden" name="searchalgo_0_1" value="weakEnd"/&gt; &lt;input
42   * type="hidden" name="searchmode_0_1" value="AND"/&gt; &lt;input type="text"
43   * name="search_0_1"/&gt; searchalgo and searchmode are set by parameter.
44   * 
45   * @author Henner Kollmann
46   */
47  public class DbSearchTag extends AbstractDbBaseHandlerTag implements
48  		javax.servlet.jsp.tagext.TryCatchFinally {
49  	private String defaultValue = null;
50  
51  	private String searchAlgo = "sharp";
52  
53  	private String searchMode = "and";
54  
55  	private String hidden = "false";
56  
57  	/***
58  	 * DOCUMENT ME!
59  	 * 
60  	 * @param value
61  	 *            DOCUMENT ME!
62  	 */
63  	public void setDefault(String value) {
64  		this.defaultValue = value;
65  	}
66  
67  	/***
68  	 * DOCUMENT ME!
69  	 * 
70  	 * @return DOCUMENT ME!
71  	 */
72  	public String getDefault() {
73  		return defaultValue;
74  	}
75  
76  	/***
77  	 * DOCUMENT ME!
78  	 * 
79  	 * @param searchAlgo
80  	 *            DOCUMENT ME!
81  	 */
82  	public void setSearchAlgo(String searchAlgo) {
83  		this.searchAlgo = searchAlgo;
84  	}
85  
86  	/***
87  	 * DOCUMENT ME!
88  	 * 
89  	 * @return DOCUMENT ME!
90  	 */
91  	public String getSearchAlgo() {
92  		return searchAlgo;
93  	}
94  
95  	/***
96  	 * DOCUMENT ME!
97  	 * 
98  	 * @param searchMode
99  	 *            DOCUMENT ME!
100 	 */
101 	public void setSearchMode(String searchMode) {
102 		this.searchMode = searchMode;
103 	}
104 
105 	/***
106 	 * DOCUMENT ME!
107 	 * 
108 	 * @return DOCUMENT ME!
109 	 */
110 	public String getSearchMode() {
111 		return searchMode;
112 	}
113 
114 	/***
115 	 * DOCUMENT ME!
116 	 * 
117 	 * @return DOCUMENT ME!
118 	 * 
119 	 * @throws javax.servlet.jsp.JspException
120 	 *             DOCUMENT ME!
121 	 * @throws JspException
122 	 *             DOCUMENT ME!
123 	 */
124 	public int doEndTag() throws javax.servlet.jsp.JspException {
125 		try {
126 			Field field = getField();
127 
128 			/*
129 			 * <input type="hidden" name="searchalgo_0_1" value="weakEnd"/>
130 			 * <input type="hidden" name="searchmode_0_1" value="AND"/> <input
131 			 * type="text" name="search_0_1"/>
132 			 */
133 			HttpServletRequest request = (HttpServletRequest) pageContext
134 					.getRequest();
135 			StringBuffer tagBuf = new StringBuffer();
136 
137 			StringBuffer paramNameBuf = new StringBuffer();
138 			paramNameBuf.append(field.getSearchFieldName());
139 
140 			String oldValue = ParseUtil.getParameter(request, paramNameBuf
141 					.toString());
142 			/*
143 			 * PG - 2005-11-13 We may want to hide this field
144 			 */
145 			if (Util.getTrue(this.getHidden())) {
146 				tagBuf.append("<input type=\"hidden\" name=\"");
147 			} else {
148 				tagBuf.append("<input type=\"text\" name=\"");
149 			}
150 
151 			tagBuf.append(paramNameBuf.toString());
152 			tagBuf.append("\" ");
153 
154 			tagBuf.append("value=\"");
155 
156 			if (oldValue != null) {
157 				tagBuf.append(oldValue);
158 			} else if (defaultValue != null) {
159 				tagBuf.append(defaultValue);
160 			}
161 
162 			tagBuf.append("\"");
163 			tagBuf.append(prepareStyles());
164 			tagBuf.append(prepareEventHandlers());
165 			tagBuf.append("/>\n");
166 
167 			pageContext.getOut().write(renderPatternHtmlInputField());
168 			pageContext.getOut().write(RenderHiddenFields(field));
169 			pageContext.getOut().write(tagBuf.toString());
170 		} catch (java.io.IOException ioe) {
171 			throw new JspException("IO Error: " + ioe.getMessage());
172 		}
173 
174 		return EVAL_PAGE;
175 	}
176 
177 	/*
178 	 * Must be overloaded to get the right fieldName in
179 	 * renderPatternHtmlInputField!
180 	 */
181 	protected String getFormFieldName() {
182 		return getField().getSearchFieldName();
183 	}
184 
185 	/***
186 	 * DOCUMENT ME!
187 	 */
188 	public void doFinally() {
189 		searchAlgo = "sharp";
190 		searchMode = "and";
191 		defaultValue = null;
192 		super.doFinally();
193 	}
194 
195 	/***
196 	 * DOCUMENT ME!
197 	 * 
198 	 * @param f
199 	 *            DOCUMENT ME!
200 	 * 
201 	 * @return DOCUMENT ME!
202 	 */
203 	protected String RenderHiddenFields(Field f) {
204 		StringBuffer tagBuf = new StringBuffer();
205 		StringBuffer paramNameBufA = new StringBuffer();
206 		paramNameBufA.append(f.getSearchAlgoName());
207 		tagBuf.append("<input type=\"hidden\" name=\"");
208 		tagBuf.append(paramNameBufA.toString());
209 		tagBuf.append("\" value=\"");
210 		tagBuf.append(getSearchAlgo());
211 		tagBuf.append("\"/>\n");
212 
213 		StringBuffer paramNameBufB = new StringBuffer();
214 		paramNameBufB.append(f.getSearchModeName());
215 		tagBuf.append("<input type=\"hidden\" name=\"");
216 		tagBuf.append(paramNameBufB.toString());
217 		tagBuf.append("\" value=\"");
218 		tagBuf.append(getSearchMode());
219 		tagBuf.append("\"/>\n");
220 
221 		return tagBuf.toString();
222 	}
223 
224 	public String getHidden() {
225 		return hidden;
226 	}
227 
228 	public void setHidden(String hidden) {
229 		this.hidden = hidden;
230 	}
231 }