KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator 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.PropertyDefinition;
37 import com.sslexplorer.core.CoreException;
38 import com.sslexplorer.core.forms.CoreForm;
39 import com.sslexplorer.properties.attributes.AttributeValueItem;
40 import com.sslexplorer.tabs.TabModel;
41
42 /**
43  * Implementation of {@link com.sslexplorer.core.forms.CoreForm} that allows a
44  * user to edit or view their <i>User Attributes</i>.
45  *
46  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
47  */

48 public class UserAttributesForm extends CoreForm implements TabModel {
49
50     static Log log = LogFactory.getLog(UserAttributesForm.class);
51
52     // Private instance variables
53

54     private List JavaDoc<AttributeValueItem> userAttributeValueItems;
55     private String JavaDoc selectedTab;
56     private List JavaDoc<String JavaDoc> categoryIds;
57     private List JavaDoc<String JavaDoc> categoryTitles;
58
59     /**
60      * Initialise the form
61      *
62      * @param userAttributeValueItems list of
63      * {@link com.sslexplorer.properties.attributes.AttributeValueItem}
64      * objects
65      * @throws Exception on any error
66      */

67     public void initialize(List JavaDoc<AttributeValueItem> userAttributeValueItems) throws Exception JavaDoc {
68         this.userAttributeValueItems = userAttributeValueItems;
69         categoryIds = new ArrayList JavaDoc<String JavaDoc>();
70         categoryTitles = new ArrayList JavaDoc<String JavaDoc>();
71         for (Iterator JavaDoc i = userAttributeValueItems.iterator(); i.hasNext();) {
72             AttributeValueItem item = (AttributeValueItem) i.next();
73             int idx = categoryIds.indexOf(item.getCategoryId());
74             if (idx == -1) {
75                 categoryIds.add(item.getCategoryId());
76                 categoryTitles.add(item.getCategoryLabel());
77             }
78         }
79         selectedTab = categoryIds.size() > 0 ? categoryIds.get(0) : "";
80     }
81
82     /**
83      * Get a list of the category ids
84      *
85      * @return category ids
86      */

87     public List JavaDoc getCategoryIds() {
88         return categoryIds;
89     }
90
91     /**
92      * Get the list of
93      * {@link com.sslexplorer.properties.attributes.AttributeValueItem} objects
94      *
95      * @return user attributre value items
96      */

97     public List JavaDoc getUserAttributeValueItems() {
98         return userAttributeValueItems;
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see com.sslexplorer.tabs.TabModel#getTabCount()
105      */

106     public int getTabCount() {
107         return categoryIds.size();
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
114      */

115     public String JavaDoc getTabName(int idx) {
116         return (String JavaDoc) categoryIds.get(idx);
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
123      */

124     public String JavaDoc getSelectedTab() {
125         return selectedTab;
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
132      */

133     public void setSelectedTab(String JavaDoc selectedTab) {
134         this.selectedTab = selectedTab;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
141      */

142     public String JavaDoc getTabTitle(int idx) {
143         return (String JavaDoc) categoryTitles.get(idx);
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
150      */

151     public String JavaDoc getTabBundle(int idx) {
152         return null;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see com.sslexplorer.core.forms.CoreForm#reset(org.apache.struts.action.ActionMapping,
159      * javax.servlet.http.HttpServletRequest)
160      */

161     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
162         super.reset(mapping, request);
163         if (userAttributeValueItems != null) {
164             for (Iterator JavaDoc i = userAttributeValueItems.iterator(); i.hasNext();) {
165                 AttributeValueItem item = (AttributeValueItem) i.next();
166                 if (item.getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) {
167                     item.setSelected(false);
168                 }
169             }
170         }
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
177      * javax.servlet.http.HttpServletRequest)
178      */

179     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
180         if (isCommiting()) {
181             ActionErrors errs = null;
182             for (Iterator JavaDoc i = userAttributeValueItems.iterator(); i.hasNext();) {
183                 AttributeValueItem item = (AttributeValueItem) i.next();
184                 PropertyDefinition def = item.getDefinition();
185                 try {
186                     def.validate(item.getValue().toString(), getClass().getClassLoader());
187                 } catch (CoreException ce) {
188                     ce.getBundleActionMessage().setArg3(item.getLabel());
189                     if (errs == null) {
190                         errs = new ActionErrors();
191                     }
192                     errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
193                 } catch (Exception JavaDoc e) {
194                     errs.add(Globals.ERROR_KEY, new ActionMessage("userAttributes.error.failedToValidate", e.getMessage()));
195                 }
196             }
197             if (errs != null)
198                 return errs;
199         }
200         return super.validate(mapping, request);
201     }
202 }
203
Popular Tags