001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.security.auth.PrincipalException;
029    import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    
033    import java.io.Serializable;
034    
035    import java.util.Map;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Marcellus Tavares
049     */
050    public class EditWorkflowInstanceAction extends PortletAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            if (cmd.equals(Constants.DELETE)) {
062                                    deleteInstance(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.SIGNAL)) {
065                                    signalInstance(actionRequest);
066                            }
067    
068                            sendRedirect(actionRequest, actionResponse);
069                    }
070                    catch (Exception e) {
071                            if (e instanceof PrincipalException ||
072                                    e instanceof WorkflowException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass().getName());
075    
076                                    setForward(actionRequest, "portlet.workflow_instances.error");
077                            }
078                            else {
079                                    throw e;
080                            }
081                    }
082            }
083    
084            @Override
085            public ActionForward render(
086                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
087                            RenderRequest renderRequest, RenderResponse renderResponse)
088                    throws Exception {
089    
090                    try {
091                            ActionUtil.getWorkflowInstance(renderRequest);
092                    }
093                    catch (Exception e) {
094                            if (e instanceof WorkflowException) {
095    
096                                    SessionErrors.add(renderRequest, e.getClass().getName());
097    
098                                    return mapping.findForward("portlet.workflow_instances.error");
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104    
105                    String forward = getForward(
106                            renderRequest, "portlet.workflow_instances.edit_workflow_instance");
107    
108                    return mapping.findForward(forward);
109            }
110    
111            protected void deleteInstance(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
115                            WebKeys.THEME_DISPLAY);
116    
117                    long workflowInstanceId = ParamUtil.getLong(
118                            actionRequest, "workflowInstanceId");
119    
120                    WorkflowInstance workflowInstance =
121                            WorkflowInstanceManagerUtil.getWorkflowInstance(
122                                    themeDisplay.getCompanyId(), workflowInstanceId);
123    
124                    Map<String, Serializable> workflowContext =
125                            workflowInstance.getWorkflowContext();
126    
127                    long companyId = GetterUtil.getLong(
128                            workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
129                    long groupId = GetterUtil.getLong(
130                            workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
131                    String className =  GetterUtil.getString(
132                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));
133                    long classPK = GetterUtil.getLong(
134                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
135    
136                    WorkflowHandler workflowHandler =
137                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
138    
139                    workflowHandler.updateStatus(
140                                    WorkflowConstants.STATUS_DRAFT, workflowContext);
141    
142                    WorkflowInstanceLinkLocalServiceUtil.deleteWorkflowInstanceLink(
143                            companyId, groupId, className, classPK);
144            }
145    
146            protected void signalInstance(ActionRequest actionRequest)
147                    throws Exception {
148    
149                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150                            WebKeys.THEME_DISPLAY);
151    
152                    long workflowInstanceId = ParamUtil.getLong(
153                            actionRequest, "workflowInstanceId");
154    
155                    String transitionName = ParamUtil.getString(
156                            actionRequest, "transitionName");
157    
158                    WorkflowInstanceManagerUtil.signalWorkflowInstance(
159                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
160                            workflowInstanceId, transitionName, null);
161            }
162    
163            @Override
164            protected boolean isCheckMethodOnProcessAction() {
165                    return _CHECK_METHOD_ON_PROCESS_ACTION;
166            }
167    
168            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
169    
170    }