KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > attributes > forms > AttributeDefinitionForm


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.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34
35 import com.sslexplorer.boot.CodedException;
36 import com.sslexplorer.boot.DefaultPropertyDefinition;
37 import com.sslexplorer.boot.PropertyClass;
38 import com.sslexplorer.boot.PropertyClassManager;
39 import com.sslexplorer.core.CoreException;
40 import com.sslexplorer.core.forms.CoreForm;
41 import com.sslexplorer.properties.attributes.AttributeDefinition;
42 import com.sslexplorer.properties.attributes.AttributesPropertyClass;
43 import com.sslexplorer.properties.impl.userattributes.UserAttributes;
44
45
46 /**
47  * Implementation of a {@link com.sslexplorer.core.forms.CoreForm}
48  * that allows an administrator to edit an <i>Attribute Definition</i>.
49  *
50  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
51  * @see com.sslexplorer.security.AuthenticationScheme
52  */

53 public class AttributeDefinitionForm extends CoreForm {
54     
55     final static Log log = LogFactory.getLog(AttributeDefinitionForm.class);
56     
57     // Protected instance variables
58

59     protected String JavaDoc name;
60     protected String JavaDoc label;
61     protected String JavaDoc category;
62     protected String JavaDoc typeMeta;
63     protected int type;
64     protected String JavaDoc validationString;
65     protected String JavaDoc description;
66     protected String JavaDoc defaultValue;
67     protected int visibility;
68     protected int sortOrder;
69     protected AttributeDefinition definition;
70     
71     /**
72      * Initialise the form
73      *
74      * @param definition definition
75      */

76     public void initialise(AttributeDefinition definition) {
77         this.definition = definition;
78         this.name = definition.getName();
79         this.label = definition.getLabel();
80         this.visibility = definition.getVisibility();
81         this.defaultValue = definition.getDefaultValue();
82         this.sortOrder = definition.getSortOrder();
83         this.description = definition.getDescription();
84         this.category = definition.getCategoryLabel();
85         this.type = definition.getType();
86         this.validationString = definition.getValidationString();
87         this.typeMeta = definition.getTypeMeta();
88     }
89     
90     /**
91      * Get a list of available attribute property clases
92      *
93      * @return attribute property classes
94      */

95     public Collection JavaDoc<AttributesPropertyClass> getAttributePropertyClasses() {
96         List JavaDoc<AttributesPropertyClass> l = new ArrayList JavaDoc<AttributesPropertyClass>();
97         for(PropertyClass propertyClass : PropertyClassManager.getInstance().getPropertyClasses()) {
98             if(propertyClass instanceof AttributesPropertyClass) {
99                 l.add((AttributesPropertyClass)propertyClass);
100             }
101         }
102         return l;
103     }
104
105     /**
106      * Get the default value
107      *
108      * @return default value
109      */

110     public String JavaDoc getDefaultValue() {
111         return defaultValue;
112     }
113
114     /**
115      * Set the default value
116      *
117      * @param defaultValue default value
118      */

119     public void setDefaultValue(String JavaDoc defaultValue) {
120         this.defaultValue = defaultValue;
121     }
122
123     /**
124      * Get the label
125      *
126      * @return label
127      */

128     public String JavaDoc getLabel() {
129         return label;
130     }
131
132     /**
133      * Set the label
134      *
135      * @param label label
136      */

137     public void setLabel(String JavaDoc label) {
138         this.label = label;
139     }
140
141     /**
142      * Get the description
143      *
144      * @return description
145      */

146     public String JavaDoc getDescription() {
147         return description;
148     }
149
150     /**
151      * Set the description
152      *
153      * @param description description
154      */

155     public void setDescription(String JavaDoc description) {
156         this.description = description;
157     }
158
159     /**
160      * Get type type meta.
161      *
162      * @return type meta.
163      */

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

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

182     public String JavaDoc getCategory() {
183         return category;
184     }
185
186     /**
187      * Set the category
188      *
189      * @param category category
190      */

191     public void setCategory(String JavaDoc category) {
192         this.category = category;
193     }
194
195     /**
196      * Get the name
197      *
198      * @return name
199      */

200     public String JavaDoc getName() {
201         return name;
202     }
203
204     /**
205      * Set the name
206      *
207      * @param name
208      */

209     public void setName(String JavaDoc name) {
210         this.name = name;
211     }
212
213     /**
214      * Get the visibility. See {@link AttributeDefinitionForm} for
215      * the constants to use.
216      *
217      * @return visibility
218      */

219     public int getVisibility() {
220         return visibility;
221     }
222
223     /**
224      * Set the visibility. See {@link AttributeDefinitionForm} for
225      * the contants to use.
226      *
227      * @param visibility visibility
228      */

229     public void setVisibility(int visibility) {
230         this.visibility = visibility;
231     }
232     
233     /**
234      * Set the sort order
235      *
236      * @param sortOrder sort order
237      */

238     public void setSortOrder(int sortOrder) {
239         this.sortOrder = sortOrder;
240     }
241     
242     /**
243      * Get the sort order
244      *
245      * @return sort order
246      */

247     public int getSortOrder() {
248         return sortOrder;
249     }
250
251     /**
252      * Get the selected type.
253      *
254      * @return type
255      */

256     public int getType() {
257         return type;
258     }
259
260     /**
261      * Set the selected type
262      *
263      * @param type type
264      */

265     public void setType(int type) {
266         this.type = type;
267     }
268
269     /**
270      * Get the validation string
271      *
272      * @return validation string
273      * @see DefaultPropertyDefinition
274      */

275     public String JavaDoc getValidationString() {
276         return validationString;
277     }
278
279     /**
280      * Set the validation string
281      *
282      * @param validationString validation string
283      * @see DefaultPropertyDefinition
284      */

285     public void setValidationString(String JavaDoc validationString) {
286         this.validationString = validationString;
287     }
288
289     /**
290      * Get the attribute definition object being edited / created
291      *
292      * @return definition
293      */

294     public AttributeDefinition getDefinition() {
295         return definition;
296     }
297
298     /**
299      * Apply ethe entered values to the attribute definition being
300      * edited.
301      */

302     public void applyToDefinition() {
303         definition.setDefaultValue(getDefaultValue().trim());
304         definition.setLabel(getLabel().trim());
305         definition.setSortOrder(getSortOrder());
306         definition.setCategoryLabel(getCategory().trim());
307         definition.setDescription(getDescription().trim());
308         if(!getEditing()) {
309             definition.setVisibility(getVisibility());
310             definition.setName(getName().trim());
311             definition.setType(getType());
312         }
313         definition.setValidationString(getValidationString());
314         definition.setTypeMeta(getTypeMeta());
315     }
316
317     /* (non-Javadoc)
318      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
319      */

320     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
321         if(isCommiting()) {
322             ActionErrors errs = new ActionErrors();
323             if(getName().trim().equals("")) {
324                 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noName"));
325             } else if(getDescription().trim().equals("")) {
326                 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noDescription"));
327             } else {
328                 if(!getEditing()) {
329                     try {
330                         PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME);
331                         AttributeDefinition def = (AttributeDefinition)propertyClass.getDefinition(getName());
332                         if(def != null) {
333                             errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.duplicateName", getName()));
334                         }
335                     }
336                     catch(Exception JavaDoc e) {
337                         log.error("Failed to test if attribute exists.", e);
338                         errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.duplicateName", getName()));
339                     }
340                 }
341                 if (!getName().matches("^[a-zA-Z0-9_-]*$")) {
342                     errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.invalidName"));
343                 }
344             }
345             if(!validationString.trim().equals("")) {
346                 String JavaDoc className = null;
347                 int idx = validationString.indexOf('(');
348                 if(idx == -1) {
349                     className = validationString;
350                 }
351                 else {
352                     if(!validationString.endsWith(")")) {
353                         errs = new ActionErrors();
354                         errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.invalidValidationStringFormat"));
355                     }
356                     className = validationString.substring(0, idx);
357                 }
358                 
359                 try {
360                     Class.forName(className);
361                 }
362                 catch(ClassNotFoundException JavaDoc cnfe) {
363                     errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noSuchValidator", className));
364                 }
365
366                 try {
367                     // If there is a default value then validate it
368

369                     String JavaDoc defaultValue = getDefaultValue().trim();
370                     if(!defaultValue.equals("")) {
371                         AttributeDefinition def = ((AttributesPropertyClass)definition.getPropertyClass()).createAttributeDefinition(type,
372                             name,
373                             typeMeta,
374                             -1,
375                             category,
376                             defaultValue,
377                             visibility,
378                             sortOrder,
379                             null,
380                             false,
381                             label,
382                             description,
383                             false,
384                             true,
385                             validationString);
386                         try {
387                             def.validate(defaultValue, getClass().getClassLoader());
388                         } catch (CoreException ce) {
389                             ce.getBundleActionMessage().setArg3(def.getLabel());
390                             if (errs == null) {
391                                 errs = new ActionErrors();
392                             }
393                             errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
394                         }
395                     }
396                 }
397                 catch(Exception JavaDoc e) {
398                     if(errs == null) {
399                         errs = new ActionErrors();
400                     }
401                     errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.failedToValidate", className));
402                 }
403             }
404             return errs;
405         }
406         return null;
407     }
408 }
409
Popular Tags