KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > GridTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.taglib.core.grid;
17
18 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor;
19 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
20 import com.blandware.atleap.webapp.taglib.core.grid.util.SortField;
21 import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants;
22 import com.blandware.atleap.webapp.util.core.RequestUtil;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
31 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * <p>Tag for marking special area on JSP as grid.
39  * <em>Grid</em> is an object that serves to displaying some data and navigating it.
40  * Data is represented with <em>rows</em>. Each row corresponds to some object,
41  * that can be stored in persistent storage, for example. Some properties of such
42  * objects are dedicated and called <em>fields</em>. Each field has it's own name
43  * (for example, such names may match names of row columns in database). Fields
44  * may be used to filter rows (so grid will display only a subset of all rows) and
45  * to sort them.
46  * </p>
47  * <p>
48  * Each field may have a filter assigned to it. Such assignment is made with
49  * some filter tag. Currently, four types of filters (and four corresponding tags)
50  * are implemented: {@link DateFilterTag}, {@link NumberFilterTag},
51  * {@link SetFilterTag} and {@link StringFilterTag}.
52  * </p>
53  * <p>
54  * Grid may be sorted by some fields. This sorting may be simultaneous: first
55  * sort is made on one field, then on another and so on (this is similar to
56  * SQL statements: there you can specify several ORDER BY clauses like
57  * SELECT * FROM user ORDER BY last_name, first_name...). Sort is specified with
58  * <em>sort</em> tag, which generates a hyperlink that causes rows to be sorted.
59  * </p>
60  * <p>
61  * Grid also gives ability to split rows by pages. This is done with
62  * <em>pager</em>, <em>pagesIterator</em> and other tags.
63  * </p>
64  * <p>
65  * Allowed attributes are:
66  * <ul>
67  * <li>
68  * <b>name</b> - name of grid. If not specified, this will be generated
69  * automatically (currently this will be equal to current URI taken from
70  * request).
71  * </li>
72  * <li>
73  * <b>pageUrl</b> - URL that corresponds to page on which this grid is
74  * displayed. It's used as base for URLs of links that are produced for sorting,
75  * paging and so on. If not specified, current page URL will be taken.
76  * </li>
77  * <li>
78  * <b>pageSize</b> - number of elements per page on this grid. If not specified,
79  * it's 1000.
80  * </li>
81  * </ul>
82  * </p>
83  * <p>
84  * For example on using this tag, see package.html in this package.
85  * </p>
86  * <p><a HREF="GridTag.java.htm"><i>View Source</i></a></p>
87  *
88  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
89  * @version $Revision: 1.15 $ $Date: 2005/09/27 08:48:28 $
90  * @jsp.tag name="grid"
91  * body-content="scriptless"
92  */

93 public class GridTag extends SimpleTagSupport JavaDoc {
94
95     protected transient final Log log = LogFactory.getLog(GridTag.class);
96
97     /**
98      * Grid name. This is the key in map of grids that stored in session under
99      * <code>com.blandware.atleap.webapp.taglib.core.util.TaglibConstants.GRIDS'</code> key.
100      * By default request URI is used.
101      *
102      * @see javax.servlet.http.HttpServletRequest#getRequestURI()
103      */

104     protected String JavaDoc name;
105
106     /**
107      * Uniform Resource Locator of the action that used to display page
108      * where this grid is located. By default it is URL of current page plus query string if it exists.
109      * URL, if specified, MUST contain protocol and host name.
110      * Optionally it may contain some query string
111      */

112     protected String JavaDoc pageUrl;
113
114     /**
115      * Contains all request parameters mapped to their values
116      */

117     protected Map JavaDoc parameterMap;
118
119     /**
120      * Number of rows on each page. By default it is 1000
121      */

122     protected Integer JavaDoc pageSize = new Integer JavaDoc(1000);
123
124     /**
125      * Current page number
126      */

127     protected Integer JavaDoc currentPageNumber = new Integer JavaDoc(1);
128
129     /**
130      * Grid associated with this tag
131      */

132     protected Grid grid = null;
133
134     /**
135      * Returns grid name
136      *
137      * @return grid name
138      * @see #name
139      * @jsp.attribute required="false"
140      * rtexprvalue="true"
141      * type="java.lang.String"
142      * description="Grid name. This is the key in map of grids that stored in session under 'com.blandware.atleap.webapp.taglib.core.util.TaglibConstants.GRIDS' key. By default request URI is used."
143      */

144     public String JavaDoc getName() {
145         return name;
146     }
147
148     /**
149      * Sets grid name
150      *
151      * @param name grid name to set
152      * @see #name
153      */

154     public void setName(String JavaDoc name) {
155         this.name = name;
156     }
157
158     /**
159      * Returns page URL for grid
160      *
161      * @return page URL
162      * @see #pageUrl
163      * @jsp.attribute required="false"
164      * rtexprvalue="true"
165      * type="java.lang.String"
166      * description="Uniform Resource Identifier of the action that used to display page where this grid is located"
167      */

168     public String JavaDoc getPageUrl() {
169         return pageUrl;
170     }
171
172     /**
173      * Sets page URL for grid
174      *
175      * @param pageUrl page URL
176      * @see #pageUrl
177      */

178     public void setPageUrl(String JavaDoc pageUrl) {
179         this.pageUrl = pageUrl;
180     }
181
182     /**
183      * Returns map of parameters
184      *
185      * @return parameter map
186      */

187     public Map JavaDoc getParameterMap() {
188         return parameterMap;
189     }
190
191     /**
192      * Returns number of items on page
193      *
194      * @return page size
195      * @see #pageSize
196      * @jsp.attribute required="false"
197      * rtexprvalue="true"
198      * type="java.lang.Integer"
199      * description="Number of rows on each page. By default it is 1000"
200      */

201     public Integer JavaDoc getPageSize() {
202         return pageSize;
203     }
204
205     /**
206      * Sets number of items on page
207      *
208      * @param pageSize page size
209      * @see #pageSize
210      */

211     public void setPageSize(Integer JavaDoc pageSize) {
212         this.pageSize = pageSize;
213     }
214
215     /**
216      * Returns number of current page being displayed
217      *
218      * @return current page number
219      * @see #currentPageNumber
220      */

221     public Integer JavaDoc getCurrentPageNumber() {
222         return currentPageNumber;
223     }
224
225     /**
226      * Sets number of current page being displayed
227      *
228      * @param currentPageNumber current page number
229      * @see #currentPageNumber
230      */

231     public void setCurrentPageNumber(Integer JavaDoc currentPageNumber) {
232         this.currentPageNumber = currentPageNumber;
233     }
234
235     /**
236      * Returns Grid object corresponding to this grid
237      *
238      * @return Grid object
239      */

240     protected Grid getGrid() {
241         return grid;
242     }
243
244     /**
245      * Processes the tag
246      *
247      * @throws JspException
248      * @throws IOException
249      */

250     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
251
252         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
253
254         // If name atribute is omitted, set name to current request URI
255

256         if ( name == null ) {
257             name = ((HttpServletRequest JavaDoc) pageContext.getRequest()).getRequestURI();
258         }
259
260         // Get page URL and query string. Convert query string to map
261

262         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
263         String JavaDoc queryString = new String JavaDoc();
264         if ( pageUrl == null ) {
265             pageUrl = (String JavaDoc) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL);
266         }
267         int k = pageUrl.indexOf('?');
268         if ( k != -1 ) {
269             queryString = pageUrl.substring(k + 1);
270             pageUrl = pageUrl.substring(0, k);
271         }
272
273         if ( queryString == null ) {
274             queryString = new String JavaDoc();
275         }
276
277         parameterMap = RequestUtil.getRequestParametersFromString(queryString);
278
279         // Find grid in session if it was previously created or create new
280

