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