Coverage Report - org.kuali.rice.ken.web.spring.UserPreferencesController
 
Classes in this File Line Coverage Branch Coverage Complexity
UserPreferencesController
0%
0/80
0%
0/6
1.429
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
 public class UserPreferencesController extends MultiActionController {
 43  
     
 44  0
    private static String view = "";
 45  
    
 46  
    /** Logger for this class and subclasses */
 47  0
    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  0
       this.notificationChannelService = notificationChannelService;
 60  0
    }
 61  
    
 62  
    /**
 63  
     * Set the UserPreferenceService
 64  
     * @param userPreferenceService
 65  
     */   
 66  
    public void setUserPreferenceService(UserPreferenceService userPreferenceService) {
 67  0
       this.userPreferenceService = userPreferenceService;
 68  0
    }
 69  
    
 70  
    /**
 71  
     * Set the NotificationMessageDelivererRegistryService
 72  
     * @param notificationMessageDelivererRegistryService
 73  
     */   
 74  
    public void setNotificationMessageDelivererRegistryService(Object notificationMessageDelivererRegistryService) {
 75  0
       this.notificationMessageDelivererRegistryService = notificationMessageDelivererRegistryService;
 76  0
    }
 77  
    
 78  
    /**
 79  
     * This method displays the actionList preferences 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  0
        view = "ActionListPreferences";
 88  0
        LOG.debug("remoteUser: "+request.getRemoteUser());
 89  0
        Map<String, Object> model = new HashMap<String, Object>(); 
 90  0
        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  0
       view = "UserPreferencesForm";
 104  0
       String userid = request.getRemoteUser();
 105  0
       LOG.debug("remoteUser: "+userid);
 106  
       // get subscribable channels
 107  0
       Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
 108  
       // get current subscriptions for this user
 109  0
       Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
 110  0
       Map<String, Object> currentsubs = new HashMap<String, Object>();
 111  0
       Iterator<UserChannelSubscription> i = subscriptions.iterator();
 112  0
       while (i.hasNext()) {
 113  0
           UserChannelSubscription sub = i.next();
 114  0
           String subid = Long.toString(sub.getChannel().getId());
 115  0
            currentsubs.put(subid, subid);
 116  0
           LOG.debug("currently subscribed to: "+sub.getChannel().getId());
 117  0
       }
 118  0
       Map<String, Object> model = new HashMap<String, Object>();
 119  0
       model.put("channels", channels);
 120  0
       model.put("currentsubs", currentsubs);
 121  
       
 122  0
       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  0
        view = "UserPreferencesForm";
 136  0
        String userid = request.getRemoteUser();
 137  0
        LOG.debug("remoteUser: "+userid);
 138  0
        String channelid = request.getParameter("channelid");
 139  0
        NotificationChannel newChannel = this.notificationChannelService.getNotificationChannel(channelid);
 140  0
        LOG.debug("newChannel name:"+newChannel.getName());
 141  0
        UserChannelSubscription newSub = new UserChannelSubscription();
 142  0
        newSub.setUserId(userid);
 143  0
        newSub.setChannel(newChannel);
 144  0
        LOG.debug("Calling service to subscribe to channel: "+newChannel.getName());
 145  0
        this.userPreferenceService.subscribeToChannel(newSub);
 146  
        
 147  
        // get current subscription channel ids
 148  0
        Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
 149  0
        Map<String, Object> currentsubs = new HashMap<String, Object>();;
 150  0
        Iterator<UserChannelSubscription> i = subscriptions.iterator();
 151  0
        while (i.hasNext()) {
 152  0
            UserChannelSubscription sub = i.next();
 153  0
            String subid = Long.toString(sub.getChannel().getId());
 154  0
            currentsubs.put(subid, subid);
 155  0
            LOG.debug("currently subscribed to: "+sub.getChannel().getId());
 156  0
        }
 157  
        
 158  
        // get all subscribable channels       
 159  0
        Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
 160  
        
 161  0
        Map<String, Object> model = new HashMap<String, Object>();
 162  0
        model.put("channels", channels);
 163  0
        model.put("currentsubs", currentsubs);
 164  0
        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  0
        view = "UserPreferencesForm";
 177  0
        String userid = request.getRemoteUser();
 178  0
        LOG.debug("remoteUser: "+userid);
 179  0
        String channelid = request.getParameter("channelid");
 180  
        
 181  0
        NotificationChannel newChannel = this.notificationChannelService.getNotificationChannel(channelid);
 182  0
        LOG.debug("getting channel (id, user): "+channelid+","+userid); 
 183  0
        UserChannelSubscription oldsub = this.userPreferenceService.getSubscription(channelid, userid);
 184  0
        oldsub.setChannel(newChannel);
 185  
        
 186  0
        LOG.debug("Calling service to unsubscribe: "+newChannel.getName());
 187  0
        this.userPreferenceService.unsubscribeFromChannel(oldsub);
 188  0
        LOG.debug("Finished unsubscribe service: "+newChannel.getName());
 189  
        
 190  
        // get current subscription channel ids
 191  0
        Collection<UserChannelSubscription> subscriptions = this.userPreferenceService.getCurrentSubscriptions(userid);
 192  0
        Map<String, Object> currentsubs = new HashMap<String, Object>();
 193  0
        Iterator<UserChannelSubscription> i = subscriptions.iterator();
 194  0
        while (i.hasNext()) {
 195  0
            UserChannelSubscription sub = i.next();
 196  0
            String subid = Long.toString(sub.getChannel().getId());
 197  0
           currentsubs.put(subid, subid);
 198  0
            LOG.debug("currently subscribed to: "+sub.getChannel().getId());
 199  0
        }
 200  
        
 201  
        // get all subscribable channels       
 202  0
        Collection<NotificationChannel> channels = this.notificationChannelService.getSubscribableChannels();
 203  
        
 204  0
        Map<String, Object> model = new HashMap<String, Object>();
 205  0
        model.put("channels", channels);
 206  0
        model.put("currentsubs", currentsubs);
 207  0
        return new ModelAndView(view, model);    
 208  
         
 209  
    }
 210  
 }