KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > config > FormsConfig


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site (http://www.enhydra.org/).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: FormsConfig.java,v 1.1 2004/05/28 19:39:27 slobodan Exp $
22  */

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 /**
41  * This class provides the methods needed to configure the forms
42  * screen
43  */

44 public class FormsConfig extends DefaultEventGateway {
45
46     protected static final Logger logger = Logger.getLogger(EventConfig.class.getName());
47
48     //form id (must be unique)
49
public static final String JavaDoc FORM = FormsConfig.class.getName()+".Form";
50
51     //model name
52
public static final String JavaDoc MODEL_NAME = "Forms";
53
54     //model elements (these are the data items supported)
55
public static final String JavaDoc ABSTRACT_FORM_VALIDATOR_DL = "AbstractFormValidator_DebugLevel";
56     public static final String JavaDoc DEFAULT_FORM_MAP_DL = "DefaultFormMap_DebugLevel";
57     public static final String JavaDoc DEFAULT_FORM_ELEMENT_DL = "DefaultFormElement_DebugLevel";
58     public static final String JavaDoc DEFAULT_FORM_VALIDATOR_DL = "DefaultFormValidator_DebugLevel";
59     public static final String JavaDoc ERROR_MESSAGE = "ErrorMessage";
60     public static final String JavaDoc SUCCESS_MESSAGE = "SuccessMessage";
61     public static final String JavaDoc UPDATE_BUTTON = "UpdateButton";
62
63     //private vars
64
private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String JavaDoc getListenerID() {return getID(UpdateConfigHandler.class);}};
65
66     public FormsConfig() {
67         //specify generic interest
68
specifyLocalEventInterests(updateConfigFactory);
69     }
70
71     //------------------------------------------------------------
72
// Data Models
73
//------------------------------------------------------------
74
/**
75      * define the template model that backs this screen
76      */

77     class FormsModel extends AbstractTemplateModel {
78
79         //register the model by name
80
public String JavaDoc getName() {return MODEL_NAME;}
81         
82         //provide items by key
83
public Object JavaDoc getItem(String JavaDoc key) {
84             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
85             ViewContext vc = getViewContext();
86
87             //Handle requests for data
88
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     //------------------------------------------------------------
107
// Form Mappings, Validators
108
//------------------------------------------------------------
109
/**
110      * define the form map that backs the model
111      */

112     class FormsForm extends DefaultFormMap {
113         public FormsForm() {
114             //define the elements (note: these don't need any validators). Note
115
//also that we set the defaults to the current values. This allows
116
//us just to reset all current values down below without checking to
117
//see if the elements actually got passed in from the form.
118
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
119             this.defineElement(new DefaultFormElement(DEFAULT_FORM_MAP_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultFormMap.class)), null, false));
120             this.defineElement(new DefaultFormElement(DEFAULT_FORM_ELEMENT_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultFormElement.class)), null, false));
121             this.defineElement(new DefaultFormElement(ABSTRACT_FORM_VALIDATOR_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(AbstractFormValidator.class)), null, false));
122             this.defineElement(new DefaultFormElement(DEFAULT_FORM_VALIDATOR_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultFormValidator.class)), null, false));
123         }
124     }
125
126     //------------------------------------------------------------
127
// Model 2 - Controller Event Handlers
128
//------------------------------------------------------------
129
/**
130      * UpdateConfigHandler - handle the request to update the config
131      * screen.
132      */

133     class UpdateConfigHandler extends DefaultBaseEventListener {
134         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
135             //figure out our target locale and get the appropriate screen
136
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             //create the login form
141
ValidationException ve = null;
142             FormsForm formMap = new FormsForm();
143             try {
144                 //map/validate the form
145
formMap.map(context.getRequest()).validate(true);
146                 
147                 //If there were no errors, update the data. Note that we just
148
//assume that all the data is here. The reason we can do this is
149
//because we prepopulated the form up above with the default values.
150
//The more proper way would be to check each value, see if it's set,
151
//and then only update it if the value has actually changed. Lot more
152
//code to do that though, and since we're just setting statics, the
153
//cost of doing it this way is minimal.
154
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                 //remember our success
160
formMap.putState(SUCCESS_MESSAGE, new Boolean JavaDoc(true));
161
162             } catch (ValidationException e) {
163                 ve = e;
164             }
165
166             //store the error message in the form and then put the form in the
167
//the event context
168
formMap.putState(ERROR_MESSAGE, ve);
169             context.putState(FORM, formMap);
170
171             //fire an update so that the screen will redraw
172
((FormsModel) screen.formsModel).fireModelChanged();
173
174             //redirect to the get screen again
175
throw new ClientSideRedirectException(new GetBConfig());
176         }
177     }
178
179     //------------------------------------------------------------
180
// Utility Methods
181
//------------------------------------------------------------
182
public TemplateModel getModel() {
183         return new FormsModel();
184     }
185 }
186
187
Popular Tags