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