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: UtilConfig.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.core.forms.validators.*;
038: import org.barracudamvc.core.util.l10n.*;
039: import org.barracudamvc.core.util.srv.*;
040: import org.barracudamvc.plankton.data.*;
041: import org.barracudamvc.plankton.http.*;
042:
043: /**
044: * This class provides the methods needed to configure the util
045: * screen
046: */
047: public class UtilConfig extends DefaultEventGateway {
048:
049: protected static final Logger logger = Logger
050: .getLogger(UtilConfig.class.getName());
051:
052: //form id (must be unique)
053: public static final String FORM = UtilConfig.class.getName()
054: + ".Form";
055:
056: //model name
057: public static final String MODEL_NAME = "Util";
058:
059: //model elements (these are the data items supported)
060: // public static final String DEBUG_DL = "Debug_DebugLevel";
061: public static final String LOCALES_DL = "Locales_DebugLevel";
062: // public static final String SERVLET_UTIL_DL = "ServletUtil_DebugLevel";
063: public static final String SESSION_SERVICES_DL = "SessionServices_DebugLevel";
064: // public static final String SESSION_SERVICES_DEFAULT_TIMEOUT = "SessionServices_DefaultTimeout";
065: public static final String SIMPLE_SERVICE_FINDER_DL = "SimpleServiceFinder_DebugLevel";
066: public static final String ERROR_MESSAGE = "ErrorMessage";
067: public static final String SUCCESS_MESSAGE = "SuccessMessage";
068: public static final String UPDATE_BUTTON = "UpdateButton";
069:
070: //private vars
071: private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {
072: public BaseEventListener getInstance() {
073: return new UpdateConfigHandler();
074: }
075:
076: public String getListenerID() {
077: return getID(UpdateConfigHandler.class);
078: }
079: };
080:
081: public UtilConfig() {
082: //specify generic interest
083: specifyLocalEventInterests(updateConfigFactory);
084: }
085:
086: //------------------------------------------------------------
087: // Data Models
088: //------------------------------------------------------------
089: /**
090: * define the template model that backs this screen
091: */
092: class UtilModel extends AbstractTemplateModel {
093:
094: //register the model by name
095: public String getName() {
096: return MODEL_NAME;
097: }
098:
099: //provide items by key
100: public Object getItem(String key) {
101: if (logger.isDebugEnabled())
102: logger.debug("Asking for key:" + key);
103: ViewContext vc = getViewContext();
104:
105: //Handle requests for data
106: if (key.equals(LOCALES_DL)) {
107: return ScreenUtil.getDebugLevelComp2(vc, key,
108: Locales.class);
109: } else if (key.equals(SESSION_SERVICES_DL)) {
110: return ScreenUtil.getDebugLevelComp2(vc, key,
111: SessionServices.class);
112: // } else if (key.equals(SESSION_SERVICES_DEFAULT_TIMEOUT)) {
113: // DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
114: // return new BInput(null, SESSION_SERVICES_DEFAULT_TIMEOUT, ""+SessionServices.DEFAULT_TIMEOUT, dlv, null);
115: } else if (key.equals(SIMPLE_SERVICE_FINDER_DL)) {
116: return ScreenUtil.getDebugLevelComp2(vc, key,
117: SimpleServiceFinder.class);
118: } else if (key.equals(ERROR_MESSAGE)) {
119: return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
120: } else if (key.equals(SUCCESS_MESSAGE)) {
121: return ScreenUtil.getSuccessMsg(vc, FORM,
122: SUCCESS_MESSAGE);
123: } else if (key.equals(UPDATE_BUTTON)) {
124: return ScreenUtil.getUpdateButtonComp(vc,
125: updateConfigFactory);
126: } else
127: return super .getItem(key);
128: }
129: }
130:
131: //------------------------------------------------------------
132: // Form Mappings, Validators
133: //------------------------------------------------------------
134: /**
135: * define the form map that backs the model
136: */
137: class UtilForm extends DefaultFormMap {
138: public UtilForm() {
139: //define the elements (note: these don't need any validators). Note
140: //also that we set the defaults to the current values. This allows
141: //us just to reset all current values down below without checking to
142: //see if the elements actually got passed in from the form.
143: if (logger.isDebugEnabled())
144: logger.debug("Defining form elements");
145: this
146: .defineElement(new DefaultFormElement(LOCALES_DL,
147: FormType.INTEGER, new Integer(ScreenUtil
148: .cvtLevelToInt(Locales.class)),
149: null, false));
150: this .defineElement(new DefaultFormElement(
151: SESSION_SERVICES_DL, FormType.INTEGER,
152: new Integer(ScreenUtil
153: .cvtLevelToInt(SessionServices.class)),
154: null, false));
155: // FormElement feTimeout = new DefaultFormElement(SESSION_SERVICES_DEFAULT_TIMEOUT, FormType.INTEGER, new Integer(SessionServices.DEFAULT_TIMEOUT), null, false);
156: // feTimeout.setValidator(new ValidTypeValidator("Invalid Timeout value! You must enter an integer value."));
157: // this.defineElement(feTimeout);
158: this .defineElement(new DefaultFormElement(
159: SIMPLE_SERVICE_FINDER_DL, FormType.INTEGER,
160: new Integer(ScreenUtil
161: .cvtLevelToInt(SimpleServiceFinder.class)),
162: null, false));
163: }
164: }
165:
166: //------------------------------------------------------------
167: // Model 2 - Controller Event Handlers
168: //------------------------------------------------------------
169: /**
170: * UpdateConfigHandler - handle the request to update the config
171: * screen.
172: */
173: class UpdateConfigHandler extends DefaultBaseEventListener {
174: public void handleControlEvent(ControlEventContext context)
175: throws EventException, ServletException, IOException {
176: //figure out our target locale and get the appropriate screen
177: Locale locale = context.getViewCapabilities()
178: .getClientLocale();
179: MasterScreen screen = new MasterScreenFactory()
180: .getInstance(UtilConfig.this , context, locale);
181: if (logger.isDebugEnabled())
182: ServletUtil.showParams(context.getRequest(), logger);
183:
184: //create the login form
185: ValidationException ve = null;
186: UtilForm formMap = new UtilForm();
187: try {
188: //map/validate the form
189: formMap.map(context.getRequest()).validate(true);
190:
191: //If there were no errors, update the data. Note that we just
192: //assume that all the data is here. The reason we can do this is
193: //because we prepopulated the form up above with the default values.
194: //The more proper way would be to check each value, see if it's set,
195: //and then only update it if the value has actually changed. Lot more
196: //code to do that though, and since we're just setting statics, the
197: //cost of doing it this way is minimal.
198: ScreenUtil.setLevel(Locales.class, ((Integer) formMap
199: .getVal(LOCALES_DL)).intValue());
200: ScreenUtil.setLevel(SessionServices.class,
201: ((Integer) formMap.getVal(SESSION_SERVICES_DL))
202: .intValue());
203: // SessionServices.DEFAULT_TIMEOUT = ((Integer)formMap.getVal(SESSION_SERVICES_DEFAULT_TIMEOUT)).intValue();
204: ScreenUtil.setLevel(SimpleServiceFinder.class,
205: ((Integer) formMap
206: .getVal(SIMPLE_SERVICE_FINDER_DL))
207: .intValue());
208:
209: //remember our success
210: formMap.putState(SUCCESS_MESSAGE, new Boolean(true));
211:
212: } catch (ValidationException e) {
213: ve = e;
214: }
215:
216: //store the error message in the form and then put the form in the
217: //the event context
218: formMap.putState(ERROR_MESSAGE, ve);
219: context.putState(FORM, formMap);
220:
221: //fire an update so that the screen will redraw
222: ((UtilModel) screen.utilModel).fireModelChanged();
223:
224: //redirect to the get screen again
225: throw new ClientSideRedirectException(new GetBConfig());
226: }
227: }
228:
229: //------------------------------------------------------------
230: // Utility Methods
231: //------------------------------------------------------------
232: public TemplateModel getModel() {
233: return new UtilModel();
234: }
235: }
|