KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > repository > RepositoryBeanProperty


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.repository;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.util.StringTools;
14 import com.inversoft.util.typeconverter.TypeConversionException;
15 import com.inversoft.verge.repository.config.Config;
16 import com.inversoft.verge.util.WebBean;
17 import com.inversoft.verge.util.WebBeanProperty;
18
19
20 /**
21  * <p>
22  * This class is used for accessing bea properties on a
23  * repository item. This class is setup using the item id
24  * and the property name.
25  * </p>
26  *
27  * <p>
28  * This class fully supports nested properties.
29  * </p>
30  *
31  * @author Brian Pontarelli
32  * @since 1.0
33  * @version 2.0
34  */

35 public class RepositoryBeanProperty extends WebBeanProperty {
36
37     String JavaDoc repositoryProperty;
38
39
40     /**
41      * Constructs a new BeanProperty for the repository item and property name
42      * contained the the given String. The given String contains the id of the
43      * repository item, followed but a period, followed by the name
44      * of the property of that repository item.
45      * <p>
46      * For example,<br>
47      * foo.property<br>
48      * This references a repository item with an id of <code>foo</code>
49      * and a property of <code>property</code>.
50      * <b>NOTE:</b> The bean is not looked up until the value is retrieved or
51      * updated using the getPropertyValue and setPropertyValue methods
52      *
53      * @param repositoryProperty The name of the repository item and the property
54      * of that item
55      * @throws BeanException If anything is invalid or anything went wrong
56      */

57     public RepositoryBeanProperty(String JavaDoc repositoryProperty,
58             HttpServletRequest JavaDoc request)
59     throws BeanException {
60         super(); // This does not call initialize inside NestedBeanProperty
61

62         assert (!StringTools.isEmpty(repositoryProperty)) :
63             "repositoryProperty is null or empty";
64
65         int index = repositoryProperty.indexOf(".");
66         if (index == -1) {
67             throw new BeanException("Invalid repository property String");
68         }
69
70         initialize(repositoryProperty.substring(0, index),
71             repositoryProperty.substring(index + 1), request);
72
73         this.repositoryProperty = repositoryProperty;
74     }
75
76     /**
77      * Constructs a new BeanProperty for the repository item with the given id
78      * and property name.
79      * <p>
80      * <b>NOTE:</b> The bean is not looked up until the value is retrieved or
81      * updated using the getPropertyValue and setPropertyValue methods
82      *
83      * @param id The name of the repository item
84      * @param property The property of that item
85      * @throws BeanException If anything is invalid or anything went wrong
86      */

87     public RepositoryBeanProperty(String JavaDoc id, String JavaDoc property,
88             HttpServletRequest JavaDoc request)
89     throws BeanException {
90         super(); // This does not call initialize inside NestedBeanProperty
91

92         assert (!StringTools.isEmpty(id)) : "id is null or empty";
93         assert (!StringTools.isEmpty(property)) : "property is null or empty";
94
95         initialize(id, property, request);
96         this.repositoryProperty = id + "." + property;
97     }
98
99
100     /** Setups the rest of the bean property stuff */
101     protected void initialize(String JavaDoc id, String JavaDoc property,
102             HttpServletRequest JavaDoc request)
103     throws BeanException {
104         propertyName = property;
105
106         // Do the repository lookup
107
try {
108             Config config = Repository.getInstance().lookupConfig(request, id);
109             if (config == null) {
110                 throw new BeanException("Invalid repository id '" + id + "'");
111             }
112
113             beanClass = config.getJavaBean().getBeanClass();
114             webBean = new WebBean(id, config.getScope(), beanClass);
115         } catch (RepositoryException re) {
116             throw new BeanException(re.toString());
117         }
118
119         super.initialize();
120     }
121
122     /**
123      * Returns the repository id of the item
124      */

125     public String JavaDoc getRepositoryId() {
126         return webBean.getID();
127     }
128
129     /**
130      * Returns the full name of the bean property, which for this class is the name
131      * of the item in the repository followed by the property of the bean.
132      */

133     public String JavaDoc getFullName() {
134         return repositoryProperty;
135     }
136
137     /**
138      * Gets the current value of the property from the instance of the bean in
139      * the repository. This method does a lookup in the WebRepository using the
140      * HttpServletRequest object given. If the request object is null, this method
141      * will only work for Application scoped repository beans.
142      *
143      * @param request The request used to lookup the bean from the web repository
144      * @return The current value of the property
145      * @throws BeanException If anything went wrong whilst trying to get the property value
146      */

147     public Object JavaDoc getPropertyValue(HttpServletRequest JavaDoc request) throws BeanException {
148
149         // If the item is not loaded, just look it up to load it and then the super
150
// method will look it up from the scope and use that object
151
Repository rep = Repository.getInstance();
152         if (!rep.isItemLoaded(request, webBean.getID())) {
153             rep.lookupItem(request, webBean.getID());
154         }
155
156         return super.getPropertyValue(request);
157     }
158
159     /**
160      * Sets the current value of the property on the instance of the bean in
161      * the repository. This method does a lookup in the WebRepository using the
162      * HttpServletRequest object given. If the request object is null, this method
163      * will fail.
164      *
165      * @param request The request used to lookup the bean from the web repository
166      * @param value The value to set on the bean
167      * @param convert Determines whether or not the value should be converted
168      * to the correct parameter type for the property set method
169      * @throws BeanException If anything went wrong such as invalid method
170      * calls, invocation exceptions, create is false and a null value is
171      * returned from a getter, etc.
172      * @throws TypeConversionException If there was a problem doing an auto-
173      * type conversion
174      */

175     public void setPropertyValue(HttpServletRequest JavaDoc request, Object JavaDoc value,
176             boolean convert)
177     throws BeanException, TypeConversionException {
178
179         assert (request != null) : "request == null";
180
181         // If the item is not loaded, just look it up to load it and then the super
182
// method will look it up from the scope and use that object
183
Repository rep = Repository.getInstance();
184         if (!rep.isItemLoaded(request, webBean.getID())) {
185             rep.lookupItem(request, webBean.getID());
186         }
187         super.setPropertyValue(request, value, convert);
188     }
189
190     /**
191      * <p>
192      * Sets the current value of the property on the instance of the bean in
193      * the repository. This method does a lookup in the WebRepository using the
194      * HttpServletRequest object given. If the request object is null, this method
195      * will fail.
196      * </p>
197      *
198      * <p>
199      * This method is the same as calling setPropertyValue(request, value, true)
200      * </p>
201      *
202      * @param request The request used to lookup the bean from the web repository
203      * @param value The value to set on the bean
204      * @throws BeanException If anything went wrong such as invalid method
205      * calls, invocation exceptions, create is false and a null value is
206      * returned from a getter, etc.
207      * @throws TypeConversionException If there was a problem doing an auto-
208      * type conversion
209      */

210     public void setPropertyValue(HttpServletRequest JavaDoc request, Object JavaDoc value)
211     throws BeanException, TypeConversionException {
212
213         assert (request != null) : "request == null";
214
215         // If the item is not loaded, just look it up to load it and then the super
216
// method will look it up from the scope and use that object
217
Repository rep = Repository.getInstance();
218         if (!rep.isItemLoaded(request, webBean.getID())) {
219             rep.lookupItem(request, webBean.getID());
220         }
221         super.setPropertyValue(request, value, true);
222     }
223 }
Popular Tags