KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > forms > ApplicationShortcutForm


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.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.ApplicationShortcut;
39 import com.sslexplorer.boot.PropertyDefinition;
40 import com.sslexplorer.boot.PropertyList;
41 import com.sslexplorer.extensions.ApplicationParameterDefinition;
42 import com.sslexplorer.extensions.ExtensionDescriptor;
43 import com.sslexplorer.extensions.ShortcutParameterItem;
44 import com.sslexplorer.extensions.store.ExtensionStore;
45 import com.sslexplorer.input.MultiSelectSelectionModel;
46 import com.sslexplorer.policyframework.Resource;
47 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm;
48 import com.sslexplorer.security.User;
49
50 /**
51  * Extension of
52  * {@link com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm}
53  * that allows editing of an <i>Application Shortcut</i>.
54  *
55  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
56  */

57 public class ApplicationShortcutForm extends AbstractFavoriteResourceForm {
58     static Log log = LogFactory.getLog(ApplicationShortcutForm.class);
59
60     // Private instance variables
61

62     private String JavaDoc selectedTab = "details";
63     private List JavaDoc<ShortcutParameterItem> parameterItems;
64     private List JavaDoc<String JavaDoc> categories;
65     private List JavaDoc<String JavaDoc> categoryTitles;
66     private Locale JavaDoc locale;
67
68     /**
69      * Get the list of {@link com.sslexplorer.extensions.ShortcutParameterItem}
70      * objects that are appropriate for the selected application.
71      *
72      * @return list of application shortcut parameter items
73      */

74     public List JavaDoc getParameterItems() {
75         return parameterItems;
76     }
77
78     /**
79      * Get a {@link com.sslexplorer.extensions.ShortcutParameterItem} from this
80      * list of objects that are appropriate for the selected application at the
81      * specified index.
82      *
83      * @param idx index of parameter
84      * @return application shortcut parameter item
85      */

86     public ShortcutParameterItem getParameterItem(int idx) {
87         return (ShortcutParameterItem) parameterItems.get(idx);
88     }
89
90     /**
91      * Get a list of category IDs as {@link String} objects.
92      *
93      * @return categories
94      */

95     public List JavaDoc getCategories() {
96         return categories;
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
103      * javax.servlet.http.HttpServletRequest)
104      */

105     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
106         ActionErrors errors = super.validate(mapping, request);
107         if (isCommiting()) {
108             for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
109                 ShortcutParameterItem item = (ShortcutParameterItem) i.next();
110                 try {
111                     ActionMessage err = item.validateItem();
112                     if (err != null) {
113                         if (errors == null) {
114                             errors = new ActionErrors();
115                         }
116                         errors.add(Globals.ERROR_KEY, err);
117                     }
118                 } catch (Exception JavaDoc e) {
119                     log.error("Failed to validate.", e);
120                     if (errors == null) {
121                         errors = new ActionErrors();
122                     }
123                     errors.add(Globals.ERROR_KEY, new ActionMessage("editApplicationShortcut.error.failedToValidate", e.getMessage()));
124                 }
125             }
126         }
127         return errors;
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping,
134      * javax.servlet.http.HttpServletRequest)
135      */

136     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
137         super.reset(mapping, request);
138         if (parameterItems != null) {
139             for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
140                 ShortcutParameterItem item = (ShortcutParameterItem) i.next();
141                 if (item.getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) {
142                     item.setValue(Boolean.FALSE.toString());
143                 } else if (item.getDefinition().getType() == PropertyDefinition.TYPE_LIST) {
144                     item.setValue(item.getDefinition().getDefaultValue());
145                 }
146             }
147         }
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see com.sslexplorer.tabs.TabModel#getTabCount()
154      */

