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