View Javadoc

1   /**
2    * Copyright 2005-2012 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.NotificationChannelBo;
30  import org.kuali.rice.ken.bo.UserChannelSubscriptionBo;
31  import org.kuali.rice.ken.service.NotificationChannelService;
32  import org.kuali.rice.ken.service.UserPreferenceService;
33  import org.springframework.web.servlet.ModelAndView;
34  import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
35  
36  /**
37   * This class is the controller that handles management of various user preferences interfaces (deliver types, user subscriptions, etc).
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class UserPreferencesController extends MultiActionController {
41      
42     private static String view = "";
43     
44     /** Logger for this class and subclasses */
45     private static final Logger LOG = Logger.getLogger(UserPreferencesController.class);
46     
47     protected NotificationChannelService notificationChannelService;
48     protected UserPreferenceService userPreferenceService;
49     protected Object notificationMessageDelivererRegistryService;
50  
51     
52     /**
53      * Set the NotificationChannelService
54      * @param notificationChannelService
55      */   
56     public void setNotificationChannelService(NotificationChannelService notificationChannelService) {
57        this.notificationChannelService = notificationChannelService;
58     }
59     
60     /**
61      * Set the UserPreferenceService
62      * @param userPreferenceService
63      */   
64     public void setUserPreferenceService(UserPreferenceService userPreferenceService) {
65        this.userPreferenceService = userPreferenceService;
66     }
67     
68     /**
69      * Set the NotificationMessageDelivererRegistryService
70      * @param notificationMessageDelivererRegistryService
71      */   
72     public void setNotificationMessageDelivererRegistryService(Object notificationMessageDelivererRegistryService) {
73        this.notificationMessageDelivererRegistryService = notificationMessageDelivererRegistryService;
74     }
75     
76     /**
77      * This method displays the actionList preferences screen.
78      * @param request
79      * @param response
80      * @return
81      * @throws ServletException
82      * @throws IOException
83      */
84     public ModelAndView displayActionListPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85         view = "ActionListPreferences";
86         LOG.debug("remoteUser: "+request.getRemoteUser());
87         Map<String, Object> model = new HashMap<String, Object>(); 
88         return new ModelAndView(view, model);
89     }
90      
91     /**
92      * This method handles displaying the user preferences UI.
93      * @param request
94      * @param response
95      * @return
96      * @throws ServletException
97      * @throws IOException
98      */
99     public ModelAndView displayUserPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100 
101       view = "UserPreferencesForm";
102       String userid = request.getRemoteUser();
103       LOG.debug("remoteUser: "+userid);
104       // get subscribable channels
105       Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
106       // get current subscriptions for this user
107       Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
108       Map<String, Object> currentsubs = new HashMap<String, Object>();
109       Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
110       while (i.hasNext()) {
111 	  UserChannelSubscriptionBo sub = i.next();
112 	  String subid = Long.toString(sub.getChannel().getId());
113  	  currentsubs.put(subid, subid);
114 	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
115       }
116       Map<String, Object> model = new HashMap<String, Object>();
117       model.put("channels", channels);
118       model.put("currentsubs", currentsubs);
119       
120       return new ModelAndView(view, model);
121    }
122 
123    /**
124     * Subscribe To a Channel
125     * @param request
126     * @param response
127     * @return
128     * @throws ServletException
129     * @throws IOException
130    */
131    public ModelAndView subscribeToChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
132 
133        view = "UserPreferencesForm";
134        String userid = request.getRemoteUser();
135        LOG.debug("remoteUser: "+userid);
136        String channelid = request.getParameter("channelid");
137        NotificationChannelBo newChannel = this.notificationChannelService.getNotificationChannel(channelid);
138        LOG.debug("newChannel name:"+newChannel.getName());
139        UserChannelSubscriptionBo newSub = new UserChannelSubscriptionBo();
140        newSub.setUserId(userid);
141        newSub.setChannel(newChannel);
142        LOG.debug("Calling service to subscribe to channel: "+newChannel.getName());
143        this.userPreferenceService.subscribeToChannel(newSub);
144        
145        // get current subscription channel ids
146        Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
147        Map<String, Object> currentsubs = new HashMap<String, Object>();;
148        Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
149        while (i.hasNext()) {
150  	  UserChannelSubscriptionBo sub = i.next();
151  	  String subid = Long.toString(sub.getChannel().getId());
152  	  currentsubs.put(subid, subid);
153  	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
154        }
155        
156        // get all subscribable channels       
157        Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
158        
159        Map<String, Object> model = new HashMap<String, Object>();
160        model.put("channels", channels);
161        model.put("currentsubs", currentsubs);
162        return new ModelAndView(view, model);       
163    }
164    
165    /**
166     * Unsubscribe from Channel
167     * @param request
168     * @param response
169     * @return
170     * @throws ServletException
171     * @throws IOException
172     */
173    public ModelAndView unsubscribeFromChannel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
174        view = "UserPreferencesForm";
175        String userid = request.getRemoteUser();
176        LOG.debug("remoteUser: "+userid);
177        String channelid = request.getParameter("channelid");
178        
179        NotificationChannelBo newChannel = this.notificationChannelService.getNotificationChannel(channelid);
180        LOG.debug("getting channel (id, user): "+channelid+","+userid); 
181        UserChannelSubscriptionBo oldsub = this.userPreferenceService.getSubscription(channelid, userid);
182        oldsub.setChannel(newChannel);
183        
184        LOG.debug("Calling service to unsubscribe: "+newChannel.getName());
185        this.userPreferenceService.unsubscribeFromChannel(oldsub);
186        LOG.debug("Finished unsubscribe service: "+newChannel.getName());
187        
188        // get current subscription channel ids
189        Collection<UserChannelSubscriptionBo> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
190        Map<String, Object> currentsubs = new HashMap<String, Object>();
191        Iterator<UserChannelSubscriptionBo> i = subscriptions.iterator();
192        while (i.hasNext()) {
193  	  UserChannelSubscriptionBo sub = i.next();
194  	  String subid = Long.toString(sub.getChannel().getId());
195 	  currentsubs.put(subid, subid);
196  	  LOG.debug("currently subscribed to: "+sub.getChannel().getId());
197        }
198        
199        // get all subscribable channels       
200        Collection<NotificationChannelBo> channels = this.notificationChannelService.getSubscribableChannels();
201        
202        Map<String, Object> model = new HashMap<String, Object>();
203        model.put("channels", channels);
204        model.put("currentsubs", currentsubs);
205        return new ModelAndView(view, model);    
206         
207    }
208 }