KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > WizardHandlers


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 /*
25  * WizardHandlers.java
26  *
27  * Created on October 6, 2003, 10:10 AM
28  */

29
30 package com.sun.enterprise.tools.admingui.handlers;
31
32 import com.iplanet.jato.ModelManager;
33 import com.iplanet.jato.RequestContext;
34 import com.iplanet.jato.RequestContextImpl;
35 import com.iplanet.jato.RequestManager;
36 import com.iplanet.jato.model.DefaultModel;
37 import com.iplanet.jato.model.Model;
38 import com.iplanet.jato.model.ModelControlException;
39 import com.iplanet.jato.view.View;
40 import com.iplanet.jato.view.ViewBean;
41
42 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
43 import com.sun.enterprise.tools.admingui.util.Util;
44
45 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
46 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
47 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
48 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
49 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
50 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
51 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
52
53 import com.sun.web.ui.model.CCActionTableModel;
54 import com.sun.web.ui.model.CCActionTableModelInterface;
55 import com.sun.web.ui.model.CCPropertySheetModelInterface;
56
57 import java.util.ArrayList JavaDoc;
58 import java.util.EventObject JavaDoc;
59 import java.util.List JavaDoc;
60
61 import javax.management.MBeanException JavaDoc;
62 import javax.management.ObjectName JavaDoc;
63 import javax.management.AttributeList JavaDoc;
64 import javax.management.Attribute JavaDoc;
65 import javax.servlet.ServletRequest JavaDoc;
66 import javax.servlet.http.HttpServletRequest JavaDoc;
67 import javax.servlet.http.HttpSession JavaDoc;
68
69
70 /**
71  *
72  */

73 public class WizardHandlers {
74
75     /**
76      *
77      */

78     public void create(RequestContext ctx, HandlerContext handlerCtx) {
79         DescriptorContainerView descView = (DescriptorContainerView)ctx.getRequest().getAttribute("wizardView");
80         ArrayList JavaDoc displayNames = (ArrayList JavaDoc)ctx.getRequest().getAttribute("displayNames");
81         ArrayList JavaDoc modelNames = (ArrayList JavaDoc)ctx.getRequest().getAttribute("modelNames");
82         String JavaDoc objectName = (String JavaDoc)ctx.getRequest().getAttribute("objectName");
83         String JavaDoc methodName = (String JavaDoc)ctx.getRequest().getAttribute("methodName");
84         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
85
86         for (int i = 0; i < displayNames.size(); i++) {
87             Attribute JavaDoc attr = new Attribute JavaDoc((String JavaDoc)modelNames.get(i),
88                 (String JavaDoc)descView.getDisplayFieldValue(((String JavaDoc)(displayNames.get(i)))));
89             attrList.add(attr);
90         }
91
92         Object JavaDoc params[] = {attrList};
93         String JavaDoc types[] = {"javax.management.AttributeList"};
94         try {
95         Object JavaDoc obj = MBeanUtil.invoke(objectName, methodName, params, types);
96         ObjectName JavaDoc objName = (ObjectName JavaDoc)obj;
97     if (Util.isLoggableFINEST()) {
98         Util.logFINEST("In create object name is: "+objName.toString());
99     }
100         ctx.getRequest().setAttribute(PROPS_OBJECT_NAME, objName.toString());
101         } catch (Exception JavaDoc ex) {
102             throw new FrameworkException(
103         ex, descView.getViewDescriptor(), handlerCtx.getView());
104         }
105     }
106
107
108     /**
109      *
110      */

111     public void saveProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
112         DescriptorContainerView descView = (DescriptorContainerView)ctx.getRequest().getAttribute("wizardView");
113
114         CCActionTableModelInterface model =
115             (CCActionTableModelInterface)ctx.getRequest().getAttribute("wizardTableModel");
116         String JavaDoc objectName = (String JavaDoc)ctx.getRequest().getAttribute(PROPS_OBJECT_NAME);
117     if (Util.isLoggableFINEST()) {
118         Util.logFINEST("In save object name is: "+objectName);
119     }
120         model.setRowSelectionType("multiple");
121         model.beforeFirst();
122         AttributeList JavaDoc attrs = new AttributeList JavaDoc();
123         String JavaDoc[] type = new String JavaDoc[]{"javax.management.Attribute"};
124         try {
125             while(model.next()) {
126                 String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
127                 String JavaDoc value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
128                 if (name != null && (!name.trim().equals(""))) {
129                     Object JavaDoc params[] = {new Attribute JavaDoc(name, value)};
130                     MBeanUtil.invoke(objectName, "setProperty", params, type);
131                 }
132             }
133         } catch (Exception JavaDoc ex) {
134             throw new FrameworkException(
135         ex, descView.getViewDescriptor(), handlerCtx.getView());
136         }
137     }
138
139
140     /**
141      *
142      */

