KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionErrors;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33
34 import com.sslexplorer.boot.PropertyClass;
35 import com.sslexplorer.boot.PropertyClassManager;
36 import com.sslexplorer.properties.attributes.AttributeDefinition;
37 import com.sslexplorer.properties.attributes.AttributesPropertyClass;
38 import com.sslexplorer.properties.attributes.wizards.actions.AttributeDefinitionDetailsAction;
39 import com.sslexplorer.properties.impl.userattributes.UserAttributes;
40 import com.sslexplorer.wizard.AbstractWizardSequence;
41 import com.sslexplorer.wizard.forms.DefaultWizardForm;
42
43 /**
44  * Form that allows an administrator to create a new <i>Attribute Definition</i> of
45  * some type.
46  *
47  * @author brett
48  */

49 public class AttributeDefinitionDetailsForm extends DefaultWizardForm {
50
51     final static Log log = LogFactory.getLog(AttributeDefinitionDetailsForm.class);
52     
53     private String JavaDoc attributeClass;
54     private String JavaDoc name;
55     private String JavaDoc description;
56
57     /**
58      * Constructor
59      */

60     public AttributeDefinitionDetailsForm() {
61         super(true, false, "/WEB-INF/jsp/content/properties/attributeDefinitionWizard/attributeDefinitionDetails.jspf", "name", true, false,
62                         "attributeDefinitionDetails", "properties", "attributeDefinitionWizard.attributeDefinitionDetails", 1);
63     }
64     
65     /**
66      * Get the name of the attribute definition
67      *
68      * @return name
69      */

70     public String JavaDoc getName() {
71         return name;
72     }
73     
74     /**
75      * Set the name of the attribute definition
76      *
77      * @param name name
78      */

79     public void setName(String JavaDoc name) {
80         this.name = name;
81     }
82     
83     /**
84      * Get the description of the attribute definition.
85      *
86      * @return desription
87      */

88     public String JavaDoc getDescription() {
89         return description;
90     }
91     
92     /**
93      * Set the description of the attribute definition.
94      *
95      * @param description description
96      */

97     public void setDescription(String JavaDoc description) {
98         this.description = description;
99     }
100
101     /**
102      * Get the selected <i>Attribute class</i>. An
103      * Attribute class is a specialisation of a <i>Property Class</i>
104      * and the class names can normally be found as constants in
105      * the <i>Property Class</i> implementation. See {@link UserAttributes#NAME}
106      * for example.
107      *
108      * @return attribute class
109      */

110     public String JavaDoc getAttributeClass() {
111         return attributeClass;
112     }
113
114     /**
115      * Get the selected <i>Attribute class</i>.
116      *
117      * @param attributeClass
118      * @see #getAttributeClass()
119      */

120     public void setAttributeClass(String JavaDoc attributeClass) {
121         this.attributeClass = attributeClass;
122     }
123     
124     /**
125      * Get a list of <i>Attribute Classes</i> that may be
126      * chosen in this first step.
127      *
128      * @return attribute classes
129      */

130     public List JavaDoc<String JavaDoc> getAttributeClasses() {
131         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
132         for(PropertyClass propertyClass : PropertyClassManager.getInstance().getPropertyClasses()) {
133             if(propertyClass instanceof AttributesPropertyClass) {
134                 l.add(propertyClass.getName());
135             }
136         }
137         return l;
138     }
139
140     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
141         super.init(sequence, request);
142         name = (String JavaDoc)sequence.getAttribute(AttributeDefinitionDetailsAction.ATTR_NAME, "");
143         description = (String JavaDoc)sequence.getAttribute(AttributeDefinitionDetailsAction.ATTR_DESCRIPTION, "");
144         attributeClass = (String JavaDoc)sequence.getAttribute(AttributeDefinitionDetailsAction.ATTR_CLASS, UserAttributes.NAME);
145     }
146     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
147         super.apply(sequence);
148         sequence.putAttribute(AttributeDefinitionDetailsAction.ATTR_NAME, name);
149         sequence.putAttribute(AttributeDefinitionDetailsAction.ATTR_DESCRIPTION, description);
150         sequence.putAttribute(AttributeDefinitionDetailsAction.ATTR_CLASS, attributeClass);
151     }
152
153     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
154         if(isCommiting()) {
155             ActionErrors errs = new ActionErrors();
156             if(getName().trim().equals("")) {
157                 errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.noName"));
158             } else if(getDescription().trim().equals("")) {
159                 errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.noDescription"));
160             } else {
161                 if(!getEditing()) {
162                     try {
163                         PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(getAttributeClass());
164                         AttributeDefinition def = (AttributeDefinition)propertyClass.getDefinition(getName());
165                         if(def != null) {
166                             errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.duplicateName", getName()));
167                         }
168                     }
169                     catch(Exception JavaDoc e) {
170                         log.error("Failed to test if attribute exists.", e);
171                         errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.duplicateName", getName()));
172                     }
173                 }
174                 if (!getName().matches("^[a-zA-Z0-9_-]*$")) {
175                     errs.add(Globals.ERROR_KEY, new ActionMessage(getResourcePrefix() + ".error.invalidName"));
176                 }
177             }
178             return errs;
179         }
180         return null;
181     }
182 }
183
Popular Tags