001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site (http://www.enhydra.org/).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: FormsConfig.java,v 1.1 2006-09-11 12:30:02 sinisa Exp $
022: */
023: package org.enhydra.barracuda.config;
024:
025: import java.io.*;
026: import java.util.*;
027: import javax.servlet.*;
028: import javax.servlet.http.*;
029:
030: import org.apache.log4j.*;
031:
032: import org.enhydra.barracuda.config.events.*;
033: import org.barracudamvc.core.comp.*;
034: import org.barracudamvc.core.util.dom.*;
035: import org.barracudamvc.core.event.*;
036: import org.barracudamvc.core.forms.*;
037: import org.barracudamvc.plankton.data.*;
038: import org.barracudamvc.plankton.http.*;
039:
040: /**
041: * This class provides the methods needed to configure the forms
042: * screen
043: */
044: public class FormsConfig extends DefaultEventGateway {
045:
046: protected static final Logger logger = Logger
047: .getLogger(EventConfig.class.getName());
048:
049: //form id (must be unique)
050: public static final String FORM = FormsConfig.class.getName()
051: + ".Form";
052:
053: //model name
054: public static final String MODEL_NAME = "Forms";
055:
056: //model elements (these are the data items supported)
057: public static final String ABSTRACT_FORM_VALIDATOR_DL = "AbstractFormValidator_DebugLevel";
058: public static final String DEFAULT_FORM_MAP_DL = "DefaultFormMap_DebugLevel";
059: public static final String DEFAULT_FORM_ELEMENT_DL = "DefaultFormElement_DebugLevel";
060: public static final String DEFAULT_FORM_VALIDATOR_DL = "DefaultFormValidator_DebugLevel";
061: public static final String ERROR_MESSAGE = "ErrorMessage";
062: public static final String SUCCESS_MESSAGE = "SuccessMessage";
063: public static final String UPDATE_BUTTON = "UpdateButton";
064:
065: //private vars
066: private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {
067: public BaseEventListener getInstance() {
068: return new UpdateConfigHandler();
069: }
070:
071: public String getListenerID() {
072: return getID(UpdateConfigHandler.class);
073: }
074: };
075:
076: public FormsConfig() {
077: //specify generic interest
078: specifyLocalEventInterests(updateConfigFactory);
079: }
080:
081: //------------------------------------------------------------
082: // Data Models
083: //------------------------------------------------------------
084: /**
085: * define the template model that backs this screen
086: */
087: class FormsModel extends AbstractTemplateModel {
088:
089: //register the model by name
090: public String getName() {
091: return MODEL_NAME;
092: }
093:
094: //provide items by key
095: public Object getItem(String key) {
096: if (logger.isDebugEnabled())
097: logger.debug("Asking for key:" + key);
098: ViewContext vc = getViewContext();
099:
100: //Handle requests for data
101: if (key.equals(DEFAULT_FORM_MAP_DL)) {
102: return ScreenUtil.getDebugLevelComp2(vc, key,
103: DefaultFormMap.class);
104: } else if (key.equals(DEFAULT_FORM_ELEMENT_DL)) {
105: return ScreenUtil.getDebugLevelComp2(vc, key,
106: DefaultFormElement.class);
107: } else if (key.equals(ABSTRACT_FORM_VALIDATOR_DL)) {
108: return ScreenUtil.getDebugLevelComp2(vc, key,
109: AbstractFormValidator.class);
110: } else if (key.equals(DEFAULT_FORM_VALIDATOR_DL)) {
111: return ScreenUtil.getDebugLevelComp2(vc, key,
112: DefaultFormValidator.class);
113: } else if (key.equals(ERROR_MESSAGE)) {
114: return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
115: } else if (key.equals(SUCCESS_MESSAGE)) {
116: return ScreenUtil.getSuccessMsg(vc, FORM,
117: SUCCESS_MESSAGE);
118: } else if (key.equals(UPDATE_BUTTON)) {
119: return ScreenUtil.getUpdateButtonComp(vc,
120: updateConfigFactory);
121: } else
122: return super .getItem(key);
123: }
124: }
125:
126: //------------------------------------------------------------
127: // Form Mappings, Validators
128: //------------------------------------------------------------
129: /**
130: * define the form map that backs the model
131: */
132: class FormsForm extends DefaultFormMap {
133: public FormsForm() {
134: //define the elements (note: these don't need any validators). Note
135: //also that we set the defaults to the current values. This allows
136: //us just to reset all current values down below without checking to
137: //see if the elements actually got passed in from the form.
138: if (logger.isDebugEnabled())
139: logger.debug("Defining form elements");
140: this .defineElement(new DefaultFormElement(
141: DEFAULT_FORM_MAP_DL, FormType.INTEGER,
142: new Integer(ScreenUtil
143: .cvtLevelToInt(DefaultFormMap.class)),
144: null, false));
145: this .defineElement(new DefaultFormElement(
146: DEFAULT_FORM_ELEMENT_DL, FormType.INTEGER,
147: new Integer(ScreenUtil
148: .cvtLevelToInt(DefaultFormElement.class)),
149: null, false));
150: this
151: .defineElement(new DefaultFormElement(
152: ABSTRACT_FORM_VALIDATOR_DL,
153: FormType.INTEGER,
154: new Integer(
155: ScreenUtil
156: .cvtLevelToInt(AbstractFormValidator.class)),
157: null, false));
158: this
159: .defineElement(new DefaultFormElement(
160: DEFAULT_FORM_VALIDATOR_DL,
161: FormType.INTEGER,
162: new Integer(
163: ScreenUtil
164: .cvtLevelToInt(DefaultFormValidator.class)),
165: null, false));
166: }
167: }
168:
169: //------------------------------------------------------------
170: // Model 2 - Controller Event Handlers
171: //------------------------------------------------------------
172: /**
173: * UpdateConfigHandler - handle the request to update the config
174: * screen.
175: */
176: class UpdateConfigHandler extends DefaultBaseEventListener {
177: public void handleControlEvent(ControlEventContext context)
178: throws EventException, ServletException, IOException {
179: //figure out our target locale and get the appropriate screen
180: Locale locale = context.getViewCapabilities()
181: .getClientLocale();
182: MasterScreen screen = new MasterScreenFactory()
183: .getInstance(FormsConfig.this , context, locale);
184: if (logger.isDebugEnabled())
185: ServletUtil.showParams(context.getRequest(), logger);
186:
187: //create the login form
188: ValidationException ve = null;
189: FormsForm formMap = new FormsForm();
190: try {
191: //map/validate the form
192: formMap.map(context.getRequest()).validate(true);
193:
194: //If there were no errors, update the data. Note that we just
195: //assume that all the data is here. The reason we can do this is
196: //because we prepopulated the form up above with the default values.
197: //The more proper way would be to check each value, see if it's set,
198: //and then only update it if the value has actually changed. Lot more
199: //code to do that though, and since we're just setting statics, the
200: //cost of doing it this way is minimal.
201: ScreenUtil.setLevel(DefaultFormMap.class,
202: ((Integer) formMap.getVal(DEFAULT_FORM_MAP_DL))
203: .intValue());
204: ScreenUtil.setLevel(DefaultFormElement.class,
205: ((Integer) formMap
206: .getVal(DEFAULT_FORM_ELEMENT_DL))
207: .intValue());
208: ScreenUtil.setLevel(AbstractFormValidator.class,
209: ((Integer) formMap
210: .getVal(ABSTRACT_FORM_VALIDATOR_DL))
211: .intValue());
212: ScreenUtil.setLevel(DefaultFormValidator.class,
213: ((Integer) formMap
214: .getVal(DEFAULT_FORM_VALIDATOR_DL))
215: .intValue());
216:
217: //remember our success
218: formMap.putState(SUCCESS_MESSAGE, new Boolean(true));
219:
220: } catch (ValidationException e) {
221: ve = e;
222: }
223:
224: //store the error message in the form and then put the form in the
225: //the event context
226: formMap.putState(ERROR_MESSAGE, ve);
227: context.putState(FORM, formMap);
228:
229: //fire an update so that the screen will redraw
230: ((FormsModel) screen.formsModel).fireModelChanged();
231:
232: //redirect to the get screen again
233: throw new ClientSideRedirectException(new GetBConfig());
234: }
235: }
236:
237: //------------------------------------------------------------
238: // Utility Methods
239: //------------------------------------------------------------
240: public TemplateModel getModel() {
241: return new FormsModel();
242: }
243: }
|