143     public void isTableLoaded(RequestContext ctx, HandlerContext handlerCtx) {
144         boolean tL = false;
145         if (ctx.getRequest().getAttribute(TABLE_LOAD) != null){
146             tL = new Boolean JavaDoc(""+ctx.getRequest().getAttribute(TABLE_LOAD)).booleanValue();
147         }
148
149         if(tL) {
150             ctx.getRequest().setAttribute(TABLE_LOAD, "true");
151         } else {
152             ctx.getRequest().setAttribute(TABLE_LOAD, "false");
153         }
154
155     }
156
157
158     /**
159      *
160      */

161     public void addProperty(RequestContext ctx, HandlerContext handlerCtx) {
162     ServletRequest JavaDoc req = ctx.getRequest();
163
164         DescriptorContainerView descView = (DescriptorContainerView)req.getAttribute("property.child");
165
166     if (descView == null) {
167         throw new FrameworkException(
168         "addPropertyTest: 'child' attributes were null!", null, handlerCtx.getView());
169     }
170
171         CCActionTableModelInterface model = (CCActionTableModelInterface)req.getAttribute(PROP_MODEL);
172     if (Util.isLoggableFINEST()) {
173         Util.logFINEST("**MODEL IS: "+model);
174     }
175
176         //model.setRowSelectionType("multiple");
177
try {
178         if ((model != null) && (model instanceof DefaultModel)) {
179                 model.last();
180
181         if (Util.isLoggableFINEST()) {
182             // Log some stuff...
183
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
184             buf.append("MODEL IS NOT NULL:\n");
185             buf.append("**Number of rows: "+model.getNumRows()+"***");
186             buf.append("**Last row index: "+((CCActionTableModel)model).getLastRowIndex()+"***");
187             Util.logFINEST(buf.toString());
188         }
189
190                 model.appendRow();
191                 ((CCActionTableModel)model).sort();
192                 model.setValue(PROPERTY_NAME, "");
193                 model.setValue(PROPERTY_VALUE, "");
194         if (Util.isLoggableFINEST()) {
195             Util.logFINEST("***Number of rows: "+model.getNumRows()+"***");
196         }
197                 model.beforeFirst();
198             } else {
199         if (Util.isLoggableFINEST()) {
200             Util.logFINEST("MODEL IS NULL");
201         }
202             }
203         } catch (Exception JavaDoc ex) {
204         throw new FrameworkException(
205         ex, descView.getViewDescriptor(), handlerCtx.getView());
206     }
207         descView.forwardTo(ctx);
208     }
209
210
211     /**
212      *
213      */

