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.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.core.api.exception.RiceRuntimeException;
25  import org.kuali.rice.kew.api.preferences.Preferences;
26  import org.kuali.rice.kns.util.WebUtils;
27  import org.kuali.rice.kns.web.struts.form.KualiForm;
28  import org.kuali.rice.krad.exception.ValidationException;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.KRADConstants;
31  
32  
33  /**
34   * Struts ActionForm for {@link PreferencesAction}.
35   *
36   * @see PreferencesAction
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class PreferencesForm extends KualiForm {
41  
42      private static final long serialVersionUID = 4536869031291955777L;
43      private static final String ERR_KEY_REFRESH_RATE_WHOLE_NUM = "preferences.refreshRate";
44      private static final String ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM = "preferences.pageSize";
45  	private Preferences.Builder preferences;
46      private String methodToCall = "";
47      private String returnMapping;
48      private boolean showOutbox = true;
49      private String documentTypePreferenceName;
50      private String documentTypePreferenceValue;
51  
52      // KULRICE-3137: Added a backLocation parameter similar to the one from lookups.
53      private String backLocation;
54      
55  	public String getReturnMapping() {
56          return returnMapping;
57      }
58      public void setReturnMapping(String returnMapping) {
59          this.returnMapping = returnMapping;
60      }
61      public PreferencesForm() {
62          preferences = Preferences.Builder.create();
63      }
64      public String getMethodToCall() {
65          return methodToCall;
66      }
67      public void setMethodToCall(String methodToCall) {
68          Pattern p = Pattern.compile("\\w");
69          if (!StringUtils.isBlank(methodToCall)) {
70              Matcher m = p.matcher(methodToCall);
71              if (m.find()) {
72                  this.methodToCall = methodToCall;
73              } else {
74                  throw new RiceRuntimeException("invalid characters found in the parameter methodToCall");
75              }
76          } else {
77              this.methodToCall = methodToCall;
78          }
79      }
80      public Preferences.Builder getPreferences() {
81          return preferences;
82      }
83      public void setPreferences(Preferences.Builder preferences) {
84          this.preferences = preferences;
85      }
86      public boolean isShowOutbox() {
87          return this.showOutbox;
88      }
89      public void setShowOutbox(boolean showOutbox) {
90          this.showOutbox = showOutbox;
91      }
92      
93  	public String getBackLocation() {
94  	    return WebUtils.sanitizeBackLocation(this.backLocation);
95  	}
96  	public void setBackLocation(String backLocation) {
97  		this.backLocation = backLocation;
98  	}
99  	
100 	public String getDocumentTypePreferenceName() {
101         return documentTypePreferenceName;
102     }
103     
104     public void setDocumentTypePreferenceName(String documentTypePreferenceName) {
105         this.documentTypePreferenceName = documentTypePreferenceName;
106     }
107     
108     public String getDocumentTypePreferenceValue() {
109         return documentTypePreferenceValue;
110     }
111     
112     public void setDocumentTypePreferenceValue(String documentTypePreferenceValue) {
113         this.documentTypePreferenceValue = documentTypePreferenceValue;
114     }
115     
116     public Object getDocumentTypeNotificationPreference(String documentType) {
117         return preferences.getDocumentTypeNotificationPreference(documentType);
118     }
119     
120     public void setDocumentTypeNotificationPreference(String documentType, String preferenceValue) {
121         preferences.addDocumentTypeNotificationPreference(documentType, preferenceValue);
122     }
123 	
124 	/**
125 	 * Retrieves the "returnLocation" parameter after calling "populate" on the superclass.
126 	 * 
127 	 * @see org.kuali.rice.krad.web.struts.form.KualiForm#populate(javax.servlet.http.HttpServletRequest)
128 	 */
129 	@Override
130 	public void populate(HttpServletRequest request) {
131 		super.populate(request);
132 		
133         if (getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER) != null) {
134             String returnLocation = getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER);
135             if(returnLocation.contains(">") || returnLocation.contains("<") || returnLocation.contains("\"")) {
136                 returnLocation = returnLocation.replaceAll("\"", "%22");
137                 returnLocation = returnLocation.replaceAll("<", "%3C");
138                 returnLocation = returnLocation.replaceAll(">","%3E");
139                 
140             }
141             setBackLocation(returnLocation);
142         }
143 	}
144 
145     public void validatePreferences() {
146         if((!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorSaved()))  ||
147                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorInitiated())) ||
148                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorDisapproved())) ||
149                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorEnroute())) ||
150                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorApproved())) ||
151                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorFinal())) ||
152                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorProcessed())) ||
153                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorException())) ||
154                 (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorCanceled()))
155                 ){
156             throw new RiceRuntimeException("Preferences cannot be saved since they have been tampered with. Please refresh the page and try again");
157         }
158 
159         if(!PreferencesConstants.EmailNotificationPreferences.getEmailNotificationPreferences().contains(preferences.getEmailNotification())) {
160             throw new RiceRuntimeException("Email notifications cannot be saved since they have been tampered with. Please refresh the page and try again");
161         }
162 
163         if(!PreferencesConstants.DelegatorFilterValues.getDelegatorFilterValues().contains(preferences.getDelegatorFilter())) {
164             throw new RiceRuntimeException("Delegator filter values cannot be saved since they have been tampered with. Please refresh the page and try again");
165 
166         }
167 
168         if(!PreferencesConstants.PrimaryDelegateFilterValues.getPrimaryDelegateFilterValues().contains(preferences.getPrimaryDelegateFilter())) {
169             throw new RiceRuntimeException("Primary delegator filter values cannot be saved since they have been tampered with. Please refresh the page and try again");
170         }
171 
172         if((!StringUtils.isBlank(preferences.getNotifyPrimaryDelegation())) &&
173            (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getNotifyPrimaryDelegation()))) {
174             throw new RiceRuntimeException("Invalid value found for checkbox \"Recieve Primary Delegate Email\"");
175         }
176 
177         if((!StringUtils.isBlank(preferences.getNotifySecondaryDelegation())) &&
178            (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getNotifySecondaryDelegation()))) {
179             throw new RiceRuntimeException("Invalid value found for checkbox \"Recieve Secondary Delegate Email\"");
180         }
181 
182         if((!StringUtils.isBlank(preferences.getShowDocType())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocType())) ||
183                 (!StringUtils.isBlank(preferences.getShowDocTitle())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocTitle())) ||
184                 (!StringUtils.isBlank(preferences.getShowActionRequested())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowActionRequested())) ||
185                 (!StringUtils.isBlank(preferences.getShowInitiator())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowInitiator())) ||
186                 (!StringUtils.isBlank(preferences.getShowDelegator())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDelegator())) ||
187                 (!StringUtils.isBlank(preferences.getShowDateCreated())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDateCreated())) ||
188                 (!StringUtils.isBlank(preferences.getShowDateApproved())) &&(!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDateApproved())) ||
189                 (!StringUtils.isBlank(preferences.getShowCurrentNode())) &&	(!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowCurrentNode())) ||
190                 (!StringUtils.isBlank(preferences.getShowWorkgroupRequest())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowWorkgroupRequest())) ||
191                 (!StringUtils.isBlank(preferences.getShowDocumentStatus())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocumentStatus())) ||
192                 (!StringUtils.isBlank(preferences.getShowClearFyi())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowClearFyi())) ||
193                 (!StringUtils.isBlank(preferences.getUseOutbox())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getUseOutbox()))) {
194             throw new RiceRuntimeException("Preferences for fields displayed in action list cannot be saved since they have in tampered with. Please refresh the page and try again");
195         }
196 
197         try {
198             new Integer(preferences.getRefreshRate().trim());
199         } catch (NumberFormatException e) {
200             GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
201         } catch (NullPointerException e1) {
202             GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
203         }
204 
205         try {
206             new Integer(preferences.getPageSize().trim());
207             if((new Integer(preferences.getPageSize().trim()) <= 0) || (new Integer(preferences.getPageSize().trim()) > 500)) {
208                 GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be between 1 and 500");
209             }    
210         } catch (NumberFormatException e) {
211             GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be a whole number");
212         } catch (NullPointerException e1) {
213             GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be a whole number");
214         }
215       
216         if (GlobalVariables.getMessageMap().hasErrors()) {
217             throw new ValidationException("errors in preferences");
218         }
219     }
220 }