KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > runtime > config > DefaultDataGridURLBuilder


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.runtime.config;
19
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.beehive.netui.databinding.datagrid.api.DataGridState;
28 import org.apache.beehive.netui.databinding.datagrid.api.pager.PagerModel;
29 import org.apache.beehive.netui.databinding.datagrid.api.sort.SortModel;
30 import org.apache.beehive.netui.databinding.datagrid.api.sort.SortStrategy;
31 import org.apache.beehive.netui.databinding.datagrid.api.sort.Sort;
32 import org.apache.beehive.netui.databinding.datagrid.api.DataGridURLBuilder;
33 import org.apache.beehive.netui.util.Bundle;
34
35 /**
36  * <p>
37  * This class exposes parameter maps that contain URL key / value pairs which can be data bound to anchors
38  * and other markup during page rendering. Methods exposed here are useful for building URLs that can be clicked on
39  * in the <i>future</i>. A case of this would be a pager URL that will move a UI to the "next" page of data. The
40  * URL parameters would be computed using this class and rendered to the UI so that it is clickable for the
41  * next HTTP submit.
42  * <p/>
43  * <p>
44  * An instance of this class is associated with a single data grid by name. Instances should be obtained
45  * via the {@link org.apache.beehive.netui.databinding.datagrid.api.DataGridStateFactory#getDataGridURLBuilder(String)}
46  * method.
47  * </p>
48  */

