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