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.GetterUtil;
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 (serviceContext.getWorkflowAction() !=
096                                    WorkflowConstants.ACTION_PUBLISH) {
097    
098                            return;
099                    }
100    
101                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
102    
103                    if (workflowHandler == null) {
104                            if (WorkflowThreadLocal.isEnabled()) {
105                                    throw new WorkflowException(
106                                            "No workflow handler found for " + className);
107                            }
108    
109                            return;
110                    }
111    
112                    WorkflowInstanceLink workflowInstanceLink =
113                            WorkflowInstanceLinkLocalServiceUtil.fetchWorkflowInstanceLink(
114                                    companyId, groupId, className, classPK);
115    
116                    if (workflowInstanceLink != null) {
117                            if (_log.isWarnEnabled()) {
118                                    _log.warn(
119                                            "Workflow already started for class " + className +
120                                                    " with primary key " + classPK + " in group " +
121                                                            groupId);
122                            }
123    
124                            return;
125                    }
126    
127                    WorkflowDefinitionLink workflowDefinitionLink = null;
128    
129                    if (WorkflowThreadLocal.isEnabled() &&
130                            WorkflowEngineManagerUtil.isDeployed()) {
131    
132                            workflowDefinitionLink = workflowHandler.getWorkflowDefinitionLink(
133                                    companyId, groupId, classPK);
134                    }
135    
136                    int status = WorkflowConstants.STATUS_PENDING;
137    
138                    if (workflowDefinitionLink == null) {
139                            status = WorkflowConstants.STATUS_APPROVED;
140                    }
141    
142                    workflowContext = new HashMap<String, Serializable>(workflowContext);
143    
144                    workflowContext.put(
145                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
146                    workflowContext.put(
147                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
148                    workflowContext.put(
149                            WorkflowConstants.CONTEXT_USER_ID, String.valueOf(userId));
150                    workflowContext.put(
151                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
152                    workflowContext.put(
153                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
154                    workflowContext.put(
155                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
156                            workflowHandler.getType(LocaleUtil.getDefault()));
157                    workflowContext.put(
158                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT, serviceContext);
159                    workflowContext.put(
160                            WorkflowConstants.CONTEXT_TASK_COMMENTS,
161                            GetterUtil.getString(serviceContext.getAttribute("comments")));
162    
163                    workflowHandler.updateStatus(status, workflowContext);
164    
165                    if (workflowDefinitionLink != null) {
166                            workflowHandler.startWorkflowInstance(
167                                    companyId, groupId, userId, classPK, model, workflowContext);
168                    }
169            }
170    
171            public static void startWorkflowInstance(
172                            long companyId, long userId, String className, long classPK,
173                            Object model, ServiceContext serviceContext)
174                    throws PortalException, SystemException {
175    
176                    Map<String, Serializable> workflowContext =
177                            (Map<String, Serializable>)serviceContext.removeAttribute(
178                                    "workflowContext");
179    
180                    if (workflowContext == null) {
181                            workflowContext = Collections.emptyMap();
182                    }
183    
184                    startWorkflowInstance(
185                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
186                            classPK, model, serviceContext, workflowContext);
187            }
188    
189            public static void startWorkflowInstance(
190                            long companyId, long userId, String className, long classPK,
191                            Object model, ServiceContext serviceContext,
192                            Map<String, Serializable> workflowContext)
193                    throws PortalException, SystemException {
194    
195                    startWorkflowInstance(
196                            companyId, WorkflowConstants.DEFAULT_GROUP_ID, userId, className,
197                            classPK, model, serviceContext, workflowContext);
198            }
199    
200            public static void unregister(List<WorkflowHandler> workflowHandlers) {
201                    for (WorkflowHandler workflowHandler : workflowHandlers) {
202                            unregister(workflowHandler);
203                    }
204            }
205    
206            public static void unregister(WorkflowHandler workflowHandler) {
207                    getWorkflowHandlerRegistry().unregister(workflowHandler);
208            }
209    
210            public static Object updateStatus(
211                            int status, Map<String, Serializable> workflowContext)
212                    throws PortalException, SystemException {
213    
214                    String className = (String)workflowContext.get(
215                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
216    
217                    WorkflowHandler workflowHandler = getWorkflowHandler(className);
218    
219                    if (workflowHandler != null) {
220                            return workflowHandler.updateStatus(status, workflowContext);
221                    }
222    
223                    return null;
224            }
225    
226            public void setWorkflowHandlerRegistry(
227                    WorkflowHandlerRegistry workflowHandlerRegistry) {
228    
229                    PortalRuntimePermission.checkSetBeanProperty(getClass());
230    
231                    _workflowHandlerRegistry = workflowHandlerRegistry;
232            }
233    
234            private static Log _log = LogFactoryUtil.getLog(
235                    WorkflowHandlerRegistryUtil.class);
236    
237            private static WorkflowHandlerRegistry _workflowHandlerRegistry;
238    
239    }