49 class DefaultDataGridURLBuilder
50     extends DataGridURLBuilder {
51
52     private transient DefaultDataGridStateCodec _codec;
53
54     /**
55      * <p>
56      * Package protected constructor. This class is intended only to be created via the default
57      * {@link org.apache.beehive.netui.databinding.datagrid.api.DataGridConfig} object.
58      * </p>
59      * @param codec the state codec providing state information which will be converted into URLs here.
60      */

61     DefaultDataGridURLBuilder(DefaultDataGridStateCodec codec) {
62         _codec = codec;
63     }
64
65     /**
66      * <p>
67      * Get the URL query parameter key used to store the current row for a data grid's pager. The value
68      * of this key is:
69      * <br/>
70      * <pre>
71      * netui_row
72      * </pre>
73      * </p>
74      * @return the URL parameter key for the current row
75      */

76     public String JavaDoc getPagerRowQueryParamKey() {
77         return DefaultDataGridStateCodec.PARAM_KEY_ROW;
78     }
79
80     /**
81      * <p>
82      * Get the query parameters for the currene state of the data grid. The {@link Map} returned by this method
83      * will maintain the <i>current</i> state of the data grid and all of the associated URL parameters. The
84      * {@link Map} contains key / value pairs of type String / String[].
85      * </p>
86      * @return the current URL and data grid state
87      */

88     public Map JavaDoc getQueryParams() {
89         Map JavaDoc params = _codec.getExistingParams();
90         Map JavaDoc newParams = new HashMap JavaDoc();
91
92         addSortParams(newParams);
93         addFilterParams(newParams);
94         addPagerParams(newParams);
95
96         params = mergeMaps(params, newParams);
97         params = transformMap(params);
98
99         return params;
100     }
101
102     /**
103      * <p>
104      * Get a URL query parameter map that will change the data grid's page value to display the
105      * <i>first</i> page in a data set. This map also contains all of the other existing URL
106      * parameters. The {@link Map} contains key / value pairs of type String / String[].
107      * </p>
108      * @return the URL and data grid state needed to change to the <i>first</i> page for a data grid
109      */

110     public Map JavaDoc getQueryParamsForFirstPage() {
111         Map JavaDoc params = _codec.getExistingParams();
112         Map JavaDoc newParams = new HashMap JavaDoc();
113
114         PagerModel pagerModel = getDataGridState().getPagerModel();
115         assert pagerModel != null;
116
117         addSortParams(newParams);
118         addFilterParams(newParams);
119         newParams.putAll(_codec.buildPageParamMap(new Integer JavaDoc(pagerModel.getRowForFirstPage()),
120                                                   new Integer JavaDoc(pagerModel.getPageSize())));
121
122         params = mergeMaps(params, newParams);
123         params = transformMap(params);
124
125         return params;
126     }
127
128     /**
129      * <p>
130      * Get a URL query parameter map that will change the data grid's page value to display the
131      * <i>previous</i> page in a data set relative to the current page. This map also contains all of
132      * the other existing URL parameters. The {@link Map} contains key / value pairs of type
133      * String / String[].
134      * </p>
135      * @return the URL and data grid state needed to change to the <i>previous</i> page for a data grid
136      */

137     public Map JavaDoc getQueryParamsForPreviousPage() {
138         Map JavaDoc params = _codec.getExistingParams();
139         Map JavaDoc newParams = new HashMap JavaDoc();
140
141         PagerModel pagerModel = getDataGridState().getPagerModel();
142         assert pagerModel != null;
143
144         addSortParams(newParams);
145         addFilterParams(newParams);
146         newParams.putAll(_codec.buildPageParamMap(new Integer JavaDoc(pagerModel.getRowForPreviousPage()),
147                                                   new Integer JavaDoc(pagerModel.getPageSize())));
148
149         params = mergeMaps(params, newParams);
150         params = transformMap(params);
151
152         return params;
153     }
154
155     /**
156      * <p>
157      * Get a URL query parameter map that will change the data grid's page value to display the
158      * <i>next</i> page in a data set relative to the current page. This map also contains all of
159      * the other existing URL parameters. The {@link Map} contains key / value pairs of type
160      * String / String[].
161      * </p>
162      * @return the URL and data grid state needed to change to the <i>next</i> page for a data grid
163      */

164     public Map JavaDoc getQueryParamsForNextPage() {
165         Map JavaDoc params = _codec.getExistingParams();
166         Map JavaDoc newParams = new HashMap JavaDoc();
167
168         PagerModel pagerModel = getDataGridState().getPagerModel();
169         assert pagerModel != null;
170
171         addSortParams(newParams);
172         addFilterParams(newParams);
173         newParams.putAll(_codec.buildPageParamMap(new Integer JavaDoc(pagerModel.getRowForNextPage()),
174                                                   new Integer JavaDoc(pagerModel.getPageSize())));
175
176         params = mergeMaps(params, newParams);
177         params = transformMap(params);
178
179         return params;
180     }
181
182     /**
183      * <p>
184      * Get a URL query parameter map that will change the data grid's page value to display the
185      * <i>last</i> page in a data set. This map also contains all of the other existing URL parameters.
186      * The {@link Map} contains key / value pairs of type String / String[].
187      * </p>
188      * @return the URL and data grid state needed to change to the <i>last</i> page for a data grid
189      */

190     public Map JavaDoc getQueryParamsForLastPage() {
191         Map JavaDoc params = _codec.getExistingParams();
192         Map JavaDoc newParams = new HashMap JavaDoc();
193
194         PagerModel pagerModel = getDataGridState().getPagerModel();
195         assert pagerModel != null;
196
197         addSortParams(newParams);
198         addFilterParams(newParams);
199         newParams.putAll(_codec.buildPageParamMap(new Integer JavaDoc(pagerModel.getRowForLastPage()),
200                                                   new Integer JavaDoc(pagerModel.getPageSize())));
201
202         params = mergeMaps(params, newParams);
203         params = transformMap(params);
204
205         return params;
206     }
207
208     /**
209      * <p>
210      * Get a String[] of the URL query parameter values that could when submitted to a server to make a request
211      * can explicitly change the current page for a data grid to a specific page. The returned pager parameter
212      * values are structured as:
213      * <pre>
214      * <datagrid-name>~<row-number>
215      * </pre>
216      * These values can be attached to a URL in order to submit a query string which will set the data grid's
217      * current page.
218      * </p>
219      * @return
220      */

221     public String JavaDoc[] getPagerParamValues() {
222         PagerModel pagerModel = getDataGridState().getPagerModel();
223
224         String JavaDoc[] params = new String JavaDoc[pagerModel.getPageCount()];
225         for(int i = 0; i < params.length; i++) {
226             params[i] = _codec.encodePageSize(pagerModel.encodeRowForPage(i));
227         }
228
229         return params;
230     }
231
232     /**
233      * <p>
234      * Get a URL query parameter map that contains state which will change the direction of a
235      * {@link Sort} whose sort expression matches the given sort expression value. The {@link SortStrategy}
236      * associated with the data grid's {@link SortModel} will be used to choose the next
237      * {@link org.apache.beehive.netui.databinding.datagrid.api.sort.SortDirection} given the sort's current
238      * sort direction. This map also contains all of the other existing URL parameters. The {@link Map}
239      * contains key / value pairs of type String / String[].
240      * </p>
241      * @param sortExpression the sort expression whose direction to change
242      * @return the URL and data grid state needed to change the direction of a {@link Sort} with the given
243      * sort expression
244      */

245     public Map JavaDoc buildSortQueryParamsMap(String JavaDoc sortExpression) {
246
247         SortModel sortModel = getDataGridState().getSortModel();
248         SortStrategy sortStrategy = sortModel.getSortStrategy();
249
250         List JavaDoc currSorts = sortModel.getSorts();
251         ArrayList JavaDoc newSorts = new ArrayList JavaDoc();
252         if(currSorts == null || currSorts.size() == 0) {
253             Sort sort = new Sort();
254             sort.setSortExpression(sortExpression);
255             sort.setDirection(sortStrategy.getDefaultDirection());
256             newSorts.add(sort);
257         }
258         else {
259             boolean foundSort = false;
260             for(int i = 0; i < currSorts.size(); i++) {
261                 Sort sort = (Sort)currSorts.get(i);
262                 if(!sort.getSortExpression().equals(sortExpression)) {
263                     newSorts.add(sort);
264                 }
265                 else {
266                     Sort newSort = new Sort();
267                     newSort.setSortExpression(sortExpression);
268                     newSort.setDirection(sortStrategy.nextDirection(sort.getDirection()));
269                     newSorts.add(newSort);
270                     foundSort = true;
271                 }
272             }
273             if(!foundSort) {
274                 Sort newSort = new Sort();
275                 newSort.setSortExpression(sortExpression);
276                 newSort.setDirection(sortStrategy.getDefaultDirection());
277                 newSorts.add(newSort);
278             }
279         }
280
281         Map JavaDoc params = _codec.getExistingParams();
282         Map JavaDoc newParams = new HashMap JavaDoc();
283
284         Map JavaDoc tmp = _codec.buildSortParamMap(newSorts);
285         if(tmp != null)
286             newParams.putAll(tmp);
287
288         addFilterParams(newParams);
289         addPagerParams(newParams);
290
291         params = mergeMaps(params, newParams);
292         params = transformMap(params);
293
294         return params;
295     }
296
297     private final DataGridState getDataGridState() {
298         return _codec.getDataGridState();
299     }
300
301     private void addSortParams(Map JavaDoc map) {
302         Map JavaDoc tmp = _codec.buildSortParamMap(getDataGridState().getSortModel().getSorts());
303         if(tmp != null)
304             map.putAll(tmp);
305     }
306
307     private void addFilterParams(Map JavaDoc map) {
308         Map JavaDoc tmp = _codec.buildFilterParamMap(getDataGridState().getFilterModel().getFilters());
309         if(tmp != null)
310             map.putAll(tmp);
311     }
312
313     private void addPagerParams(Map JavaDoc map) {
314         Map JavaDoc tmp = _codec.buildPageParamMap(new Integer JavaDoc(getDataGridState().getPagerModel().getRow()),
315                 new Integer JavaDoc(getDataGridState().getPagerModel().getPageSize()));
316         if(tmp != null)
317             map.putAll(tmp);
318     }
319
320     private Map JavaDoc mergeMaps(Map JavaDoc curr, Map JavaDoc merge) {
321         Map JavaDoc newMap = new HashMap JavaDoc();
322         if(curr != null)
323             newMap.putAll(curr);
324
325         Iterator JavaDoc iterator = merge.keySet().iterator();
326         while(iterator.hasNext()) {
327             String JavaDoc key = (String JavaDoc)iterator.next();
328             assert merge.get(key) instanceof String JavaDoc[];
329             String JavaDoc[] values = (String JavaDoc[])merge.get(key);
330             if(newMap.containsKey(key)) {
331                 Object JavaDoc currValues = newMap.get(key);
332                 if(currValues instanceof List JavaDoc) {
333                     List JavaDoc currList = (List JavaDoc)currValues;
334                     for(int i = 0; i < values.length; i++) {
335                         currList.add(values[i]);
336                     }
337                 }
338                 else
339                     throw new IllegalStateException JavaDoc(Bundle.getErrorString("DataGridURLBuilder_UnableToMergeValues",
340                             new Object JavaDoc[]{currValues.getClass().getName()}));
341             }
342             else
343                 newMap.put(key, values);
344         }
345         return newMap;
346     }
347
348     private Map JavaDoc transformMap(Map JavaDoc map) {
349         HashMap JavaDoc newMap = new HashMap JavaDoc();
350         Iterator JavaDoc iterator = map.keySet().iterator();
351         while(iterator.hasNext()) {
352             String JavaDoc key = (String JavaDoc)iterator.next();
353             Object JavaDoc values = map.get(key);
354             if(values instanceof String JavaDoc[])
355                 newMap.put(key, values);
356             else if(values instanceof List JavaDoc)
357                 newMap.put(key, ((List JavaDoc)values).toArray(new String JavaDoc[0]));
358             else if(values == null)
359                 newMap.put(key, null);
360             else
361                 assert false : "Found invalid type in map: " + values.getClass().getName();
362         }
363
364         return newMap;
365     }
366 }
367
Popular Tags