001 /** 002 * Copyright 2005-2013 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.kcb.web.spring; 017 018 import org.apache.log4j.Logger; 019 import org.kuali.rice.kcb.bo.RecipientDelivererConfig; 020 import org.kuali.rice.kcb.deliverer.MessageDeliverer; 021 import org.kuali.rice.kcb.exception.ErrorList; 022 import org.kuali.rice.kcb.service.KENIntegrationService; 023 import org.kuali.rice.kcb.service.MessageDelivererRegistryService; 024 import org.kuali.rice.kcb.service.RecipientPreferenceService; 025 import org.springframework.beans.factory.annotation.Required; 026 import org.springframework.web.servlet.ModelAndView; 027 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 028 029 import javax.servlet.ServletException; 030 import javax.servlet.http.HttpServletRequest; 031 import javax.servlet.http.HttpServletResponse; 032 import java.io.IOException; 033 import java.util.ArrayList; 034 import java.util.Collection; 035 import java.util.HashMap; 036 import java.util.Map; 037 038 /** 039 * This class is the controller that handles management of various user preferences interfaces (deliver types, user subscriptions, etc). 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 */ 042 public class UserPreferencesController extends MultiActionController { 043 /** Logger for this class and subclasses */ 044 private static final Logger LOG = Logger.getLogger(UserPreferencesController.class); 045 046 private static final String VIEW = "DelivererPreferences"; 047 private static final String KEW_CHANNEL = "KEW"; 048 protected RecipientPreferenceService recipientPreferenceService; 049 protected MessageDelivererRegistryService messageDelivererRegistryService; 050 protected KENIntegrationService kenIntegrationService; 051 052 /** 053 * Set the RecipientPreferenceService 054 * @param recipientPreferenceService 055 */ 056 @Required 057 public void setRecipientPreferenceService(RecipientPreferenceService userPreferenceService) { 058 this.recipientPreferenceService = userPreferenceService; 059 } 060 061 /** 062 * Set the MessageDelivererRegistryService 063 * @param messageDelivererRegistryService 064 */ 065 @Required 066 public void setMessageDelivererRegistryService(MessageDelivererRegistryService messageDelivererRegistryService) { 067 this.messageDelivererRegistryService = messageDelivererRegistryService; 068 } 069 070 /** 071 * Sets the KENIntegrationService 072 * @param kis the KENIntegrationService 073 */ 074 @Required 075 public void setKenIntegrationService(KENIntegrationService kis) { 076 this.kenIntegrationService = kis; 077 } 078 079 /** 080 * @return all channels for Rice, including the builtin KEW action list "channel" 081 */ 082 protected Collection<String> getAllChannels() { 083 // TODO: does not traverse bus yet 084 Collection<String> allChannels = new ArrayList<String>(); 085 //allChannels.add(KEW_CHANNEL); 086 allChannels.addAll(kenIntegrationService.getAllChannelNames()); 087 return allChannels; 088 } 089 090 /** 091 * displayDelivererConfigurationForm - obtain information necessary 092 * for displaying all possible Deliverer types and forward to the form 093 * @param request 094 * @param response 095 * @return 096 * @throws ServletException 097 * @throws IOException 098 */ 099 public ModelAndView displayDelivererConfigurationForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 100 String userid = request.getRemoteUser(); 101 LOG.debug("remoteUser: "+userid); 102 103 // Get DeliveryType classes 104 Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers(); 105 106 // get all channels 107 Collection<String> channels = getAllChannels(); 108 109 // get all user preferences in a HashMap 110 HashMap<String, String> preferences = this.recipientPreferenceService.getRecipientPreferences(userid); 111 112 // get existing configured deliverers 113 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 Map<String, Boolean> currentDeliverersMap = new HashMap<String, Boolean>(); 116 for (RecipientDelivererConfig udc: currentDeliverers) { 117 String channelName = udc.getChannel(); 118 currentDeliverersMap.put(udc.getDelivererName() + "." + channelName, Boolean.TRUE); 119 } 120 121 Map<String, Object> model = new HashMap<String, Object>(); 122 model.put("channels", channels); 123 model.put("deliveryTypes", deliveryTypes); 124 model.put("preferences", preferences); 125 model.put("currentDeliverersMap", currentDeliverersMap); 126 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 String userid = request.getRemoteUser(); 139 LOG.debug("remoteUser: "+userid); 140 boolean error = false; 141 142 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 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 Collection<MessageDeliverer> deliveryTypes = this.messageDelivererRegistryService.getAllDeliverers(); 152 153 // first remove all configured user delivers for this user 154 this.recipientPreferenceService.removeRecipientDelivererConfigs(userid); 155 156 for (MessageDeliverer dt: deliveryTypes) { 157 String deliveryTypeName = dt.getName(); 158 HashMap<String,String> prefMap = dt.getPreferenceKeys(); 159 LOG.debug("deliveryName: "+deliveryTypeName); 160 HashMap<String, String> userprefs = new HashMap<String, String>(); 161 for (String prefKey:prefMap.keySet()) { 162 LOG.debug(" key: "+prefKey+", value: "+request.getParameter(deliveryTypeName+"."+prefKey)); 163 userprefs.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey )); 164 preferences.put(deliveryTypeName+"."+prefKey, request.getParameter(deliveryTypeName+"."+prefKey )); 165 } 166 try { 167 this.recipientPreferenceService.saveRecipientPreferences(userid, userprefs, dt); 168 } catch (ErrorList errorlist) { 169 error = true; 170 model.put("errorList", errorlist.getErrors()) ; 171 } 172 173 // get channelName.channels 174 String[] channels = request.getParameterValues(deliveryTypeName+".channels"); 175 if (channels != null && channels.length > 0) { 176 for (int j=0; j < channels.length; j++) { 177 LOG.debug(deliveryTypeName+".channels["+j+"] "+channels[j]); 178 } 179 } 180 // now save the userid, channel selection 181 this.recipientPreferenceService.saveRecipientDelivererConfig(userid, deliveryTypeName, channels); 182 } 183 184 // get all channels 185 Collection<String> channels = getAllChannels(); 186 187 // get existing configured deliverers 188 Collection<RecipientDelivererConfig> currentDeliverers = this.recipientPreferenceService.getDeliverersForRecipient(userid); 189 Map<String, Object> currentDeliverersMap = new HashMap<String, Object>(); 190 for (RecipientDelivererConfig udc: currentDeliverers) { 191 String channelId = udc.getChannel(); 192 currentDeliverersMap.put(udc.getDelivererName()+"."+channelId, Boolean.TRUE); 193 } 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 model.put("channels", channels); 204 model.put("deliveryTypes", deliveryTypes); 205 model.put("preferences", preferences); 206 model.put("currentDeliverersMap", currentDeliverersMap); 207 model.put("message", "Update Successful"); 208 209 return new ModelAndView(VIEW, model); 210 } 211 }