View Javadoc

1   /**
2    * Copyright 2005-2012 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.kns.web.struts.form.KualiForm;
26  import org.kuali.rice.krad.exception.ValidationException;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.KRADConstants;
29  
30  
31  /**
32   * Struts ActionForm for {@link PreferencesAction}.
33   *
34   * @see PreferencesAction
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class PreferencesForm extends KualiForm {
39  
40      private static final long serialVersionUID = 4536869031291955777L;
41      private static final String ERR_KEY_REFRESH_RATE_WHOLE_NUM = "preferences.refreshRate";
42      private static final String ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM = "preferences.pageSize";
43  	private Preferences.Builder preferences;
44      private String methodToCall = "";
45      private String returnMapping;
46      private boolean showOutbox = true;
47      private String documentTypePreferenceName;
48      private String documentTypePreferenceValue;
49  
50      // KULRICE-3137: Added a backLocation parameter similar to the one from lookups.
51      private String backLocation;
52      
53  	public String getReturnMapping() {
54          return returnMapping;
55      }
56      public void setReturnMapping(String returnMapping) {
57          this.returnMapping = returnMapping;
58      }
59      public PreferencesForm() {
60          preferences = Preferences.Builder.create();
61      }
62      public String getMethodToCall() {
63          return methodToCall;
64      }
65      public void setMethodToCall(String methodToCall) {
66          Pattern p = Pattern.compile("\\w");
67          if (!StringUtils.isBlank(methodToCall)) {
68              Matcher m = p.matcher(methodToCall);
69              if (m.find()) {
70                  this.methodToCall = methodToCall;
71              } else {
72                  throw new RiceRuntimeException("invalid characters found in the parameter methodToCall");
73              }
74          } else {
75              this.methodToCall = methodToCall;
76          }
77      }
78      public Preferences.Builder getPreferences() {
79          return preferences;
80      }
81      public void setPreferences(Preferences.Builder preferences) {
82          this.preferences = preferences;
83      }
84      public boolean isShowOutbox() {
85          return this.showOutbox;
86      }
87      public void setShowOutbox(boolean showOutbox) {
88          this.showOutbox = showOutbox;
89      }
90      
91  	public String getBackLocation() {
92  		return this.backLocation;
93  	}
94  	public void setBackLocation(String backLocation) {
95  		this.backLocation = backLocation;
96  	}
97  	
98  	public String getDocumentTypePreferenceName() {
99          return documentTypePreferenceName;
100     }
101     
102     public void setDocumentTypePreferenceName(String documentTypePreferenceName) {
103         this.documentTypePreferenceName = documentTypePreferenceName;
104     }
105     
106     public String getDocumentTypePreferenceValue() {
107         return documentTypePreferenceValue;
108     }
109     
110     public void setDocumentTypePreferenceValue(String documentTypePreferenceValue) {
111         this.documentTypePreferenceValue = documentTypePreferenceValue;
112     }
113     
114     public Object getDocumentTypeNotificationPreference(String documentType) {
115         return preferences.getDocumentTypeNotificationPreference(documentType);
116     }
117     
118     public void setDocumentTypeNotificationPreference(String documentType, String preferenceValue) {
119         preferences.addDocumentTypeNotificationPreference(documentType, preferenceValue);
120     }
121 	
122 	/**
123 	 * Retrieves the "returnLocation" parameter after calling "populate" on the superclass.
124 	 * 
125 	 * @see org.kuali.rice.krad.web.struts.form.KualiForm#populate(javax.servlet.http.HttpServletRequest)
126 	 */
127 	@Override
128 	public void populate(HttpServletRequest request) {
129 		super.populate(request);
130 		
131         if (getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER) != null) {
132             String returnLocation = getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER);
133             if(returnLocation.contains(">") || returnLocation.contains("<") || returnLocation.contains("\"")) {
134                 returnLocation = returnLocation.replaceAll("\"", "%22");
135                 returnLocation = returnLocation.replaceAll("<", "%3C");
136                 returnLocation = returnLocation.replaceAll(">","%3E");
137                 
138             }
139             setBackLocation(returnLocation);
140         }
141 	}
142 
143     public void validatePreferences() {
144      
145         try {
146             new Integer(preferences.getRefreshRate().trim());
147         } catch (NumberFormatException e) {
148             GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
149         } catch (NullPointerException e1) {
150             GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
151         }
152 
153         try {
154             if(new Integer(preferences.getPageSize().trim()) == 0){
155             	 GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be non-zero ");
156             }    
157         } catch (NumberFormatException e) {
158             GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be in whole minutes");
159         } catch (NullPointerException e1) {
160             GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be in whole minutes");
161         }
162       
163         if (GlobalVariables.getMessageMap().hasErrors()) {
164             throw new ValidationException("errors in preferences");
165         }
166     }
167 }