KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > listdata > www > ListDataActionTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ListDataActionTag.java,v 1.2 2007/02/20 04:18:18 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.listdata.www;
23
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import org.opensubsystems.core.util.GlobalConstants;
28 import org.opensubsystems.core.util.StringUtils;
29 import org.opensubsystems.core.www.TagUtils;
30 import org.opensubsystems.core.www.WebSessionServlet;
31 import org.opensubsystems.core.www.WebUIServlet;
32 import org.opensubsystems.patterns.listdata.data.DataCondition;
33 import org.opensubsystems.patterns.listdata.data.ListOptions;
34
35 /**
36  * Custom tag to generate hyperlink that can invoke instance of
37  * ListBrowserServlet and perform the desired action such as going to the next
38  * or the previous page. This class implements functionality similar to methods
39  * in listdata.js but only based on the data available at the time when the page
40  * is generated. If the page dynamically changes some of the list data pattern
41  * variables for example based on users' action, you should use methods in
42  * the listdata.js instead. Keep this functionality synchronized with
43  * the one provided by the JavaScript file.
44  *
45  * @version $Id: ListDataActionTag.java,v 1.2 2007/02/20 04:18:18 bastafidli Exp $
46  * @author Miro Halas
47  * @code.reviewer Miro Halas
48  * @code.reviewed Initial revision
49  */

