1 7 package com.inversoft.verge.mvc.controller.form.config; 8 9 10 import java.util.Iterator ; 11 12 import org.jdom.Element; 13 14 import com.inversoft.config.ConfigurationException; 15 import com.inversoft.config.component.ComponentConfigBuilderRegistry; 16 import com.inversoft.error.BasicError; 17 import com.inversoft.error.ErrorList; 18 import com.inversoft.util.ReflectionException; 19 import com.inversoft.util.ReflectionTools; 20 import com.inversoft.verge.mvc.config.BaseConfig; 21 import com.inversoft.verge.mvc.config.BaseFormConfig; 22 import com.inversoft.verge.mvc.config.BaseFormConfigBuilder; 23 import com.inversoft.verge.mvc.controller.LongTxnSetup; 24 25 26 32 public class FormConfigBuilder extends BaseFormConfigBuilder { 33 34 ActionConfigBuilder actionConfigBuilder; 35 MappingConfigBuilder mappingConfigBuilder; 36 37 38 41 public FormConfigBuilder() { 42 } 43 44 45 49 public void setRegistry(ComponentConfigBuilderRegistry registry) { 50 super.setRegistry(registry); 51 actionConfigBuilder = 52 (ActionConfigBuilder) registry.lookup(Constants.ACTION_BUILDER_NAME); 53 mappingConfigBuilder = 54 (MappingConfigBuilder) registry.lookup(Constants.MAPPING_BUILDER_NAME); 55 } 56 57 67 public BaseConfig build(Element element) throws ConfigurationException { 68 69 ErrorList errors = new ErrorList(); 70 BaseFormConfig base = null; 71 try { 72 base = (BaseFormConfig) super.build(element); 73 } catch (ConfigurationException ce) { 74 errors.addErrorList(ce.getErrors()); 75 } 76 77 FormConfig formConfig = null; 78 if (errors.isEmpty()) { 79 formConfig = new FormConfig(base); 80 81 buildActions(element, formConfig, errors); 82 buildMappings(element, formConfig, errors); 83 } 84 85 String longTxnSetup = element.getAttributeValue( 86 Constants.LONG_TXN_SETUP_ATTRIBUTE); 87 LongTxnSetup longTxnSetupObj = null; 88 if (longTxnSetup != null) { 89 try { 90 longTxnSetupObj = 91 (LongTxnSetup) ReflectionTools.instantiate(longTxnSetup); 92 } catch (ReflectionException re) { 93 errors.addError("Unable to instantiate LongTxnSetup class: " + 94 longTxnSetup); 95 } catch (ClassCastException cce) { 96 errors.addError("Class: " + longTxnSetup + " not an instance of " + 97 "com.inversoft.verge.mvc.controller.LongTxnSetup"); 98 } 99 formConfig.setLongTxnSetup(longTxnSetupObj); 100 } 101 102 if (!errors.isEmpty()) { 103 throw new ConfigurationException(errors); 104 } 105 106 return formConfig; 107 } 108 109 113 void buildActions(Element element, FormConfig formConfig, ErrorList errors) { 114 115 Iterator iter = element.getChildren(Constants.ACTION_ELEMENT).iterator(); 118 Element actionElement; 119 ActionConfig actionConfig; 120 String actionName; 121 while (iter.hasNext()) { 122 actionElement = (Element) iter.next(); 123 try { 124 actionConfig = (ActionConfig) actionConfigBuilder.build(actionElement); 125 } catch (ConfigurationException ce) { 126 Iterator errorIter = ce.getErrors().iterator(); 127 BasicError error; 128 while (errorIter.hasNext()) { 129 error = (BasicError) errorIter.next(); 130 error = new BasicError("For form named: " 131 + formConfig.getName() + " -> " + error.getMessage()); 132 errors.addError(error); 133 } 134 continue; 135 } 136 137 actionName = actionConfig.getName(); 138 if (formConfig.getActionConfig(actionName) != null) { 139 errors.addError("action named: " + actionName + " is already" + 140 " defined for the form: " + formConfig.getName()); 141 } 142 143 formConfig.addActionConfig(actionName, actionConfig); 144 } 145 } 146 147 151 void buildMappings(Element element, FormConfig formConfig, ErrorList errors) { 152 153 Iterator iter = element.getChildren(Constants.MAPPING_ELEMENT).iterator(); 156 Element mappingElement; 157 MappingConfig mappingConfig; 158 String mappingName; 159 while (iter.hasNext()) { 160 mappingElement = (Element) iter.next(); 161 try { 162 mappingConfig = (MappingConfig) mappingConfigBuilder.build(mappingElement); 163 } catch (ConfigurationException ce) { 164 Iterator errorIter = ce.getErrors().iterator(); 165 BasicError error; 166 while (errorIter.hasNext()) { 167 error = (BasicError) errorIter.next(); 168 error = new BasicError("For form named: " 169 + formConfig.getName() + " -> " + error.getMessage()); 170 errors.addError(error); 171 } 172 continue; 173 } 174 175 mappingName = mappingConfig.getName(); 176 if (formConfig.getMappingConfig(mappingName) != null) { 177 errors.addError("mapping named: " + mappingName + " is already" + 178 " defined for the form: " + formConfig.getName()); 179 } 180 181 formConfig.addMappingConfig(mappingName, mappingConfig); 182 } 183 } 184 } | Popular Tags |