View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.web.spring;
17  
18  import java.io.IOException;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.log4j.Logger;
29  import org.kuali.rice.ken.bo.NotificationChannel;
30  import org.kuali.rice.ken.bo.UserChannelSubscription;
31  import org.kuali.rice.ken.deliverer.NotificationMessageDeliverer;
32  import org.kuali.rice.ken.exception.ErrorList;
33  import org.kuali.rice.ken.service.NotificationChannelService;
34  import org.kuali.rice.ken.service.UserPreferenceService;
35  import org.springframework.web.servlet.ModelAndView;
36  import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
37  
38  /**
39   * This class is the controller that handles management of various user preferences interfaces (deliver types, user subscriptions, etc).
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class UserPreferencesController extends MultiActionController {
43      
44     private static String view = "";
45     
46     /** Logger for this class and subclasses */
47     private static final Logger LOG = Logger.getLogger(UserPreferencesController.class);
48     
49     protected NotificationChannelService notificationChannelService;
50     protected UserPreferenceService userPreferenceService;
51     protected Object notificationMessageDelivererRegistryService;
52  
53     
54     /**
55      * Set the NotificationChannelService
56      * @param notificationChannelService
57      */   
58     public void setNotificationChannelService(NotificationChannelService notificationChannelService) {
59        this.notificationChannelService = notificationChannelService;
60     }
61     
62     /**
63      * Set the UserPreferenceService
64      * @param userPreferenceService
65      */   
66     public void setUserPreferenceService(UserPreferenceService userPreferenceService) {
67        this.userPreferenceService = userPreferenceService;
68     }
69     
70     /**
71      * Set the NotificationMessageDelivererRegistryService
72      * @param notificationMessageDelivererRegistryService
73      */   
74     public void setNotificationMessageDelivererRegistryService(Object notificationMessageDelivererRegistryService) {
75        this.notificationMessageDelivererRegistryService = notificationMessageDelivererRegistryService;
76     }
77     
78     /**
79      * This method displays the actionList preference screen.
80      * @param request
81      * @param response
82      * @return
83      * @throws ServletException
84      * @throws IOException
85      */
86     public ModelAndView displayActionListPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
87         view = "ActionListPreferences";
88         LOG.debug("remoteUser: "+request.getRemoteUser());
89         Map<String, Object> model = new HashMap<String, Object>(); 
90         return new ModelAndView(view, model);
91     }
92      
93     /**
94      * This method handles displaying the user preferences UI.
95      * @param request
96      * @param response
97      * @return
98      * @throws ServletException
99      * @throws IOException
100     */
101    public ModelAndView displayUserPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
102 
103       view = "UserPreferencesForm";
104       String userid = request.getRemoteUser();
105       LOG.debug("remoteUser: "+userid);
106       // get subscribable channels
107       Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
108       // get current subscriptions for this user
109       Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
110       Map<String, Object> currentsubs = new HashMap<String, Object>();
111       Iterator<UserChannelSubscription> i = subscriptions.iterator();
112       while (i.hasNext()) {
113 	  UserChannelSubscription sub = i.next();
114 	  String subid = Long.toString(sub.getChannel().getId());
115  	  currentsubs.put(subid, subid);
116 	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
117       }
118       Map<String, Object> model = new HashMap<String, Object>();
119       model.put("channels", channels);
120       model.put("currentsubs", currentsubs);
121       
122       return new ModelAndView(view, model);
123    }
124 
125    /**
126     * Subscribe To a Channel
127     * @param request
128     * @param response
129     * @return
130     * @throws ServletException
131     * @throws IOException
132    */
133    public ModelAndView subscribeToChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
134 
135        view = "UserPreferencesForm";
136        String userid = request.getRemoteUser();
137        LOG.debug("remoteUser: "+userid);
138        String channelid = request.getParameter("channelid");
139        NotificationChannel newChannel = this.notificationChannelService.getNotificationChannel(channelid);
140        LOG.debug("newChannel name:"+newChannel.getName());
141        UserChannelSubscription newSub = new UserChannelSubscription();
142        newSub.setUserId(userid);
143        newSub.setChannel(newChannel);
144        LOG.debug("Calling service to subscribe to channel: "+newChannel.getName());
145        this.userPreferenceService.subscribeToChannel(newSub);
146        
147        // get current subscription channel ids
148        Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
149        Map<String, Object> currentsubs = new HashMap<String, Object>();;
150        Iterator<UserChannelSubscription> i = subscriptions.iterator();
151        while (i.hasNext()) {
152  	  UserChannelSubscription sub = i.next();
153  	  String subid = Long.toString(sub.getChannel().getId());
154  	  currentsubs.put(subid, subid);
155  	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
156        }
157        
158        // get all subscribable channels       
159        Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
160        
161        Map<String, Object> model = new HashMap<String, Object>();
162        model.put("channels", channels);
163        model.put("currentsubs", currentsubs);
164        return new ModelAndView(view, model);       
165    }
166    
167    /**
168     * Unsubscribe from Channel
169     * @param request
170     * @param response
171     * @return
172     * @throws ServletException
173     * @throws IOException
174     */
175    public ModelAndView unsubscribeFromChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
176        view = "UserPreferencesForm";
177        String userid = request.getRemoteUser();
178        LOG.debug("remoteUser: "+userid);
179        String channelid = request.getParameter("channelid");
180        
181        NotificationChannel newChannel = this.notificationChannelService.getNotificationChannel(channelid);
182        LOG.debug("getting channel (id, user): "+channelid+","+userid); 
183        UserChannelSubscription oldsub = this.userPreferenceService.getSubscription(channelid, userid);
184        oldsub.setChannel(newChannel);
185        
186        LOG.debug("Calling service to unsubscribe: "+newChannel.getName());
187        this.userPreferenceService.unsubscribeFromChannel(oldsub);
188        LOG.debug("Finished unsubscribe service: "+newChannel.getName());
189        
190        // get current subscription channel ids
191        Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
192        Map<String, Object> currentsubs = new HashMap<String, Object>();
193        Iterator<UserChannelSubscription> i = subscriptions.iterator();
194        while (i.hasNext()) {
195  	  UserChannelSubscription sub = i.next();
196  	  String subid = Long.toString(sub.getChannel().getId());
197 	  currentsubs.put(subid, subid);
198  	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
199        }
200        
201        // get all subscribable channels       
202        Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
203        
204        Map<String, Object> model = new HashMap<String, Object>();
205        model.put("channels", channels);
206        model.put("currentsubs", currentsubs);
207        return new ModelAndView(view, model);    
208         
209    }
210 }