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.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.KewApiServiceLocator;
029import org.kuali.rice.kew.api.preferences.Preferences;
030import org.kuali.rice.kew.api.preferences.PreferencesService;
031import org.kuali.rice.kew.service.KEWServiceLocator;
032import org.kuali.rice.kew.api.KewApiConstants;
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 targetSpec back to the ActionList
106            ActionRedirect redirect = new ActionRedirect(forward);
107            redirect.addParameter("targetSpec", filterForm.getTargetSpec());
108            return redirect;
109        }
110        return mapping.findForward("viewFilter");    
111    }
112
113    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
114        ActionListFilterForm filterForm = (ActionListFilterForm) form;
115        filterForm.setFilter(new ActionListFilter());
116        filterForm.setCreateDateFrom("");
117        filterForm.setCreateDateTo("");
118        filterForm.setLastAssignedDateFrom("");
119        filterForm.setLastAssignedDateTo("");
120        filterForm.setDocTypeFullName("");
121        UserSession session = getUserSession();
122        session.removeObject(KewApiConstants.ACTION_LIST_FILTER_ATTR_NAME);
123        return mapping.findForward("viewFilter");
124    }
125
126    public void initForm(HttpServletRequest request, ActionForm form) throws Exception {
127        ActionListFilterForm filterForm = (ActionListFilterForm) form;
128        filterForm.setUserWorkgroups(getUserWorkgroupsDropDownList(getUserSession().getPrincipalId()));
129        PreferencesService prefSrv = KewApiServiceLocator.getPreferencesService();
130        Preferences preferences = prefSrv.getPreferences(getUserSession().getPrincipalId());
131        request.setAttribute("preferences", preferences);
132        ActionListService actionListSrv = KEWServiceLocator.getActionListService();
133        request.setAttribute("delegators", ActionListUtil.getWebFriendlyRecipients(actionListSrv.findUserSecondaryDelegators(getUserSession().getPrincipalId())));
134        request.setAttribute("primaryDelegates", ActionListUtil.getWebFriendlyRecipients(actionListSrv.findUserPrimaryDelegations(getUserSession().getPrincipalId())));
135        if (! filterForm.getMethodToCall().equalsIgnoreCase("clear")) {
136            filterForm.validateDates();
137        }
138        // make sure the back location includes the targetSpec for the Action List
139        if (!StringUtils.isBlank(filterForm.getBackLocation()) && !StringUtils.isBlank(filterForm.getTargetSpec())) {
140            URI uri = new URIBuilder(filterForm.getBackLocation()).addParameter("targetSpec", filterForm.getTargetSpec()).build();
141            filterForm.setBackLocation(uri.toString());
142        }
143
144    }
145
146    private List<? extends KeyValue> getUserWorkgroupsDropDownList(String principalId) {
147        List<String> userWorkgroups =
148                KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
149
150        //note that userWorkgroups is unmodifiable so we need to create a new list that we can sort
151        List<String> userGroupsToSort = new ArrayList<String>(userWorkgroups);
152
153        List<KeyValue> sortedUserWorkgroups = new ArrayList<KeyValue>();
154        KeyValue keyValue = null;
155        keyValue = new ConcreteKeyValue(KewApiConstants.NO_FILTERING, KewApiConstants.NO_FILTERING);
156        sortedUserWorkgroups.add(keyValue);
157        if (userGroupsToSort != null && userGroupsToSort.size() > 0) {
158            Collections.sort(userGroupsToSort);
159
160            Group group;
161            for (String groupId : userGroupsToSort)
162            {
163                group = KimApiServiceLocator.getGroupService().getGroup(groupId);
164                keyValue = new ConcreteKeyValue(groupId, group.getName());
165                sortedUserWorkgroups.add(keyValue);
166            }
167        }
168        return sortedUserWorkgroups;
169    }
170
171    private UserSession getUserSession(){
172        return GlobalVariables.getUserSession();
173    }
174
175}
176