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