001    /**
002     * Copyright (c) 2000-present 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.service.impl;
016    
017    import com.liferay.portal.exception.NoSuchWorkflowInstanceLinkException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.kernel.workflow.WorkflowHandler;
023    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowInstance;
025    import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.model.WorkflowDefinitionLink;
029    import com.liferay.portal.model.WorkflowInstanceLink;
030    import com.liferay.portal.service.base.WorkflowInstanceLinkLocalServiceBaseImpl;
031    
032    import java.io.Serializable;
033    
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Bruno Farache
041     * @author Marcellus Tavares
042     */
043    public class WorkflowInstanceLinkLocalServiceImpl
044            extends WorkflowInstanceLinkLocalServiceBaseImpl {
045    
046            @Override
047            public WorkflowInstanceLink addWorkflowInstanceLink(
048                            long userId, long companyId, long groupId, String className,
049                            long classPK, long workflowInstanceId)
050                    throws PortalException {
051    
052                    User user = userPersistence.findByPrimaryKey(userId);
053                    long classNameId = classNameLocalService.getClassNameId(className);
054    
055                    long workflowInstanceLinkId = counterLocalService.increment();
056    
057                    WorkflowInstanceLink workflowInstanceLink =
058                            workflowInstanceLinkPersistence.create(workflowInstanceLinkId);
059    
060                    workflowInstanceLink.setUserId(userId);
061                    workflowInstanceLink.setUserName(user.getFullName());
062                    workflowInstanceLink.setGroupId(groupId);
063                    workflowInstanceLink.setCompanyId(companyId);
064                    workflowInstanceLink.setClassNameId(classNameId);
065                    workflowInstanceLink.setClassPK(classPK);
066                    workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);
067    
068                    workflowInstanceLinkPersistence.update(workflowInstanceLink);
069    
070                    return workflowInstanceLink;
071            }
072    
073            @Override
074            public WorkflowInstanceLink deleteWorkflowInstanceLink(
075                            long workflowInstanceLinkId)
076                    throws PortalException {
077    
078                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
079                            workflowInstanceLinkId);
080    
081                    return deleteWorkflowInstanceLink(workflowInstanceLink);
082            }
083    
084            @Override
085            public WorkflowInstanceLink deleteWorkflowInstanceLink(
086                            long companyId, long groupId, String className, long classPK)
087                    throws PortalException {
088    
089                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
090                            companyId, groupId, className, classPK);
091    
092                    return deleteWorkflowInstanceLink(workflowInstanceLink);
093            }
094    
095            @Override
096            public WorkflowInstanceLink deleteWorkflowInstanceLink(
097                            WorkflowInstanceLink workflowInstanceLink)
098                    throws PortalException {
099    
100                    if (workflowInstanceLink == null) {
101                            return null;
102                    }
103    
104                    super.deleteWorkflowInstanceLink(workflowInstanceLink);
105    
106                    subscriptionLocalService.deleteSubscriptions(
107                            workflowInstanceLink.getCompanyId(),
108                            WorkflowInstance.class.getName(),
109                            workflowInstanceLink.getWorkflowInstanceId());
110    
111                    WorkflowInstanceManagerUtil.deleteWorkflowInstance(
112                            workflowInstanceLink.getCompanyId(),
113                            workflowInstanceLink.getWorkflowInstanceId());
114    
115                    return workflowInstanceLink;
116            }
117    
118            @Override
119            public void deleteWorkflowInstanceLinks(
120                            long companyId, long groupId, String className, long classPK)
121                    throws PortalException {
122    
123                    List<WorkflowInstanceLink> workflowInstanceLinks =
124                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
125    
126                    for (WorkflowInstanceLink workflowInstanceLink :
127                                    workflowInstanceLinks) {
128    
129                            deleteWorkflowInstanceLink(workflowInstanceLink);
130                    }
131            }
132    
133            @Override
134            public WorkflowInstanceLink fetchWorkflowInstanceLink(
135                    long companyId, long groupId, String className, long classPK) {
136    
137                    List<WorkflowInstanceLink> workflowInstanceLinks =
138                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
139    
140                    if (!workflowInstanceLinks.isEmpty()) {
141                            return workflowInstanceLinks.get(0);
142                    }
143                    else {
144                            return null;
145                    }
146            }
147    
148            @Override
149            public String getState(
150                            long companyId, long groupId, String className, long classPK)
151                    throws PortalException {
152    
153                    WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
154                            companyId, groupId, className, classPK);
155    
156                    WorkflowInstance workflowInstance =
157                            WorkflowInstanceManagerUtil.getWorkflowInstance(
158                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
159    
160                    return workflowInstance.getState();
161            }
162    
163            @Override
164            public WorkflowInstanceLink getWorkflowInstanceLink(
165                            long companyId, long groupId, String className, long classPK)
166                    throws PortalException {
167    
168                    List<WorkflowInstanceLink> workflowInstanceLinks =
169                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
170    
171                    if (workflowInstanceLinks.isEmpty()) {
172                            StringBundler sb = new StringBundler(9);
173    
174                            sb.append("{companyId=");
175                            sb.append(companyId);
176                            sb.append(", groupId=");
177                            sb.append(groupId);
178                            sb.append(", className=");
179                            sb.append(className);
180                            sb.append(", classPK=");
181                            sb.append(classPK);
182                            sb.append("}");
183    
184                            throw new NoSuchWorkflowInstanceLinkException(sb.toString());
185                    }
186                    else {
187                            return workflowInstanceLinks.get(0);
188                    }
189            }
190    
191            @Override
192            public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
193                    long companyId, long groupId, String className, long classPK) {
194    
195                    long classNameId = classNameLocalService.getClassNameId(className);
196    
197                    return workflowInstanceLinkPersistence.findByG_C_C_C(
198                            groupId, companyId, classNameId, classPK);
199            }
200    
201            @Override
202            public boolean hasWorkflowInstanceLink(
203                    long companyId, long groupId, String className, long classPK) {
204    
205                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
206                            companyId, groupId, className, classPK);
207    
208                    if (workflowInstanceLink != null) {
209                            return true;
210                    }
211    
212                    return false;
213            }
214    
215            @Override
216            public boolean isEnded(
217                            long companyId, long groupId, String className, long classPK)
218                    throws PortalException {
219    
220                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
221                            companyId, groupId, className, classPK);
222    
223                    if (workflowInstanceLink == null) {
224                            return false;
225                    }
226    
227                    WorkflowInstance workflowInstance =
228                            WorkflowInstanceManagerUtil.getWorkflowInstance(
229                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
230    
231                    if (workflowInstance.getEndDate() != null) {
232                            return true;
233                    }
234    
235                    return false;
236            }
237    
238            @Override
239            public void startWorkflowInstance(
240                            long companyId, long groupId, long userId, String className,
241                            long classPK, Map<String, Serializable> workflowContext)
242                    throws PortalException {
243    
244                    if (!WorkflowThreadLocal.isEnabled()) {
245                            return;
246                    }
247    
248                    if (userId == 0) {
249                            userId = userLocalService.getDefaultUserId(companyId);
250                    }
251    
252                    WorkflowHandler<?> workflowHandler =
253                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
254    
255                    WorkflowDefinitionLink workflowDefinitionLink =
256                            workflowHandler.getWorkflowDefinitionLink(
257                                    companyId, groupId, classPK);
258    
259                    String workflowDefinitionName =
260                            workflowDefinitionLink.getWorkflowDefinitionName();
261                    int workflowDefinitionVersion =
262                            workflowDefinitionLink.getWorkflowDefinitionVersion();
263    
264                    if (workflowContext != null) {
265                            workflowContext = new HashMap<>(workflowContext);
266                    }
267                    else {
268                            workflowContext = new HashMap<>();
269                    }
270    
271                    workflowContext.put(
272                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
273                    workflowContext.put(
274                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
275                    workflowContext.put(
276                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
277                    workflowContext.put(
278                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
279                    workflowContext.put(
280                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
281                            workflowHandler.getType(LocaleUtil.getDefault()));
282    
283                    WorkflowInstance workflowInstance =
284                            WorkflowInstanceManagerUtil.startWorkflowInstance(
285                                    companyId, groupId, userId, workflowDefinitionName,
286                                    workflowDefinitionVersion, null, workflowContext);
287    
288                    addWorkflowInstanceLink(
289                            userId, companyId, groupId, className, classPK,
290                            workflowInstance.getWorkflowInstanceId());
291            }
292    
293            @Override
294            public void updateClassPK(
295                            long companyId, long groupId, String className, long oldClassPK,
296                            long newClassPK)
297                    throws PortalException {
298    
299                    if (!WorkflowThreadLocal.isEnabled()) {
300                            return;
301                    }
302    
303                    List<WorkflowInstanceLink> workflowInstanceLinks =
304                            getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
305    
306                    for (WorkflowInstanceLink workflowInstanceLink :
307                                    workflowInstanceLinks) {
308    
309                            WorkflowInstance workflowInstance =
310                                    WorkflowInstanceManagerUtil.getWorkflowInstance(
311                                            workflowInstanceLink.getCompanyId(),
312                                            workflowInstanceLink.getWorkflowInstanceId());
313    
314                            workflowInstanceLink.setClassPK(newClassPK);
315    
316                            workflowInstanceLinkPersistence.update(workflowInstanceLink);
317    
318                            Map<String, Serializable> workflowContext = new HashMap<>(
319                                    workflowInstance.getWorkflowContext());
320    
321                            workflowContext.put(
322                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
323                                    String.valueOf(newClassPK));
324    
325                            WorkflowInstanceManagerUtil.updateWorkflowContext(
326                                    workflowInstanceLink.getCompanyId(),
327                                    workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
328                    }
329            }
330    
331    }