001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.workflowinstances.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.kernel.workflow.WorkflowException;
024    import com.liferay.portal.kernel.workflow.WorkflowHandler;
025    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowInstance;
027    import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.permission.PermissionChecker;
032    import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
033    import com.liferay.portal.service.permission.PortletPermissionUtil;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    
038    import java.io.Serializable;
039    
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Marcellus Tavares
054     */
055    public class EditWorkflowInstanceAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
064    
065                    try {
066                            String redirect = null;
067    
068                            if (cmd.equals(Constants.DELETE)) {
069                                    redirect = deleteInstance(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.SIGNAL)) {
072                                    signalInstance(actionRequest);
073                            }
074    
075                            if (redirect == null) {
076                                    redirect = ParamUtil.getString(actionRequest, "redirect");
077                            }
078    
079                            sendRedirect(actionRequest, actionResponse, redirect);
080                    }
081                    catch (Exception e) {
082                            if (e instanceof PrincipalException ||
083                                    e instanceof WorkflowException) {
084    
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    setForward(actionRequest, "portlet.workflow_instances.error");
088                            }
089                            else {
090                                    throw e;
091                            }
092                    }
093            }
094    
095            @Override
096            public ActionForward render(
097                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
098                            RenderRequest renderRequest, RenderResponse renderResponse)
099                    throws Exception {
100    
101                    try {
102                            ActionUtil.getWorkflowInstance(renderRequest);
103                    }
104                    catch (Exception e) {
105                            if (e instanceof WorkflowException) {
106    
107                                    SessionErrors.add(renderRequest, e.getClass());
108    
109                                    return mapping.findForward("portlet.workflow_instances.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115    
116                    String forward = getForward(
117                            renderRequest, "portlet.workflow_instances.edit_workflow_instance");
118    
119                    return mapping.findForward(forward);
120            }
121    
122            protected String deleteInstance(ActionRequest actionRequest)
123                    throws Exception {
124    
125                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
126                            WebKeys.THEME_DISPLAY);
127    
128                    long workflowInstanceId = ParamUtil.getLong(
129                            actionRequest, "workflowInstanceId");
130    
131                    WorkflowInstance workflowInstance =
132                            WorkflowInstanceManagerUtil.getWorkflowInstance(
133                                    themeDisplay.getCompanyId(), workflowInstanceId);
134    
135                    Map<String, Serializable> workflowContext =
136                            workflowInstance.getWorkflowContext();
137    
138                    long companyId = GetterUtil.getLong(
139                            workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
140                    long userId = GetterUtil.getLong(
141                            workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
142                    long groupId = GetterUtil.getLong(
143                            workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
144                    String className = GetterUtil.getString(
145                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));
146                    long classPK = GetterUtil.getLong(
147                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
148    
149                    WorkflowHandler workflowHandler =
150                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
151    
152                    workflowHandler.updateStatus(
153                            WorkflowConstants.STATUS_DRAFT, workflowContext);
154    
155                    WorkflowInstanceLinkLocalServiceUtil.deleteWorkflowInstanceLink(
156                            companyId, groupId, className, classPK);
157    
158                    Layout layout = themeDisplay.getLayout();
159    
160                    Group layoutGroup = layout.getGroup();
161    
162                    if (layoutGroup.isControlPanel() &&
163                            (WorkflowInstanceManagerUtil.getWorkflowInstanceCount(
164                                    companyId, userId, null, null, null) == 0)) {
165    
166                            PermissionChecker permissionChecker =
167                                    themeDisplay.getPermissionChecker();
168    
169                            String portletId = PortalUtil.getPortletId(actionRequest);
170    
171                            if (!PortletPermissionUtil.hasControlPanelAccessPermission(
172                                            permissionChecker, groupId, portletId)) {
173    
174                                    return themeDisplay.getURLControlPanel();
175                            }
176                    }
177    
178                    return null;
179            }
180    
181            @Override
182            protected boolean isCheckMethodOnProcessAction() {
183                    return _CHECK_METHOD_ON_PROCESS_ACTION;
184            }
185    
186            protected void signalInstance(ActionRequest actionRequest)
187                    throws Exception {
188    
189                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
190                            WebKeys.THEME_DISPLAY);
191    
192                    long workflowInstanceId = ParamUtil.getLong(
193                            actionRequest, "workflowInstanceId");
194    
195                    String transitionName = ParamUtil.getString(
196                            actionRequest, "transitionName");
197    
198                    WorkflowInstanceManagerUtil.signalWorkflowInstance(
199                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
200                            workflowInstanceId, transitionName, null);
201            }
202    
203            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
204    
205    }