View Javadoc

1   /**
2    * Copyright 2005-2015 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 java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.apache.struts.action.ActionMessages;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.kuali.rice.core.api.util.ConcreteKeyValue;
32  import org.kuali.rice.core.api.util.KeyValue;
33  import org.kuali.rice.core.api.util.RiceConstants;
34  import org.kuali.rice.kew.api.KewApiConstants;
35  import org.kuali.rice.kew.api.KewApiServiceLocator;
36  import org.kuali.rice.kew.api.doctype.DocumentType;
37  import org.kuali.rice.kew.api.preferences.Preferences;
38  import org.kuali.rice.kew.api.preferences.PreferencesService;
39  import org.kuali.rice.kew.web.KewKualiAction;
40  import org.kuali.rice.krad.UserSession;
41  import org.kuali.rice.krad.util.GlobalVariables;
42  import org.kuali.rice.krad.util.KRADConstants;
43  
44  
45  /**
46   * A Struts Action for interfaces with {@link Preferences}.
47   *
48   * @see PreferencesService
49   * @see Preferences
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  public class PreferencesAction extends KewKualiAction {
54  
55      private static final String DOC_TYPE_NAME_PROPERTY = "documentTypePreferenceName";
56      private static final String DOCUMENT_TYPE_ERROR = "docType.preference.name.required";
57      private static final String DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE = "docType.preference.added.message";
58      private static final String DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE = "docType.preference.removed.message";
59      private static final String DOC_TYPE_PARAM = "documentType";
60      private static final String PREFERENCE_VALUE_PARAM = "preferenceValue";
61      public static final String SAVE_REMINDER_ATTR = "saveReminder";
62      
63      private PreferencesService preferencesService;
64      
65      @Override
66  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
67          initForm(request, form);
68          request.setAttribute("Constants", getServlet().getServletContext().getAttribute("KewApiConstants"));
69          return super.execute(mapping, form, request, response);
70      }
71  
72      @Override
73  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
74          PreferencesForm preferencesForm = (PreferencesForm) form;
75          org.kuali.rice.kew.api.preferences.Preferences preferences = getPreferencesService().getPreferences(
76                  getUserSession().getPrincipalId());
77          preferencesForm.setPreferences(org.kuali.rice.kew.api.preferences.Preferences.Builder.create(preferences));
78          return mapping.findForward("basic");
79      }
80  
81      public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
82          PreferencesForm prefForm = (PreferencesForm) form;
83  
84          prefForm.validatePreferences();
85          if (GlobalVariables.getMessageMap().hasNoErrors()) {
86              getPreferencesService().savePreferences(getUserSession().getPrincipalId(), prefForm.getPreferences().build());
87          }
88          
89          GlobalVariables.getUserSession().addObject(KewApiConstants.UPDATE_ACTION_LIST_ATTR_NAME, Boolean.TRUE);
90          GlobalVariables.getUserSession().removeObject(KewApiConstants.PREFERENCES);
91          
92          if (! StringUtils.isEmpty(prefForm.getReturnMapping())) {
93              return mapping.findForward(prefForm.getReturnMapping());
94          }
95          return mapping.findForward("basic");
96      }
97  
98      public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
99          request.setAttribute("actionListContent", KewApiConstants.ACTION_LIST_CONTENT);
100         getDelegatorFilterChoices(request);
101         getPrimaryDelegateFilterChoices(request);
102         PreferencesForm prefForm = (PreferencesForm)form;
103         prefForm.setShowOutbox(ConfigContext.getCurrentContextConfig().getOutBoxOn());
104         return null;
105     }
106 
107     public void getDelegatorFilterChoices(HttpServletRequest request) {
108         List<KeyValue> delegatorFilterChoices = new ArrayList<KeyValue>();
109         delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_FILTER_PAGE, KewApiConstants.DELEGATORS_ON_FILTER_PAGE));
110         delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE, KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE));
111         request.setAttribute("delegatorFilter", delegatorFilterChoices);
112     }
113     
114     public void getPrimaryDelegateFilterChoices(HttpServletRequest request) {
115     	List<KeyValue> primaryDelegateFilterChoices = new ArrayList<KeyValue>();
116     	primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE));
117         primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE));
118         request.setAttribute("primaryDelegateFilter", primaryDelegateFilterChoices);
119     }
120 
121     private static UserSession getUserSession() {
122         return GlobalVariables.getUserSession();
123     }
124     
125     public ActionForward addNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
126         PreferencesForm preferencesForm = (PreferencesForm) form;
127         if(validateAddNotificationPreference(preferencesForm)) {
128             preferencesForm.getPreferences().addDocumentTypeNotificationPreference(preferencesForm.getDocumentTypePreferenceName(), preferencesForm.getDocumentTypePreferenceValue());
129             preferencesForm.setDocumentTypePreferenceName(null);
130             preferencesForm.setDocumentTypePreferenceValue(null);
131             GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE);
132             request.setAttribute(SAVE_REMINDER_ATTR, "true");
133         }
134         return mapping.findForward(RiceConstants.MAPPING_BASIC);
135     }
136     
137     public ActionForward deleteNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
138         PreferencesForm preferencesForm = (PreferencesForm) form;
139         String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
140         String documentType = StringUtils.substringAfter(StringUtils.substringBeforeLast(parameterName, "."), "deleteNotificationPreference.");
141         preferencesForm.getPreferences().removeDocumentTypeNotificationPreference(documentType);
142         GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE);
143         request.setAttribute(SAVE_REMINDER_ATTR, "true");
144         return mapping.findForward(RiceConstants.MAPPING_BASIC);
145     }
146     
147     private boolean validateAddNotificationPreference(PreferencesForm form) {
148         if (StringUtils.isEmpty(form.getDocumentTypePreferenceName()) || StringUtils.isEmpty(form.getDocumentTypePreferenceValue())) {
149             GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
150         } else {
151             DocumentType docType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(form.getDocumentTypePreferenceName());
152             if (docType == null) {
153                 GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
154             }
155         }
156         return GlobalVariables.getMessageMap().getErrorMessages().size() == 0;
157     }
158 
159     public ActionForward registerDocumentTypePreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
160         PreferencesForm preferencesForm = (PreferencesForm) form;
161         this.start(mapping, preferencesForm, request, response);
162         preferencesForm.setDocumentTypePreferenceName(request.getParameter(DOC_TYPE_PARAM));
163         preferencesForm.setDocumentTypePreferenceValue(request.getParameter(PREFERENCE_VALUE_PARAM));
164         this.addNotificationPreference(mapping, preferencesForm, request, response);
165         return this.save(mapping, preferencesForm, request, response);
166     }
167 
168     /**
169      * @return the preferencesService
170      */
171     public PreferencesService getPreferencesService() {
172         if(this.preferencesService == null) {
173             this.preferencesService = KewApiServiceLocator.getPreferencesService();
174         }
175         return this.preferencesService;
176     }
177 
178     /**
179      * @param preferencesService the preferencesService to set
180      */
181     public void setPreferencesService(PreferencesService preferencesService) {
182         this.preferencesService = preferencesService;
183     }
184 }