KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > web > tag > ActionTag


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors. This library is
3  * free software; you can redistribute it and/or modify it under the terms of
4  * the GNU Lesser General Public License as published by the Free Software
5  * Foundation; either version 2.1 of the License, or (at your option) any later
6  * version. This library is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
9  * License for more details. You should have received a copy of the GNU Lesser
10  * General Public License along with this library; if not, write to the Free
11  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
12  * USA. > http://www.gnu.org/copyleft/lesser.html >
13  * http://www.opensource.org/licenses/lgpl-license.php
14  */

15 package net.mlw.vlh.web.tag;
16
17 import java.util.HashMap JavaDoc;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20
21 import net.mlw.vlh.web.tag.support.ParamAddable;
22 import net.mlw.vlh.web.util.JspUtils;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * This tag creates action. It is ment to be used along with ControlsTag
29  * </code> <pre>
30  *
31  *
32  * &lt;vlh:action url=&quot;/delete.do?&quot; customParameters=&quot; &lt;%=HashMapOfCustomParams%&gt;&quot;&gt;
33  * &lt;vlh:addParam name=&quot;rowId&quot; property=&quot;id&quot; temp=&quot;true&quot;/&gt;
34  * &lt;vlh:addParam name=&quot;rowStaticParamName&quot; value=&quot;CommonForAllRows&quot; temp=&quot;true&quot;/&gt;
35  * Temp dynamic and static params.
36  * &lt;/vlh:action&gt;
37  *
38  *
39  * </pre></code>
40  * @author Andrej Zachar
41  * @version $Revision: 1.9 $ $Date: 2005/11/23 14:51:53 $ $Author: smarek $
42  */

43 public class ActionTag extends ConfigurableTag implements ParamAddable
44 {
45
46    private static final long serialVersionUID = -4765405639511065820L;
47
48    /**
49     * Logger for this class
50     */

51    private static final Log LOGGER = LogFactory.getLog(ActionTag.class);
52
53    /**
54     * The url use to append all needed parameters, if not set rootTag url is
55     * used.
56     */

57    private String JavaDoc url = null;
58
59    /** Any custom parameters are appended to url. */
60    private HashMap JavaDoc customParameters = null;
61
62    /**
63     * Any row properties from nested AddParamTag are stored in
64     * actionParameters, which are appended to url.
65     */

66    private HashMap JavaDoc actionParameters = new HashMap JavaDoc();
67
68    /**
69     * This prefix is used to recognize, which url parameters are temporaly
70     * requested for an action.
71     * <li> Default is set as String <b>"ACT"</b> </li>
72     */

73    public static final String JavaDoc ACTION_TEMP_PARAM_PREFIX = "ACT";
74
75    public int doStartTag() throws JspException JavaDoc
76    {
77       actionParameters.clear();
78       return super.doStartTag();
79    }
80
81    public int doEndTag() throws JspException JavaDoc
82    {
83
84       ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class);
85
86       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<a ");
87       sb.append(getCellAttributes().getCellAttributesAsString());
88
89       sb.append("href=\"");
90       sb.append(getUrl(rootTag));
91
92       sb.append(rootTag.getTableInfo().getConfig().getLinkEncoder().encode(pageContext, getAllParameters(rootTag)));
93       sb.append("\">");
94       sb.append(bodyContent.getString().trim());
95       sb.append("</a>");
96       JspUtils.write(pageContext, sb.toString());
97
98       release();
99
100       return EVAL_PAGE;
101    }
102
103    /**
104     * Make a final map of parameters for url encoder. At first ar added table
105     * parameters, then are added tableId with paramName
106     * ACTION_TEMP_PARAM_PREFIX, then customParams and finally are added
107     * actionParams.
108     */

109    private HashMap JavaDoc getAllParameters(ValueListSpaceTag rootTag)
110    {
111       HashMap JavaDoc map = new HashMap JavaDoc();
112       if (rootTag.getTableInfo().getParameters() != null)
113       {
114          map.putAll(rootTag.getTableInfo().getParameters());
115       }
116       // if (rootTag.getTableInfo().getId() != TableInfo.DEFAULT_ID) {
117
// map.put(ACTION_TEMP_PARAM_PREFIX, rootTag.getTableInfo().getId());
118
// }
119
if (customParameters != null)
120       {
121          map.putAll(customParameters);
122       }
123
124       //need to be last to overwrite the same params
125
map.putAll(actionParameters);
126       return map;
127    }
128
129    /**
130     * @param customParameters The customParameters to encode in the action's
131     * url.
132     */

133    public void setCustomParameters(HashMap JavaDoc customParameters)
134    {
135       this.customParameters = customParameters;
136    }
137
138    /**
139     * @return String Url, if in tag is nothing setted, return base table url.
140     */

141    private String JavaDoc getUrl(ValueListSpaceTag rootTag)
142    {
143       if (url == null)
144       {
145          return rootTag.getTableInfo().getUrl();
146       }
147       else
148       {
149          return url;
150       }
151    }
152
153    /**
154     * @param url The base url to set.
155     */

156    public void setUrl(String JavaDoc url)
157    {
158       this.url = url;
159    }
160
161    /**
162     * Add parameters to url for an action.
163     *
164     * <pre>
165     *
166     *
167     *
168     * url ? key = value &amp;
169     *
170     *
171     *
172     * </pre>
173     */

174    public void addParam(String JavaDoc key, String JavaDoc value)
175    {
176       if (LOGGER.isDebugEnabled() && value == null)
177       {
178          LOGGER.debug("Do you really want to add param '" + key + "' which value is null?");
179       }
180       actionParameters.put(key, value);
181    }
182
183    private void reset()
184    {
185       this.actionParameters.clear();
186       this.customParameters = null;
187       this.url = null;
188    }
189
190    /**
191     * Called on a Tag handler to release state.
192     * The page compiler guarantees that JSP page implementation
193     * objects will invoke this method on all tag handlers,
194     * but there may be multiple invocations on doStartTag and doEndTag in between.
195     *
196     * @see javax.servlet.jsp.tagext.Tag#release()
197     */

198    public void release()
199    {
200       super.release();
201       reset();
202    }
203 }
Popular Tags