155     public int getTabCount() {
156         return 2 + (categories == null ? 0 : categories.size());
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
163      */

164     public String JavaDoc getTabName(int idx) {
165         switch (idx) {
166             case 0:
167                 return "details";
168             case 1:
169                 return "policies";
170             default:
171                 return (String JavaDoc) categories.get(idx - 2);
172         }
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
179      */

180     public String JavaDoc getTabTitle(int idx) {
181         return idx < 2 || ((String JavaDoc) categoryTitles.get(idx - 2)).equals("") ? null : (String JavaDoc) categoryTitles.get(idx - 2);
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#initialise(com.sslexplorer.security.User,
188      * com.sslexplorer.boot.policyframework.Resource, boolean,
189      * com.sslexplorer.boot.MultiSelectSelectionModel,
190      * com.sslexplorer.boot.PropertyList, com.sslexplorer.security.User)
191      */

192     public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel,
193                             PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception JavaDoc {
194         super.initialise(user, resource, editing, policyModel, selectedPolicies, owner, assignOnly);
195
196         //
197
parameterItems = new ArrayList JavaDoc<ShortcutParameterItem>();
198
199         // Get the application selected in the previous step and retrieve all of
200
// the shortcut parameter items
201
ExtensionDescriptor des = ExtensionStore.getInstance()
202                         .getExtensionDescriptor(((ApplicationShortcut) resource).getApplication());
203         if (des == null) {
204             throw new Exception JavaDoc("No descriptor named " + ((ApplicationShortcut) resource).getApplication());
205         }
206
207         for (Iterator JavaDoc i = des.getParametersAndDefaults().entrySet().iterator(); i.hasNext();) {
208             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
209             ApplicationParameterDefinition def = (ApplicationParameterDefinition) entry.getValue();
210             if (!def.isHidden()) {
211                 try {
212                     String JavaDoc value = (String JavaDoc) ((ApplicationShortcut) resource).getParameters().get(def.getName());
213                     if (value == null) {
214                         value = def.getDefaultValue();
215                     }
216                     ShortcutParameterItem item = new ShortcutParameterItem(des, def, value, locale);
217                     if (log.isDebugEnabled())
218                         log.debug("Adding item " + item.getName());
219                     parameterItems.add(item);
220                 } catch (Exception JavaDoc e) {
221                     log.warn("Failed to create shortcut parameter for " + def.getName()
222                         + ". Probably a problem with the extension descriptor.", e);
223                 }
224             }
225         }
226         Collections.sort(parameterItems);
227
228         // Now we have a sorted list of parameter items, build up the list of
229
// categories
230
categories = new ArrayList JavaDoc<String JavaDoc>();
231         categoryTitles = new ArrayList JavaDoc<String JavaDoc>();
232         for (Iterator JavaDoc i = parameterItems.iterator(); i.hasNext();) {
233             ShortcutParameterItem spi = (ShortcutParameterItem) i.next();
234             String JavaDoc category = String.valueOf(spi.getCategory());
235             if (!categories.contains(category)) {
236                 categories.add(category);
237                 categoryTitles.add(spi.getLocalisedCategory());
238             }
239
240         }
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
247      */

248     public String JavaDoc getSelectedTab() {
249         return selectedTab;
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
256      */

257     public void setSelectedTab(String JavaDoc selectedTab) {
258         this.selectedTab = selectedTab;
259     }
260
261     /*
262      * (non-Javadoc)
263      *
264      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#applyToResource()
265      */

266     @SuppressWarnings JavaDoc("unchecked")
267     public void applyToResource() throws Exception JavaDoc {
268         ApplicationShortcut as = (ApplicationShortcut) getResource();
269         Map JavaDoc<String JavaDoc, String JavaDoc> parameterMap = as.getParameters();
270         parameterMap.clear();
271         for (Iterator JavaDoc i = getParameterItems().iterator(); i.hasNext();) {
272             ShortcutParameterItem pi = (ShortcutParameterItem) i.next();
273             parameterMap.put(pi.getName(), pi.getPropertyValue().toString());
274         }
275     }
276
277     /*
278      * (non-Javadoc)
279      *
280      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
281      */

282     public String JavaDoc getTabBundle(int idx) {
283         return null;
284     }
285
286     /**
287      * Set the locale to use for application shortcut parameter item names and
288      * descriptions. This must be called before
289      * {@link #initialise(User, Resource, boolean, MultiSelectSelectionModel, PropertyList, User, boolean)}
290      *
291      * @param locale locale
292      */

293     public void setLocale(Locale JavaDoc locale) {
294         this.locale = locale;
295     }
296 }
Popular Tags