View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
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 org.extremecomponents.table.model;
17  
18  import java.net.URLEncoder;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.servlet.jsp.PageContext;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.extremecomponents.table.bean.Table;
31  
32  
33  /***
34   *
35   * @author jeff johnston
36   */
37  public class ParameterRegistry
38  {
39  	private static Log logger = LogFactory.getLog(ParameterRegistry.class);
40  	
41  	public final static String PAGE = "page_";
42  	public final static String SORT = "sort_";
43  	public final static String FILTER = "filter_";
44  	public final static String EXPORT = "export_";
45  	public final static String APPEND = "append_";
46  	public final static String OPEN = "open_";
47  	private TableModel model;
48  	private Map parameters = new HashMap();
49  
50  	public ParameterRegistry(TableModel model)
51  	{
52  		this.model = model;
53  	}
54  
55  	public void init(PageContext pageContext)
56  	{
57  		Enumeration enumeration = pageContext.getRequest().getParameterNames();
58  
59  		while (enumeration.hasMoreElements())
60  		{
61  			String name = (String) enumeration.nextElement();
62  			String value = pageContext.getRequest().getParameter(name);
63  
64  			if (StringUtils.isEmpty(value))
65  			{
66  				continue; //whats the point
67  			}
68  
69  			if (name.startsWith(model.getTableKey() + PAGE) || 
70  				name.startsWith(model.getTableKey() + SORT) || 
71  				name.startsWith(model.getTableKey() + FILTER) || 
72  				name.startsWith(model.getTableKey() + EXPORT) ||
73  				name.startsWith(model.getTableKey() + OPEN))
74  			{
75  				parameters.put(name, value);
76  			}
77  			else
78  			{
79  				if (model.getProperties().getProperty(TableProperties.AUTO_INCLUDE_PARAMETERS).equals("true")) //accept all parameters
80  				{
81  					if (!name.equals(Table.EXTREME_TABLE)) //filter out this variable
82  					{
83  						parameters.put(name, value);
84  					}
85  				}
86  			}
87  		}
88  	}
89  
90  	/***
91  	 * Add additional parameters that need to be passed around
92  	 */
93  	public void addParameter(String name, String value)
94  	{
95  		parameters.put(name, value);
96  	}
97  
98  	/***
99  	 * Get the parameter
100 	 */
101 	public String getParameter(String parameter)
102 	{
103 		return (String) parameters.get(model.getTableKey() + parameter);
104 	}
105 
106 	/***
107 	 * Get a map of specific parameters
108 	 */
109 	public Map getParameters(String parameter)
110 	{
111 		Map subset = new HashMap();
112 
113 		Set set = parameters.keySet();
114 
115 		for (Iterator iter = set.iterator(); iter.hasNext();)
116 		{
117 			String name = (String) iter.next();
118 
119 			if (name.startsWith(model.getTableKey() + parameter))
120 			{
121 				String value = (String) parameters.get(name);
122 				subset.put(name, value);
123 			}
124 		}
125 
126 		return subset;
127 	}
128 
129 	/***
130 	 * Get a map of specific parameters
131 	 */
132 	public Map clearParameters(String parameter)
133 	{
134 		Map subset = new HashMap();
135 
136 		Set set = parameters.keySet();
137 
138 		for (Iterator iter = set.iterator(); iter.hasNext();)
139 		{
140 			String name = (String) iter.next();
141 
142 			if (name.startsWith(model.getTableKey() + parameter))
143 			{
144 				iter.remove();
145 			}
146 		}
147 
148 		return subset;
149 	}
150 
151 	/***
152 	 * Take the parameters and format into hidden fields
153 	 */
154 	public String getHiddenFields(String parameter)
155 	{
156 		StringBuffer sb = new StringBuffer();
157 
158 		Set keys = parameters.keySet();
159 
160 		for (Iterator iter = keys.iterator(); iter.hasNext();)
161 		{
162 			String name = (String) iter.next();
163 
164 			if (((parameter == null) && !name.startsWith(model.getTableKey())) || (name.startsWith(model.getTableKey() + parameter)))
165 			{
166 				String value = (String) parameters.get(name);
167 				sb.append("\n\t<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\">\n");
168 			}
169 		}
170 
171 		return sb.toString();
172 	}
173 
174 	/***
175 	 * Get a URI formated parameter string. Will look like &key=value
176 	 */
177 	public String getParameterString(String parameter)
178 	{
179 		StringBuffer sb = new StringBuffer();
180 
181 		Set keys = parameters.keySet();
182 
183 		for (Iterator iter = keys.iterator(); iter.hasNext();)
184 		{
185 			String name = (String) iter.next();
186 
187 			if (((parameter == null) && !name.startsWith(model.getTableKey())) || (name.startsWith(model.getTableKey() + parameter)))
188 			{
189 				String value = (String) parameters.get(name);
190 				value = URLEncoder.encode(value);
191 				sb.append("&" + name + "=" + value);
192 			}
193 		}
194 
195 		return sb.toString();
196 	}
197 
198 	/***
199 	 * Get parameter datum.
200 	 */
201 	public String getURLParameterString(boolean filter, boolean sort, boolean page)
202 	{
203 		StringBuffer sb = new StringBuffer();
204 
205 		if (filter)
206 		{
207 			sb.append(getParameterString(FILTER));
208 		}
209 
210 		if (sort)
211 		{
212 			sb.append(getParameterString(SORT));
213 		}
214 
215 		if (page)
216 		{
217 			sb.append(getParameterString(PAGE));
218 		}
219 
220 		sb.append(getParameterString(null)); //include any additional parameters
221 
222 		return sb.toString();
223 	}
224 
225 	/***
226 	 * Get all the action as hidden fields.
227 	 */
228 	public String getFormHiddenFields(boolean filter, boolean sort, boolean page)
229 	{
230 		StringBuffer sb = new StringBuffer();
231 
232 		if (filter)
233 		{
234 			sb.append(getHiddenFields(FILTER));
235 		}
236 
237 		if (sort)
238 		{
239 			sb.append(getHiddenFields(SORT));
240 		}
241 
242 		if (page)
243 		{
244 			sb.append(getHiddenFields(PAGE));
245 		}
246 
247 		sb.append(getHiddenFields(null)); //include any additional parameters
248 
249 		return sb.toString();
250 	}
251 	
252 	public void destroy()
253 	{
254 		parameters.clear();
255 		parameters = null;
256 	}
257 }