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