KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > wizards > forms > ApplicationShortcutWizardAdditionalDetailsForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.applications.wizards.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.struts.Globals;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37
38 import com.sslexplorer.applications.ApplicationsPlugin;
39 import com.sslexplorer.boot.PropertyDefinition;
40 import com.sslexplorer.extensions.ApplicationParameterDefinition;
41 import com.sslexplorer.extensions.ExtensionDescriptor;
42 import com.sslexplorer.extensions.ShortcutParameterItem;
43 import com.sslexplorer.extensions.store.ExtensionStore;
44 import com.sslexplorer.tabs.TabModel;
45 import com.sslexplorer.wizard.AbstractWizardSequence;
46 import com.sslexplorer.wizard.forms.DefaultWizardForm;
47
48 /**
49  * Extension of a {@link com.sslexplorer.wizard.forms.DefaultWizardForm} that
50  * allows an administrator to enter the application shortcut parameters for the
51  * selected application.
52  * <p>
53  * The list of available parameters is also categorised. This allows the UI to
54  * organise the items (into tabs in the default UI).
55  *
56  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
57  */

58 public class ApplicationShortcutWizardAdditionalDetailsForm extends DefaultWizardForm implements TabModel {
59
60     final static Log log = LogFactory.getLog(ApplicationShortcutWizardAdditionalDetailsForm.class);
61
62     /**
63      * Key used to store the map of applicaiton shortcut parameters
64      */

65     public final static String JavaDoc ATTR_PARAMETERS = "parameters";
66     // Private instance variables
67

68     private List JavaDoc<ShortcutParameterItem> parameterItems;
69     private List JavaDoc<String JavaDoc> categories;
70     private List JavaDoc<String JavaDoc> categoryTitles;
71     private String JavaDoc selectedTab;
72
73     /**
74      * Constructor
75      */

76     public ApplicationShortcutWizardAdditionalDetailsForm() {
77         super(true,
78                         true,
79                         "/WEB-INF/jsp/content/applications/applicationShortcutWizard/additionalDetails.jspf",
80                         "resourceName",
81                         true,
82                         false,
83                         "applicationShortcutAdditionalDetails",
84                         ApplicationsPlugin.MESSAGE_RESOURCES_KEY,
85                         "applicationShortcutWizard.applicationShortcutAdditionalDetails",
86                         3);
87     }
88
89     /**
90      * Get the list of {@link com.sslexplorer.extensions.ShortcutParameterItem}
91      * objects that are appropriate for the selected application.
92      *
93      * @return list of application shortcut parameter items
94      */

95     public List JavaDoc getParameterItems() {
96         return parameterItems;
97     }
98
99     /**
100      * Get a {@link com.sslexplorer.extensions.ShortcutParameterItem} from this
101      * list of objects that are appropriate for the selected application at the
102      * specified index.
103      *
104      * @param idx index of parameter
105      * @return application shortcut parameter item
106      */

107     public ShortcutParameterItem getParameterItem(int idx) {
108         return (ShortcutParameterItem) parameterItems.get(idx);
109     }
110
111     /**
112      * Get a list of category IDs as {@link String} objects.
113      *
114      * @return categories
115      */

116     public List JavaDoc getCategories() {
117         return categories;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence,
124      * javax.servlet.http.HttpServletRequest)
125      */

126     @SuppressWarnings JavaDoc("unchecked")
127     public void init(AbstractWizardSequence wizardSequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
128         super.init(wizardSequence, request);
129
130         //
131
parameterItems = (List JavaDoc<ShortcutParameterItem>) wizardSequence.getAttribute(ATTR_PARAMETERS, null);
132
133         // Get the application selected in the previous step and retrieve all of
134
// the shortcut parameter items
135
ExtensionDescriptor des = ExtensionStore.getInstance()
136                         .getExtensionDescriptor((String JavaDoc) wizardSequence.getAttribute(ApplicationShortcutWizardApplicationForm.ATTR_SELECTED_APPLICATION,
137                             null));
138         if (parameterItems == null) {
139             parameterItems = new ArrayList JavaDoc<ShortcutParameterItem>();
140             for (Iterator JavaDoc i = des.getParametersAndDefaults().entrySet().iterator(); i.hasNext();) {
141                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
142                 ApplicationParameterDefinition def = (ApplicationParameterDefinition) entry.getValue();
143                 if (!def.isHidden()) {
144                     ShortcutParameterItem item = new ShortcutParameterItem(des,
145                                     def,
146                                     def.getDefaultValue().equals(PropertyDefinition.UNDEFINED_PARAMETER) ? ""
147                                         : def.getDefaultValue(),
148                                     (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY));
149                     if (log.isDebugEnabled())
150                         log.debug("Adding item " + item.getName());
151                     parameterItems.add(item);
152                 }
153             }
154             Collections.sort(parameterItems);
155
156             // Now we have a sorted list of parameter items, build up the list
157
// of categories
158
categories = new ArrayList JavaDoc<String JavaDoc>();
159             categoryTitles = new ArrayList JavaDoc<String JavaDoc>();
160             for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
161                 ShortcutParameterItem spi = (ShortcutParameterItem) i.next();
162                 String JavaDoc category = String.valueOf(spi.getCategory());
163                 if (!categories.contains(category)) {
164                     categories.add(category);
165                     categoryTitles.add(spi.getLocalisedCategory());
166                 }
167
168             }
169         }
170     }
171
172     /*
173      * (non-Javadoc)
174      *
175      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
176      * javax.servlet.http.HttpServletRequest)
177      */

