Source code: com/RuntimeCollective/webapps/tag/LoadBeanTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/LoadBeanTag.java,v 1.8 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.8 $
3 * $Date: 2003/09/30 15:13:18 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import com.RuntimeCollective.webapps.bean.EntityBean;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import javax.servlet.jsp.JspWriter;
36 import javax.servlet.jsp.PageContext;
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.JspTagException;
39 import javax.servlet.jsp.tagext.Tag;
40 import javax.servlet.jsp.tagext.TagSupport;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import org.apache.struts.action.Action;
44 import org.apache.struts.util.RequestUtils;
45 import org.apache.struts.taglib.html.BaseHandlerTag;
46
47 /** A custom JSP tag that sets the request parameters necessary for calling a <code>BeanLoaderAction</code>. This tag takes the following attributes:
48 * <ul>
49 * <li> <code> action </code> - the name of the action that will load the bean. This will normally be of type <code>BeanLoaderAction</code>, and the action mapping for this action (defined in struts-config.xml) defines the type of bean to load, and the form to populate. [Mandatory]. </li>
50 * <li> <code> name </code> - the name of the bean, under page or request or session scope, that holds the id of the bean to load as a property [Optional]. </li>
51 * <li> <code> scope </code> - the scope of the bean to load [Optional]. </li>
52 * <li> <code> property </code> - the property of the bean, specified by 'name', that holds the id of the bean to load as a property. If this is not set, then 'id' is used. [Optional].</li>
53 * <li> <code> id </code> - the id of the bean to load, or "new" if a new bean should be created. [Optional].</li>
54 * <li> <code> parameter </code> - the request parameter that holds the id of the bean to load [Optional].</li>
55 * <li> <code> forward </code> - the name of the forward to go to if the bean was loaded successfully. If this is not set then `success' will be returned. [Optional].</li>
56 * <li> <code> failForward </code> - the name of the forward to go to if the bean failed to load successfully. If this is not set then `failure' will be returned. [Optional].</li>
57 * </ul>
58 * Exactly one of id, parameter, or (name and property) must be set.
59 * <p><strong>Note </strong> the body of the tag should be set to the label of the link.
60 * @version $Id: LoadBeanTag.java,v 1.8 2003/09/30 15:13:18 joe Exp $
61 */
62 public class LoadBeanTag extends BaseHandlerTag {
63
64 // == Properties ===================================================
65
66 /** The path to the BeanLoaderAction which loads the bean. */
67 protected String action = "";
68 /** Get the path to the BeanLoaderAction which loads the bean. */
69 public String getAction() { return this.action; }
70 /** Set the path to the BeanLoaderAction which loads the bean. */
71 public void setAction(String action) { this.action = action; }
72
73 /** The name of the bean that holds the id of the bean to load as a property. */
74 protected String name = "";
75 /** Get the name of the bean that holds the id of the bean to load as a property. */
76 public String getName() { return this.name; }
77 /** Set the name of the bean that holds the id of the bean to load as a property. */
78 public void setName(String name) { this.name = name; }
79
80 /** The property of the bean specified by 'name', which holds the id of the bean to load. Defaults to id. */
81 protected String property = "id";
82 /** Get the property of the bean specified by 'name', which holds the id of the bean to load. */
83 public String getProperty() { return this.property; }
84 /** Set the property of the bean specified by 'name', which holds the id of the bean to load. */
85 public void setProperty(String property) { this.property = property; }
86
87 /** The scope of the bean to look for. */
88 protected String scope = null;
89 /** Get the scope of the bean to look for. */
90 public String getScope() { return this.scope; }
91 /** Set the scope of the bean to look for. */
92 public void setScope(String scope) { this.scope = scope; }
93
94 /** The id of the bean to load. */
95 protected String id = "";
96 /** Get the id of the bean to load. */
97 public String getId() { return this.id; }
98 /** Set the id of the bean to load. */
99 public void setId(String id) { this.id = id; }
100
101 /** The parameter that holds the id of the bean to load. */
102 protected String parameter = "";
103 /** Get the parameter that holds the id of the bean to load. */
104 public String getParameter() { return this.parameter; }
105 /** Set the parameter that holds the id of the bean to load. */
106 public void setParameter(String parameter) { this.parameter = parameter; }
107
108 /** The name of the forward to go to if the bean was loaded successfully. */
109 protected String forward = "";
110 /** Get the name of the forward to go to if the bean was loaded successfully. */
111 public String getForward() { return this.forward; }
112 /** Set the name of the forward to go to if the bean was loaded successfully. */
113 public void setForward(String forward) { this.forward = forward; }
114
115
116
117 /** The name of the forward to go to if the bean failed to load successfully. */
118 protected String failForward = "";
119 /** Get the name of the forward to go to if the bean failed to load successfully. */
120 public String getFailForward() { return this.failForward; }
121 /** Set the name of the forward to go to if the bean failed to load successfully. */
122 public void setFailForward(String failForward) { this.failForward = failForward; }
123
124 // == Tag methods ==================================
125
126 public int doStartTag() throws JspException {
127
128 if (action.equals("")) throw new JspException("Undefined 'action' attribute on LoadBeanTag.");
129
130 // The parameters to add to the request.
131 String idParam="";
132
133 // If id attribute has been set, then use that.
134 if ( !id.equals("") ) idParam = id;
135
136 // If parameter attribute has been set, then use that.
137 else if ( !parameter.equals("") ) idParam = pageContext.getRequest().getParameter( parameter );
138
139 // Otherwise get the id from a bean.
140 else if ( !name.equals("") ) {
141 idParam = RequestUtils.lookup( pageContext, name, property, scope ).toString();
142
143 // DEBUG!
144 //com.RuntimeCollective.webapps.RuntimeParameters.logDebug(this, "NAME="+name+", PROPERTY="+property+", SCOPE="+scope+", LOOKUP="+RequestUtils.lookup( pageContext, name, property, scope ));
145
146 }
147 if ( idParam==null ) throw new JspTagException("Undefined id for LoadBeanTag.");
148
149
150
151
152 // Construct a response
153 HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
154
155 StringBuffer html = new StringBuffer(80);
156 html.append("<a href=\"");
157 html.append(response.encodeURL(TagUtils.getActionMappingURL(pageContext,action)));
158 html.append("?id=" + idParam);
159 html.append(forward.equals("") ? "" : ("&forward=" + forward));
160 html.append(failForward.equals("") ? "" : ("&failForward=" + failForward));
161 html.append("\"");
162 html.append(prepareStyles());
163 html.append(prepareEventHandlers());
164 html.append(">");
165
166 try {
167 pageContext.getOut().println(html.toString());
168 } catch (IOException e) {
169 throw new JspTagException("I/O Exception " + e.getMessage());
170 }
171
172 return EVAL_BODY_INCLUDE;
173 }
174
175 public int doEndTag() throws JspException {
176 try {
177 pageContext.getOut().println( "</a>" );
178 } catch (IOException e) {
179 throw new JspTagException("I/O Exception " + e.getMessage() );
180 }
181 return EVAL_PAGE;
182 }
183
184
185 /**
186 * Reset the state of the LoadBeanTag.
187 */
188 public void release() {
189 super.release();
190 action="";
191 name="";
192 property="id";
193 scope=null;
194 id="";
195 parameter="";
196 forward="";
197 failForward="";
198 }
199 }