KickJava   Java API By Example, From Geeks To Geeks.

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


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.taglib.core.BaseHandlerTag;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.TagUtils;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * <p>Tag for marking column as sortable.
33  * <br />
34  * This tag is only valid when nested within <em>grid</em> tag
35  * </p>
36  * <p>
37  * Allowed attributes are:
38  * <ul>
39  * <li>
40  * <b>fieldName</b> - name of field to sort by
41  * </li>
42  * <li>
43  * <b>property</b> - name of property to lookup value in order to compare
44  * entities, when collection is not taken from persistent storage but given to
45  * <em>rowsIterator</em> through its <b>collection</b> attribute.
46  * </li>
47  * <li>
48  * <b>rowIterators</b> - comma-separated list of rowIterators IDs this filter
49  * is applicable to. This is needed to constrain what row iterators will be
50  * affected by this filter (as there may be several row iterators in a single
51  * grid) - those IDs must be listed here; if this list is empty or not given,
52  * any iterator will match (so this filter will affect all iterators in grid).
53  * </li>
54  * </ul>
55  * </p>
56  * <p>
57  * This tag must either be contained in <em>column</em> tag or have
58  * <b>fieldName</b> specified.
59  * </p>
60  * <p><a HREF="SortTag.java.htm"><i>View Source</i></a></p>
61  *
62  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
63  * @version $Revision: 1.12 $ $Date: 2005/10/12 13:34:58 $
64  * @see SortAscTag
65  * @see SortDescTag
66  * @jsp.tag name="sort"
67  * body-content="scriptless"
68  * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
69  */

70 public class SortTag extends BaseHandlerTag {
71
72     protected transient final Log log = LogFactory.getLog(SortTag.class);
73
74     /**
75      * Name of field to sort by
76      */

77     protected String JavaDoc fieldName;
78
79     /**
80      * Name of property to lookup value in order to compare entities, when
81      * collection is not taken from persistent storage
82      */

83     protected String JavaDoc property;
84
85     /**
86      * Comma-separated list of rowIterators IDs this filter is applicable to
87      */

88     protected String JavaDoc rowIterators = null;
89
90
91     /**
92      * Returns name of field by which to sort
93      *
94      * @return field name
95      * @jsp.attribute required="false"
96      * rtexprvalue="true"
97      * type="java.lang.String"
98      * description="Name of field to sort by"
99      */

100     public String JavaDoc getFieldName() {
101         return fieldName;
102     }
103
104     /**
105      * Sets name of field by which to sort
106      *
107      * @param fieldName field name to set
108      */

109     public void setFieldName(String JavaDoc fieldName) {
110         this.fieldName = fieldName;
111     }
112
113     /**
114      * Returns name of property by which to compare beans
115      *
116      * @return property name
117      * @jsp.attribute required="false"
118      * rtexprvalue="true"
119      * type="java.lang.String"
120      * description="Name of property to lookup value in order to compare entities, when collection is not taken from persistent storage"
121      */

122     public String JavaDoc getProperty() {
123         return property;
124     }
125
126     /**
127      * Sets name of property by which to compare beans
128      *
129      * @param property property name
130      */

131     public void setProperty(String JavaDoc property) {
132         this.property = property;
133     }
134
135     /**
136      * Returns comma-separated list of rowIterators IDs this filter is
137      * applicable to
138      *
139      * @return row iterators list
140      * @jsp.attribute required="false"
141      * rtexprvalue="true"
142      * type="java.lang.String"
143      * description="Comma-separated list of rowIterators IDs this filter is applicable to"
144      */

145     public String JavaDoc getRowIterators() {
146         return rowIterators;
147     }
148
149     /**
150      * Sets comma-separated list of rowIterators IDs this filter is
151      * applicable to
152      *
153      * @param rowIterators row iterators list to set
154      */

155     public void setRowIterators(String JavaDoc rowIterators) {
156         this.rowIterators = rowIterators;
157     }
158
159     /**
160      * Processes the tag
161      *
162      * @throws JspException
163      * @throws IOException
164      */

165     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
166
167         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
168
169         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
170
171         if ( parentGridTag == null ) {
172             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
173             throw e;
174         }
175
176         // Get field name from parent 'column' tag
177
ColumnTag parentColumnTag = (ColumnTag) findAncestorWithClass(this, ColumnTag.class);
178         if ( parentColumnTag != null ) {
179             if ( fieldName == null || fieldName.length() == 0 ) {
180                 fieldName = parentColumnTag.getFieldName();
181             }
182         }
183
184         if ( fieldName == null || "".equals(fieldName) ) {
185             // Error: there is no ancestor 'column' tag and no fieldName attribute is specified
186
String JavaDoc errorMessage = "There is no ancestor 'column' tag and no fieldName attribute is specified!";
187             if ( log.isErrorEnabled() ) {
188                 log.error(errorMessage);
189             }
190             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
191             throw e;
192         }
193
194         String JavaDoc pageUrl = parentGridTag.getPageUrl();
195         Map JavaDoc parameterMap = parentGridTag.getParameterMap();
196         parameterMap.put("sortField", fieldName);
197         if ( property != null ) {
198             parameterMap.put("property", property);
199         }
200         parameterMap.put("gridName", parentGridTag.getName());
201         parameterMap.put("pageNumber", parentGridTag.getCurrentPageNumber());
202         if ( rowIterators != null ) {
203             parameterMap.put("rowIterators", rowIterators);
204         }
205
206         pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");
207
208         StringWriter JavaDoc sw = new StringWriter JavaDoc();
209         StringBuffer JavaDoc sb = sw.getBuffer();
210         sb.append("<a HREF=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
211         getJspBody().invoke(sw);
212         sb.append("</a>");
213         TagUtils.getInstance().write(pageContext, sw.toString());
214     }
215
216 }
217
Popular Tags