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