View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: CollectionAddTag.java,v $
31   * Revision 1.2  2005/04/09 18:04:20  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.1  2005/01/19 12:58:20  colinmacleod
35   * Moved from ivata groupware.
36   *
37   * Revision 1.1  2004/09/30 15:16:03  colinmacleod
38   * Split off addressbook elements into security subproject.
39   *
40   * Revision 1.3  2004/03/21 21:16:09  colinmacleod
41   * Shortened name to ivata op.
42   *
43   * Revision 1.2  2004/02/01 22:00:34  colinmacleod
44   * Added full names to author tags
45   *
46   * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
47   * Moved ivata openportal to SourceForge..
48   *
49   * Revision 1.1.1.1  2003/10/13 20:50:11  colin
50   * Restructured portal into subprojects
51   *
52   * Revision 1.1  2003/02/25 08:15:50  colin
53   * moved to jsp category
54   *
55   * Revision 1.3  2003/02/04 17:43:51  colin
56   * copyright notice
57   *
58   * Revision 1.2  2002/09/25 11:40:53  colin
59   * added checking for null collection at page scope -> session scope
60   *
61   * Revision 1.1  2002/07/01 10:38:41  colin
62   * first version of collection add tag
63   * -----------------------------------------------------------------------------
64   */
65  package com.ivata.mask.web.tag.util;
66  
67  
68  import java.util.Collection;
69  
70  import javax.servlet.jsp.JspException;
71  import javax.servlet.jsp.PageContext;
72  import javax.servlet.jsp.tagext.TagSupport;
73  
74  
75  /***
76   * <p>This class is a wrapper tag to <code>Collection.add</code>.</p>
77   *
78   * <p><b>Tag attributes:</b><br/>
79   * <table cellpadding='2' cellspacing='5' border='0' align='center'
80   * width='85%'>
81   *   <tr class='TableHeadingColor'>
82   *     <th>attribute</th>
83   *     <th>reqd.</th>
84   *     <th>param. class</th>
85   *     <th width='100%'>description</th>
86   *   </tr>
87   *   <tr class='TableRowColor'>
88   *     <td>name</td>
89   *     <td>true</td>
90   *     <td><code>String</code></td>
91   *     <td>The name of the <code>Collection</code> you .want
92   *     to add an element to. This should have been instantiated with
93   *     page scope using the <code>useBean</code> tag.</td>
94   *   </tr>
95   *   <tr class='TableRowColor'>
96   *     <td>value</td>
97   *     <td>true</td>
98   *     <td><code>Object</code></td>
99   *     <td>The value of the object you want to add.</td>
100  *   </tr>
101  * </table>
102  * </p>
103  *
104  * @since ivata masks 0.5 (2002-06-29)
105  * @author Colin MacLeod
106  * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
107  * @version $Revision: 1.2 $
108  */
109 public class CollectionAddTag extends TagSupport {
110 
111     /***
112      * <p>Stores the name of the colleciton to add a new value to.</p>
113      */
114     private String name;
115 
116     /***
117      * <p>Stores the value of the object to be added to the
118      * <code>Colleciton</code>.</p>
119      */
120     private Object value;
121 
122     /***
123      * <p>Default constructor.</p>
124 
125      */
126     public CollectionAddTag() {
127         super();
128     }
129 
130     /***
131      * <p>Performs the action of adding the value supplied to the
132      * <code>Collection</code>.</p>
133      *
134      * <p>This method is called when the JSP engine encounters the start tag,
135      * after the attributes are processed.<p>
136      *
137      * <p>Scripting variables (if any) have their values set here.</p>
138      *
139      * @exception JspException if the <code>Collection</code> with
140      * the name specified does not exist at page scope.
141      * @return <code>SKIP_BODY</code> since this tag has no body.
142      */
143     public int doStartTag() throws JspException {
144         //super.doStartTag();
145         Collection collection = (Collection)
146             pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
147 
148         // if there is nothing at page scope, try session scope
149         if (collection == null) {
150             collection = (Collection) pageContext.getAttribute(name,
151                     PageContext.SESSION_SCOPE);
152         }
153         // if there is no collection with this name, cry now
154         if (collection == null) {
155             throw new JspException("Error in CollectionAddTag: No collection "
156                     + "found in page or session scope called '"
157                     + name
158                     + "'");
159         }
160         collection.add(value);
161         return SKIP_BODY;
162     }
163 
164     /***
165      * <p>Get the name of the collection to add a new value to.</p>
166      *
167      * @return the current value of the <code>Collection</code> name.
168      */
169     public final String getName() {
170         return name;
171     }
172 
173     /***
174      * <p>Get the value of the object to be added to the
175      * <code>Colleciton</code>.</p>
176      *
177      * @return the current value of the object to be added.
178      */
179     public final Object getValue() {
180         return value;
181     }
182 
183     /***
184      * <p>Set the name of the colleciton to add a new value to.</p>
185      *
186      * @param nameParam the new value of the <code>Collection</code> name.
187      */
188     public final void setName(final String nameParam) {
189         this.name = nameParam;
190     }
191 
192     /***
193      * <p>Set  the value of the object to be added to the
194      * <code>Colleciton</code>.</p>
195      *
196      * @param valueParam the new value of object to be added.
197      */
198     public final void setValue(final Object valueParam) {
199         this.value = valueParam;
200     }
201 }