50 public class ListDataActionTag extends ListDataBaseTag
51 {
52    // Attributes ///////////////////////////////////////////////////////////////
53

54    /**
55     * Generated serial version UID
56     */

57    private static final long serialVersionUID = -5189455790209408072L;
58
59    /**
60     * Base url that should be used to generate the link. The url should be able
61     * to accept parameters using the standard notation [&]paramname=paramvalue.
62     * If no url is specified the tag will be used value of servletpath
63     * parameter sent by the WebSessionServlet derived servlet that processed
64     * request causing generation of this page.
65     */

66    protected String JavaDoc m_strUrl;
67
68    /**
69     * Command specifying action to execute. Recognized values are previous,
70     * next, goto, pagesize, but you can specify your own if you implement
71     * support for it on the backend in your ListBrowserServlet derived class.
72     * Required.
73     */

74    protected String JavaDoc m_strCommand;
75
76    // Constructors /////////////////////////////////////////////////////////////
77

78    /**
79     * Constructor for custom tag.
80     */

81    public ListDataActionTag()
82    {
83       super();
84    }
85    
86    // Business logic ///////////////////////////////////////////////////////////
87

88    /**
89     * {@inheritDoc}
90     */

91    public int doStartTag(
92    ) throws JspException JavaDoc
93    {
94       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
95       ServletRequest JavaDoc request = pageContext.getRequest();
96       ListOptions listoptions;
97       DataCondition extracondition;
98       String JavaDoc strUrl;
99       String JavaDoc strCommand;
100       
101       listoptions = getOptionsObject();
102       extracondition = getExtraConditionObject();
103       strUrl = getUrl();
104       strCommand = getCommand();
105
106       if ((strUrl == null) || (strUrl.length() == 0))
107       {
108          strUrl = (String JavaDoc)request.getAttribute(
109                              WebSessionServlet.SERVLET_PATH_REQUEST_PARAM);
110       }
111       
112       if (GlobalConstants.ERROR_CHECKING)
113       {
114          assert listoptions != null
115                 : "Options attribute doesnt't identify name of existing list" +
116                   " options.";
117          assert extracondition != null
118                 : "Extra condition attribute doesnt't identify name of" +
119                   " existing data condition.";
120       }
121       
122       
123       if (sbHtml.indexOf("?") < 0)
124       {
125          sbHtml.append('?');
126       }
127       else
128       {
129          sbHtml.append('&');
130       }
131       
132       // Insted of getting the values directly, get them using helper methods
133
// so that derived tags can modify these values.
134
sbHtml.append(WebUIServlet.FORM_NAME_REQUEST_PARAM);
135       sbHtml.append("=");
136       sbHtml.append(strCommand);
137       sbHtml.append("&");
138       sbHtml.append(ListBrowserServlet.LIST_PARAM_LIST_FILTER_ID);
139       sbHtml.append("=");
140       sbHtml.append(getDefinitionId(listoptions));
141       sbHtml.append("&");
142       sbHtml.append(ListBrowserServlet.LIST_PARAM_LIST_DOMAIN_ID);
143       sbHtml.append("=");
144       sbHtml.append(getDomainId(listoptions));
145       sbHtml.append("&");
146       sbHtml.append(ListBrowserServlet.LIST_PARAM_LIST_PAGE_SIZE);
147       sbHtml.append("=");
148       sbHtml.append(getPageSize(listoptions));
149       sbHtml.append("&");
150       sbHtml.append(ListBrowserServlet.LIST_PARAM_ORDER_CODE);
151       sbHtml.append("=");
152       sbHtml.append(getOrderColumnCodes(listoptions));
153       sbHtml.append("&");
154       sbHtml.append(ListBrowserServlet.LIST_PARAM_ORDER_TYPE);
155       sbHtml.append("=");
156       sbHtml.append(getOrderDirections(listoptions));
157       sbHtml.append("&");
158       sbHtml.append(ListBrowserServlet.LIST_PARAM_CLIENT_ORDER_CODE);
159       sbHtml.append("=");
160       sbHtml.append(getClientOrderCode(listoptions));
161       sbHtml.append("&");
162       sbHtml.append(ListBrowserServlet.LIST_PARAM_CLIENT_ORDER_TYPE);
163       sbHtml.append("=");
164       sbHtml.append(getClientOrderDirection(listoptions));
165       if (ListBrowserServlet.ACTION_NEXT_PAGE_NAME.equalsIgnoreCase(strCommand))
166       {
167          sbHtml.append("&");
168          sbHtml.append(ListBrowserServlet.LIST_PARAM_END_POSITION);
169          sbHtml.append("=");
170          sbHtml.append(getEndPosition(listoptions));
171       }
172       else if (ListBrowserServlet.ACTION_GO_TO_PAGE_NAME.equalsIgnoreCase(strCommand))
173       {
174          sbHtml.append("&");
175          sbHtml.append(ListBrowserServlet.LIST_PARAM_ACTUAL_PAGE);
176          sbHtml.append("=");
177          sbHtml.append(getActualPage(listoptions));
178       }
179       else
180       {
181          sbHtml.append("&");
182          sbHtml.append(ListBrowserServlet.LIST_PARAM_BEGIN_POSITION);
183          sbHtml.append("=");
184          sbHtml.append(getBeginPosition(listoptions));
185       }
186       sbHtml.append("&");
187       sbHtml.append(ListBrowserServlet.LIST_PARAM_SHOW_COLUMN_CODES);
188       sbHtml.append("=");
189       sbHtml.append(getShowColumnCodes(listoptions));
190       sbHtml.append("&");
191       sbHtml.append(ListBrowserServlet.LIST_PARAM_FIRST_VISIBLE_ITEM);
192       sbHtml.append("=");
193       sbHtml.append(getFirstVisibleItem(listoptions));
194       sbHtml.append("&");
195       sbHtml.append(ListBrowserServlet.LIST_PARAM_SELECTED_ITEMS);
196       sbHtml.append("=");
197       sbHtml.append(getSelectedItemIDs(listoptions));
198       sbHtml.append("&");
199       sbHtml.append(ListBrowserServlet.LIST_PARAM_MARKED_ITEMS);
200       sbHtml.append("=");
201       sbHtml.append(getMarkedItemIDs(listoptions));
202       sbHtml.append("&");
203       sbHtml.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE);
204       sbHtml.append("=");
205       sbHtml.append(getParentDataType(listoptions));
206       sbHtml.append("&");
207       sbHtml.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER);
208       sbHtml.append("=");
209       sbHtml.append(getParentId(listoptions));
210       sbHtml.append("&");
211       sbHtml.append(ListBrowserServlet.LIST_PARAM_IGNORED_ITEMS);
212       sbHtml.append("=");
213       sbHtml.append(getIgnoredItemIDs(listoptions));
214       sbHtml.append("&");
215       sbHtml.append(ListBrowserServlet.LIST_PARAM_KEEP_SELECTED);
216       sbHtml.append("=");
217       sbHtml.append(getKeepSelected(listoptions));
218       sbHtml.append("&");
219       sbHtml.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_ATTRIBUTE);
220       sbHtml.append("=");
221       sbHtml.append(getExtraConditionAttribute(extracondition));
222       sbHtml.append("&");
223       sbHtml.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_OPERAND);
224       sbHtml.append("=");
225       sbHtml.append(getExtraConditionOperation(extracondition));
226       sbHtml.append("&");
227       sbHtml.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE_TYPE);
228       sbHtml.append("=");
229       sbHtml.append(getExtraConditionValueType(extracondition));
230       sbHtml.append("&");
231       sbHtml.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE);
232       sbHtml.append("=");
233       sbHtml.append(getExtraConditionValue(extracondition));
234       
235       TagUtils.write(pageContext, sbHtml.toString());
236       
237       return (SKIP_BODY);
238    }
239
240    /**
241     * {@inheritDoc}
242     */

