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