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.actionlist.web;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.http.client.utils.URIBuilder;
020import org.apache.struts.action.ActionForm;
021import org.apache.struts.action.ActionForward;
022import org.apache.struts.action.ActionMapping;
023import org.apache.struts.action.ActionRedirect;
024import org.kuali.rice.core.api.util.ConcreteKeyValue;
025import org.kuali.rice.core.api.util.KeyValue;
026import org.kuali.rice.kew.actionlist.ActionListFilter;
027import org.kuali.rice.kew.actionlist.service.ActionListService;
028import org.kuali.rice.kew.api.KewApiConstants;
029import org.kuali.rice.kew.api.KewApiServiceLocator;
030import org.kuali.rice.kew.api.preferences.Preferences;
031import org.kuali.rice.kew.api.preferences.PreferencesService;
032import org.kuali.rice.kew.service.KEWServiceLocator;
033import org.kuali.rice.kim.api.group.Group;
034import org.kuali.rice.kim.api.services.KimApiServiceLocator;
035import org.kuali.rice.kns.web.struts.action.KualiAction;
036import org.kuali.rice.krad.UserSession;
037import org.kuali.rice.krad.util.GlobalVariables;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.http.HttpServletResponse;
041import java.net.URI;
042import java.util.ArrayList;
043import java.util.Collections;
044import java.util.List;
045
046
047/**
048 * Action for Action List Filter page.
049 *
050 * @author Kuali Rice Team (rice.collab@kuali.org)
051 */
052public class ActionListFilterAction extends KualiAction {
053
054    @Override
055    public ActionForward execute(ActionMapping mapping, ActionForm form,
056                                 HttpServletRequest request, HttpServletResponse response)
057            throws Exception {
058        request.setAttribute("Constants", getServlet().getServletContext().getAttribute("KewApiConstants"));
059        request.setAttribute("preferences", this.getUserSession().retrieveObject(KewApiConstants.PREFERENCES));
060        initForm(request, form);
061        return super.execute(mapping, form, request, response);
062    }
063
064    @Override
065    protected String getReturnLocation(HttpServletRequest request,
066                                       ActionMapping mapping)
067    {
068        String mappingPath = mapping.getPath();
069        String basePath = getApplicationBaseUrl();
070        return basePath + KewApiConstants.WEBAPP_DIRECTORY + mappingPath + ".do";
071    }
072
073    public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
074        ActionListFilterForm filterForm = (ActionListFilterForm) form;
075        final UserSession uSession = getUserSession();
076        final ActionListFilter filter = (ActionListFilter) uSession.retrieveObject(KewApiConstants.ACTION_LIST_FILTER_ATTR_NAME);
077        if (filter != null) {
078            if (filterForm.getDocTypeFullName() != null && ! "".equals(filterForm.getDocTypeFullName())) {
079                filter.setDocumentType(filterForm.getDocTypeFullName());
080                uSession.addObject(KewApiConstants.ACTION_LIST_FILTER_ATTR_NAME, filter);
081                filterForm.setFilter(filter);
082            } else {
083                filterForm.setFilter(filter);
084                filterForm.setDocTypeFullName(filter.getDocumentType());
085            }
086        }
087        return mapping.findForward("viewFilter");
088    }
089
090    public ActionForward filter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
091        ActionListFilterForm filterForm = (ActionListFilterForm) form;
092        //validate the filter through the actionitem/actionlist service (I'm thinking actionlistservice)
093        final UserSession uSession = getUserSession();
094        ActionListFilter alFilter = filterForm.getLoadedFilter();
095        if (StringUtils.isNotBlank(alFilter.getDelegatorId()) && !KewApiConstants.DELEGATION_DEFAULT.equals(alFilter.getDelegatorId()) &&
096                StringUtils.isNotBlank(alFilter.getPrimaryDelegateId()) && !KewApiConstants.PRIMARY_DELEGATION_DEFAULT.equals(alFilter.getPrimaryDelegateId())){
097            // If the primary and secondary delegation drop-downs are both visible and are both set to non-default values,
098            // then reset the secondary delegation drop-down to its default value.
099            alFilter.setDelegatorId(KewApiConstants.DELEGATION_DEFAULT);
100        }
101        uSession.addObject(KewApiConstants.ACTION_LIST_FILTER_ATTR_NAME, alFilter);
102        if (GlobalVariables.getMessageMap().hasNoErrors()) {
103            request.getSession().setAttribute(KewApiConstants.REQUERY_ACTION_LIST_KEY, "true");
104            ActionForward forward = mapping.findForward("viewActionList");
105            // make sure we pass the targetSpecs back to the ActionList
106            ActionRedirect redirect = new ActionRedirect(forward);
107            redirect.addParameter("documentTargetSpec", filterForm.getDocumentTargetSpec());
108            redirect.addParameter("routeLogTargetSpec", filterForm.getRouteLogTargetSpec());
109            return redirect;
110        }
111        return mapping.findForward("viewFilter");
112    }
113
114    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
115        ActionListFilterForm filterForm = (ActionListFilterForm) form;
116        filterForm.setFilter(new ActionListFilter());
117        filterForm.setCreateDateFrom("");
118        filterForm.setCreateDateTo("");
119        filterForm.setLastAssignedDateFrom("");
120        filterForm.setLastAssignedDateTo("");
121        filterForm.setDocTypeFullName("");
122        UserSession session = getUserSession();
123        session.removeObject(KewApiConstants.ACTION_LIST_FILTER_ATTR_NAME);
124        return mapping.findForward("viewFilter");
125    }
126
127    public void initForm(HttpServletRequest request, ActionForm form) throws Exception {
128        ActionListFilterForm filterForm = (ActionListFilterForm) form;
129        filterForm.setUserWorkgroups(getUserWorkgroupsDropDownList(getUserSession().getPrincipalId()));
130        PreferencesService prefSrv = KewApiServiceLocator.getPreferencesService();
131        Preferences preferences = prefSrv.getPreferences(getUserSession().getPrincipalId());
132        request.setAttribute("preferences", preferences);
133        ActionListService actionListSrv = KEWServiceLocator.getActionListService();
134        request.setAttribute("delegators", ActionListUtil.getWebFriendlyRecipients(actionListSrv.findUserSecondaryDelegators(getUserSession().getPrincipalId())));
135        request.setAttribute("primaryDelegates", ActionListUtil.getWebFriendlyRecipients(actionListSrv.findUserPrimaryDelegations(getUserSession().getPrincipalId())));
136        if (! filterForm.getMethodToCall().equalsIgnoreCase("clear")) {
137            filterForm.validateDates();
138        }
139        // make sure the back location includes the targetSpec for the Action List
140        if (!StringUtils.isBlank(filterForm.getBackLocation())) {
141            URIBuilder uri = new URIBuilder(filterForm.getBackLocation());
142            if (!StringUtils.isBlank(filterForm.getDocumentTargetSpec())) {
143                uri.addParameter("documentTargetSpec", filterForm.getDocumentTargetSpec()).build();
144            }
145            if (!StringUtils.isBlank(filterForm.getRouteLogTargetSpec())) {
146                uri.addParameter("routeLogTargetSpec", filterForm.getRouteLogTargetSpec()).build();
147            }
148            filterForm.setBackLocation(uri.build().toString());
149        }
150    }
151
152    private List<? extends KeyValue> getUserWorkgroupsDropDownList(String principalId) {
153        List<String> userWorkgroups =
154                KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
155
156        //note that userWorkgroups is unmodifiable so we need to create a new list that we can sort
157        List<String> userGroupsToSort = new ArrayList<String>(userWorkgroups);
158
159        List<KeyValue> sortedUserWorkgroups = new ArrayList<KeyValue>();
160        KeyValue keyValue = null;
161        keyValue = new ConcreteKeyValue(KewApiConstants.NO_FILTERING, KewApiConstants.NO_FILTERING);
162        sortedUserWorkgroups.add(keyValue);
163        if (userGroupsToSort != null && userGroupsToSort.size() > 0) {
164            Collections.sort(userGroupsToSort);
165
166            Group group;
167            for (String groupId : userGroupsToSort)
168            {
169                group = KimApiServiceLocator.getGroupService().getGroup(groupId);
170                keyValue = new ConcreteKeyValue(groupId, group.getName());
171                sortedUserWorkgroups.add(keyValue);
172            }
173        }
174        return sortedUserWorkgroups;
175    }
176
177    private UserSession getUserSession(){
178        return GlobalVariables.getUserSession();
179    }
180
181}
182