KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > config > ActionConfigBuilder


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.controller.form.config;
8
9
10 import org.jdom.Element;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.config.ConfigurationException;
14 import com.inversoft.error.ErrorList;
15 import com.inversoft.util.StringTools;
16 import com.inversoft.verge.mvc.config.BaseConfig;
17 import com.inversoft.verge.mvc.config.BaseConfigBuilder;
18 import com.inversoft.verge.mvc.controller.WebBeanHandle;
19 import com.inversoft.verge.mvc.controller.form.FormMVCConstants;
20 import com.inversoft.verge.mvc.controller.form.FormWebBeanHandle;
21 import com.inversoft.verge.repository.RepositoryBean;
22 import com.inversoft.verge.repository.config.Config;
23 import com.inversoft.verge.repository.config.RepositoryConfigRegistry;
24 import com.inversoft.verge.util.ScopeConstants;
25
26
27 /**
28  * This class is responsible for building the action config
29  * objects from the xml config files.
30  *
31  * @author Brian Pontarelli
32  */

33 public class ActionConfigBuilder extends BaseConfigBuilder {
34
35
36     /**
37      * Constructs a new <code>ActionConfigBuilder</code>
38      */

39     public ActionConfigBuilder() {
40     }
41
42
43     /**
44      * Builds the action config object from the XML element and returns it.
45      *
46      * @param element The element to build the action configs from
47      * @return The ActionConfig obejct and never null
48      * @throws ConfigurationException If there were any problems building the
49      * configuration object. This exception will contain an ErrorList
50      * of all the errors
51      */

52     public BaseConfig build(Element element) throws ConfigurationException {
53
54         ErrorList errors = new ErrorList();
55         String JavaDoc name = element.getAttributeValue(Constants.NAME_ATTRIBUTE);
56         String JavaDoc isRepository = element.getAttributeValue(
57             Constants.IS_REPOSITORY_ATTRIBUTE);
58         String JavaDoc handler = element.getAttributeValue(Constants.HANDLER_ATTRIBUTE);
59         String JavaDoc modelEnabled = element.getAttributeValue(
60             Constants.MODEL_ENABLED_ATTRIBUTE);
61         String JavaDoc validationEnabled = element.getAttributeValue(
62             Constants.VALIDATION_ENABLED_ATTRIBUTE);
63         String JavaDoc longTxnEnabled = element.getAttributeValue(
64             Constants.LONG_TXN_ENABLED_ATTRIBUTE);
65         String JavaDoc longTxnStartURL = element.getAttributeValue(
66             Constants.LONG_TXN_START_URL_ATTRIBUTE);
67         String JavaDoc longTxnEndURL = element.getAttributeValue(
68                 Constants.LONG_TXN_END_URL_ATTRIBUTE);
69         String JavaDoc longTxnCategory = element.getAttributeValue(
70                 Constants.LONG_TXN_CATEGORY_ATTRIBUTE);
71
72         if (name == null) {
73             errors.addError("Actions must have a name");
74         }
75
76         if (handler == null) {
77             errors.addError("The action named: " + name +
78                 " must have a handler definition");
79         }
80
81         boolean isRepositoryBool = false;
82         if (isRepository != null && !StringTools.isValidBoolean(isRepository)) {
83             errors.addError("The action named: " + name +
84                 " has an invalid value for the isRepository attribute");
85         } else if (isRepository != null) {
86             isRepositoryBool = Boolean.valueOf(isRepository).booleanValue();
87         }
88
89         boolean isModelEnabled = true;
90         if (modelEnabled != null && !StringTools.isValidBoolean(modelEnabled)) {
91             errors.addError("The action named: " + name +
92                 " has an invalid value for the modelEnabled attribute");
93         } else if (modelEnabled != null) {
94             isModelEnabled = Boolean.valueOf(modelEnabled).booleanValue();
95         }
96
97         boolean isValidationEnabled = true;
98         if (validationEnabled != null &&
99                 !StringTools.isValidBoolean(validationEnabled)) {
100             errors.addError("The action named: " + name +
101                 " has an invalid value for the validationEnabled attribute");
102         } else if (validationEnabled != null) {
103             isValidationEnabled = Boolean.valueOf(validationEnabled).booleanValue();
104         }
105
106         boolean isLongTxnEnabled = false;
107         if (longTxnEnabled != null &&
108                 !StringTools.isValidBoolean(longTxnEnabled)) {
109             errors.addError("The action named: " + name +
110                 " has an invalid value for the longTxnEnabled attribute");
111         } else if (longTxnEnabled != null) {
112             isLongTxnEnabled = Boolean.valueOf(longTxnEnabled).booleanValue();
113         }
114
115         WebBeanHandle handle = null;
116
117         if (errors.isEmpty()) {
118             if (!isRepositoryBool) {
119                 try {
120                     handle = new FormWebBeanHandle(
121                         FormMVCConstants.ACTION_HANDLER_REQUEST_KEY, name,
122                         ScopeConstants.REQUEST_INT, handler);
123                 } catch (BeanException be) {
124                     errors.addError(be.getMessage());
125                 }
126             }
127         }
128
129         if (!errors.isEmpty()) {
130             throw new ConfigurationException(errors);
131         }
132
133         ActionConfig config = null;
134         if (handle != null) {
135             config = new ActionConfig(name, handle, isModelEnabled,
136                 isValidationEnabled, isLongTxnEnabled, longTxnStartURL,
137                 longTxnEndURL, longTxnCategory);
138         } else {
139             config = new ActionConfig(name, handler, isModelEnabled,
140                 isValidationEnabled, isLongTxnEnabled, longTxnStartURL,
141                 longTxnEndURL, longTxnCategory);
142         }
143
144         return config;
145     }
146
147     /**
148      * Validates and completes the build process for the action configurations
149      * by optionally locating the repository item for the action and building
150      * a WebBeanHandle for the action
151      *
152      * @param config The ActionConfig to validate and finish building
153      * @param registry The RepositoryConfigRegistry used to lookup the
154      * repository item configurations
155      * @throws ConfigurationException Has all the errors in the ErrorList
156      */

157     public void validate(ActionConfig config, RepositoryConfigRegistry registry)
158     throws ConfigurationException {
159
160         // If this config already has a handle, just return
161
if (config.getHandle() != null) {
162             return;
163         }
164
165         ErrorList errors = new ErrorList();
166         String JavaDoc handleRID = config.getHandleRID();
167         Config repositoryConfig = registry.lookup(handleRID);
168
169         if (repositoryConfig == null) {
170             errors.addError("Invalid repository id: " + handleRID);
171         } else {
172
173             try {
174                 config.setHandle(new WebBeanHandle(new RepositoryBean(repositoryConfig), config.getName()));
175             } catch (BeanException be) {
176                 errors.addError(be.getMessage());
177             }
178         }
179
180         if (!errors.isEmpty()) {
181             throw new ConfigurationException(errors);
182         }
183     }
184 }
185
Popular Tags