001    /**
002     * Copyright (c) 2000-2010 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.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            public void processAction(
053                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
054                            ActionRequest actionRequest, ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.DELETE)) {
061                                    deleteInstance(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.SIGNAL)) {
064                                    signalInstance(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof PrincipalException ||
071                                    e instanceof WorkflowException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass().getName());
074    
075                                    setForward(actionRequest, "portlet.workflow_instances.error");
076                            }
077                            else {
078                                    throw e;
079                            }
080                    }
081            }
082    
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getWorkflowInstance(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof WorkflowException) {
093    
094                                    SessionErrors.add(renderRequest, e.getClass().getName());
095    
096                                    return mapping.findForward("portlet.workflow_instances.error");
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102    
103                    String forward = getForward(
104                            renderRequest, "portlet.workflow_instances.edit_workflow_instance");
105    
106                    return mapping.findForward(forward);
107            }
108    
109            protected void deleteInstance(ActionRequest actionRequest)
110                    throws Exception {
111    
112                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
113                            WebKeys.THEME_DISPLAY);
114    
115                    long workflowInstanceId = ParamUtil.getLong(
116                            actionRequest, "workflowInstanceId");
117    
118                    WorkflowInstance workflowInstance =
119                            WorkflowInstanceManagerUtil.getWorkflowInstance(
120                                    themeDisplay.getCompanyId(), workflowInstanceId);
121    
122                    Map<String, Serializable> workflowContext =
123                            workflowInstance.getWorkflowContext();
124    
125                    long companyId = GetterUtil.getLong(
126                            workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
127                    long groupId = GetterUtil.getLong(
128                            workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
129                    String className =  GetterUtil.getString(
130                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));
131                    long classPK = GetterUtil.getLong(
132                            workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
133    
134                    WorkflowHandler workflowHandler =
135                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
136    
137                    workflowHandler.updateStatus(
138                                    WorkflowConstants.STATUS_DRAFT, workflowContext);
139    
140                    WorkflowInstanceLinkLocalServiceUtil.deleteWorkflowInstanceLink(
141                            companyId, groupId, className, classPK);
142            }
143    
144            protected void signalInstance(ActionRequest actionRequest)
145                    throws Exception {
146    
147                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    long workflowInstanceId = ParamUtil.getLong(
151                            actionRequest, "workflowInstanceId");
152    
153                    String transitionName = ParamUtil.getString(
154                            actionRequest, "transitionName");
155    
156                    WorkflowInstanceManagerUtil.signalWorkflowInstance(
157                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
158                            workflowInstanceId, transitionName, null);
159            }
160    
161            protected boolean isCheckMethodOnProcessAction() {
162                    return _CHECK_METHOD_ON_PROCESS_ACTION;
163            }
164    
165            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
166    
167    }