1 23 package org.enhydra.barracuda.config; 24 25 import java.io.*; 26 import java.util.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 import org.apache.log4j.*; 31 32 import org.enhydra.barracuda.config.events.*; 33 import org.enhydra.barracuda.core.comp.*; 34 import org.enhydra.barracuda.core.util.dom.*; 35 import org.enhydra.barracuda.core.event.*; 36 import org.enhydra.barracuda.core.forms.*; 37 import org.enhydra.barracuda.plankton.data.*; 38 import org.enhydra.barracuda.plankton.http.*; 39 40 44 public class FormsConfig extends DefaultEventGateway { 45 46 protected static final Logger logger = Logger.getLogger(EventConfig.class.getName()); 47 48 public static final String FORM = FormsConfig.class.getName()+".Form"; 50 51 public static final String MODEL_NAME = "Forms"; 53 54 public static final String ABSTRACT_FORM_VALIDATOR_DL = "AbstractFormValidator_DebugLevel"; 56 public static final String DEFAULT_FORM_MAP_DL = "DefaultFormMap_DebugLevel"; 57 public static final String DEFAULT_FORM_ELEMENT_DL = "DefaultFormElement_DebugLevel"; 58 public static final String DEFAULT_FORM_VALIDATOR_DL = "DefaultFormValidator_DebugLevel"; 59 public static final String ERROR_MESSAGE = "ErrorMessage"; 60 public static final String SUCCESS_MESSAGE = "SuccessMessage"; 61 public static final String UPDATE_BUTTON = "UpdateButton"; 62 63 private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String getListenerID() {return getID(UpdateConfigHandler.class);}}; 65 66 public FormsConfig() { 67 specifyLocalEventInterests(updateConfigFactory); 69 } 70 71 77 class FormsModel extends AbstractTemplateModel { 78 79 public String getName() {return MODEL_NAME;} 81 82 public Object getItem(String key) { 84 if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key); 85 ViewContext vc = getViewContext(); 86 87 if (key.equals(DEFAULT_FORM_MAP_DL)) { 89 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultFormMap.class); 90 } else if (key.equals(DEFAULT_FORM_ELEMENT_DL)) { 91 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultFormElement.class); 92 } else if (key.equals(ABSTRACT_FORM_VALIDATOR_DL)) { 93 return ScreenUtil.getDebugLevelComp2(vc, key, AbstractFormValidator.class); 94 } else if (key.equals(DEFAULT_FORM_VALIDATOR_DL)) { 95 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultFormValidator.class); 96 } else if (key.equals(ERROR_MESSAGE)) { 97 return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE); 98 } else if (key.equals(SUCCESS_MESSAGE)) { 99 return ScreenUtil.getSuccessMsg(vc, FORM, SUCCESS_MESSAGE); 100 } else if (key.equals(UPDATE_BUTTON)) { 101 return ScreenUtil.getUpdateButtonComp(vc, updateConfigFactory); 102 } else return super.getItem(key); 103 } 104 } 105 106 112 class FormsForm extends DefaultFormMap { 113 public FormsForm() { 114 if (logger.isDebugEnabled()) logger.debug("Defining form elements"); 119 this.defineElement(new DefaultFormElement(DEFAULT_FORM_MAP_DL, FormType.INTEGER, new Integer (ScreenUtil.cvtLevelToInt(DefaultFormMap.class)), null, false)); 120 this.defineElement(new DefaultFormElement(DEFAULT_FORM_ELEMENT_DL, FormType.INTEGER, new Integer (ScreenUtil.cvtLevelToInt(DefaultFormElement.class)), null, false)); 121 this.defineElement(new DefaultFormElement(ABSTRACT_FORM_VALIDATOR_DL, FormType.INTEGER, new Integer (ScreenUtil.cvtLevelToInt(AbstractFormValidator.class)), null, false)); 122 this.defineElement(new DefaultFormElement(DEFAULT_FORM_VALIDATOR_DL, FormType.INTEGER, new Integer (ScreenUtil.cvtLevelToInt(DefaultFormValidator.class)), null, false)); 123 } 124 } 125 126 133 class UpdateConfigHandler extends DefaultBaseEventListener { 134 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 135 Locale locale = context.getViewCapabilities().getClientLocale(); 137 MasterScreen screen = new MasterScreenFactory().getInstance(FormsConfig.this, context, locale); 138 if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger); 139 140 ValidationException ve = null; 142 FormsForm formMap = new FormsForm(); 143 try { 144 formMap.map(context.getRequest()).validate(true); 146 147 ScreenUtil.setLevel(DefaultFormMap.class, formMap.getIntegerVal(DEFAULT_FORM_MAP_DL).intValue()); 155 ScreenUtil.setLevel(DefaultFormElement.class, formMap.getIntegerVal(DEFAULT_FORM_ELEMENT_DL).intValue()); 156 ScreenUtil.setLevel(AbstractFormValidator.class, formMap.getIntegerVal(ABSTRACT_FORM_VALIDATOR_DL).intValue()); 157 ScreenUtil.setLevel(DefaultFormValidator.class, formMap.getIntegerVal(DEFAULT_FORM_VALIDATOR_DL).intValue()); 158 159 formMap.putState(SUCCESS_MESSAGE, new Boolean (true)); 161 162 } catch (ValidationException e) { 163 ve = e; 164 } 165 166 formMap.putState(ERROR_MESSAGE, ve); 169 context.putState(FORM, formMap); 170 171 ((FormsModel) screen.formsModel).fireModelChanged(); 173 174 throw new ClientSideRedirectException(new GetBConfig()); 176 } 177 } 178 179 public TemplateModel getModel() { 183 return new FormsModel(); 184 } 185 } 186 187 | Popular Tags |