243    public int doEndTag(
244    ) throws JspException JavaDoc
245    {
246       return (EVAL_PAGE);
247    }
248
249    /**
250     * Get the url that should be used to generate the link. The url should be
251     * able to accept parameters using the standard notation
252     * [&]paramname=paramvalue.
253     * If no url is specified the tag will be used value of servletpath
254     * parameter sent by the WebSessionServlet derived servlet that processed
255     * request causing generation of this page.
256     *
257     * @return String - base url to generate action link
258     */

259    public String JavaDoc getUrl()
260    {
261       return m_strUrl;
262    }
263
264    /**
265     * Set the url that should be used to generate the link. The url should be
266     * able to accept parameters using the standard notation
267     * [&]paramname=paramvalue. Required.
268     *
269     * @param strUrl - base url to generate action link
270     */

271    public void setUrl(
272       String JavaDoc strUrl
273    )
274    {
275       m_strUrl = strUrl;
276    }
277
278    /**
279     * Get the command specifying action to execute. Recognized values are
280     * previous,next, goto, pagesize, but you can specify your own if you
281     * implement support for it on the backend in your ListBrowserServlet derived
282     * class. Required.
283     *
284     * @return String - command to execute
285     */

286    public String JavaDoc getCommand()
287    {
288       return m_strCommand;
289    }
290
291    /**
292     * Set the command specifying action to execute. Recognized values are
293     * previous,next, goto, pagesize, but you can specify your own if you
294     * implement support for it on the backend in your ListBrowserServlet derived
295     * class. Required.
296     *
297     * @param strCommand - command to execute
298     */

299    public void setCommand(
300       String JavaDoc strCommand
301    )
302    {
303       m_strCommand = strCommand;
304    }
305    
306    // Helper methods ///////////////////////////////////////////////////////////
307

308    /**
309     * @param listoptions - list options used to generate this list
310     * @return String
311     */

312    protected String JavaDoc getDefinitionId(
313       ListOptions listoptions
314    )
315    {
316       String JavaDoc strTemp;
317       
318       strTemp = listoptions.getDefinitionId();
319       return (strTemp != null ? strTemp : "");
320    }
321
322    /**
323     * @param listoptions - list options used to generate this list
324     * @return String
325     */

326    protected String JavaDoc getDomainId(
327       ListOptions listoptions
328    )
329    {
330       return Integer.toString(listoptions.getDomainId());
331    }
332
333    /**
334     * @param listoptions - list options used to generate this list
335     * @return String
336     */

337    protected String JavaDoc getPageSize(
338       ListOptions listoptions
339    )
340    {
341       return Integer.toString(listoptions.getPageSize());
342    }
343    
344    /**
345     * @param listoptions - list options used to generate this list
346     * @return String
347     */

348    protected String JavaDoc getOrderColumnCodes(
349       ListOptions listoptions
350    )
351    {
352       return StringUtils.parseIntArrayToString(listoptions.getOrderColumnCodes(),
353                                                ",");
354    }
355
356    /**
357     * @param listoptions - list options used to generate this list
358     * @return String
359     */

360    protected String JavaDoc getOrderDirections(
361       ListOptions listoptions
362    )
363    {
364       return StringUtils.concat(listoptions.getOrderDirections(), ",", null);
365    }
366
367    /**
368     * @param listoptions - list options used to generate this list
369     * @return String
370     */

371    protected String JavaDoc getClientOrderCode(
372       ListOptions listoptions
373    )
374    {
375       return Integer.toString(listoptions.getClientOrderCode());
376    }
377
378    /**
379     * @param listoptions - list options used to generate this list
380     * @return String
381     */

382    protected String JavaDoc getClientOrderDirection(
383       ListOptions listoptions
384    )
385    {
386       String JavaDoc strTemp;
387       
388       strTemp = listoptions.getClientOrderDirection();
389       return (strTemp != null ? strTemp : "");
390    }
391
392    /**
393     * @param listoptions - list options used to generate this list
394     * @return String
395     */

396    protected String JavaDoc getEndPosition(
397       ListOptions listoptions
398    )
399    {
400       return Integer.toString(listoptions.getEndPosition());
401    }
402
403    /**
404     * @param listoptions - list options used to generate this list
405     * @return String
406     */

