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.NoSuchWorkflowDefinitionLinkException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.model.WorkflowDefinitionLink;
025    import com.liferay.portal.model.WorkflowInstanceLink;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.WorkflowInstanceLinkLocalServiceUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Collections;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Bruno Farache
038     * @author Marcellus Tavares
039     */
040    public class WorkflowHandlerRegistryUtil {
041    
042            public static List<WorkflowHandler> getScopeableWorkflowHandlers() {
043                    return getWorkflowHandlerRegistry().getScopeableWorkflowHandlers();
044            }
045    
046            public static WorkflowHandler getWorkflowHandler(String className) {
047                    return getWorkflowHandlerRegistry().getWorkflowHandler(className);
048            }
049    
050            public static WorkflowHandlerRegistry getWorkflowHandlerRegistry() {
051                    PortalRuntimePermission.checkGetBeanProperty(
052                            WorkflowHandlerRegistryUtil.class);
053    
054                    return _workflowHandlerRegistry;
055            }
056    
057            public static List<WorkflowHandler> getWorkflowHandlers() {
058                    return getWorkflowHandlerRegistry().getWorkflowHandlers();
059            }
060    
061            public static void register(List<WorkflowHandler> workflowHandlers) {
062                    for (WorkflowHandler workflowHandler : workflowHandlers) {
063                            register(workflowHandler);
064                    }
065            }
066    
067            public static void register(WorkflowHandler workflowHandler) {
068                    getWorkflowHandlerRegistry().register(workflowHandler);
069            }
070    
071            public static void startWorkflowInstance(
072                            long companyId, long groupId, long userId, String className,
073                            long classPK, Object model, ServiceContext serviceContext)
074                    throws PortalException, SystemException {
075    
076                    Map<String, Serializable> workflowContext =
077                            (Map<String, Serializable>)serviceContext.removeAttribute(
078                                    "workflowContext");
079    
080                    if (workflowContext == null) {
081                            workflowContext = Collections.emptyMap();
082                    }
083    
084                    startWorkflowInstance(
085                            companyId, groupId, userId, className, classPK, model,
086                            serviceContext, workflowContext);
087            }
088    
089            public static void startWorkflowInstance(
090                            long companyId, long groupId, long userId, String className,
091                            long classPK, Object model, ServiceContext serviceContext,
092                            Map<String, Serializable> workflowContext)
093                    throws PortalException, SystemException {
094    
095                    if (WorkflowThreadLocal.isEnabled() &&
096                            (serviceContext.getWorkflowAction() !=
097                                    WorkflowConstants.ACTION_PUBLISH)) {
098    
099                            return;
100                    }
101    
102                    WorkflowInstanceLink workflowInstanceLink =
103                            WorkflowInstanceLinkLocalServiceUtil.fetchWorkflowInstanceLink(
104                                    companyId, groupId, className, classPK);
105    
106                    if (workflowInstanceLink != null) {
107                            if (_log.isWarnEnabled()) {
108                                    _log.warn(
109                                            "Workflow already started for class " + className +
110                                                    " with primary key " + classPK + " in group " +
111                                                            groupId);
112                            }
113    
114                            return;
115                    }
116    
117                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
118    
119                    if (workflowHandler == null) {
120                            throw new WorkflowException(
121                                    "No workflow handler found for " + className);
122                    }
123    
124                    WorkflowDefinitionLink workflowDefinitionLink = null;
125    
126                    if (WorkflowThreadLocal.isEnabled() &&
127                            WorkflowEngineManagerUtil.isDeployed()) {
128    
129                            try {
130                                    workflowDefinitionLink =
131                                            workflowHandler.getWorkflowDefinitionLink(
132                                                    companyId, groupId, classPK);
133                            }
134                            catch (NoSuchWorkflowDefinitionLinkException nswdle) {
135                            }
136                    }
137    
138                    int status = WorkflowConstants.STATUS_PENDING;
139    
140                    if (workflowDefinitionLink == null) {
141                            status = WorkflowConstants.STATUS_APPROVED;
142                    }
143    
144                    workflowContext = new HashMap<String, Serializable>(workflowContext);
145    
146                    workflowContext.put(
147                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
148                    workflowContext.put(
149                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
150                    workflowContext.put(
151                            WorkflowConstants.CONTEXT_USER_ID, String.valueOf(userId));
152                    workflowContext.put(
153                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
154                    workflowContext.put(
155                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
156                    workflowContext.put(
157                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
158                            workflowHandler.getType(LocaleUtil.getDefault()));
159                    workflowContext.put(
160                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT, serviceContext);
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    }