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