Coverage Report - org.kuali.rice.kcb.web.spring.UserPreferencesController
 
Classes in this File Line Coverage Branch Coverage Complexity
UserPreferencesController
0%
0/68
0%
0/14
2.333
 
 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.kcb.web.spring;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.kuali.rice.kcb.bo.RecipientDelivererConfig;
 20  
 import org.kuali.rice.kcb.deliverer.MessageDeliverer;
 21  
 import org.kuali.rice.kcb.exception.ErrorList;
 22  
 import org.kuali.rice.kcb.service.KENIntegrationService;
 23  
 import org.kuali.rice.kcb.service.MessageDelivererRegistryService;
 24  
 import org.kuali.rice.kcb.service.RecipientPreferenceService;
 25  
 import org.springframework.beans.factory.annotation.Required;
 26  
 import org.springframework.web.servlet.ModelAndView;
 27  
 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
 28  
 
 29  
 import javax.servlet.ServletException;
 30  
 import javax.servlet.http.HttpServletRequest;
 31  
 import javax.servlet.http.HttpServletResponse;
 32  
 import java.io.IOException;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Collection;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 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  
     /** Logger for this class and subclasses */
 44  0
     private static final Logger LOG = Logger.getLogger(UserPreferencesController.class);
 45  
 
 46  
     private static final String VIEW = "DelivererPreferences";
 47  
     private static final String KEW_CHANNEL = "KEW"; 
 48  
     protected RecipientPreferenceService recipientPreferenceService;
 49  
     protected MessageDelivererRegistryService messageDelivererRegistryService;
 50  
     protected KENIntegrationService kenIntegrationService;
 51  
 
 52  
     /**
 53  
      * Set the RecipientPreferenceService
 54  
      * @param recipientPreferenceService
 55  
      */
 56  
     @Required
 57  
     public void setRecipientPreferenceService(RecipientPreferenceService userPreferenceService) {
 58  0
         this.recipientPreferenceService = userPreferenceService;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Set the MessageDelivererRegistryService
 63  
      * @param messageDelivererRegistryService
 64  
      */
 65  
     @Required
 66  
     public void setMessageDelivererRegistryService(MessageDelivererRegistryService messageDelivererRegistryService) {
 67  0
         this.messageDelivererRegistryService = messageDelivererRegistryService;
 68  0
     }
 69  
 
 70  
     /**
 71  
      * Sets the KENIntegrationService
 72  
      * @param kis the KENIntegrationService
 73  
      */
 74  
     @Required
 75  
     public void setKenIntegrationService(KENIntegrationService kis) {
 76  0
         this.kenIntegrationService = kis;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * @return all channels for Rice, including the builtin KEW action list "channel"
 81  
      */
 82  
     protected Collection<String> getAllChannels() {
 83  
         // TODO: does not traverse bus yet
 84  0
         Collection<String> allChannels = new ArrayList<String>();
 85  
         //allChannels.add(KEW_CHANNEL);
 86  0
         allChannels.addAll(kenIntegrationService.getAllChannelNames());
 87  0
         return allChannels;
 88  
     }
 89  
 
 90  
     /**
 91  
      * displayDelivererConfigurationForm - obtain information necessary
 92  
      * for displaying all possible Deliverer types and forward to the form
 93  
      * @param request
 94  
      * @param response
 95  
      * @return
 96  
      * @throws ServletException
 97  
      * @throws IOException
 98  
      */
 99  
     public ModelAndView displayDelivererConfigurationForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 100  0
         String userid = request.getRemoteUser();
 101  0
         LOG.debug("remoteUser: "+userid); 
 102  
 
 103  
         // Get DeliveryType classes
 104  0
         Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers();
 105  
 
 106  
         // get all channels       
 107  0
         Collection<String> channels = getAllChannels();
 108  
 
 109  
         //     get all user preferences in a HashMap
 110  0
         HashMap<String, String> preferences  = this.recipientPreferenceService.getRecipientPreferences(userid);
 111  
 
 112  
         // get existing configured deliverers
 113  0
         Collection<RecipientDelivererConfig> currentDeliverers = this.recipientPreferenceService.getDeliverersForRecipient(userid);
 114  
         // create a Map as an easy way for the JSP to determine whether a deliver is enabled for channels
 115  0
         Map<String, Boolean> currentDeliverersMap = new HashMap<String, Boolean>();
 116  0
         for (RecipientDelivererConfig udc: currentDeliverers) {
 117  0
             String channelName = udc.getChannel();
 118  0
             currentDeliverersMap.put(udc.getDelivererName() + "." + channelName, Boolean.TRUE);
 119  0
         }
 120  
 
 121  0
         Map<String, Object> model = new HashMap<String, Object>();
 122  0
         model.put("channels", channels);
 123  0
         model.put("deliveryTypes", deliveryTypes);
 124  0
         model.put("preferences", preferences);
 125  0
         model.put("currentDeliverersMap", currentDeliverersMap);
 126  0
         return new ModelAndView(VIEW, model);
 127  
     }
 128  
 
 129  
     /**
 130  
      * saveDelivererConfiguration - save deliverer configuration data
 131  
      * @param request
 132  
      * @param response
 133  
      * @return
 134  
      * @throws ServletException
 135  
      * @throws IOException
 136  
      */
 137  
     public ModelAndView saveDelivererConfiguration(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 138  0
         String userid = request.getRemoteUser();
 139  0
         LOG.debug("remoteUser: "+userid);
 140  0
         boolean error = false;
 141  
 
 142  0
         Map<String, Object> model = new HashMap<String, Object>();
 143  
 
 144  
         // create preferences map here so that we can pass them all back to the view
 145  0
         HashMap<String, String> preferences  = new HashMap<String, String>();
 146  
 
 147  
         // Get DeliveryType classes.  loop through each deliverer type to 
 148  
         // to obtain preferenceKeys.  Check to see if a matching request
 149  
         // parameter was provided, then save a record for the userID, channelID, and 
 150  
         // preferences setting
 151  0
         Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers();
 152  
 
 153  
         // first remove all configured user delivers for this user
 154  0
         this.recipientPreferenceService.removeRecipientDelivererConfigs(userid);        
 155  
 
 156  0
         for (MessageDeliverer dt: deliveryTypes) {
 157  0
             String deliveryTypeName = dt.getName();
 158  0
             HashMap<String,String> prefMap = dt.getPreferenceKeys();
 159  0
             LOG.debug("deliveryName: "+deliveryTypeName);
 160  0
             HashMap<String, String> userprefs = new HashMap<String, String>();
 161  0
             for (String prefKey:prefMap.keySet()) {
 162  0
                 LOG.debug("   key: "+prefKey+", value: "+request.getParameter(deliveryTypeName+"."+prefKey));
 163  0
                 userprefs.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey ));
 164  0
                 preferences.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey ));
 165  
             }
 166  
             try {
 167  0
                 this.recipientPreferenceService.saveRecipientPreferences(userid, userprefs, dt);
 168  0
             } catch (ErrorList errorlist) {
 169  0
                 error = true;
 170  0
                 model.put("errorList", errorlist.getErrors()) ;
 171  0
             }
 172  
 
 173  
             // get channelName.channels
 174  0
             String[] channels = request.getParameterValues(deliveryTypeName+".channels");
 175  0
             if (channels != null && channels.length > 0) {
 176  0
                 for (int j=0; j < channels.length; j++) {
 177  0
                     LOG.debug(deliveryTypeName+".channels["+j+"] "+channels[j]);   
 178  
                 }
 179  
             }
 180  
             //         now save the userid, channel selection
 181  0
             this.recipientPreferenceService.saveRecipientDelivererConfig(userid, deliveryTypeName, channels);
 182  0
         }
 183  
 
 184  
         // get all channels       
 185  0
         Collection<String> channels = getAllChannels();
 186  
 
 187  
         // get existing configured deliverers
 188  0
         Collection<RecipientDelivererConfig> currentDeliverers = this.recipientPreferenceService.getDeliverersForRecipient(userid);
 189  0
         Map<String, Object> currentDeliverersMap = new HashMap<String, Object>();
 190  0
         for (RecipientDelivererConfig udc: currentDeliverers) {
 191  0
             String channelId = udc.getChannel();
 192  0
             currentDeliverersMap.put(udc.getDelivererName()+"."+channelId, Boolean.TRUE);
 193  0
         }
 194  
 
 195  
         // use for debugging, uncomment for production
 196  
         //LOG.info("CurrentDeliverersMap");
 197  
         //Iterator iter = currentDeliverersMap.keySet().iterator();
 198  
         //while (iter.hasNext()) {
 199  
         //   Object o = iter.next();           
 200  
         //   LOG.info("key: "+o.toString()+", value: "+ currentDeliverersMap.get(o) );
 201  
         //}
 202  
 
 203  0
         model.put("channels", channels);
 204  0
         model.put("deliveryTypes", deliveryTypes);
 205  0
         model.put("preferences", preferences);
 206  0
         model.put("currentDeliverersMap", currentDeliverersMap);
 207  0
         model.put("message", "Update Successful");
 208  
 
 209  0
         return new ModelAndView(VIEW, model);
 210  
     }
 211  
 }