KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > forms > AbstractPropertiesForm


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.properties.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionErrors;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionMessage;
36
37 import com.sslexplorer.boot.PropertyClass;
38 import com.sslexplorer.boot.PropertyDefinition;
39 import com.sslexplorer.core.CoreException;
40 import com.sslexplorer.core.CoreUtil;
41 import com.sslexplorer.core.EntryIterator;
42 import com.sslexplorer.core.forms.CoreForm;
43 import com.sslexplorer.properties.PropertyItem;
44
45 /**
46  * @author Brett Smith
47  */

48 public class AbstractPropertiesForm extends CoreForm implements PropertiesForm {
49
50     static Log log = LogFactory.getLog(AbstractPropertiesForm.class);
51
52     private PropertyItem[] propertyItems;
53     private List JavaDoc<PropertyClass> propertyClasses;
54     private String JavaDoc forwardTo;
55     private int parentCategory;
56     private String JavaDoc input;
57     private boolean redirect;
58     private String JavaDoc updateAction;
59     private List JavaDoc categoryDefinitions;
60     private int selectedCategory;
61     private HashMap JavaDoc store;
62     private int newSelectedCategory;
63     private List JavaDoc subCategories;
64     private List JavaDoc path;
65     private boolean supportsReplacementVariables;
66     
67     public AbstractPropertiesForm(List JavaDoc<PropertyClass> propertyClasses, boolean supportsReplacementVariables) {
68         this();
69         this.propertyClasses = propertyClasses;
70         this.supportsReplacementVariables = supportsReplacementVariables;
71     }
72
73     public AbstractPropertiesForm() {
74         super();
75         store = new HashMap JavaDoc();
76         path = new ArrayList JavaDoc();
77     }
78     
79     public boolean isSupportsReplacementVariables() {
80         return supportsReplacementVariables;
81     }
82
83     public boolean getEnabled() {
84         return true;
85     }
86
87     public String JavaDoc getForwardTo() {
88         return forwardTo;
89     }
90
91     public void setForwardTo(String JavaDoc forwardTo) {
92         this.forwardTo = forwardTo;
93     }
94
95     public String JavaDoc getInput() {
96         return input;
97     }
98
99     public void setInput(String JavaDoc input) {
100         this.input = input;
101     }
102
103     public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest JavaDoc request) {
104         if (log.isDebugEnabled())
105             log.debug("Reseting properties form");
106         super.reset(mapping, request);
107         if (propertyItems != null) {
108             for (int i = 0; i < propertyItems.length; i++) {
109                 if (propertyItems[i].getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) {
110                     propertyItems[i].setValue(Boolean.FALSE.toString());
111                 }
112                 else if (propertyItems[i].getDefinition().getType() == PropertyDefinition.TYPE_LIST) {
113                     propertyItems[i].setValue(propertyItems[i].getDefinition().getDefaultValue());
114                 }
115             }
116         }
117     }
118
119     public String JavaDoc getUpdateAction() {
120         return updateAction;
121     }
122
123     public PropertyItem[] getPropertyItems() {
124         return propertyItems;
125     }
126
127     public void setCategoryDefinitions(List JavaDoc categoryDefinitions) {
128         this.categoryDefinitions = categoryDefinitions;
129     }
130
131     public void setPropertyItems(PropertyItem[] propertyItems) {
132         this.propertyItems = propertyItems;
133         ;
134     }
135
136     public void setPropertyItem(int idx, PropertyItem item) {
137         propertyItems[idx] = item;
138     }
139
140     public PropertyItem getPropertyItem(int idx) {
141         return propertyItems[idx];
142     }
143
144     public void setPropertyClasses(List JavaDoc<PropertyClass> propertyClasses) {
145         this.propertyClasses = propertyClasses;
146     }
147
148     public List JavaDoc<PropertyClass> getPropertyClasses() {
149         return propertyClasses;
150     }
151
152     public void setParentCategory(int parentCategory) {
153         this.parentCategory = parentCategory;
154     }
155
156     public int getParentCategory() {
157         return parentCategory;
158     }
159
160     public void setRedirect(boolean redirect) {
161         this.redirect = redirect;
162     }
163
164     public boolean isRedirect() {
165         return redirect;
166     }
167
168     public void setUpdateAction(String JavaDoc updateAction) {
169         this.updateAction = updateAction;
170     }
171
172     public List JavaDoc getCategoryDefinitions() {
173         return categoryDefinitions;
174     }
175
176     public void setSelectedCategory(int selectedCategory) {
177         this.selectedCategory = selectedCategory;
178     }
179
180     public int getSelectedCategory() {
181         return selectedCategory;
182     }
183
184     public PropertyItem retrieveItem(String JavaDoc name, PropertyItem defaultValue) {
185         PropertyItem val = (PropertyItem) store.get(name);
186         return val == null ? defaultValue : val;
187     }
188
189     public void clearValues() {
190         store.clear();
191     }
192
193     public int getNewSelectedCategory() {
194         return newSelectedCategory;
195     }
196
197     public void setNewSelectedCategory(int newSelectedCategory) {
198         this.newSelectedCategory = newSelectedCategory;
199     }
200
201     public List JavaDoc getSubCategories() {
202         return subCategories;
203     }
204
205     public void setSubCategories(List JavaDoc subCategories) {
206         this.subCategories = subCategories;
207     }
208
209     public void pushCategory(int parentCategory) {
210         path.add(new Integer JavaDoc(parentCategory));
211     }
212
213     public int popCategory() {
214         if (path.size() > 0) {
215             int parentCategory = ((Integer JavaDoc) path.remove(path.size() - 1)).intValue();
216             return parentCategory;
217         }
218         return -1;
219     }
220
221     public void storeItems() {
222
223         PropertyItem[] items = getPropertyItems();
224         for (int i = 0; i < items.length; i++) {
225             PropertyDefinition def = items[i].getDefinition();
226             if (def.getCategory() == getSelectedCategory()) {
227                 storeItem(items[i]);
228             }
229         }
230
231     }
232
233     public void storeItem(PropertyItem item) {
234         store.put(item.getName(), item);
235     }
236
237     public Iterator JavaDoc storedItems() {
238         return new EntryIterator(store);
239     }
240
241     /* (non-Javadoc)
242      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
243      */

244     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
245         if (isCommiting()) {
246             ActionErrors errs = null;
247             //PropertyItem[] items = storedItems();//getPropertyItems();
248
//for (int i = 0; i < items.length; i++) {
249
storeItems();
250             for (Iterator JavaDoc i = storedItems(); i.hasNext();) {
251                 PropertyItem item = (PropertyItem) i.next();
252                 PropertyDefinition def = item.getDefinition();
253                 //PropertyDefinition def = items[i].getDefinition();
254
try {
255                     def.validate(String.valueOf(/*items[i]*/item.getPropertyValue()), getClass().getClassLoader());
256                 } catch (CoreException ce) {
257                     ce.getBundleActionMessage().setArg3(CoreUtil.getMessageResources(request.getSession(),
258                         def.getMessageResourcesKey()).getMessage((Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY),
259                         def.getName() + ".name"));
260                     if (errs == null) {
261                         errs = new ActionErrors();
262                     }
263                     errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
264                 } catch (Exception JavaDoc e) {
265                     errs.add(Globals.ERROR_KEY, new ActionMessage("properties.error.failedToValidate", e.getMessage()));
266                 }
267             }
268             if (errs != null)
269                 return errs;
270
271         }
272         return super.validate(mapping, request);
273     }
274 }
Popular Tags