214     public void deleteProperties(RequestContext ctx, HandlerContext handlerCtx) {
215         // Get the ServletRequest
216
ServletRequest JavaDoc req = ctx.getRequest();
217         DescriptorContainerView descView = (DescriptorContainerView)req.getAttribute("property.child");
218
219         CCActionTableModelInterface model = (CCActionTableModelInterface)req.getAttribute(PROP_MODEL);
220     if (Util.isLoggableFINEST()) {
221         Util.logFINEST("Model name is: "+model);
222     }
223         model.setRowSelectionType("multiple");
224
225     try {
226         model.beforeFirst();
227         AttributeList JavaDoc attrs = new AttributeList JavaDoc();
228
229         while(model.next()) {
230         if (!model.isRowSelected()) {
231             String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
232             String JavaDoc value = (String JavaDoc)model.getValue(PROPERTY_VALUE);
233             if (Util.isLoggableFINEST()) {
234             Util.logFINEST("Property '"+name+"' with value '"+value+"' not selected.");
235             }
236
237             if (name != null && (! name.trim().equals(""))) {
238             attrs.add(new Attribute JavaDoc(name, value));
239             }
240         }
241         }
242         ((DefaultModel)model).clear();
243         if (attrs != null) {
244         for (int props=0; props<attrs.size(); props++) {
245             Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(props);
246             Object JavaDoc attrValue = attr.getValue();
247             String JavaDoc attrName = attr.getName();
248             model.appendRow();
249             ((CCActionTableModel)model).sort();
250             model.setValue(PROPERTY_NAME, attr.getName());
251             model.setValue(PROPERTY_VALUE, attr.getValue());
252             model.setRowSelected(false);
253         }
254         }
255     } catch (Exception JavaDoc ex) {
256         throw new FrameworkException(
257         ex, descView.getViewDescriptor(), handlerCtx.getView());
258     }
259     }
260
261
262     /**
263      * This method iterates over an array of Model names. For each model
264      * name, it clears the Model (in the case of DefaultModels) and checks
265      * to see if the model exists in Session. If it does exist in Session,
266      * it will be removed from session. This method expects MODEL_NAMES
267      * ("modelNames") to be a request Attribute specifying the Model names
268      * to remove.
269      */

270     public void clearModels(RequestContext ctx, HandlerContext handlerCtx) {
271         // Get the array of Strings
272
HttpServletRequest JavaDoc req = ctx.getRequest();
273     Object JavaDoc obj = req.getAttribute(MODEL_NAMES);
274     if (obj == null) {
275         throw new FrameworkException(
276         "You must specifiy '"+MODEL_NAMES+"' to remove from Session.");
277     }
278     if (obj instanceof String JavaDoc) {
279         ArrayList JavaDoc list = new ArrayList JavaDoc();
280         list.add(obj);
281         obj = list;
282     }
283     if (!(obj instanceof List JavaDoc)) {
284         throw new FrameworkException(
285         "'"+MODEL_NAMES+"' must be a java.util.List!");
286     }
287     String JavaDoc names[] =
288         (String JavaDoc [])((List JavaDoc)obj).toArray(new String JavaDoc[((List JavaDoc)obj).size()]);
289
290     // Check and remove each model as necessary.
291
HttpSession JavaDoc session = req.getSession();
292     ModelManager modelMgr = ctx.getModelManager();
293     Model model = null;
294     for (int count=0; count<names.length; count++) {
295         obj = session.getAttribute(names[count]);
296         if (obj != null) {
297         model = (Model)obj;
298         if (model instanceof DefaultModel) {
299             // First clear() the model b/c the model on the next
300
// page will still be in memory and needs to be reset.
301
((DefaultModel)model).clear();
302         }
303         modelMgr.removeFromSession(model);
304         }
305     }
306     }
307
308
309     /**
310      * This is the attribute name that refers to the model names to delete.
311      */

312     public static final String JavaDoc MODEL_NAMES = "clearModels.modelNames";
313     public static final String JavaDoc PROPERTY_NAME = "propertyName";
314     public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
315     public static final String JavaDoc PROPS_OBJECT_NAME = "resourceName";
316     public static final String JavaDoc TABLE_LOAD = "wizard.loadTable";
317     public static final String JavaDoc PROP_MODEL = "property.model";
318     public static final String JavaDoc PROP_CHILD = "property.child";
319 }
320
Popular Tags