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.portal.kernel.workflow;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.model.WorkflowDefinitionLink;
024    import com.liferay.portal.model.WorkflowInstanceLink;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
027    
028    import java.io.Serializable;
029    
030    import java.util.Collections;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Bruno Farache
037     * @author Marcellus Tavares
038     */
039    public class WorkflowHandlerRegistryUtil {
040    
041            public static List<WorkflowHandler> getScopeableWorkflowHandlers() {
042                    return getWorkflowHandlerRegistry().getScopeableWorkflowHandlers();
043            }
044    
045            public static WorkflowHandler getWorkflowHandler(String className) {
046                    return getWorkflowHandlerRegistry().getWorkflowHandler(className);
047            }
048    
049            public static WorkflowHandlerRegistry getWorkflowHandlerRegistry() {
050                    PortalRuntimePermission.checkGetBeanProperty(
051                            WorkflowHandlerRegistryUtil.class);
052    
053                    return _workflowHandlerRegistry;
054            }
055    
056            public static List<WorkflowHandler> getWorkflowHandlers() {
057                    return getWorkflowHandlerRegistry().getWorkflowHandlers();
058            }
059    
060            public static void register(List<WorkflowHandler> workflowHandlers) {
061                    for (WorkflowHandler workflowHandler : workflowHandlers) {
062                            register(workflowHandler);
063                    }
064            }
065    
066            public static void register(WorkflowHandler workflowHandler) {
067                    getWorkflowHandlerRegistry().register(workflowHandler);
068            }
069    
070            public static void startWorkflowInstance(
071                            long companyId, long groupId, long userId, String className,
072                            long classPK, Object model, ServiceContext serviceContext)
073                    throws PortalException, SystemException {
074    
075                    Map<String, Serializable> workflowContext =
076                            (Map<String, Serializable>)serviceContext.removeAttribute(
077                                    "workflowContext");
078    
079                    if (workflowContext == null) {
080                            workflowContext = Collections.emptyMap();
081                    }
082    
083                    startWorkflowInstance(
084                            companyId, groupId, userId, className, classPK, model,
085                            serviceContext, workflowContext);
086            }
087    
088            public static void startWorkflowInstance(
089                            long companyId, long groupId, long userId, String className,
090                            long classPK, Object model, ServiceContext serviceContext,
091                            Map<String, Serializable> workflowContext)
092                    throws PortalException, SystemException {
093    
094                    if (serviceContext.getWorkflowAction() !=
095                                    WorkflowConstants.ACTION_PUBLISH) {
096    
097                            return;
098                    }
099    
100                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
101    
102                    if (workflowHandler == null) {
103                            if (WorkflowThreadLocal.isEnabled()) {
104                                    throw new WorkflowException(
105                                            "No workflow handler found for " + className);
106                            }
107    
108                            return;
109                    }
110    
111                    WorkflowInstanceLink workflowInstanceLink =
112                            WorkflowInstanceLinkLocalServiceUtil.fetchWorkflowInstanceLink(
113                                    companyId, groupId, className, classPK);
114    
115                    if (workflowInstanceLink != null) {
116                            if (_log.isWarnEnabled()) {
117                                    _log.warn(
118                                            "Workflow already started for class " + className +
119                                                    " with primary key " + classPK + " in group " +
120                                                            groupId);
121                            }
122    
123                            return;
124                    }
125    
126                    WorkflowDefinitionLink workflowDefinitionLink = null;
127    
128                    if (WorkflowThreadLocal.isEnabled() &&
129                            WorkflowEngineManagerUtil.isDeployed()) {
130    
131                            workflowDefinitionLink = workflowHandler.getWorkflowDefinitionLink(
132                                    companyId, groupId, classPK);
133                    }
134    
135                    int status = WorkflowConstants.STATUS_PENDING;
136    
137                    if (workflowDefinitionLink == null) {
138                            status = WorkflowConstants.STATUS_APPROVED;
139                    }
140    
141                    workflowContext = new HashMap<String, Serializable>(workflowContext);
142    
143                    workflowContext.put(
144                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
145                    workflowContext.put(
146                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
147                    workflowContext.put(
148                            WorkflowConstants.CONTEXT_USER_ID, String.valueOf(userId));
149                    workflowContext.put(
150                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
151                    workflowContext.put(
152                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
153                    workflowContext.put(
154                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
155                            workflowHandler.getType(LocaleUtil.getDefault()));
156                    workflowContext.put(
157                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT, serviceContext);
158                    workflowContext.put(
159                            WorkflowConstants.CONTEXT_TASK_COMMENTS,
160                            serviceContext.getAttribute("comments"));
161    
162                    workflowHandler.updateStatus(status, workflowContext);
163    
164                    if (workflowDefinitionLink != null) {
165                            workflowHandler.startWorkflowInstance(
166                                    companyId, groupId, userId, classPK, model, workflowContext);
167                    }
168            }
169    
170            public static void startWorkflowInstance(
171                            long companyId, long userId, String className, long classPK,
172                            Object model, ServiceContext serviceContext)
173                    throws PortalException, SystemException {
174    
175                    Map<String, Serializable> workflowContext =
176                            (Map<String, Serializable>)serviceContext.removeAttribute(
177                                    "workflowContext");
178    
179                    if (workflowContext == null) {
180                            workflowContext = Collections.emptyMap();
181                    }
182    
183                    startWorkflowInstance(
184                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
185                            classPK, model, serviceContext, workflowContext);
186            }
187    
188            public static void startWorkflowInstance(
189                            long companyId, long userId, String className, long classPK,
190                            Object model, ServiceContext serviceContext,
191                            Map<String, Serializable> workflowContext)
192                    throws PortalException, SystemException {
193    
194                    startWorkflowInstance(
195                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
196                            classPK, model, serviceContext, workflowContext);
197            }
198    
199            public static void unregister(List<WorkflowHandler> workflowHandlers) {
200                    for (WorkflowHandler workflowHandler : workflowHandlers) {
201                            unregister(workflowHandler);
202                    }
203            }
204    
205            public static void unregister(WorkflowHandler workflowHandler) {
206                    getWorkflowHandlerRegistry().unregister(workflowHandler);
207            }
208    
209            public static Object updateStatus(
210                            int status, Map<String, Serializable> workflowContext)
211                    throws PortalException, SystemException {
212    
213                    String className = (String)workflowContext.get(
214                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
215    
216                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
217    
218                    if (workflowHandler != null) {
219                            return workflowHandler.updateStatus(status, workflowContext);
220                    }
221    
222                    return null;
223            }
224    
225            public void setWorkflowHandlerRegistry(
226                    WorkflowHandlerRegistry workflowHandlerRegistry) {
227    
228                    PortalRuntimePermission.checkSetBeanProperty(getClass());
229    
230                    _workflowHandlerRegistry = workflowHandlerRegistry;
231            }
232    
233            private static Log _log = LogFactoryUtil.getLog(
234                    WorkflowHandlerRegistryUtil.class);
235    
236            private static WorkflowHandlerRegistry _workflowHandlerRegistry;
237    
238    }