View Javadoc

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.kew.preferences.web;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  import org.apache.struts.action.ActionMessages;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.api.util.ConcreteKeyValue;
26  import org.kuali.rice.core.api.util.KeyValue;
27  import org.kuali.rice.kew.preferences.Preferences;
28  import org.kuali.rice.kew.preferences.service.PreferencesService;
29  import org.kuali.rice.kew.service.KEWServiceLocator;
30  import org.kuali.rice.kew.util.KEWConstants;
31  import org.kuali.rice.kew.web.KewKualiAction;
32  import org.kuali.rice.krad.UserSession;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  
41  /**
42   * A Struts Action for interfaces with {@link Preferences}.
43   *
44   * @see PreferencesService
45   * @see Preferences
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class PreferencesAction extends KewKualiAction {
50  
51      @Override
52  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
53          initForm(request, form);
54          request.setAttribute("Constants", getServlet().getServletContext().getAttribute("KEWConstants"));
55          return super.execute(mapping, form, request, response);
56      }
57  
58      @Override
59  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
60          PreferencesService preferencesService = (PreferencesService) KEWServiceLocator.getService(KEWServiceLocator.PREFERENCES_SERVICE);
61          PreferencesForm preferencesForm = (PreferencesForm) form;
62          preferencesForm.setPreferences(preferencesService.getPreferences(getUserSession().getPrincipalId()));
63          return mapping.findForward("basic");
64      }
65  
66      public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
67          PreferencesService prefSrv = (PreferencesService) KEWServiceLocator.getService(KEWServiceLocator.PREFERENCES_SERVICE);
68          PreferencesForm prefForm = (PreferencesForm) form;
69  
70          prefForm.validatePreferences();
71          if (GlobalVariables.getMessageMap().hasNoErrors()) {
72              prefSrv.savePreferences(getUserSession().getPrincipalId(), prefForm.getPreferences());
73          }
74          
75          GlobalVariables.getUserSession().addObject(KEWConstants.UPDATE_ACTION_LIST_ATTR_NAME, Boolean.TRUE);
76          GlobalVariables.getUserSession().removeObject(KEWConstants.PREFERENCES);
77          
78          if (! StringUtils.isEmpty(prefForm.getReturnMapping())) {
79              return mapping.findForward(prefForm.getReturnMapping());
80          }
81          return mapping.findForward("basic");
82      }
83  
84      public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
85          request.setAttribute("actionListContent", KEWConstants.ACTION_LIST_CONTENT);
86          getDelegatorFilterChoices(request);
87          getPrimaryDelegateFilterChoices(request);
88          PreferencesForm prefForm = (PreferencesForm)form;
89          prefForm.setShowOutbox(ConfigContext.getCurrentContextConfig().getOutBoxOn());
90          return null;
91      }
92  
93      public void getDelegatorFilterChoices(HttpServletRequest request) {
94          List<KeyValue> delegatorFilterChoices = new ArrayList<KeyValue>();
95          delegatorFilterChoices.add(new ConcreteKeyValue(KEWConstants.DELEGATORS_ON_FILTER_PAGE, KEWConstants.DELEGATORS_ON_FILTER_PAGE));
96          delegatorFilterChoices.add(new ConcreteKeyValue(KEWConstants.DELEGATORS_ON_ACTION_LIST_PAGE, KEWConstants.DELEGATORS_ON_ACTION_LIST_PAGE));
97          request.setAttribute("delegatorFilter", delegatorFilterChoices);
98      }
99      
100     public void getPrimaryDelegateFilterChoices(HttpServletRequest request) {
101     	List<KeyValue> primaryDelegateFilterChoices = new ArrayList<KeyValue>();
102     	primaryDelegateFilterChoices.add(new ConcreteKeyValue(KEWConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE, KEWConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE));
103         primaryDelegateFilterChoices.add(new ConcreteKeyValue(KEWConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE, KEWConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE));
104         request.setAttribute("primaryDelegateFilter", primaryDelegateFilterChoices);
105     }
106 
107     private static UserSession getUserSession() {
108         return GlobalVariables.getUserSession();
109     }
110 }