KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > forms > AbstractResourceForm


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.policyframework.forms;
21
22 import java.util.Iterator JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.action.ActionMapping;
31
32 import com.sslexplorer.boot.PropertyList;
33 import com.sslexplorer.core.BundleActionMessage;
34 import com.sslexplorer.core.forms.CoreForm;
35 import com.sslexplorer.input.MultiSelectSelectionModel;
36 import com.sslexplorer.policyframework.Resource;
37 import com.sslexplorer.security.LogonControllerFactory;
38 import com.sslexplorer.security.SessionInfo;
39 import com.sslexplorer.security.User;
40 import com.sslexplorer.tabs.TabModel;
41
42 /**
43  * Abstract implementation of {@link com.sslexplorer.core.forms.CoreForm}
44  * that allows editing of {@link com.sslexplorer.policyframework.Resource}
45  * instances.
46  *
47  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
48  */

49 public abstract class AbstractResourceForm extends CoreForm implements TabModel {
50     static Log log = LogFactory.getLog(AbstractResourceForm.class);
51
52     protected String JavaDoc resourceName, resourceDescription;
53     protected int resourceId;
54     protected MultiSelectSelectionModel policyModel;
55     protected PropertyList selectedPolicies;
56     protected User owner, user;
57     protected String JavaDoc originalName;
58     protected Resource resource;
59     protected boolean readOnly;
60     protected boolean assignOnly;
61
62     /**
63      * Constructor
64      */

65     public AbstractResourceForm() {
66         selectedPolicies = new PropertyList();
67     }
68     
69     /**
70      * Get a resource given its name. Used by the {@link #validate(ActionMapping, HttpServletRequest)}
71      * method to make sure resources with the same name are not created.
72      *
73      * @param resourceName resource name
74      * @return resource
75      * @throws Exception on any erro
76      */

77     public abstract Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc;
78     
79     /**
80      * Get the Id of the resource being edited or 0 if this is a new resource.
81      *
82      * @return resource Id
83      */

84     public int getResourceId() {
85         return resourceId;
86     }
87
88     /**
89      * Set the ID of the resource being edited or 0 if this is a new resource.
90      *
91      * @param resourceId resource Id
92      */

93     public void setResourceID(int resourceId) {
94         this.resourceId = resourceId;
95     }
96
97     /**
98      * Initialise the form. All concrete classes should call this.
99      *
100      * @param user user
101      * @param resource resource
102      * @param editing editing
103      * @param policyModel policy model
104      * @param selectedPolicies selected policies
105      * @param owner owner
106      * @throws Exception on any error
107      */

108     public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel, PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception JavaDoc {
109         this.resource = resource;
110         originalName = resource.getResourceName();
111         setResourceID(resource.getResourceId());
112         setResourceName(resource.getResourceName());
113         setResourceDescription(resource.getResourceDescription());
114         if(editing) {
115             setEditing();
116         }
117         else {
118             setCreating();
119         }
120         setPolicyModel(policyModel);
121         this.selectedPolicies = selectedPolicies;
122         this.owner = owner;
123         this.user = user;
124         this.assignOnly = assignOnly;
125     }
126     
127     /**
128      * If this is an {@link com.sslexplorer.policyframework.OwnedResource}
129      * then this method will return the owner.
130      *
131      * @return owner
132      */

133     public User getOwner() {
134         return owner;
135     }
136     
137     /**
138      * Get the user that is creating / editing this resource.
139      *
140      * @return user
141      */

142     public User getUser() {
143         return user;
144     }
145     
146     /**
147      * Get the resource being editing
148      *
149      * @return resource
150      */

151     public Resource getResource() {
152         return resource;
153     }
154
155     /**
156      * Get the resource description
157      *
158      * @return resource description
159      */

160     public String JavaDoc getResourceDescription() {
161         return resourceDescription;
162     }
163
164     /**
165      * Set the resource description.
166      *
167      * @param resourceDescription resource description
168      */

169     public void setResourceDescription(String JavaDoc resourceDescription) {
170         this.resourceDescription = resourceDescription.trim();
171     }
172
173     /**
174      * Get the resource name
175      *
176      * @return resource name
177      */

178     public String JavaDoc getResourceName() {
179         return resourceName;
180     }
181
182     /* (non-Javadoc)
183      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
184      */

185     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
186         ActionErrors errs = new ActionErrors();
187         if (isCommiting()) {
188             if (getResourceName() == null || getResourceName().equals("")) {
189                 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingName"));
190             }
191             if (getResourceName().length() > Resource.MAX_RESOURCE_NAME_LENGTH) {
192                 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameTooLong", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH)));
193             }
194             if (getResourceDescription().equals("")) {
195                 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingDescription", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH)));
196             }
197             if(!getEditing() || !originalName.equals(getResourceName())) {
198                 try {
199                     Resource r = getResourceByName(getResourceName(), LogonControllerFactory.getInstance().getSessionInfo(request));
200                     if(r != null) {
201                         errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameInUse", getResourceName()));
202                     }
203                 }
204                 catch(Exception JavaDoc e) {
205                     errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.failedToDetermineIfResourceExists", e.getMessage()));
206                 }
207             }
208             /* Make sure the selected policies are all currently available, this prevents
209              * anyone fiddling the polices they are allowed to configure
210              */

211             for(Iterator JavaDoc i = selectedPolicies.iterator(); i.hasNext(); ) {
212                 String JavaDoc pol = (String JavaDoc)i.next();
213                 if(!policyModel.contains(pol)) {
214                     throw new Error JavaDoc("User doesn't have permission to select the policy '" + pol + "', this shouldn't happen.");
215                 }
216             }
217         }
218         return errs;
219     }
220
221     /**
222      * Set the resource name
223      *
224      * @param resourceName resource name
225      */

