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 org.apache.commons.lang.StringUtils; 019import org.apache.http.client.utils.URIBuilder; 020import org.apache.struts.action.*; 021import org.kuali.rice.core.api.config.property.ConfigContext; 022import org.kuali.rice.core.api.util.ConcreteKeyValue; 023import org.kuali.rice.core.api.util.KeyValue; 024import org.kuali.rice.core.api.util.RiceConstants; 025import org.kuali.rice.kew.api.KewApiConstants; 026import org.kuali.rice.kew.api.KewApiServiceLocator; 027import org.kuali.rice.kew.api.doctype.DocumentType; 028import org.kuali.rice.kew.api.preferences.Preferences; 029import org.kuali.rice.kew.api.preferences.PreferencesService; 030import org.kuali.rice.kew.web.KewKualiAction; 031import org.kuali.rice.krad.UserSession; 032import org.kuali.rice.krad.util.GlobalVariables; 033import org.kuali.rice.krad.util.KRADConstants; 034 035import javax.servlet.http.HttpServletRequest; 036import javax.servlet.http.HttpServletResponse; 037import java.util.ArrayList; 038import java.util.List; 039 040 041/** 042 * A Struts Action for interfaces with {@link Preferences}. 043 * 044 * @see PreferencesService 045 * @see Preferences 046 * 047 * @author Kuali Rice Team (rice.collab@kuali.org) 048 */ 049public class PreferencesAction extends KewKualiAction { 050 051 private static final String DOC_TYPE_NAME_PROPERTY = "documentTypePreferenceName"; 052 private static final String DOCUMENT_TYPE_ERROR = "docType.preference.name.required"; 053 private static final String DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE = "docType.preference.added.message"; 054 private static final String DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE = "docType.preference.removed.message"; 055 private static final String DOC_TYPE_PARAM = "documentType"; 056 private static final String PREFERENCE_VALUE_PARAM = "preferenceValue"; 057 public static final String SAVE_REMINDER_ATTR = "saveReminder"; 058 059 private PreferencesService preferencesService; 060 061 @Override 062 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 063 initForm(request, form); 064 request.setAttribute("Constants", getServlet().getServletContext().getAttribute("KewApiConstants")); 065 return super.execute(mapping, form, request, response); 066 } 067 068 @Override 069 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 070 PreferencesForm preferencesForm = (PreferencesForm) form; 071 org.kuali.rice.kew.api.preferences.Preferences preferences = getPreferencesService().getPreferences( 072 getUserSession().getPrincipalId()); 073 preferencesForm.setPreferences(org.kuali.rice.kew.api.preferences.Preferences.Builder.create(preferences)); 074 return mapping.findForward("basic"); 075 } 076 077 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 078 PreferencesForm prefForm = (PreferencesForm) form; 079 080 prefForm.validatePreferences(); 081 if (GlobalVariables.getMessageMap().hasNoErrors()) { 082 getPreferencesService().savePreferences(getUserSession().getPrincipalId(), prefForm.getPreferences().build()); 083 } 084 085 GlobalVariables.getUserSession().addObject(KewApiConstants.UPDATE_ACTION_LIST_ATTR_NAME, Boolean.TRUE); 086 GlobalVariables.getUserSession().removeObject(KewApiConstants.PREFERENCES); 087 088 if (! StringUtils.isEmpty(prefForm.getReturnMapping())) { 089 ActionForward forward = mapping.findForward(prefForm.getReturnMapping()); 090 if ("viewActionList".equals(prefForm.getReturnMapping())) { 091 // make sure we pass the targetSpec back to the ActionList 092 ActionRedirect redirect = new ActionRedirect(forward); 093 redirect.addParameter("documentTargetSpec", prefForm.getDocumentTargetSpec()); 094 redirect.addParameter("routeLogTargetSpec", prefForm.getRouteLogTargetSpec()); 095 forward = redirect; 096 } 097 return forward; 098 } 099 return mapping.findForward("basic"); 100 } 101 102 public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception { 103 request.setAttribute("actionListContent", KewApiConstants.ACTION_LIST_CONTENT); 104 getDelegatorFilterChoices(request); 105 getPrimaryDelegateFilterChoices(request); 106 PreferencesForm prefForm = (PreferencesForm)form; 107 prefForm.setShowOutbox(ConfigContext.getCurrentContextConfig().getOutBoxOn()); 108 // make sure the back location includes the targetSpec for the Action List 109 if (!StringUtils.isBlank(prefForm.getBackLocation())) { 110 URIBuilder uri = new URIBuilder(prefForm.getBackLocation()); 111 if (!StringUtils.isBlank(prefForm.getDocumentTargetSpec())) { 112 uri.addParameter("documentTargetSpec", prefForm.getDocumentTargetSpec()).build(); 113 } 114 if (!StringUtils.isBlank(prefForm.getRouteLogTargetSpec())) { 115 uri.addParameter("routeLogTargetSpec", prefForm.getRouteLogTargetSpec()).build(); 116 } 117 prefForm.setBackLocation(uri.build().toString()); 118 } 119 120 return null; 121 } 122 123 public void getDelegatorFilterChoices(HttpServletRequest request) { 124 List<KeyValue> delegatorFilterChoices = new ArrayList<KeyValue>(); 125 delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_FILTER_PAGE, KewApiConstants.DELEGATORS_ON_FILTER_PAGE)); 126 delegatorFilterChoices.add(new ConcreteKeyValue(KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE, KewApiConstants.DELEGATORS_ON_ACTION_LIST_PAGE)); 127 request.setAttribute("delegatorFilter", delegatorFilterChoices); 128 } 129 130 public void getPrimaryDelegateFilterChoices(HttpServletRequest request) { 131 List<KeyValue> primaryDelegateFilterChoices = new ArrayList<KeyValue>(); 132 primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_FILTER_PAGE)); 133 primaryDelegateFilterChoices.add(new ConcreteKeyValue(KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE, KewApiConstants.PRIMARY_DELEGATES_ON_ACTION_LIST_PAGE)); 134 request.setAttribute("primaryDelegateFilter", primaryDelegateFilterChoices); 135 } 136 137 private static UserSession getUserSession() { 138 return GlobalVariables.getUserSession(); 139 } 140 141 public ActionForward addNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 142 PreferencesForm preferencesForm = (PreferencesForm) form; 143 if(validateAddNotificationPreference(preferencesForm)) { 144 preferencesForm.getPreferences().addDocumentTypeNotificationPreference(preferencesForm.getDocumentTypePreferenceName(), preferencesForm.getDocumentTypePreferenceValue()); 145 preferencesForm.setDocumentTypePreferenceName(null); 146 preferencesForm.setDocumentTypePreferenceValue(null); 147 GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_ADDED_MESSAGE); 148 request.setAttribute(SAVE_REMINDER_ATTR, "true"); 149 } 150 return mapping.findForward(RiceConstants.MAPPING_BASIC); 151 } 152 153 public ActionForward deleteNotificationPreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 154 PreferencesForm preferencesForm = (PreferencesForm) form; 155 String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE); 156 String documentType = StringUtils.substringAfter(StringUtils.substringBeforeLast(parameterName, "."), "deleteNotificationPreference."); 157 preferencesForm.getPreferences().removeDocumentTypeNotificationPreference(documentType); 158 GlobalVariables.getMessageMap().putInfo(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_PREFERENCE_REMOVED_MESSAGE); 159 request.setAttribute(SAVE_REMINDER_ATTR, "true"); 160 return mapping.findForward(RiceConstants.MAPPING_BASIC); 161 } 162 163 private boolean validateAddNotificationPreference(PreferencesForm form) { 164 if (StringUtils.isEmpty(form.getDocumentTypePreferenceName()) || StringUtils.isEmpty(form.getDocumentTypePreferenceValue())) { 165 GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR); 166 } else { 167 DocumentType docType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(form.getDocumentTypePreferenceName()); 168 if (docType == null) { 169 GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR); 170 } 171 } 172 return GlobalVariables.getMessageMap().getErrorMessages().size() == 0; 173 } 174 175 public ActionForward registerDocumentTypePreference(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 176 PreferencesForm preferencesForm = (PreferencesForm) form; 177 this.start(mapping, preferencesForm, request, response); 178 preferencesForm.setDocumentTypePreferenceName(request.getParameter(DOC_TYPE_PARAM)); 179 preferencesForm.setDocumentTypePreferenceValue(request.getParameter(PREFERENCE_VALUE_PARAM)); 180 this.addNotificationPreference(mapping, preferencesForm, request, response); 181 return this.save(mapping, preferencesForm, request, response); 182 } 183 184 /** 185 * @return the preferencesService 186 */ 187 public PreferencesService getPreferencesService() { 188 if(this.preferencesService == null) { 189 this.preferencesService = KewApiServiceLocator.getPreferencesService(); 190 } 191 return this.preferencesService; 192 } 193 194 /** 195 * @param preferencesService the preferencesService to set 196 */ 197 public void setPreferencesService(PreferencesService preferencesService) { 198 this.preferencesService = preferencesService; 199 } 200}