281         HttpSession JavaDoc session = pageContext.getSession();
282         Map JavaDoc grids = (Map JavaDoc) session.getAttribute(TaglibConstants.GRIDS);
283         if ( grids == null ) {
284             grids = Collections.synchronizedMap(new HashMap JavaDoc());
285             session.setAttribute(TaglibConstants.GRIDS, grids);
286         }
287
288
289         grid = (Grid) grids.get(this.name);
290
291         // Create grid if it does not exist
292
if ( grid == null ) {
293             grid = new Grid(this.name);
294         }
295
296         grid.setTotal(new Integer JavaDoc(0));
297
298         // Set fields sorting and clear filters if it was specified in request parameters
299
if ( request.getParameter("gridName") != null && request.getParameter("gridName").equalsIgnoreCase(this.name) ) {
300             if ( request.getParameter("sortField") != null ) {
301                 String JavaDoc fieldName = request.getParameter("sortField");
302                 SortField sortField = grid.getSortFieldByFieldName(fieldName);
303                 if ( sortField != null ) {
304                     sortField.reverseOrder();
305                 } else {
306                     sortField = new SortField(fieldName);
307                 }
308                 String JavaDoc rowIteratorsParam = new String JavaDoc();
309                 if ( request.getParameter("rowIterators") != null ) {
310                     rowIteratorsParam = request.getParameter("rowIterators");
311                 }
312                 String JavaDoc[] rowIterators = rowIteratorsParam.split(",");
313                 for ( int i = 0; i < rowIterators.length; i++ ) {
314                     sortField.addRowIterator(rowIterators[i].trim());
315                 }
316                 String JavaDoc property = request.getParameter("property");
317                 if ( property != null && property.trim().length() > 0 ) {
318                     sortField.setProperty(property);
319                 }
320                 grid.addSortField(sortField);
321             }
322
323             if ( request.getParameter("pageNumber") != null ) {
324                 currentPageNumber = new Integer JavaDoc(Integer.parseInt(request.getParameter("pageNumber")));
325             }
326
327             if ( request.getParameter("clearAllFilters") != null && request.getParameter("clearAllFilters").equalsIgnoreCase("true") ) {
328                 grid.clearFilters();
329             }
330
331             if ( request.getParameter("clearFilter") != null ) {
332                 String JavaDoc fieldName = request.getParameter("clearFilter");
333                 grid.removeFilter(fieldName);
334             }
335         }
336
337         grid.setPageSize(pageSize);
338
339         JspFragment JavaDoc body = getJspBody();
340         if ( body != null ) {
341             body.invoke(null);
342         }
343
344         // Save grid in session
345
grids.put(this.name, grid);
346         session.setAttribute(TaglibConstants.GRIDS, grids);
347     }
348
349 }
350
Popular Tags