View Javadoc
1   /**
2    * Copyright 2005-2016 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.kew.preferences.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.http.client.utils.URIBuilder;
20  import org.apache.struts.action.*;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.core.api.util.ConcreteKeyValue;
23  import org.kuali.rice.core.api.util.KeyValue;
24  import org.kuali.rice.core.api.util.RiceConstants;
25  import org.kuali.rice.kew.api.KewApiConstants;
26  import org.kuali.rice.kew.api.KewApiServiceLocator;
27  import org.kuali.rice.kew.api.doctype.DocumentType;
28  import org.kuali.rice.kew.api.preferences.Preferences;
29  import org.kuali.rice.kew.api.preferences.PreferencesService;
30  import org.kuali.rice.kew.web.KewKualiAction;
31  import org.kuali.rice.krad.UserSession;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.KRADConstants;
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      private static final String DOC_TYPE_NAME_PROPERTY = "documentTypePreferenceName";
52      private static final String DOCUMENT_TYPE_ERROR = "docType.preference.name.required";
53      private static final String DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE = "docType.preference.added.message";
54      private static final String DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE = "docType.preference.removed.message";
55      private static final String DOC_TYPE_PARAM = "documentType";
56      private static final String PREFERENCE_VALUE_PARAM = "preferenceValue";
57      public static final String SAVE_REMINDER_ATTR = "saveReminder";
58      
59      private PreferencesService preferencesService;
60      
61      @Override
62  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
63          initForm(request, form);
64          request.setAttribute("Constants", getServlet().getServletContext().getAttribute("KewApiConstants"));
65          return super.execute(mapping, form, request, response);
66      }
67  
68      @Override
69  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
70          PreferencesForm preferencesForm = (PreferencesForm) form;
71          org.kuali.rice.kew.api.preferences.Preferences preferences = getPreferencesService().getPreferences(
72                  getUserSession().getPrincipalId());
73          preferencesForm.setPreferences(org.kuali.rice.kew.api.preferences.Preferences.Builder.create(preferences));
74          return mapping.findForward("basic");
75      }
76  
77      public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
78          PreferencesForm prefForm = (PreferencesForm) form;
79  
80          prefForm.validatePreferences();
81          if (GlobalVariables.getMessageMap().hasNoErrors()) {
82              getPreferencesService().savePreferences(getUserSession().getPrincipalId(), prefForm.getPreferences().build());
83          }
84          
85          GlobalVariables.getUserSession().addObject(KewApiConstants.UPDATE_ACTION_LIST_ATTR_NAME, Boolean.TRUE);
86          GlobalVariables.getUserSession().removeObject(KewApiConstants.PREFERENCES);
87          
88          if (! StringUtils.isEmpty(prefForm.getReturnMapping())) {
89              ActionForward forward = mapping.findForward(prefForm.getReturnMapping());
90              if ("viewActionList".equals(prefForm.getReturnMapping())) {
91                  // make sure we pass the targetSpec back to the ActionList
92                  ActionRedirect redirect = new ActionRedirect(forward);
93                  redirect.addParameter("documentTargetSpec", prefForm.getDocumentTargetSpec());
94                  redirect.addParameter("routeLogTargetSpec", prefForm.getRouteLogTargetSpec());
95                  forward = redirect;
96              }
97              return forward;
98          }
99          return mapping.findForward("basic");
100     }
101 
102     public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
103         request.setAttribute("actionListContent", KewApiConstants.ACTION_LIST_CONTENT);
104         getDelegatorFilterChoices(request);
105         getPrimaryDelegateFilterChoices(request);
106         PreferencesForm prefForm = (PreferencesForm)form;
107         prefForm.setShowOutbox(ConfigContext.getCurrentContextConfig().getOutBoxOn());
108         // make sure the back location includes the targetSpec for the Action List
109         if (!StringUtils.isBlank(prefForm.getBackLocation())) {
110             URIBuilder uri = new URIBuilder(prefForm.getBackLocation());
111             if (!StringUtils.isBlank(prefForm.getDocumentTargetSpec())) {
112                 uri.addParameter("documentTargetSpec", prefForm.getDocumentTargetSpec()).build();
113             }
114             if (!StringUtils.isBlank(prefForm.getRouteLogTargetSpec())) {
115                 uri.addParameter("routeLogTargetSpec", prefForm.getRouteLogTargetSpec()).build();
116             }
117             prefForm.setBackLocation(uri.build().toString());
118         }
119 
120         return null;
121     }
122 
123     public void getDelegatorFilterChoices(HttpServletRequest request) {
124         List<KeyValue> delegatorFilterChoices = new ArrayList<KeyValue>();
125         delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_FILTER_PAGE, KewApiConstants.DELEGATORS_ON_FILTER_PAGE));
126         delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE, KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE));
127         request.setAttribute("delegatorFilter", delegatorFilterChoices);
128     }
129     
130     public void getPrimaryDelegateFilterChoices(HttpServletRequest request) {
131     	List<KeyValue> primaryDelegateFilterChoices = new ArrayList<KeyValue>();
132     	primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE));
133         primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE));
134         request.setAttribute("primaryDelegateFilter", primaryDelegateFilterChoices);
135     }
136 
137     private static UserSession getUserSession() {
138         return GlobalVariables.getUserSession();
139     }
140     
141     public ActionForward addNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
142         PreferencesForm preferencesForm = (PreferencesForm) form;
143         if(validateAddNotificationPreference(preferencesForm)) {
144             preferencesForm.getPreferences().addDocumentTypeNotificationPreference(preferencesForm.getDocumentTypePreferenceName(), preferencesForm.getDocumentTypePreferenceValue());
145             preferencesForm.setDocumentTypePreferenceName(null);
146             preferencesForm.setDocumentTypePreferenceValue(null);
147             GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE);
148             request.setAttribute(SAVE_REMINDER_ATTR, "true");
149         }
150         return mapping.findForward(RiceConstants.MAPPING_BASIC);
151     }
152     
153     public ActionForward deleteNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
154         PreferencesForm preferencesForm = (PreferencesForm) form;
155         String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
156         String documentType = StringUtils.substringAfter(StringUtils.substringBeforeLast(parameterName, "."), "deleteNotificationPreference.");
157         preferencesForm.getPreferences().removeDocumentTypeNotificationPreference(documentType);
158         GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE);
159         request.setAttribute(SAVE_REMINDER_ATTR, "true");
160         return mapping.findForward(RiceConstants.MAPPING_BASIC);
161     }
162     
163     private boolean validateAddNotificationPreference(PreferencesForm form) {
164         if (StringUtils.isEmpty(form.getDocumentTypePreferenceName()) || StringUtils.isEmpty(form.getDocumentTypePreferenceValue())) {
165             GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
166         } else {
167             DocumentType docType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(form.getDocumentTypePreferenceName());
168             if (docType == null) {
169                 GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
170             }
171         }
172         return GlobalVariables.getMessageMap().getErrorMessages().size() == 0;
173     }
174 
175     public ActionForward registerDocumentTypePreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
176         PreferencesForm preferencesForm = (PreferencesForm) form;
177         this.start(mapping, preferencesForm, request, response);
178         preferencesForm.setDocumentTypePreferenceName(request.getParameter(DOC_TYPE_PARAM));
179         preferencesForm.setDocumentTypePreferenceValue(request.getParameter(PREFERENCE_VALUE_PARAM));
180         this.addNotificationPreference(mapping, preferencesForm, request, response);
181         return this.save(mapping, preferencesForm, request, response);
182     }
183 
184     /**
185      * @return the preferencesService
186      */
187     public PreferencesService getPreferencesService() {
188         if(this.preferencesService == null) {
189             this.preferencesService = KewApiServiceLocator.getPreferencesService();
190         }
191         return this.preferencesService;
192     }
193 
194     /**
195      * @param preferencesService the preferencesService to set
196      */
197     public void setPreferencesService(PreferencesService preferencesService) {
198         this.preferencesService = preferencesService;
199     }
200 }