KickJava   Java API By Example, From Geeks To Geeks.

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


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: DomConfig.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.http.*;
38
39 /**
40  * This class provides the methods needed to configure the dom
41  * screen
42  */

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

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

109     class DomForm extends DefaultFormMap {
110         public DomForm() {
111             //define the elements (note: these don't need any validators). Note
112
//also that we set the defaults to the current values. This allows
113
//us just to reset all current values down below without checking to
114
//see if the elements actually got passed in from the form.
115
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
116             this.defineElement(new DefaultFormElement(DEFAULT_DOM_LOADER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultDOMLoader.class)), null, false));
117             this.defineElement(new DefaultFormElement(DEFAULT_DOM_WRITER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultDOMWriter.class)), null, false));
118             this.defineElement(new DefaultFormElement(DEFAULT_DOM_WRITER_DPP, FormType.BOOLEAN, new Boolean JavaDoc(false), null, false)); //if a checkbox element is not present its equiv to being false
119
}
120     }
121
122     //------------------------------------------------------------
123
// Model 2 - Controller Event Handlers
124
//------------------------------------------------------------
125
/**
126      * UpdateConfigHandler - handle the request to update the config
127      * screen.
128      */

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