KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > web > WebMetaData


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.mvc.model.web;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import com.inversoft.beans.BeanException;
14 import com.inversoft.verge.mvc.MVCConstants;
15 import com.inversoft.verge.mvc.MVCException;
16 import com.inversoft.verge.mvc.MVCRegistry;
17 import com.inversoft.verge.mvc.model.AbstractMetaData;
18 import com.inversoft.verge.mvc.model.ModelHandler;
19 import com.inversoft.verge.util.ScopeTools;
20 import com.inversoft.verge.util.WebBean;
21 import com.inversoft.verge.util.WebBeanProperty;
22
23
24 /**
25  * <p>
26  * This class is used to help determine the web model object
27  * either from the extra parameter map built by the default
28  * model parser or from a list of pertinent information that
29  * is going to be written out to the page in input tags
30  * </p>
31  *
32  * @author Brian Pontarelli
33  * @since 2.0
34  * @version 2.0
35  */

36 public class WebMetaData extends AbstractMetaData {
37
38     /**
39      * The key into the parameters map or the name of the hidden input tag
40      * written out to the page that stores the model's class
41      */

42     public static final String JavaDoc CLASS_PARAMETER = "class";
43
44     /**
45      * The key into the parameters map or the name of the hidden input tag
46      * written out to the page that stores the model's scope
47      */

48     public static final String JavaDoc SCOPE_PARAMETER = "scope";
49
50     private int scope;
51     private Class JavaDoc klass;
52
53
54     /**
55      * Constructs a new <code>WebMetaData</code> that uses the defintion
56      * given to determine the id and property name, the parameters to determine
57      * the class and scope of the model.
58      *
59      * @param definition The definition of the model's id and property
60      * @param parameters A Map that stores extra information such as the scope
61      * and class of the model
62      */

63     public WebMetaData(String JavaDoc definition, Map JavaDoc parameters) throws MVCException {
64         super(definition);
65         
66         assert (parameters != null) : "parameters == null";
67
68         String JavaDoc className = (String JavaDoc) parameters.get(CLASS_PARAMETER);
69         try {
70             klass = Class.forName(className);
71         } catch (ClassNotFoundException JavaDoc cnfe) {
72             throw new MVCException("Invalid class: " + className);
73         }
74
75         String JavaDoc scopeStr = (String JavaDoc) parameters.get(SCOPE_PARAMETER);
76         assert (scopeStr != null) : "scope not found in map";
77         try {
78             scope = Integer.parseInt(scopeStr);
79         } catch (NumberFormatException JavaDoc nfe) {
80             assert (false) : "Invalid scope int: " + scopeStr;
81         }
82     }
83
84     /**
85      * Constructs a new <code>WebMetaData</code> with the given information
86      *
87      * @param id The id of the web model object in a scope
88      * @param property The name of the property of the web model
89      * @param className The name of the class of the web model
90      * @param scope An integer that defines the scope that web model is stored
91      * in
92      */

93     public WebMetaData(String JavaDoc id, String JavaDoc property, String JavaDoc className,
94             int scope)
95     throws MVCException {
96         super(id, property);
97         assert (property != null) : "property == null";
98         assert (className != null) : "className == null";
99         assert (ScopeTools.isValidScope(scope)) : "Invalid scope int";
100
101         this.scope = scope;
102
103         try {
104             this.klass = Class.forName(className);
105         } catch (ClassNotFoundException JavaDoc cnfe) {
106             throw new MVCException("Invalid class: " + className);
107         }
108     }
109
110     /**
111      * Constructs a new <code>WebMetaData</code> with the id and no property. This
112      * is used by the view and the property is added later using the setProperty
113      * method.
114      *
115      * @param id The id of the web model object in a scope
116      * @param className The name of the class of the web model
117      * @param scope An integer that defines the scope that web model is stored
118      * in
119      */

120     public WebMetaData(String JavaDoc id, String JavaDoc className, int scope)
121     throws MVCException {
122         super(id, null);
123         assert (id != null) : "id == null";
124         assert (className != null) : "className == null";
125         assert (ScopeTools.isValidScope(scope)) : "Invalid scope int";
126
127         super.id = id;
128         this.scope = scope;
129
130         try {
131             this.klass = Class.forName(className);
132         } catch (ClassNotFoundException JavaDoc cnfe) {
133             throw new MVCException("Invalid class: " + className);
134         }
135     }
136
137
138     /**
139      * Sets the property of this web model
140      *
141      * @param property The name of the property of this web model
142      */

143     public void setProperty(String JavaDoc property) {
144         super.property = property;
145     }
146
147     /**
148      * Returns the Class of this web model
149      *
150      * @return The Class of the model
151      */

152     public Class JavaDoc getModelClass() {
153         return klass;
154     }
155
156     /**
157      * Returns the scope of this web model
158      *
159      * @return The scope that the the model is stored in
160      */

161     public int getScope() {
162         return scope;
163     }
164
165     /**
166      * Constructs the parameters map from the information in the instance. This
167      * map can be used when constructing the ModelMVCInfo.
168      *
169      * @return The parameters map
170      */

171     public Map JavaDoc getExtraParams() {
172         Map JavaDoc map = new HashMap JavaDoc();
173         map.put(SCOPE_PARAMETER, Integer.toString(scope));
174         map.put(CLASS_PARAMETER, klass.getName());
175         
176         return map;
177     }
178
179     /**
180      * Convience method that returns a WebBean using the information stored in
181      * this meta data
182      */

183     public WebBean createWebBean() throws BeanException {
184         return new WebBean(id, scope, klass);
185     }
186
187     /**
188      * Convience method that returns a WebBeanProperty using the information
189      * stored in this meta data
190      */

191     public WebBeanProperty createWebBeanProperty() throws BeanException {
192         assert (property != null) : "property == null";
193         return new WebBeanProperty(getDefinition(), scope, klass);
194     }
195
196     /**
197      * Returns the instance of the WebModelHandler class. This is the handler that
198      * uses this MetaData
199      *
200      * @return The instance of the WebModelHandler class from the MVCRegistry
201      */

202     public ModelHandler getModelHandler() {
203         return MVCRegistry.lookupModelHandler(MVCConstants.WEB_NAME);
204     }
205
206     /**
207      * Returns web
208      *
209      * @return web
210      */

211     public String JavaDoc getModelSystem() {
212         return MVCConstants.WEB_NAME;
213     }
214 }
Popular Tags