KickJava   Java API By Example, From Geeks To Geeks.

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


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: DataConfig.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.event.*;
35 import org.enhydra.barracuda.core.forms.*;
36 import org.enhydra.barracuda.plankton.data.*;
37 import org.enhydra.barracuda.plankton.http.*;
38
39 /**
40  * This class provides the methods needed to configure the data
41  * screen
42  */

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

73     class DataModel extends AbstractTemplateModel {
74
75         //register the model by name
76
public String JavaDoc getName() {return MODEL_NAME;}
77         
78         //provide items by key
79
public Object JavaDoc getItem(String JavaDoc key) {
80             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
81             ViewContext vc = getViewContext();
82             
83             //Handle requests for data
84
if (key.equals(DEFAULT_STATE_MAP_DL)) {
85                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultStateMap.class);
86             } else if (key.equals(ERROR_MESSAGE)) {
87                 return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
88             } else if (key.equals(SUCCESS_MESSAGE)) {
89                 return ScreenUtil.getSuccessMsg(vc, FORM, SUCCESS_MESSAGE);
90             } else if (key.equals(UPDATE_BUTTON)) {
91                 return ScreenUtil.getUpdateButtonComp(vc, updateConfigFactory);
92             } else return super.getItem(key);
93         }
94     }
95
96     //------------------------------------------------------------
97
// Form Mappings, Validators
98
//------------------------------------------------------------
99
/**
100      * define the form map that backs the model
101      */

102     class DataForm extends DefaultFormMap {
103         public DataForm() {
104             //define the elements (note: these don't need any validators). Note
105
//also that we set the defaults to the current values. This allows
106
//us just to reset all current values down below without checking to
107
//see if the elements actually got passed in from the form.
108
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
109             this.defineElement(new DefaultFormElement(DEFAULT_STATE_MAP_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultStateMap.class)), null, false));
110         }
111     }
112
113     //------------------------------------------------------------
114
// Model 2 - Controller Event Handlers
115
//------------------------------------------------------------
116
/**
117      * UpdateConfigHandler - handle the request to update the config
118      * screen.
119      */

120     class UpdateConfigHandler extends DefaultBaseEventListener {
121         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
122             //figure out our target locale and get the appropriate screen
123
Locale locale = context.getViewCapabilities().getClientLocale();
124             MasterScreen screen = new MasterScreenFactory().getInstance(DataConfig.this, context, locale);
125             if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger);
126             
127             //create the login form
128
ValidationException ve = null;
129             DataForm formMap = new DataForm();
130             try {
131                 //map/validate the form
132
formMap.map(context.getRequest()).validate(true);
133                 
134                 //If there were no errors, update the data. Note that we just
135
//assume that all the data is here. The reason we can do this is
136
//because we prepopulated the form up above with the default values.
137
//The more proper way would be to check each value, see if it's set,
138
//and then only update it if the value has actually changed. Lot more
139
//code to do that though, and since we're just setting statics, the
140
//cost of doing it this way is minimal.
141
ScreenUtil.setLevel(DefaultStateMap.class, formMap.getIntegerVal(DEFAULT_STATE_MAP_DL).intValue());
142
143                 //remember our success
144
formMap.putState(SUCCESS_MESSAGE, new Boolean JavaDoc(true));
145
146             } catch (ValidationException e) {
147                 ve = e;
148             }
149
150             //store the error message in the form and then put the form in the
151
//the event context
152
formMap.putState(ERROR_MESSAGE, ve);
153             context.putState(FORM, formMap);
154
155             //fire an update so that the screen will redraw
156
((DataModel) screen.dataModel).fireModelChanged();
157
158             //redirect to the get screen again
159
throw new ClientSideRedirectException(new GetBConfig());
160         }
161     }
162
163     //------------------------------------------------------------
164
// Utility Methods
165
//------------------------------------------------------------
166
public TemplateModel getModel() {
167         return new DataModel();
168     }
169 }
170
171
Popular Tags