407    protected String JavaDoc getActualPage(
408       ListOptions listoptions
409    )
410    {
411       return Integer.toString(listoptions.getActualPage());
412    }
413
414    /**
415     * @param listoptions - list options used to generate this list
416     * @return String
417     */

418    protected String JavaDoc getBeginPosition(
419       ListOptions listoptions
420    )
421    {
422       return Integer.toString(listoptions.getBeginPosition());
423    }
424
425    /**
426     * @param listoptions - list options used to generate this list
427     * @return String
428     */

429    protected String JavaDoc getShowColumnCodes(
430       ListOptions listoptions
431    )
432    {
433       return StringUtils.parseIntArrayToString(listoptions.getShowColumnCodes(),
434                                                 ",");
435    }
436
437    /**
438     * @param listoptions - list options used to generate this list
439     * @return String
440     */

441    protected String JavaDoc getFirstVisibleItem(
442       ListOptions listoptions
443    )
444    {
445       return Integer.toString(listoptions.getFirstVisibleItem());
446    }
447
448    /**
449     * @param listoptions - list options used to generate this list
450     * @return String
451     */

452    protected String JavaDoc getSelectedItemIDs(
453       ListOptions listoptions
454    )
455    {
456       String JavaDoc strTemp;
457       
458       strTemp = listoptions.getSelectedItemIDs();
459       return (strTemp != null ? strTemp : "");
460    }
461
462    /**
463     * @param listoptions - list options used to generate this list
464     * @return String
465     */

466    protected String JavaDoc getMarkedItemIDs(
467       ListOptions listoptions
468    )
469    {
470       String JavaDoc strTemp;
471       
472       strTemp = listoptions.getMarkedItemIDs();
473       return (strTemp != null ? strTemp : "");
474    }
475
476    /**
477     * @param listoptions - list options used to generate this list
478     * @return String
479     */

480    protected String JavaDoc getParentDataType(
481       ListOptions listoptions
482    )
483    {
484       return Integer.toString(listoptions.getParentDataType());
485    }
486
487    /**
488     * @param listoptions - list options used to generate this list
489     * @return String
490     */

491    protected String JavaDoc getParentId(
492       ListOptions listoptions
493    )
494    {
495       return Integer.toString(listoptions.getParentId());
496    }
497
498    /**
499     * @param listoptions - list options used to generate this list
500     * @return String
501     */

502    protected String JavaDoc getIgnoredItemIDs(
503       ListOptions listoptions
504    )
505    {
506       String JavaDoc strTemp;
507       
508       strTemp = listoptions.getIgnoredItemIDs();
509       return (strTemp != null ? strTemp : "");
510    }
511
512    /**
513     * @param listoptions - list options used to generate this list
514     * @return String
515     */

516    protected String JavaDoc getKeepSelected(
517       ListOptions listoptions
518    )
519    {
520       return (listoptions.getIsKeepSelectedChecked() ? "1" : "0");
521    }
522
523    /**
524     * @param extracondition - extra condition applied to this list
525     * @return String
526     */

527    protected String JavaDoc getExtraConditionAttribute(
528       DataCondition extracondition
529    )
530    {
531       String JavaDoc strTemp = "";
532       
533       if (extracondition != null)
534       {
535          strTemp = Integer.toString(extracondition.getAttribute());
536       }
537       
538       return strTemp;
539    }
540
541    /**
542     * @param extracondition - extra condition applied to this list
543     * @return String
544     */

545    protected String JavaDoc getExtraConditionOperation(
546       DataCondition extracondition
547    )
548    {
549       String JavaDoc strTemp = "";
550       
551       if (extracondition != null)
552       {
553          strTemp = Integer.toString(extracondition.getOperation());
554       }
555       
556       return strTemp;
557    }
558
559    /**
560     * @param extracondition - extra condition applied to this list
561     * @return String
562     */

563    protected String JavaDoc getExtraConditionValueType(
564       DataCondition extracondition
565    )
566    {
567       String JavaDoc strTemp = "";
568       
569       if (extracondition != null)
570       {
571          strTemp = Integer.toString(extracondition.getValueType());
572       }
573       
574       return strTemp;
575    }
576
577    /**
578     * @param extracondition - extra condition applied to this list
579     * @return String
580     */

581    protected String JavaDoc getExtraConditionValue(
582       DataCondition extracondition
583    )
584    {
585       String JavaDoc strTemp = "";
586       
587       if (extracondition != null)
588       {
589          strTemp = extracondition.getValueString();
590       }
591       return (strTemp != null ? strTemp : "");
592    }
593 }
594
Popular Tags