KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > event > handlers > ModelHandlers


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.event.handlers;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.model.Model;
28 import com.iplanet.jato.model.DefaultModel;
29
30 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
31 import com.sun.enterprise.tools.guiframework.model.ModelManager;
32
33 import java.util.Map JavaDoc;
34
35
36 /**
37  * This class defines both Source and Target mappings to and from a model.
38  */

39 public class ModelHandlers {
40
41     /**
42      * <p> Returns the Model from the Model Manager.</p>
43      *
44      * @param reqCtx The RequestContext
45      * @param handlerCtx The HandlerContext
46      */

47     public void getModel(RequestContext reqCtx, HandlerContext handlerCtx) {
48     // Pull out the required parameters
49
Object JavaDoc classname = handlerCtx.getInputValue(MODEL_CLASS);
50     if (classname == null) {
51         throw new IllegalArgumentException JavaDoc(
52         "The parameter map did not contain a parameter named '"+
53         MODEL_CLASS+"'!");
54     }
55         Object JavaDoc instanceName = handlerCtx.getInputValue(INSTANCE_NAME);
56     if (instanceName == null) {
57         throw new IllegalArgumentException JavaDoc(
58         "The parameter map did not contain a parameter named '"+
59         INSTANCE_NAME+"'!");
60     }
61
62     // Get the optional parameters...
63
boolean fromSession = false;
64     boolean toSession = false;
65     Boolean JavaDoc boolValue = (Boolean JavaDoc)handlerCtx.getInputValue(FROM_SESSION);
66     if (boolValue != null) {
67         fromSession = boolValue.booleanValue();
68     }
69     boolValue = (Boolean JavaDoc)handlerCtx.getInputValue(TO_SESSION);
70     if (boolValue != null) {
71         toSession = boolValue.booleanValue();
72     }
73
74     // Get the model class
75
if (!(classname instanceof Class JavaDoc)) {
76         try {
77         classname = Class.forName(classname.toString());
78         } catch (ClassNotFoundException JavaDoc ex) {
79         throw new RuntimeException JavaDoc(ex);
80         }
81     }
82
83     // Set the output value
84
handlerCtx.setOutputValue(VALUE,
85         reqCtx.getModelManager().getModel(
86         (Class JavaDoc)classname,
87         instanceName.toString(),
88         fromSession, toSession));
89     }
90
91
92     /**
93      * This target mapper saves a Model in the ModelManager
94      *
95      * @param reqCtx The RequestContext
96      * @param handlerCtx The HandlerContext
97      */

98     public void registerModel(RequestContext reqCtx, HandlerContext handlerCtx) {
99     // Get the instance name
100
Object JavaDoc instanceName = handlerCtx.getInputValue(INSTANCE_NAME);
101     if (instanceName == null) {
102         throw new IllegalArgumentException JavaDoc(
103         "The parameter map did not contain a parameter named '"+
104         INSTANCE_NAME+"'!");
105     }
106
107     // Store the model in the ModelManager
108
Model model = (Model)handlerCtx.getInputValue(MODEL);
109     ModelManager modelManager = (ModelManager)reqCtx.getModelManager();
110     modelManager.registerModel(instanceName.toString(), model);
111     }
112
113
114     /**
115      * <p> Returns the requested model value.</p>
116      *
117      * @param reqCtx The RequestContext
118      * @param handlerCtx The HandlerContext
119      */

120     public void getModelValue(RequestContext reqCtx, HandlerContext handlerCtx) {
121     // Make sure we got the key
122
Object JavaDoc key = handlerCtx.getInputValue(MODEL_KEY);
123     if (key == null) {
124         throw new IllegalArgumentException JavaDoc(
125         "The parameter map did not contain a parameter named '"+
126         MODEL_KEY+"'!");
127     }
128
129     // First check to see if they supplied the Model itself
130
Model model = (Model)handlerCtx.getInputValue(MODEL);
131     if (model == null) {
132         // Try executing the getModel() handler and using its result
133
getModel(reqCtx, handlerCtx);
134         model = (Model)handlerCtx.getOutputValue(VALUE);
135     }
136
137     // Set the output value
138
handlerCtx.setOutputValue(VALUE, model.getValue(key.toString()));
139     }
140
141     public void dumpModelValues(RequestContext reqCtx, HandlerContext handlerCtx) {
142     Model model = (Model)handlerCtx.getInputValue(MODEL);
143         if (model == null) {
144             getModel(reqCtx, handlerCtx);
145             model = (Model)handlerCtx.getOutputValue(VALUE);
146         }
147         if (model != null)
148             ((DefaultModel)model).dumpValues(System.out);
149     }
150     
151     /**
152      * This target mapper...
153      *
154      * @param reqCtx The RequestContext
155      * @param handlerCtx The HandlerContext
156      */

157     public void setModelValue(RequestContext reqCtx, HandlerContext handlerCtx) {
158     // Make sure we got the key
159
Object JavaDoc key = handlerCtx.getInputValue(MODEL_KEY);
160     if (key == null) {
161         throw new IllegalArgumentException JavaDoc(
162         "The parameter map did not contain a parameter named '"+
163         MODEL_KEY+"'!");
164     }
165
166     // Set the value
167
Object JavaDoc value = handlerCtx.getInputValue(VALUE);
168     Model model = (Model)handlerCtx.getInputValue(MODEL);
169     if (model != null) {
170         // Set the Model value
171
model.setValue(key.toString(), value);
172         return;
173     }
174
175     // Try executing the getModel() handler and using its result
176
getModel(reqCtx, handlerCtx);
177     model = (Model)handlerCtx.getOutputValue(VALUE);
178     model.setValue(key.toString(), value);
179     }
180
181
182     /**
183      *
184      */

185     public static final String JavaDoc VALUE = "value";
186
187     /**
188      * Parameter specifying whether to look in Session
189      */

190     public static final String JavaDoc FROM_SESSION = "modelFromSession";
191
192     /**
193      * Parameter specifying whether to store in Session
194      */

195     public static final String JavaDoc TO_SESSION = "modelToSession";
196
197     /**
198      * Parameter specifying the model class
199      */

200     public static final String JavaDoc MODEL_CLASS = "modelClass";
201
202     /**
203      * Parameter specifying the model instance name
204      */

205     public static final String JavaDoc INSTANCE_NAME = "instanceName";
206
207     /**
208      * The key used to retrieve a value from the model.
209      */

210     public static final String JavaDoc MODEL_KEY = "key";
211
212     /**
213      * Parameter used to pass in a model to getModelValue
214      */

215     public static final String JavaDoc MODEL = "model";
216 }
217
Popular Tags