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