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 (WorkflowThreadLocal.isEnabled() &&
092                            (serviceContext.getWorkflowAction() !=
093                                    WorkflowConstants.ACTION_PUBLISH)) {
094    
095                            return;
096                    }
097    
098                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
099    
100                    if (workflowHandler == null) {
101                            throw new WorkflowException(
102                                    "No workflow handler found for " + className);
103                    }
104    
105                    WorkflowDefinitionLink workflowDefinitionLink = null;
106    
107                    if (WorkflowThreadLocal.isEnabled() &&
108                            WorkflowEngineManagerUtil.isDeployed()) {
109    
110                            try {
111                                    workflowDefinitionLink =
112                                            workflowHandler.getWorkflowDefinitionLink(
113                                                    companyId, groupId, classPK);
114                            }
115                            catch (NoSuchWorkflowDefinitionLinkException nswdle) {
116                            }
117                    }
118    
119                    int status = WorkflowConstants.STATUS_PENDING;
120    
121                    if (workflowDefinitionLink == null) {
122                            status = WorkflowConstants.STATUS_APPROVED;
123                    }
124    
125                    workflowContext = new HashMap<String, Serializable>(workflowContext);
126    
127                    workflowContext.put(
128                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
129                    workflowContext.put(
130                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
131                    workflowContext.put(
132                            WorkflowConstants.CONTEXT_USER_ID, String.valueOf(userId));
133                    workflowContext.put(
134                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
135                    workflowContext.put(
136                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
137                    workflowContext.put(
138                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
139                            workflowHandler.getType(LocaleUtil.getDefault()));
140                    workflowContext.put(
141                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT, serviceContext);
142    
143                    workflowHandler.updateStatus(status, workflowContext);
144    
145                    if (workflowDefinitionLink != null) {
146                            workflowHandler.startWorkflowInstance(
147                                    companyId, groupId, userId, classPK, model, workflowContext);
148                    }
149            }
150    
151            public static void startWorkflowInstance(
152                            long companyId, long userId, String className, long classPK,
153                            Object model, ServiceContext serviceContext)
154                    throws PortalException, SystemException {
155    
156                    Map<String, Serializable> workflowContext =
157                            (Map<String, Serializable>)serviceContext.removeAttribute(
158                                    "workflowContext");
159    
160                    if (workflowContext == null) {
161                            workflowContext = Collections.emptyMap();
162                    }
163    
164                    startWorkflowInstance(
165                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
166                            classPK, model, serviceContext, workflowContext);
167            }
168    
169            public static void startWorkflowInstance(
170                            long companyId, long userId, String className, long classPK,
171                            Object model, ServiceContext serviceContext,
172                            Map<String, Serializable> workflowContext)
173                    throws PortalException, SystemException {
174    
175                    startWorkflowInstance(
176                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
177                            classPK, model, serviceContext, workflowContext);
178            }
179    
180            public static void unregister(List<WorkflowHandler> workflowHandlers) {
181                    for (WorkflowHandler workflowHandler : workflowHandlers) {
182                            unregister(workflowHandler);
183                    }
184            }
185    
186            public static void unregister(WorkflowHandler workflowHandler) {
187                    getWorkflowHandlerRegistry().unregister(workflowHandler);
188            }
189    
190            public static Object updateStatus(
191                            int status, Map<String, Serializable> workflowContext)
192                    throws PortalException, SystemException {
193    
194                    String className = (String)workflowContext.get(
195                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
196    
197                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
198    
199                    if (workflowHandler != null) {
200                            return workflowHandler.updateStatus(status, workflowContext);
201                    }
202    
203                    return null;
204            }
205    
206            public void setWorkflowHandlerRegistry(
207                    WorkflowHandlerRegistry workflowHandlerRegistry) {
208    
209                    PortalRuntimePermission.checkSetBeanProperty(getClass());
210    
211                    _workflowHandlerRegistry = workflowHandlerRegistry;
212            }
213    
214            private static WorkflowHandlerRegistry _workflowHandlerRegistry;
215    
216    }