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