1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchWorkflowInstanceLinkException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.workflow.WorkflowConstants;
21  import com.liferay.portal.kernel.workflow.WorkflowHandler;
22  import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
23  import com.liferay.portal.kernel.workflow.WorkflowInstance;
24  import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
25  import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.model.WorkflowDefinitionLink;
28  import com.liferay.portal.model.WorkflowInstanceLink;
29  import com.liferay.portal.service.base.WorkflowInstanceLinkLocalServiceBaseImpl;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.io.Serializable;
33  
34  import java.util.Date;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * <a href="WorkflowInstanceLinkLocalServiceImpl.java.html"><b><i>View Source
41   * </i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Bruno Farache
45   * @author Marcellus Tavares
46   */
47  public class WorkflowInstanceLinkLocalServiceImpl
48      extends WorkflowInstanceLinkLocalServiceBaseImpl {
49  
50      public WorkflowInstanceLink addWorkflowInstanceLink(
51              long userId, long companyId, long groupId, String className,
52              long classPK, long workflowInstanceId)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57          Date now = new Date();
58  
59          long workflowInstanceLinkId = counterLocalService.increment();
60  
61          WorkflowInstanceLink workflowInstanceLink =
62              workflowInstanceLinkPersistence.create(workflowInstanceLinkId);
63  
64          workflowInstanceLink.setCreateDate(now);
65          workflowInstanceLink.setModifiedDate(now);
66          workflowInstanceLink.setUserId(userId);
67          workflowInstanceLink.setUserName(user.getFullName());
68          workflowInstanceLink.setGroupId(groupId);
69          workflowInstanceLink.setCompanyId(companyId);
70          workflowInstanceLink.setClassNameId(classNameId);
71          workflowInstanceLink.setClassPK(classPK);
72          workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);
73  
74          workflowInstanceLinkPersistence.update(workflowInstanceLink, false);
75  
76          return workflowInstanceLink;
77      }
78  
79      public void deleteWorkflowInstanceLink(
80              long companyId, long groupId, String className, long classPK)
81          throws PortalException, SystemException {
82  
83          try {
84              WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
85                  companyId, groupId, className, classPK);
86  
87              deleteWorkflowInstanceLink(workflowInstanceLink);
88  
89              WorkflowInstanceManagerUtil.deleteWorkflowInstance(
90                  companyId, workflowInstanceLink.getWorkflowInstanceId());
91          }
92          catch (NoSuchWorkflowInstanceLinkException nswile) {
93          }
94      }
95  
96      public void deleteWorkflowInstanceLinks(
97              long companyId, long groupId, String className, long classPK)
98          throws PortalException, SystemException {
99  
100         List<WorkflowInstanceLink> workflowInstanceLinks =
101             getWorkflowInstanceLinks(companyId, groupId, className, classPK);
102 
103         for (WorkflowInstanceLink workflowInstanceLink :
104                 workflowInstanceLinks) {
105 
106             deleteWorkflowInstanceLink(workflowInstanceLink);
107 
108             WorkflowInstanceManagerUtil.deleteWorkflowInstance(
109                 companyId, workflowInstanceLink.getWorkflowInstanceId());
110         }
111     }
112 
113     public String getState(
114             long companyId, long groupId, String className, long classPK)
115         throws PortalException, SystemException {
116 
117         WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
118             companyId, groupId, className, classPK);
119 
120         WorkflowInstance workflowInstance =
121             WorkflowInstanceManagerUtil.getWorkflowInstance(
122                 companyId, workflowInstanceLink.getWorkflowInstanceId());
123 
124         return workflowInstance.getState();
125     }
126 
127     public WorkflowInstanceLink getWorkflowInstanceLink(
128             long companyId, long groupId, String className, long classPK)
129         throws PortalException, SystemException {
130 
131         List<WorkflowInstanceLink> workflowInstanceLinks =
132             getWorkflowInstanceLinks(companyId, groupId, className, classPK);
133 
134         if (workflowInstanceLinks.isEmpty()) {
135             throw new NoSuchWorkflowInstanceLinkException();
136         }
137         else {
138             return workflowInstanceLinks.get(0);
139         }
140     }
141 
142     public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
143             long companyId, long groupId, String className, long classPK)
144         throws SystemException {
145 
146         long classNameId = PortalUtil.getClassNameId(className);
147 
148         return workflowInstanceLinkPersistence.findByG_C_C_C(
149             groupId, companyId, classNameId, classPK);
150     }
151 
152     public boolean hasWorkflowInstanceLink(
153             long companyId, long groupId, String className, long classPK)
154         throws PortalException, SystemException {
155 
156         try {
157             getWorkflowInstanceLink(companyId, groupId, className, classPK);
158 
159             return true;
160         }
161         catch (NoSuchWorkflowInstanceLinkException nswile) {
162             return false;
163         }
164     }
165 
166     public boolean isEnded(
167             long companyId, long groupId, String className, long classPK)
168         throws PortalException, SystemException {
169 
170         try {
171             WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
172                 companyId, groupId, className, classPK);
173 
174             WorkflowInstance workflowInstance =
175                 WorkflowInstanceManagerUtil.getWorkflowInstance(
176                     companyId, workflowInstanceLink.getWorkflowInstanceId());
177 
178             if (workflowInstance.getEndDate() != null) {
179                 return true;
180             }
181         }
182         catch (NoSuchWorkflowInstanceLinkException nswile) {
183         }
184 
185         return false;
186     }
187 
188     public void startWorkflowInstance(
189             long companyId, long groupId, long userId, String className,
190             long classPK, Map<String, Serializable> workflowContext)
191         throws PortalException, SystemException {
192 
193         if (!WorkflowThreadLocal.isEnabled()) {
194             return;
195         }
196 
197         WorkflowDefinitionLink workflowDefinitionLink =
198             workflowDefinitionLinkLocalService.getWorkflowDefinitionLink(
199                 companyId, groupId, className);
200 
201         String workflowDefinitionName =
202             workflowDefinitionLink.getWorkflowDefinitionName();
203         int workflowDefinitionVersion =
204             workflowDefinitionLink.getWorkflowDefinitionVersion();
205 
206         if (workflowContext != null) {
207             workflowContext = new HashMap<String, Serializable>(
208                 workflowContext);
209         }
210         else {
211             workflowContext = new HashMap<String, Serializable>();
212         }
213 
214         workflowContext.put(
215             WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
216         workflowContext.put(
217             WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
218         workflowContext.put(
219             WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
220         workflowContext.put(
221             WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
222 
223         WorkflowHandler workflowHandler =
224             WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
225 
226         workflowContext.put(
227             WorkflowConstants.CONTEXT_ENTRY_TYPE, workflowHandler.getType());
228 
229         WorkflowInstance workflowInstance =
230             WorkflowInstanceManagerUtil.startWorkflowInstance(
231                 companyId, groupId, userId, workflowDefinitionName,
232             workflowDefinitionVersion, null, workflowContext);
233 
234         addWorkflowInstanceLink(
235             userId, companyId, groupId, className, classPK,
236             workflowInstance.getWorkflowInstanceId());
237     }
238 
239     public void updateClassPK(
240             long companyId, long groupId, String className, long oldClassPK,
241             long newClassPK)
242         throws PortalException, SystemException {
243 
244         if (!WorkflowThreadLocal.isEnabled()) {
245             return;
246         }
247 
248         List<WorkflowInstanceLink> workflowInstanceLinks =
249             getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
250 
251         for (WorkflowInstanceLink workflowInstanceLink :
252                 workflowInstanceLinks) {
253 
254             WorkflowInstance workflowInstance =
255                 WorkflowInstanceManagerUtil.getWorkflowInstance(
256                     workflowInstanceLink.getCompanyId(),
257                     workflowInstanceLink.getWorkflowInstanceId());
258 
259             workflowInstanceLink.setClassPK(newClassPK);
260 
261             workflowInstanceLinkPersistence.update(
262                 workflowInstanceLink, false);
263 
264             Map<String, Serializable> workflowContext =
265                 new HashMap<String, Serializable>(
266                     workflowInstance.getWorkflowContext());
267 
268             workflowContext.put(
269                 WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
270                 String.valueOf(newClassPK));
271 
272             WorkflowInstanceManagerUtil.updateWorkflowContext(
273                 workflowInstanceLink.getCompanyId(),
274                 workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
275         }
276     }
277 
278 }