1 7 package com.inversoft.verge.mvc.config; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import org.jdom.Element; 14 15 import com.inversoft.beans.BeanException; 16 import com.inversoft.config.ConfigurationException; 17 import com.inversoft.config.component.ComponentConfigBuilderRegistry; 18 import com.inversoft.error.BasicError; 19 import com.inversoft.error.ErrorList; 20 import com.inversoft.util.StringTools; 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 import com.inversoft.verge.util.ScopeTools; 26 import com.inversoft.verge.util.WebBean; 27 28 29 35 public class BaseFormConfigBuilder extends BaseConfigBuilder { 36 37 private BaseValidatorConfigBuilder validatorConfigBuilder; 38 39 40 44 public BaseFormConfigBuilder() { 45 } 46 47 48 52 public void setRegistry(ComponentConfigBuilderRegistry registry) { 53 super.setRegistry(registry); 54 validatorConfigBuilder = (BaseValidatorConfigBuilder) 55 registry.lookup(Constants.VALIDATOR_BUILDER_NAME); 56 } 57 58 67 public BaseConfig build(Element element) throws ConfigurationException { 68 69 ErrorList errors = new ErrorList(); 70 String name = element.getAttributeValue(Constants.NAME_ATTRIBUTE); 71 72 if (name == null) { 73 errors.addError("Forms must have a name"); 74 } else if (name.indexOf(".") != -1) { 75 errors.addError("The form name: " + name + 76 " is invalid because it contains the '.' character"); 77 } 78 79 BaseFormConfig formConfig = null; 80 if (errors.isEmpty()) { 81 formConfig = new BaseFormConfig(name); 82 buildFormBeans(element, formConfig, errors); 83 buildValidators(element, formConfig, errors); 84 } 85 86 if (!errors.isEmpty()) { 87 throw new ConfigurationException(errors); 88 } 89 90 return formConfig; 91 } 92 93 96 void buildFormBeans(Element form, BaseFormConfig formConfig, ErrorList errors) { 97 98 List list = form.getChildren(Constants.FORM_BEAN_ELEMENT); 99 if (list.size() == 0) { 100 return; 101 } 102 103 Iterator iter = list.iterator(); 104 while (iter.hasNext()) { 105 Element bean = (Element) iter.next(); 106 String name = bean.getAttributeValue(Constants.NAME_ATTRIBUTE); 107 String beanClass = bean.getAttributeValue(Constants.CLASS_ATTRIBUTE); 108 String scope = bean.getAttributeValue(Constants.SCOPE_ATTRIBUTE); 109 String isRepository = bean.getAttributeValue(Constants.IS_REPOSITORY_ATTRIBUTE); 110 111 if (name == null) { 112 errors.addError("All formBeans for the form: " + formConfig.getName() + 113 " must have a name"); 114 } 115 116 boolean isRepositoryBool = false; 117 if (isRepository != null && !StringTools.isValidBoolean(isRepository)) { 118 errors.addError("The formBean named: " + name + " for the form: " 119 + formConfig.getName() + " has an invalid isRepository " + 120 "attribute value"); 121 } else { 122 isRepositoryBool = Boolean.valueOf(isRepository).booleanValue(); 123 } 124 125 if (beanClass == null && !isRepositoryBool) { 126 errors.addError("The formBean named: " + name + " for the form: " + 127 formConfig.getName() + " must have a class attribute or " + 128 "have isRepository set to true"); 129 } 130 131 int scopeInt = ScopeConstants.REQUEST_INT; 132 if (scope != null) { 133 try { 134 scopeInt = ScopeTools.convertScope(scope); 135 } catch (IllegalArgumentException iae) { 136 errors.addError("The formBean for the form: " + formConfig.getName() + 137 " has an invalid scope of: " + scope); 138 } 139 } 140 141 if (errors.isEmpty()){ 142 if (!isRepositoryBool) { 143 try { 144 formConfig.addFormBean(new WebBean(name, scopeInt, beanClass)); 145 } catch (BeanException be) { 146 errors.addError( new BasicError(be.toString()) ); 147 } 148 } else { 149 formConfig.addFormBeanRID(name); 150 } 151 } 152 } 153 } 154 155 159 void buildValidators(Element element, BaseFormConfig formConfig, 160 ErrorList errors) { 161 162 Iterator iter = element.getChildren(Constants.VALIDATOR_ELEMENT).iterator(); 165 Element validatorElement; 166 BaseValidatorConfig validatorConfig; 167 while (iter.hasNext()) { 168 validatorElement = (Element) iter.next(); 169 try { 170 validatorConfig = (BaseValidatorConfig) validatorConfigBuilder.build( 171 validatorElement); 172 } catch (ConfigurationException ce) { 173 Iterator errorIter = ce.getErrors().iterator(); 174 BasicError error; 175 while (errorIter.hasNext()) { 176 error = (BasicError) errorIter.next(); 177 error = new BasicError("For form named: " 178 + formConfig.getName() + " -> " + error.getMessage()); 179 errors.addError(error); 180 } 181 continue; 182 } 183 184 formConfig.addValidatorConfig(validatorConfig); 185 } 186 } 187 188 191 public void validate(BaseFormConfig config, RepositoryConfigRegistry registry, 192 FormConfigRegistry formRegistry) 193 throws ConfigurationException { 194 195 ErrorList errors = new ErrorList(); 197 Iterator iter = config.getFormBeanRIDs().iterator(); 198 while (iter.hasNext()) { 199 String rid = (String ) iter.next(); 200 Config repositoryConfig = registry.lookup(rid); 201 202 if (repositoryConfig == null) { 203 errors.addError("Invalid repository id: " + rid); 204 } else { 205 206 try { 207 config.addFormBean(new RepositoryBean(repositoryConfig)); 208 } catch (BeanException be) { 209 errors.addError(be.getMessage()); 210 } 211 } 212 } 213 214 iter = config.getValidators().iterator(); 215 BaseValidatorConfig validatorConfig; 216 while (iter.hasNext()) { 217 validatorConfig = (BaseValidatorConfig) iter.next(); 218 try { 219 validatorConfigBuilder.validate(validatorConfig, config, 220 formRegistry); 221 } catch (ConfigurationException ce) { 222 errors.addErrorList(ce.getErrors()); 223 } 224 } 225 226 if (!errors.isEmpty()) { 227 throw new ConfigurationException(errors); 228 } 229 } 230 } | Popular Tags |