1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.vlh.web;
22
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.StringTokenizer;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.jsp.PageContext;
31
32 import net.mlw.vlh.ValueListInfo;
33 import net.mlw.vlh.web.tag.ActionTag;
34
35 /***
36 * This is a utility to assist usig the ValueList in a web enviroment.
37 *
38 * net.mlw.vlh.web.ValueListRequestUtil
39 *
40 * @author Matthew L. Wilson, Andrej Zachar
41 * @version $Revision: 1.16 $ $Date: 2006/03/28 17:06:49 $
42 */
43 public final class ValueListRequestUtil
44 {
45
46 /*** Protect singleton */
47 private ValueListRequestUtil()
48 {
49 }
50
51 public static Map getRequestParameterMap(HttpServletRequest request)
52 {
53 return getRequestParameterMap(request, "");
54 }
55
56 public static Map getRequestParameterMap(Map requestParameterMap, String id)
57 {
58
59 if (requestParameterMap == null)
60 {
61 return new HashMap();
62 }
63 int lenghtOfId = (id == null) ? 0 : id.length();
64
65 Map parameters = new HashMap(requestParameterMap.size());
66 for (Iterator keys = requestParameterMap.keySet().iterator(); keys.hasNext();)
67 {
68 String key = (String) keys.next();
69
70 String[] values;
71 Object valuesObj = requestParameterMap.get(key);
72 if (valuesObj instanceof String)
73 {
74 values = new String[] {(String) valuesObj};
75 }
76 else
77 {
78 values = (String[]) valuesObj;
79 }
80
81 Object value = ((values == null) ? null : (values.length == 1 ? (Object) values[0] : (Object) values));
82
83 if (lenghtOfId > 0)
84 {
85 if (key.endsWith(id))
86 {
87 parameters.put(key.substring(0, key.length() - lenghtOfId), value);
88 }
89 }
90 else
91 {
92 parameters.put(key, value);
93 }
94
95 }
96 return parameters;
97 }
98
99 /***
100 *
101 * @param request
102 * @param id
103 * @return A Map of string or String arrays.
104 */
105 public static Map getRequestParameterMap(HttpServletRequest request, String id)
106 {
107 int lenghtOfId = (id == null) ? 0 : id.length();
108
109 Map parameters = new HashMap(request.getParameterMap().size());
110 for (Enumeration keys = request.getParameterNames(); keys.hasMoreElements();)
111 {
112 String key = (String) keys.nextElement();
113 String[] values = request.getParameterValues(key);
114 Object value = ((values == null) ? null : (values.length == 1 ? (Object) values[0] : (Object) values));
115
116 if (lenghtOfId > 0)
117 {
118 if (key.endsWith(id))
119 {
120 parameters.put(key.substring(0, key.length() - lenghtOfId), value);
121 }
122 }
123 else
124 {
125 parameters.put(key, value);
126 }
127
128 }
129 return parameters;
130 }
131
132 /***
133 * Build the ValueListInfo from the request and parameters.
134 *
135 * @param request
136 * The HttpServletRequest
137 *
138 * @return a new ValueListInfo.
139 */
140 public static ValueListInfo buildValueListInfo(HttpServletRequest request)
141 {
142 return buildValueListInfo(request, "");
143 }
144
145 /***
146 * Build the ValueListInfo from the request for certain Table id.
147 * @param request The HttpServletRequest
148 * @param id The tableId String.
149 * @return a new ValueListInfo.
150 */
151 public static ValueListInfo buildValueListInfo(HttpServletRequest request, String id)
152 {
153 Map parameters = getRequestParameterMap(request, id);
154 return builtValueListInfo(parameters);
155 }
156
157 /***
158 * Make from parameters ValueListInfo
159 * @param parameters
160 * @return ValueListInfo
161 */
162 private static ValueListInfo builtValueListInfo(Map parameters)
163 {
164 return new ValueListInfo(parameters);
165 }
166
167
168 /***
169 * Setter for the parameters, that are forwarded in every url link in table.
170 * <ul><li><b># </b>- forward only control (like sortColumn, pagingPage, etc.) parameters of others tables in jsp
171 * <p>Automatically exclude params with the prefix of the
172 * ACTION_TEMP_PARAM_PREFIX. </p>
173 * </li>
174 * <li><b>$ </b>- forward only parameters of others tables in jsp that stats with prefix ACTION_TEMP_PARAM_PREFIX.
175 * </li>
176 * <li><b>partOfKey*</b> - forward parameteres that starts with prefix <b>partOfKey</b>.<p>
177 * Automatically are excluded keys start with <ul>ACTION_TEMP_PARAM_PREFIX and with</ul> <ul>DO_FOCUS+id of this table.</ul>
178 * </p>
179 * </li>
180 * <li><b>myForwardParameterOne|myForwardParameterTwo </b>-
181 * forward parameter from request if the name is as the same as
182 * <b>myForwardParameterOne</b> or <b>myForwardParameterTwo</b></li>
183 * </ul>
184 * <p>
185 * Note: ValueListInfo.DO_FOCUS param you cannot forward. It is forbidden for all posibilities.
186 * </p>
187 * @param userForwardParameters The userForwardParameters properties.
188 * @param id TableId of the currrent value list table.
189 * @see net.mlw.vlh.web.tag.ActionTag#ACTION_TEMP_PARAM_PREFIX
190 * @see net.mlw.vlh.ValueListInfo#DO_FOCUS
191 */
192 public static Map getAllForwardParameters(PageContext pageContext, String userForwardParameters, String id)
193 {
194
195 Map forwardedParams = new HashMap();
196
197 for (StringTokenizer st = new StringTokenizer(userForwardParameters, "|"); st.hasMoreTokens();)
198 {
199 String key = st.nextToken();
200
201 if (key.equals("#"))
202 {
203 forwardedParams.putAll(getForwardParamsOfOtherTables(pageContext, id));
204 }
205 else
206 {
207 if (key.endsWith("*"))
208 {
209 key = key.substring(0, key.length() - 1);
210 forwardedParams.putAll(getForwardParamsOfKey(pageContext, id, key));
211 }
212 else
213 {
214
215 if (key.equals("$"))
216 {
217 forwardedParams.putAll(getForwardActionTempParamsOfOtherTables(pageContext, id));
218 }
219 else
220 {
221
222 String[] values = pageContext.getRequest().getParameterValues(key);
223 if (values != null && values.length > 0)
224 {
225 forwardedParams.put(key, values);
226 }
227 }
228 }
229 }
230 }
231
232 return forwardedParams;
233 }
234
235 /***
236 * @param pageContext
237 * @param id TableId
238 * @param prefix
239 * @return Map Map of forwarded parameters starts with prefix. Automatically
240 * are excluded DO_FOCUS for this TableId and params with the prefix
241 * of the ACTION_TEMP_PARAM_PREFIX.
242 */
243 private static Map getForwardParamsOfKey(PageContext pageContext, String id, String prefix)
244 {
245 Map paramsOfKey = new HashMap();
246 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();)
247 {
248 String requestParamName = (String) enumer.nextElement();
249 if (requestParamName.startsWith(prefix) && (!requestParamName.startsWith(ValueListInfo.DO_FOCUS + id))
250 && (!requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX)))
251 {
252 String[] values = pageContext.getRequest().getParameterValues(requestParamName);
253 if (values != null)
254 {
255 paramsOfKey.put(requestParamName, values);
256 }
257 }
258 }
259 return paramsOfKey;
260 }
261
262 /***
263 * @param pageContext
264 * @param id TableId
265 * @return Map Map of action's temp parameters of others tables in jsp page.
266 */
267 private static Map getForwardActionTempParamsOfOtherTables(PageContext pageContext, String id)
268 {
269
270 Map actionTempParamsOfOthers = new HashMap();
271 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();)
272 {
273 String requestParamName = (String) enumer.nextElement();
274
275 if (requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX) && !requestParamName.endsWith(id))
276 {
277 String[] values = pageContext.getRequest().getParameterValues(requestParamName);
278 if (values != null)
279 {
280 actionTempParamsOfOthers.put(requestParamName, values);
281 }
282 }
283
284 }
285 return actionTempParamsOfOthers;
286 }
287
288 /***
289 * @param pageContext
290 * @param id TableId
291 * @return Map of table's control's parameters of other's tables in jsp
292 * page,automatically exclude params with the prefix of the
293 * ACTION_TEMP_PARAM_PREFIX.
294 */
295 private static Map getForwardParamsOfOtherTables(PageContext pageContext, String id)
296 {
297 Map otherTablesParams = new HashMap();
298 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();)
299 {
300 String requestParamName = (String) enumer.nextElement();
301
302 if ((!requestParamName.endsWith(id) && !requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX))
303 && (requestParamName.startsWith(ValueListInfo.FOCUS_PARAM_PREFIX)
304 || requestParamName.startsWith(ValueListInfo.PAGING_PARAM_PREFIX) || requestParamName
305 .startsWith(ValueListInfo.SORT_PARAM_PREFIX)))
306 {
307 String[] values = pageContext.getRequest().getParameterValues(requestParamName);
308 if (values != null)
309 {
310 otherTablesParams.put(requestParamName, values);
311 }
312 }
313
314 }
315 return otherTablesParams;
316 }
317
318 /***
319 * Return the name of parameter with ActionTag.ACTION_TEMP_PARAM_PREFIX
320 * Example: param "id" return "ACTid"
321 * @param userParamName
322 * @return ActionTag.ACTION_TEMP_PARAM_PREFIX + userParamName
323 */
324 public static String getActionTempParamName(String userParamName)
325 {
326 return ActionTag.ACTION_TEMP_PARAM_PREFIX + userParamName;
327 }
328
329 }