Source code: com/RuntimeCollective/webapps/tag/EntityBeanTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/EntityBeanTag.java,v 1.3 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.3 $
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 javax.servlet.jsp.JspException;
33 import javax.servlet.jsp.JspTagException;
34 import javax.servlet.jsp.tagext.TagSupport;
35 import org.apache.struts.util.RequestUtils;
36 import com.RuntimeCollective.webapps.EntityBeanStore;
37 import com.RuntimeCollective.webapps.RuntimeParameters;
38 import com.RuntimeCollective.webapps.bean.EntityBean;
39
40
41 /** An abstract class meant to provide a base for tags that use EntityBeans.
42 * The <code>getEntityBean(String entityName)</code> method returns an <code>EntityBean</code> based on the values of
43 * <code>name</code>, <code>scope</code>, <code>id</code>, <code>parameter</code>, or throws a <code>JspTagException</code>
44 * if the EntityBean cannot be found from these values. You should pass in the EntityBean's class name.
45 * <p>You should implement the <code>doStartTag</code> and <code>doEndTag</code> methods. They should
46 * call <code>getEntityBean(String entityName)</code> to get the EntityBean to use.
47 * <p>If you add more properties, consider overriding <code>release</code> to free these resources as well.
48 * <ul>
49 * <li> <code> name </code> - the name of the EntityBean object to use. [Optional]. </li>
50 * <li> <code> scope </code> - the scope of the <code>name</code>d EntityBean
51 * (<code>page</code>, <code>request</code>, <code>session</code>, <code>application</code>). [Optional]. </li>
52 * <li> <code> id </code> - the id of the EntityBean to use. [Optional]. </li>
53 * <li> <code> parameter </code> - the request parameter that holds the id of the EntityBean to use. [Optional]. </li>
54 * </ul>
55 *
56 * @author Joe Holmberg
57 * @version $Id: EntityBeanTag.java,v 1.3 2003/09/30 15:13:18 joe Exp $
58 */
59 public abstract class EntityBeanTag extends TagSupport {
60
61 // == Properties ===================================================
62
63 /** The name of the EntityBean object to insert. */
64 protected String name = "";
65 /** Get the name of the EntityBean object to insert. */
66 public String getName() { return this.name; }
67 /** Set the name of the EntityBean object to insert. */
68 public void setName(String name) { this.name = name; }
69
70 /** The id of the Resource to insert. */
71 protected String id = "";
72 /** Get the id of the Resource to insert. */
73 public String getId() { return this.id; }
74 /** Set the id of the Resource to insert. */
75 public void setId(String id) { this.id = id; }
76
77 /** The request parameter that holds the id of the Resource to insert. */
78 protected String parameter = "";
79 /** Get the request parameter that holds the id of the Resource to insert. */
80 public String getParameter() { return this.parameter; }
81 /** Set the request parameter that holds the id of the Resource to insert. */
82 public void setParameter(String parameter) { this.parameter = parameter; }
83
84 /** The scope of the EntityBean to look for. */
85 protected String scope = null;
86 /** Get the scope of the EntityBean to look for. */
87 public String getScope() { return this.scope; }
88 /** Set the scope of the EntityBean to look for. */
89 public void setScope(String scope) { this.scope = scope; }
90
91 // == Tag methods ==================================
92
93 /**
94 * Code to produce the start of the tag. Can call <code>getEntityBean()</code>
95 * to get the EntityBean to manipulate.
96 */
97 public abstract int doStartTag() throws JspException;
98
99 /**
100 * Code to produce the end of the tag. Can call <code>getEntityBean()</code>
101 * to get the EntityBean to manipulate.
102 *
103 * Unless overriden, returns SKIP_BODY.
104 */
105 public int doEndTag() throws JspException {
106 return EVAL_PAGE;
107 }
108
109 /**
110 * Set <code>name</code>, <code>scope</code>, <code>id</code>, <code>parameter</code> to the empty string
111 */
112 public void release() {
113 super.release();
114 name="";
115 id="";
116 parameter="";
117 scope="";
118 }
119
120
121 // == Other methods ==================================
122
123 /**
124 * Gets an EntityBean based on the values of the object's
125 * <code>name</code>, <code>scope</code>, <code>id</code>, and <code>parameter</code> properties,
126 * or throws a <code>JspTagException</code> if the EntityBean cannot be found from these values.
127 * @param entityName the class name of the EntityBeanType you want to return.
128 * @return the EntityBean identified by the object's properties
129 */
130 protected EntityBean getEntityBean(String entityName) throws JspException {
131
132 // Get the EntityBean somehow
133 EntityBean entityBean = null;
134
135 if ( !id.equals("") ) {
136
137 // If id attribute has been set, then use that.
138 entityBean = (EntityBean) RuntimeParameters.getStore().get(entityName, Integer.parseInt( id ) );
139
140 } else if ( !parameter.equals("") ) {
141
142 // If parameter attribute has been set, then use that.
143 entityBean = (EntityBean) RuntimeParameters.getStore().get("com.RuntimeCollective.webapps.bean.EntityBean", Integer.parseInt( pageContext.getRequest().getParameter( parameter ) ) );
144
145 } else if ( !name.equals("") ) {
146
147 // Otherwise, use the named EntityBean, in the specified scope (may be null)
148 entityBean = (EntityBean) RequestUtils.lookup( pageContext, name, scope);
149
150 }
151
152 // Check if we've somehow found a EntityBean.
153 if ( entityBean == null ) {
154 throw new JspTagException("Couldn't find a EntityBean to use for EntityBeanTag.");
155 }
156
157 return entityBean;
158 }
159
160 }
161
162
163
164