001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.preferences.web;
017
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.rice.core.api.exception.RiceRuntimeException;
025import org.kuali.rice.kew.api.preferences.Preferences;
026import org.kuali.rice.kns.util.WebUtils;
027import org.kuali.rice.kns.web.struts.form.KualiForm;
028import org.kuali.rice.krad.exception.ValidationException;
029import org.kuali.rice.krad.util.GlobalVariables;
030import org.kuali.rice.krad.util.KRADConstants;
031
032
033/**
034 * Struts ActionForm for {@link PreferencesAction}.
035 *
036 * @see PreferencesAction
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class PreferencesForm extends KualiForm {
041
042    private static final long serialVersionUID = 4536869031291955777L;
043    private static final String ERR_KEY_REFRESH_RATE_WHOLE_NUM = "preferences.refreshRate";
044    private static final String ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM = "preferences.pageSize";
045        private Preferences.Builder preferences;
046    private String methodToCall = "";
047    private String returnMapping;
048    private boolean showOutbox = true;
049    private String documentTypePreferenceName;
050    private String documentTypePreferenceValue;
051    private String targetSpec;
052
053    // KULRICE-3137: Added a backLocation parameter similar to the one from lookups.
054    private String backLocation;
055    
056        public String getReturnMapping() {
057        return returnMapping;
058    }
059    public void setReturnMapping(String returnMapping) {
060        this.returnMapping = returnMapping;
061    }
062    public PreferencesForm() {
063        preferences = Preferences.Builder.create();
064    }
065    public String getMethodToCall() {
066        return methodToCall;
067    }
068    public void setMethodToCall(String methodToCall) {
069        Pattern p = Pattern.compile("\\w");
070        if (!StringUtils.isBlank(methodToCall)) {
071            Matcher m = p.matcher(methodToCall);
072            if (m.find()) {
073                this.methodToCall = methodToCall;
074            } else {
075                throw new RiceRuntimeException("invalid characters found in the parameter methodToCall");
076            }
077        } else {
078            this.methodToCall = methodToCall;
079        }
080    }
081    public Preferences.Builder getPreferences() {
082        return preferences;
083    }
084    public void setPreferences(Preferences.Builder preferences) {
085        this.preferences = preferences;
086    }
087    public boolean isShowOutbox() {
088        return this.showOutbox;
089    }
090    public void setShowOutbox(boolean showOutbox) {
091        this.showOutbox = showOutbox;
092    }
093    
094        public String getBackLocation() {
095            return WebUtils.sanitizeBackLocation(this.backLocation);
096        }
097        public void setBackLocation(String backLocation) {
098                this.backLocation = backLocation;
099        }
100        
101        public String getDocumentTypePreferenceName() {
102        return documentTypePreferenceName;
103    }
104    
105    public void setDocumentTypePreferenceName(String documentTypePreferenceName) {
106        this.documentTypePreferenceName = documentTypePreferenceName;
107    }
108    
109    public String getDocumentTypePreferenceValue() {
110        return documentTypePreferenceValue;
111    }
112    
113    public void setDocumentTypePreferenceValue(String documentTypePreferenceValue) {
114        this.documentTypePreferenceValue = documentTypePreferenceValue;
115    }
116    
117    public Object getDocumentTypeNotificationPreference(String documentType) {
118        return preferences.getDocumentTypeNotificationPreference(documentType);
119    }
120    
121    public void setDocumentTypeNotificationPreference(String documentType, String preferenceValue) {
122        preferences.addDocumentTypeNotificationPreference(documentType, preferenceValue);
123    }
124
125    public String getTargetSpec() {
126        return targetSpec;
127    }
128
129    public void setTargetSpec(String targetSpec) {
130        this.targetSpec = targetSpec;
131    }
132
133    /**
134         * Retrieves the "returnLocation" parameter after calling "populate" on the superclass.
135         * 
136         * @see org.kuali.rice.krad.web.struts.form.KualiForm#populate(javax.servlet.http.HttpServletRequest)
137         */
138        @Override
139        public void populate(HttpServletRequest request) {
140                super.populate(request);
141                
142        if (getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER) != null) {
143            String returnLocation = getParameter(request, KRADConstants.RETURN_LOCATION_PARAMETER);
144            if(returnLocation.contains(">") || returnLocation.contains("<") || returnLocation.contains("\"")) {
145                returnLocation = returnLocation.replaceAll("\"", "%22");
146                returnLocation = returnLocation.replaceAll("<", "%3C");
147                returnLocation = returnLocation.replaceAll(">","%3E");
148                
149            }
150            setBackLocation(returnLocation);
151        }
152        }
153
154    public void validatePreferences() {
155        if((!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorSaved()))  ||
156                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorInitiated())) ||
157                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorDisapproved())) ||
158                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorEnroute())) ||
159                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorApproved())) ||
160                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorFinal())) ||
161                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorProcessed())) ||
162                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorException())) ||
163                (!PreferencesConstants.PreferencesDocumentRouteStatusColors.getPreferencesDocumentRouteStatusColors().contains(preferences.getColorCanceled()))
164                ){
165            throw new RiceRuntimeException("Preferences cannot be saved since they have been tampered with. Please refresh the page and try again");
166        }
167
168        if(!PreferencesConstants.EmailNotificationPreferences.getEmailNotificationPreferences().contains(preferences.getEmailNotification())) {
169            throw new RiceRuntimeException("Email notifications cannot be saved since they have been tampered with. Please refresh the page and try again");
170        }
171
172        if(!PreferencesConstants.DelegatorFilterValues.getDelegatorFilterValues().contains(preferences.getDelegatorFilter())) {
173            throw new RiceRuntimeException("Delegator filter values cannot be saved since they have been tampered with. Please refresh the page and try again");
174
175        }
176
177        if(!PreferencesConstants.PrimaryDelegateFilterValues.getPrimaryDelegateFilterValues().contains(preferences.getPrimaryDelegateFilter())) {
178            throw new RiceRuntimeException("Primary delegator filter values cannot be saved since they have been tampered with. Please refresh the page and try again");
179        }
180
181        if((!StringUtils.isBlank(preferences.getNotifyPrimaryDelegation())) &&
182           (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getNotifyPrimaryDelegation()))) {
183            throw new RiceRuntimeException("Invalid value found for checkbox \"Recieve Primary Delegate Email\"");
184        }
185
186        if((!StringUtils.isBlank(preferences.getNotifySecondaryDelegation())) &&
187           (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getNotifySecondaryDelegation()))) {
188            throw new RiceRuntimeException("Invalid value found for checkbox \"Recieve Secondary Delegate Email\"");
189        }
190
191        if((!StringUtils.isBlank(preferences.getShowDocType())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocType())) ||
192                (!StringUtils.isBlank(preferences.getShowDocTitle())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocTitle())) ||
193                (!StringUtils.isBlank(preferences.getShowActionRequested())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowActionRequested())) ||
194                (!StringUtils.isBlank(preferences.getShowInitiator())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowInitiator())) ||
195                (!StringUtils.isBlank(preferences.getShowDelegator())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDelegator())) ||
196                (!StringUtils.isBlank(preferences.getShowDateCreated())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDateCreated())) ||
197                (!StringUtils.isBlank(preferences.getShowDateApproved())) &&(!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDateApproved())) ||
198                (!StringUtils.isBlank(preferences.getShowCurrentNode())) &&     (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowCurrentNode())) ||
199                (!StringUtils.isBlank(preferences.getShowWorkgroupRequest())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowWorkgroupRequest())) ||
200                (!StringUtils.isBlank(preferences.getShowDocumentStatus())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowDocumentStatus())) ||
201                (!StringUtils.isBlank(preferences.getShowClearFyi())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getShowClearFyi())) ||
202                (!StringUtils.isBlank(preferences.getUseOutbox())) && (!PreferencesConstants.CheckBoxValues.getCheckBoxValues().contains(preferences.getUseOutbox()))) {
203            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");
204        }
205
206        try {
207            new Integer(preferences.getRefreshRate().trim());
208        } catch (NumberFormatException e) {
209            GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
210        } catch (NullPointerException e1) {
211            GlobalVariables.getMessageMap().putError(ERR_KEY_REFRESH_RATE_WHOLE_NUM, "general.message", "ActionList Refresh Rate must be in whole minutes");
212        }
213
214        try {
215            new Integer(preferences.getPageSize().trim());
216            if((new Integer(preferences.getPageSize().trim()) <= 0) || (new Integer(preferences.getPageSize().trim()) > 500)) {
217                GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be between 1 and 500");
218            }    
219        } catch (NumberFormatException e) {
220            GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be a whole number");
221        } catch (NullPointerException e1) {
222            GlobalVariables.getMessageMap().putError(ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM, "general.message", "ActionList Page Size must be a whole number");
223        }
224      
225        if (GlobalVariables.getMessageMap().hasErrors()) {
226            throw new ValidationException("errors in preferences");
227        }
228    }
229}