View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package sample.contact;
17  
18  import org.acegisecurity.acl.basic.SimpleAclEntry;
19  
20  import org.springframework.beans.factory.InitializingBean;
21  
22  import org.springframework.dao.DataAccessException;
23  
24  import org.springframework.util.Assert;
25  
26  import org.springframework.validation.BindException;
27  
28  import org.springframework.web.bind.RequestUtils;
29  import org.springframework.web.servlet.ModelAndView;
30  import org.springframework.web.servlet.mvc.SimpleFormController;
31  import org.springframework.web.servlet.view.RedirectView;
32  
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  
42  /***
43   * Controller for adding an ACL permission.
44   *
45   * @author Ben Alex
46   * @version $Id: AddPermissionController.java,v 1.4 2005/11/17 00:56:08 benalex Exp $
47   */
48  public class AddPermissionController extends SimpleFormController
49      implements InitializingBean {
50      //~ Instance fields ========================================================
51  
52      private ContactManager contactManager;
53  
54      //~ Methods ================================================================
55  
56      public void setContactManager(ContactManager contact) {
57          this.contactManager = contact;
58      }
59  
60      public ContactManager getContactManager() {
61          return contactManager;
62      }
63  
64      public void afterPropertiesSet() throws Exception {
65          Assert.notNull(contactManager,
66              "A ContactManager implementation is required");
67      }
68  
69      protected ModelAndView disallowDuplicateFormSubmission(
70          HttpServletRequest request, HttpServletResponse response)
71          throws Exception {
72          BindException errors = new BindException(formBackingObject(request),
73                  getCommandName());
74          errors.reject("err.duplicateFormSubmission",
75              "Duplicate form submission.");
76  
77          return showForm(request, response, errors);
78      }
79  
80      protected Object formBackingObject(HttpServletRequest request)
81          throws Exception {
82          int contactId = RequestUtils.getRequiredIntParameter(request,
83                  "contactId");
84  
85          Contact contact = contactManager.getById(new Long(contactId));
86  
87          AddPermission addPermission = new AddPermission();
88          addPermission.setContact(contact);
89  
90          return addPermission;
91      }
92  
93      protected ModelAndView handleInvalidSubmit(HttpServletRequest request,
94          HttpServletResponse response) throws Exception {
95          return disallowDuplicateFormSubmission(request, response);
96      }
97  
98      protected ModelAndView onSubmit(HttpServletRequest request,
99          HttpServletResponse response, Object command, BindException errors)
100         throws Exception {
101         AddPermission addPermission = (AddPermission) command;
102 
103         try {
104             contactManager.addPermission(addPermission.getContact(),
105                 addPermission.getRecipient(), addPermission.getPermission());
106         } catch (DataAccessException existingPermission) {
107             existingPermission.printStackTrace();
108             errors.rejectValue("recipient", "err.recipientExistsForContact",
109                 "This recipient already has permissions to this contact.");
110 
111             return showForm(request, response, errors);
112         }
113 
114         return new ModelAndView(new RedirectView(getSuccessView()));
115     }
116 
117     protected Map referenceData(HttpServletRequest request)
118         throws Exception {
119         Map model = new HashMap();
120         model.put("recipients", listRecipients(request));
121         model.put("permissions", listPermissions(request));
122 
123         return model;
124     }
125 
126     private Map listPermissions(HttpServletRequest request) {
127         Map map = new LinkedHashMap();
128         map.put(new Integer(SimpleAclEntry.NOTHING),
129             getApplicationContext().getMessage("select.none", null, "None",
130                 request.getLocale()));
131         map.put(new Integer(SimpleAclEntry.ADMINISTRATION),
132             getApplicationContext().getMessage("select.administer", null,
133                 "Administer", request.getLocale()));
134         map.put(new Integer(SimpleAclEntry.READ),
135             getApplicationContext().getMessage("select.read", null, "Read",
136                 request.getLocale()));
137         map.put(new Integer(SimpleAclEntry.DELETE),
138             getApplicationContext().getMessage("select.delete", null, "Delete",
139                 request.getLocale()));
140         map.put(new Integer(SimpleAclEntry.READ_WRITE_DELETE),
141             getApplicationContext().getMessage("select.readWriteDelete", null,
142                 "Read+Write+Delete", request.getLocale()));
143 
144         return map;
145     }
146 
147     private Map listRecipients(HttpServletRequest request) {
148         Map map = new LinkedHashMap();
149         map.put("",
150             getApplicationContext().getMessage("select.pleaseSelect", null,
151                 "-- please select --", request.getLocale()));
152 
153         Iterator recipientsIter = contactManager.getAllRecipients().iterator();
154 
155         while (recipientsIter.hasNext()) {
156             String recipient = (String) recipientsIter.next();
157             map.put(recipient, recipient);
158         }
159 
160         return map;
161     }
162 }