001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ken.web.spring; 017 018 import java.io.IOException; 019 import java.util.Collection; 020 import java.util.HashMap; 021 import java.util.Iterator; 022 import java.util.Map; 023 024 import javax.servlet.ServletException; 025 import javax.servlet.http.HttpServletRequest; 026 import javax.servlet.http.HttpServletResponse; 027 028 import org.apache.log4j.Logger; 029 import org.kuali.rice.ken.bo.NotificationChannelBo; 030 import org.kuali.rice.ken.bo.UserChannelSubscriptionBo; 031 import org.kuali.rice.ken.service.NotificationChannelService; 032 import org.kuali.rice.ken.service.UserPreferenceService; 033 import org.springframework.web.servlet.ModelAndView; 034 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 035 036 /** 037 * This class is the controller that handles management of various user preferences interfaces (deliver types, user subscriptions, etc). 038 * @author Kuali Rice Team (rice.collab@kuali.org) 039 */ 040 public class UserPreferencesController extends MultiActionController { 041 042 private static String view = ""; 043 044 /** Logger for this class and subclasses */ 045 private static final Logger LOG = Logger.getLogger(UserPreferencesController.class); 046 047 protected NotificationChannelService notificationChannelService; 048 protected UserPreferenceService userPreferenceService; 049 protected Object notificationMessageDelivererRegistryService; 050 051 052 /** 053 * Set the NotificationChannelService 054 * @param notificationChannelService 055 */ 056 public void setNotificationChannelService(NotificationChannelService notificationChannelService) { 057 this.notificationChannelService = notificationChannelService; 058 } 059 060 /** 061 * Set the UserPreferenceService 062 * @param userPreferenceService 063 */ 064 public void setUserPreferenceService(UserPreferenceService userPreferenceService) { 065 this.userPreferenceService = userPreferenceService; 066 } 067 068 /** 069 * Set the NotificationMessageDelivererRegistryService 070 * @param notificationMessageDelivererRegistryService 071 */ 072 public void setNotificationMessageDelivererRegistryService(Object notificationMessageDelivererRegistryService) { 073 this.notificationMessageDelivererRegistryService = notificationMessageDelivererRegistryService; 074 } 075 076 /** 077 * This method displays the actionList preferences screen. 078 * @param request 079 * @param response 080 * @return 081 * @throws ServletException 082 * @throws IOException 083 */ 084 public ModelAndView displayActionListPreferences(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 085 view = "ActionListPreferences"; 086 LOG.debug("remoteUser: "+request.getRemoteUser()); 087 Map<String, Object> model = new HashMap<String, Object>(); 088 return new ModelAndView(view, model); 089 } 090 091 /** 092 * This method handles displaying the user preferences UI. 093 * @param request 094 * @param response 095 * @return 096 * @throws ServletException 097 * @throws IOException 098 */ 099 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 }