226     public void setResourceName(String JavaDoc resourceName) {
227         this.resourceName = resourceName.trim();
228     }
229
230     /**
231      * Get the selected policies as a <i>Property List</i> string.
232      *
233      * @return the selected policies
234      * @see PropertyList
235      */

236     public String JavaDoc getSelectedPolicies() {
237         return selectedPolicies.getAsTextFieldText();
238     }
239
240     /**
241      * Set the selected policies as a <i>Property List</i> string.
242      *
243      * @param selectedPolicies the selected policies
244      * @see PropertyList
245      */

246     public void setSelectedPolicies(String JavaDoc selectedPolicies) {
247         this.selectedPolicies.setAsTextFieldText(selectedPolicies);
248     }
249
250     /**
251      * Get the model to use for policy selection
252      *
253      * @return policy selection model
254      */

255     public MultiSelectSelectionModel getPolicyModel() {
256         return policyModel;
257     }
258
259     /**
260      * Set the model to use for policy selection
261      *
262      * @param policyModel policy selection model
263      */

264     public void setPolicyModel(MultiSelectSelectionModel policyModel) {
265         this.policyModel = policyModel;
266     }
267     
268     /**
269      * Get the list of selected policies
270      *
271      * @return selected policies
272      */

273     public PropertyList getSelectedPoliciesList() {
274         return selectedPolicies;
275     }
276     
277     /**
278      * Get if this resource is read only
279      *
280      * @return resource read only
281      */

282     public boolean getReadOnly() {
283         return readOnly;
284     }
285     
286     /**
287      * Set this resource to be read only. This does not accept a boolean
288      * argument to prevent struts forms being able to modify it.
289      */

290     public void setReadOnly() {
291         readOnly = true;
292     }
293     
294     /**
295      * Set this resource to be writeable only. This does not accept a boolean
296      * argument to prevent struts forms being able to modify it.
297      */

298     public void setWriteable() {
299         readOnly = false;
300     }
301
302     /**
303      * Apply the collected form fields to the resource object
304      *
305      * @throws Exception on any error
306      */

307     public void apply() throws Exception JavaDoc {
308         if(getReadOnly()) {
309             throw new Exception JavaDoc("Read only");
310         }
311         resource.setResourceName(getResourceName());
312         resource.setResourceDescription(getResourceDescription());
313         applyToResource();
314     }
315     
316     /**
317      * Concrete implementations must provide this method to persist any
318      * additional form fields to the resource object being edited.
319      *
320      * @throws Exception on any error
321      */

322     public abstract void applyToResource() throws Exception JavaDoc;
323
324     /**
325      * @return boolean
326      */

327     public boolean isAssignOnly() {
328         return assignOnly;
329     }
330
331     /**
332      * @param assignOnly
333      */

334     public void setAssignOnly(boolean assignOnly) {
335         this.assignOnly = assignOnly;
336     }
337 }
Popular Tags