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