KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > attributes > wizards > forms > AttributeDefinitionOptionsForm


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.attributes.wizards.forms;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.ActionErrors;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30
31 import com.sslexplorer.boot.CodedException;
32 import com.sslexplorer.boot.DefaultPropertyDefinition;
33 import com.sslexplorer.boot.PropertyClassManager;
34 import com.sslexplorer.boot.PropertyDefinition;
35 import com.sslexplorer.core.CoreException;
36 import com.sslexplorer.properties.attributes.AttributeDefinition;
37 import com.sslexplorer.properties.attributes.AttributesPropertyClass;
38 import com.sslexplorer.properties.attributes.forms.AttributeDefinitionForm;
39 import com.sslexplorer.properties.attributes.wizards.actions.AttributeDefinitionDetailsAction;
40 import com.sslexplorer.properties.attributes.wizards.actions.AttributeDefinitionOptionsAction;
41 import com.sslexplorer.properties.impl.userattributes.UserAttributes;
42 import com.sslexplorer.wizard.AbstractWizardSequence;
43 import com.sslexplorer.wizard.forms.DefaultWizardForm;
44
45 /**
46  * Additional attribute options. These may be edited after
47  * the attribute has been created.
48  *
49  * @author brett
50  */

51 public class AttributeDefinitionOptionsForm extends DefaultWizardForm {
52
53     final static Log log = LogFactory.getLog(AttributeDefinitionOptionsForm.class);
54
55     // Protected instance variables
56

57     protected String JavaDoc label;
58     protected String JavaDoc category;
59     protected String JavaDoc typeMeta;
60     protected int type;
61     protected String JavaDoc validationString;
62     protected String JavaDoc defaultValue;
63     protected int visibility;
64     protected int sortOrder;
65     protected AttributesPropertyClass attributesClass;
66     
67     /**
68      * Constructor
69      */

70     public AttributeDefinitionOptionsForm() {
71         super(true, true, "/WEB-INF/jsp/content/properties/attributeDefinitionWizard/attributeDefinitionOptions.jspf", "type", true, false,
72                 "attributeDefinitionOptions", "properties", "attributeDefinitionWizard.attributeDefinitionOptions", 2);
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence,
79      * javax.servlet.http.HttpServletRequest)
80      */

81     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
82         super.init(sequence, request);
83         label = (String JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_LABEL, "");
84         category = (String JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_CATEGORY, "");
85         typeMeta = (String JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_TYPE_META, "");
86         type = ((Integer JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_TYPE, PropertyDefinition.TYPE_STRING)).intValue();
87         validationString = (String JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_VALIDATION_STRING, "com.sslexplorer.input.validators.StringValidator(minLength=0,maxLength=30,trim=true,regExp=,pattern=)");
88         defaultValue = (String JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_DEFAULT_VALUE, "");
89         visibility = ((Integer JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_VISIBILITY, AttributeDefinition.USER_USEABLE_ATTRIBUTE)).intValue();
90         sortOrder = ((Integer JavaDoc)sequence.getAttribute(AttributeDefinitionOptionsAction.ATTR_SORT_ORDER, 0)).intValue();
91         attributesClass = (AttributesPropertyClass)PropertyClassManager.getInstance().getPropertyClass((String JavaDoc)sequence.getAttribute(AttributeDefinitionDetailsAction.ATTR_CLASS, UserAttributes.NAME));
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
98      */

99     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
100         super.apply(sequence);
101         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_LABEL, label);
102         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_CATEGORY, category);
103         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_TYPE_META, typeMeta);
104         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_TYPE, new Integer JavaDoc(type));
105         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_VALIDATION_STRING, validationString);
106         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_DEFAULT_VALUE, defaultValue);
107         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_VISIBILITY, new Integer JavaDoc(visibility));
108         sequence.putAttribute(AttributeDefinitionOptionsAction.ATTR_SORT_ORDER, new Integer JavaDoc(sortOrder));
109     }
110     
111     /**
112      * Get the <i>Property Class</i> used for the selected <i>Attribute Class</i>
113      *
114      * @return attribute class
115      */

116     public AttributesPropertyClass getAttributesClass() {
117         return attributesClass;
118     }
119
120     /**
121      * Get the default value
122      *
123      * @return default value
124      */

125     public String JavaDoc getDefaultValue() {
126         return defaultValue;
127     }
128
129     /**
130      * Set the default value
131      *
132      * @param defaultValue default value
133      */

134     public void setDefaultValue(String JavaDoc defaultValue) {
135         this.defaultValue = defaultValue;
136     }
137
138     /**
139      * Get the label
140      *
141      * @return label
142      */