178     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
179         ActionErrors errors = null;
180         if (isCommiting()) {
181             for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
182                 ShortcutParameterItem item = (ShortcutParameterItem) i.next();
183                 try {
184                     ActionMessage err = item.validateItem();
185                     if (err != null) {
186                         if (errors == null) {
187                             errors = new ActionErrors();
188                         }
189                         errors.add(Globals.ERROR_KEY, err);
190                     }
191                 } catch (Exception JavaDoc e) {
192                     log.error("Failed to validate.", e);
193                     if (errors == null) {
194                         errors = new ActionErrors();
195                     }
196                     errors.add(Globals.ERROR_KEY, new ActionMessage("editApplicationShortcut.error.failedToValidate", e.getMessage()));
197                 }
198             }
199         }
200         return errors;
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping,
207      * javax.servlet.http.HttpServletRequest)
208      */

209     public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest JavaDoc request) {
210         if (parameterItems != null) {
211             for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
212                 ShortcutParameterItem item = (ShortcutParameterItem) i.next();
213                 if (item.getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) {
214                     item.setValue(Boolean.FALSE.toString());
215                 } else if (item.getDefinition().getType() == PropertyDefinition.TYPE_LIST) {
216                     item.setValue(item.getDefinition().getDefaultValue());
217                 }
218             }
219         }
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see com.sslexplorer.tabs.TabModel#getTabCount()
226      */

227     public int getTabCount() {
228         return categories.size();
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
235      */

236     public String JavaDoc getTabName(int idx) {
237         return (String JavaDoc) categories.get(idx);
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
244      */

245     public String JavaDoc getTabTitle(int idx) {
246         String JavaDoc title = (String JavaDoc) categoryTitles.get(idx);
247         return title.equals("") ? null : title;
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
254      */

255     public String JavaDoc getSelectedTab() {
256         return selectedTab;
257     }
258
259     /*
260      * (non-Javadoc)
261      *
262      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
263      */

264     public void setSelectedTab(String JavaDoc selectedTab) {
265         this.selectedTab = selectedTab;
266     }
267
268     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
269         super.apply(sequence);
270         sequence.putAttribute(ATTR_PARAMETERS, parameterItems);
271     }
272
273     /*
274      * (non-Javadoc)
275      *
276      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
277      */

278     public String JavaDoc getTabBundle(int idx) {
279         return null;
280     }
281 }
282
Popular Tags