View Javadoc

1   /**
2    * Copyright 2005-2013 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  public class UserPreferencesController extends MultiActionController {
43      /** Logger for this class and subclasses */
44      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          this.recipientPreferenceService = userPreferenceService;
59      }
60  
61      /**
62       * Set the MessageDelivererRegistryService
63       * @param messageDelivererRegistryService
64       */
65      @Required
66      public void setMessageDelivererRegistryService(MessageDelivererRegistryService messageDelivererRegistryService) {
67          this.messageDelivererRegistryService = messageDelivererRegistryService;
68      }
69  
70      /**
71       * Sets the KENIntegrationService
72       * @param kis the KENIntegrationService
73       */
74      @Required
75      public void setKenIntegrationService(KENIntegrationService kis) {
76          this.kenIntegrationService = kis;
77      }
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          Collection<String> allChannels = new ArrayList<String>();
85          //allChannels.add(KEW_CHANNEL);
86          allChannels.addAll(kenIntegrationService.getAllChannelNames());
87          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         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 }