143     public String JavaDoc getLabel() {
144         return label;
145     }
146
147     /**
148      * Set the label
149      *
150      * @param label label
151      */

152     public void setLabel(String JavaDoc label) {
153         this.label = label;
154     }
155
156     /**
157      * Get type type meta.
158      *
159      * @return type meta.
160      */

161     public String JavaDoc getTypeMeta() {
162         return typeMeta;
163     }
164
165     /**
166      * Set type type meta.
167      *
168      * @param typeMeta type meta.
169      */

170     public void setTypeMeta(String JavaDoc typeMeta) {
171         this.typeMeta = typeMeta;
172     }
173
174     /**
175      * Get the category
176      *
177      * @return category
178      */

179     public String JavaDoc getCategory() {
180         return category;
181     }
182
183     /**
184      * Set the category
185      *
186      * @param category category
187      */

188     public void setCategory(String JavaDoc category) {
189         this.category = category;
190     }
191
192     /**
193      * Get the visibility. See {@link AttributeDefinitionForm} for
194      * the constants to use.
195      *
196      * @return visibility
197      */

198     public int getVisibility() {
199         return visibility;
200     }
201
202     /**
203      * Set the visibility. See {@link AttributeDefinitionForm} for
204      * the contants to use.
205      *
206      * @param visibility visibility
207      */

208     public void setVisibility(int visibility) {
209         this.visibility = visibility;
210     }
211     
212     /**
213      * Set the sort order
214      *
215      * @param sortOrder sort order
216      */

217     public void setSortOrder(int sortOrder) {
218         this.sortOrder = sortOrder;
219     }
220     
221     /**
222      * Get the sort order
223      *
224      * @return sort order
225      */

226     public int getSortOrder() {
227         return sortOrder;
228     }
229
230     /**
231      * Get the selected type.
232      *
233      * @return type
234      */

235     public int getType() {
236         return type;
237     }
238
239     /**
240      * Set the selected type
241      *
242      * @param type type
243      */

244     public void setType(int type) {
245         this.type = type;
246     }
247
248     /**
249      * Get the validation string
250      *
251      * @return validation string
252      * @see DefaultPropertyDefinition
253      */

254     public String JavaDoc getValidationString() {
255         return validationString;
256     }
257
258     /**
259      * Set the validation string
260      *
261      * @param validationString validation string
262      * @see DefaultPropertyDefinition
263      */

264     public void setValidationString(String JavaDoc validationString) {
265         this.validationString = validationString;
266     }
267
268
269     /* (non-Javadoc)
270      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
271      */

272     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
273         if(isCommiting()) {
274             ActionErrors errs = new ActionErrors();
275             if(!validationString.trim().equals("")) {
276                 String JavaDoc className = null;
277                 int idx = validationString.indexOf('(');
278                 if(idx == -1) {
279                     className = validationString;
280                 }
281                 else {
282                     if(!validationString.endsWith(")")) {
283                         errs = new ActionErrors();
284                         errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.invalidValidationStringFormat"));
285                     }
286                     className = validationString.substring(0, idx);
287                 }
288                 
289                 try {
290                     Class.forName(className);
291                 }
292                 catch(ClassNotFoundException JavaDoc cnfe) {
293                     errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.noSuchValidator", className));
294                 }
295                 
296                 try {
297                     // If there is a default value then validate it
298

299                     String JavaDoc defaultValue = getDefaultValue().trim();
300                     if(!defaultValue.equals("")) {
301                         AttributeDefinition def = getAttributesClass().createAttributeDefinition(type,
302                             (String JavaDoc)getWizardSequence().getAttribute(AttributeDefinitionDetailsAction.ATTR_NAME, null),
303                             typeMeta,
304                             -1,
305                             category,
306                             defaultValue,
307                             visibility,
308                             sortOrder,
309                             null,
310                             false,
311                             label,
312                             (String JavaDoc)getWizardSequence().getAttribute(AttributeDefinitionDetailsAction.ATTR_DESCRIPTION, null),
313                             false,
314                             true,
315                             validationString);
316                         try {
317                             def.validate(defaultValue, getClass().getClassLoader());
318                         } catch (CoreException ce) {
319                             ce.getBundleActionMessage().setArg3(def.getLabel());
320                             if (errs == null) {
321                                 errs = new ActionErrors();
322                             }
323                             errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
324                         }
325                     }
326                 }
327                 catch(Exception JavaDoc e) {
328                     if(errs == null) {
329                         errs = new ActionErrors();
330                     }
331                     errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.failedToValidate", className));
332                 }
333             }
334             return errs;
335         }
336         return null;
337     }
338 }
339
Popular Tags