KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > WebBeanProperty


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.util;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12
13 import com.inversoft.beans.BeanException;
14 import com.inversoft.beans.NestedBeanProperty;
15 import com.inversoft.util.typeconverter.TypeConversionException;
16
17
18 /**
19  * <p>
20  * This class is used for accessing a web bean property.
21  * That is a bean that resides in the web repository and
22  * which has one or more standard JavaBean properties. Web
23  * bean was retrieved using string names in a similar
24  * manner to Bean proeprties. This string has the format:
25  * </p>
26  *
27  * <p>
28  * beanName.property1...propertyN.
29  * </p>
30  *
31  * <p>
32  * <code>beanName</code> is the key of the bean in one of
33  * the scopes (i.e. page, request, session or context).
34  * </p>
35  *
36  * @author Brian Pontarelli
37  * @since 1.0
38  * @version 2.0
39  */

40 public class WebBeanProperty extends NestedBeanProperty {
41
42     protected WebBean webBean;
43
44     /**
45      * Empty constructor for sub-classes. Sub-classes must initialize the
46      * propertyName and beanClass member variables in NestedBeanProperty and
47      * explicitly call initialize.
48      */

49     public WebBeanProperty() {
50         // Empty
51
}
52
53     /**
54      * <p>
55      * Constructs a new WebBeanProperty using the definition, scope and Class
56      * object.
57      * </p>
58      *
59      * <p>
60      * The definition includes the key as well as the property of the bean.
61      * </p>
62      *
63      * <p>
64      * For example:
65      * </p>
66      *
67      * <p>
68      * user.address.city - user is the key, address.city is the property
69      * </p>
70      *
71      * @param definition The key and property of the bean
72      * @param scope The scope that the bean is or will be stored in
73      * @param beanClass The Class object of the bean used for instantiation
74      * and JavaBean property handling
75      * @throws BeanException If anything is invalid or anything went wrong
76      */

77     public WebBeanProperty(String JavaDoc definition, int scope, Class JavaDoc beanClass)
78     throws BeanException {
79
80         // This does not call initialize inside NestedBeanProperty
81
super();
82
83         // Parse out the bean name and the properties list
84
int index = definition.indexOf(".");
85
86         if (index == -1) {
87             throw new BeanException("Missing property name for the bean named: " +
88                 definition);
89         }
90
91         webBean = new WebBean(definition.substring(0, index), scope, beanClass);
92         super.propertyName = definition.substring(index + 1);
93         super.beanClass = beanClass;
94         initialize();
95     }
96
97     /**
98      * <p>
99      * Constructs a new WebBeanProperty using the definition, scope and Class
100      * object.
101      * </p>
102      *
103      * <p>
104      * The definition includes the key as well as the property of the bean.
105      * </p>
106      *
107      * <p>
108      * For example:
109      * </p>
110      *
111      * <p>
112      * user.address.city - user is the key, address.city is the property
113      * </p>
114      *
115      * @param propertyName The property of the bean
116      * @param webBean The WebBean that describes the bean itself
117      * @throws BeanException If anything is invalid or anything went wrong
118      */

119     public WebBeanProperty(String JavaDoc propertyName, WebBean webBean)
120     throws BeanException {
121
122         // This calls initialize inside NestedBeanProperty
123
super(propertyName, webBean.getBeanClass());
124         this.webBean = webBean;
125     }
126
127
128     /**
129      * Returns the WebBean instance of this bean
130      */

131     public WebBean getWebBean() {
132         return webBean;
133     }
134
135     /**
136      * Returns the key of the bean in the scope
137      */

138     public String JavaDoc getBeanName() {
139         return webBean.getID();
140     }
141
142     /**
143      * Returns the full name of the bean property, which for this class is the
144      * name of the bean in the repository followed by the property of the bean.
145      * This is compiled using calls to getBeanName and getPropertyName
146      */

147     public String JavaDoc getFullName() {
148         return webBean.getID() + "." + getPropertyName();
149     }
150
151     /** Not implemented */
152     public Object JavaDoc getPropertyValue(Object JavaDoc bean) throws BeanException {
153         throw new UnsupportedOperationException JavaDoc("This method not implemented" +
154             " for the web bean property");
155     }
156
157     /** Not implemented */
158     public void setPropertyValue(Object JavaDoc bean, Object JavaDoc value, boolean convert)
159     throws BeanException {
160         throw new UnsupportedOperationException JavaDoc("This method not implemented" +
161             " for the web bean property");
162     }
163
164     /** Not implemented */
165     public void setPropertyValue(final Object JavaDoc bean, Object JavaDoc value) {
166         throw new UnsupportedOperationException JavaDoc("This method not implemented" +
167             " for the web bean property");
168     }
169
170     /**
171      * Gets the current value of the property from the instance of the bean.
172      * First this retrieves the bean from the correct scope. If it does not exist
173      * the bean is created and stored in the scope. Then the property is
174      * retrieved. if the bean is in the page scope, this method will fail.
175      *
176      * @param request The request used to lookup the bean from the scope
177      * @return The current value of the property
178      * @throws BeanException If anything went wrong whilst trying to get the
179      * property value
180      */

181     public Object JavaDoc getPropertyValue(HttpServletRequest JavaDoc request) throws BeanException {
182         Object JavaDoc obj = webBean.getInstance(request);
183         return super.getPropertyValue(obj);
184     }
185
186     /**
187      * Gets the current value of the property from the instance of the bean.
188      * First this retrieves the bean from the correct scope. If it does not exist
189      * the beanis created and stored in the scope. Then the property is retrieved.
190      *
191      * @param context The page context used to lookup the bean from the scope
192      * @return The current value of the property
193      * @throws BeanException If anything went wrong whilst trying to get the
194      * property value
195      */

196     public Object JavaDoc getPropertyValue(PageContext JavaDoc context) throws BeanException {
197         Object JavaDoc obj = webBean.getInstance(context);
198         return super.getPropertyValue(obj);
199     }
200
201     /**
202      * Sets the current value of the property from the instance of the bean.
203      * First this retrieves the bean from the correct scope. If it does not exist
204      * the bean is created and stored in the scope. Then the property is set.
205      * If the bean is in the page scope, this will fail.
206      *
207      * @param request The request used to lookup the bean from the scope
208      * @param value The value to set on the bean
209      * @param convert Determines whether or not the value should be converted
210      * to the correct parameter type for the property set method
211      * @throws BeanException If anything went wrong such as invalid method
212      * calls, invocation exceptions, create is false and a null value is
213      * returned from a getter, etc.
214      * @throws TypeConversionException If the auto-conversion failed
215      */

216     public void setPropertyValue(HttpServletRequest JavaDoc request, Object JavaDoc value,
217             boolean convert)
218     throws BeanException, TypeConversionException {
219         Object JavaDoc obj = webBean.getInstance(request);
220         super.setPropertyValue(obj, value, convert);
221     }
222
223     /**
224      * <p>
225      * Sets the current value of the property from the instance of the bean.
226      * First this retrieves the bean from the correct scope. If it does not exist
227      * the bean is created and stored in the scope. Then the property is set.
228      * If the bean is in the page scope, this will fail.
229      * </p>
230      *
231      * <p>
232      * This method always attempts to convert the value to the correct type.
233      * This method is the same as calling setPropertyValue(request, value, true).
234      * </p>
235      *
236      * @param request The request used to lookup the bean from the scope
237      * @param value The value to set on the bean
238      * @throws BeanException If anything went wrong such as invalid method
239      * calls, invocation exceptions, create is false and a null value is
240      * returned from a getter, etc.
241      * @throws TypeConversionException If the auto-conversion failed
242      */

243     public void setPropertyValue(HttpServletRequest JavaDoc request, Object JavaDoc value)
244     throws BeanException, TypeConversionException {
245         Object JavaDoc obj = webBean.getInstance(request);
246         super.setPropertyValue(obj, value, true);
247     }
248
249     /**
250      * Sets the current value of the property from the instance of the bean.
251      * First this retrieves the bean from the correct scope. If it does not exist
252      * the bean is created and stored in the scope. Then the property is set.
253      *
254      * @param context The page context used to lookup the bean from the scope
255      * @param value The value to set on the bean
256      * @param convert Determines whether or not the value should be converted
257      * to the correct parameter type for the property set method
258      * @throws BeanException If anything went wrong such as invalid method
259      * calls, invocation exceptions, create is false and a null value is
260      * returned from a getter, etc.
261      * @throws TypeConversionException If the auto-conversion failed
262      */

263     public void setPropertyValue(PageContext JavaDoc context, Object JavaDoc value,
264             boolean convert)
265     throws BeanException, TypeConversionException {
266         Object JavaDoc obj = webBean.getInstance(context);
267         super.setPropertyValue(obj, value, convert);
268     }
269 }
270
Popular Tags