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