001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.preferences.web;
017    
018    import java.util.regex.Matcher;
019    import java.util.regex.Pattern;
020    import javax.servlet.http.HttpServletRequest;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.core.api.exception.RiceRuntimeException;
024    import org.kuali.rice.kew.api.preferences.Preferences;
025    import org.kuali.rice.kew.preferences.web.PreferencesConstants;
026    import org.kuali.rice.kns.web.struts.form.KualiForm;
027    import org.kuali.rice.krad.exception.ValidationException;
028    import org.kuali.rice.krad.util.GlobalVariables;
029    import org.kuali.rice.krad.util.KRADConstants;
030    
031    
032    /**
033     * Struts ActionForm for {@link PreferencesAction}.
034     *
035     * @see PreferencesAction
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    public class PreferencesForm extends KualiForm {
040    
041        private static final long serialVersionUID = 4536869031291955777L;
042        private static final String ERR_KEY_REFRESH_RATE_WHOLE_NUM = "preferences.refreshRate";
043        private static final String ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM = "preferences.pageSize";
044            private Preferences.Builder preferences;
045        private String methodToCall = "";
046        private String returnMapping;
047        private boolean showOutbox = true;
048        private String documentTypePreferenceName;
049        private String documentTypePreferenceValue;
050    
051        // KULRICE-3137: Added a backLocation parameter similar to the one from lookups.
052        private String backLocation;
053        
054            public String getReturnMapping() {
055            return returnMapping;
056        }
057        public void setReturnMapping(String returnMapping) {
058            this.returnMapping = returnMapping;
059        }
060        public PreferencesForm() {
061            preferences = Preferences.Builder.create();
062        }
063        public String getMethodToCall() {
064            return methodToCall;
065        }
066        public void setMethodToCall(String methodToCall) {
067            Pattern p = Pattern.compile("\\w");
068            if (!StringUtils.isBlank(methodToCall)) {
069                Matcher m = p.matcher(methodToCall);
070                if (m.find()) {
071                    this.methodToCall = methodToCall;
072                } else {
073                    throw new RiceRuntimeException("invalid characters found in the parameter methodToCall");
074                }
075            } else {
076                this.methodToCall = methodToCall;
077            }
078        }
079        public Preferences.Builder getPreferences() {
080            return preferences;
081        }
082        public void setPreferences(Preferences.Builder preferences) {
083            this.preferences = preferences;
084        }
085        public boolean isShowOutbox() {
086            return this.showOutbox;
087        }
088        public void setShowOutbox(boolean showOutbox) {
089            this.showOutbox = showOutbox;
090        }
091        
092            public String getBackLocation() {
093                    return this.backLocation;
094            }
095            public void setBackLocation(String backLocation) {
096                    this.backLocation = backLocation;
097            }
098            
099            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 in whole minutes");
211            } catch (NullPointerException e1) {
212                GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be in whole minutes");
213            }
214          
215            if (GlobalVariables.getMessageMap().hasErrors()) {
216                throw new ValidationException("errors in preferences");
217            }
218        }
219    }