001
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.ActionKeys;
032 import com.liferay.portal.security.permission.PermissionChecker;
033 import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
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
055 public class EditWorkflowInstanceAction extends PortletAction {
056
057 @Override
058 public void processAction(
059 ActionMapping actionMapping, ActionForm actionForm,
060 PortletConfig portletConfig, ActionRequest actionRequest,
061 ActionResponse actionResponse)
062 throws Exception {
063
064 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065
066 try {
067 String redirect = null;
068
069 if (cmd.equals(Constants.DELETE)) {
070 redirect = deleteInstance(actionRequest);
071 }
072 else if (cmd.equals(Constants.SIGNAL)) {
073 signalInstance(actionRequest);
074 }
075
076 if (redirect == null) {
077 redirect = ParamUtil.getString(actionRequest, "redirect");
078 }
079
080 sendRedirect(actionRequest, actionResponse, redirect);
081 }
082 catch (Exception e) {
083 if (e instanceof PrincipalException ||
084 e instanceof WorkflowException) {
085
086 SessionErrors.add(actionRequest, e.getClass());
087
088 setForward(actionRequest, "portlet.workflow_instances.error");
089 }
090 else {
091 throw e;
092 }
093 }
094 }
095
096 @Override
097 public ActionForward render(
098 ActionMapping actionMapping, ActionForm actionForm,
099 PortletConfig portletConfig, RenderRequest renderRequest,
100 RenderResponse renderResponse)
101 throws Exception {
102
103 try {
104 ActionUtil.getWorkflowInstance(renderRequest);
105 }
106 catch (Exception e) {
107 if (e instanceof WorkflowException) {
108 SessionErrors.add(renderRequest, e.getClass());
109
110 return actionMapping.findForward(
111 "portlet.workflow_instances.error");
112 }
113 else {
114 throw e;
115 }
116 }
117
118 String forward = getForward(
119 renderRequest, "portlet.workflow_instances.edit_workflow_instance");
120
121 return actionMapping.findForward(forward);
122 }
123
124 protected String deleteInstance(ActionRequest actionRequest)
125 throws Exception {
126
127 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128 WebKeys.THEME_DISPLAY);
129
130 long workflowInstanceId = ParamUtil.getLong(
131 actionRequest, "workflowInstanceId");
132
133 WorkflowInstance workflowInstance =
134 WorkflowInstanceManagerUtil.getWorkflowInstance(
135 themeDisplay.getCompanyId(), workflowInstanceId);
136
137 Map<String, Serializable> workflowContext =
138 workflowInstance.getWorkflowContext();
139
140 long companyId = GetterUtil.getLong(
141 workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
142 long userId = GetterUtil.getLong(
143 workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
144 long groupId = GetterUtil.getLong(
145 workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
146 String className = GetterUtil.getString(
147 workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));
148 long classPK = GetterUtil.getLong(
149 workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
150
151 WorkflowHandler workflowHandler =
152 WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
153
154 workflowHandler.updateStatus(
155 WorkflowConstants.STATUS_DRAFT, workflowContext);
156
157 WorkflowInstanceLinkLocalServiceUtil.deleteWorkflowInstanceLink(
158 companyId, groupId, className, classPK);
159
160 Layout layout = themeDisplay.getLayout();
161
162 Group layoutGroup = layout.getGroup();
163
164 if (layoutGroup.isControlPanel() &&
165 (WorkflowInstanceManagerUtil.getWorkflowInstanceCount(
166 companyId, userId, null, null, null) == 0)) {
167
168 PermissionChecker permissionChecker =
169 themeDisplay.getPermissionChecker();
170
171 String portletId = PortalUtil.getPortletId(actionRequest);
172
173 if (!permissionChecker.hasPermission(
174 groupId, portletId, 0,
175 ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
176
177 return themeDisplay.getURLControlPanel();
178 }
179 }
180
181 return null;
182 }
183
184 @Override
185 protected boolean isCheckMethodOnProcessAction() {
186 return _CHECK_METHOD_ON_PROCESS_ACTION;
187 }
188
189 protected void signalInstance(ActionRequest actionRequest)
190 throws Exception {
191
192 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
193 WebKeys.THEME_DISPLAY);
194
195 long workflowInstanceId = ParamUtil.getLong(
196 actionRequest, "workflowInstanceId");
197
198 String transitionName = ParamUtil.getString(
199 actionRequest, "transitionName");
200
201 WorkflowInstanceManagerUtil.signalWorkflowInstance(
202 themeDisplay.getCompanyId(), themeDisplay.getUserId(),
203 workflowInstanceId, transitionName, null);
204 }
205
206 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
207
208 }