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.NoSuchWorkflowDefinitionLinkException;
18  import com.liferay.portal.NoSuchWorkflowInstanceLinkException;
19  import com.liferay.portal.kernel.exception.PortalException;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.workflow.ContextConstants;
22  import com.liferay.portal.kernel.workflow.WorkflowHandler;
23  import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
24  import com.liferay.portal.kernel.workflow.WorkflowInstance;
25  import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
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> workflowInstaneLinks =
132             getWorkflowInstanceLinks(companyId, groupId, className, classPK);
133 
134         if (workflowInstaneLinks.isEmpty()) {
135             throw new NoSuchWorkflowInstanceLinkException();
136         }
137         else {
138             return workflowInstaneLinks.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)
191         throws PortalException, SystemException {
192 
193         try {
194             WorkflowDefinitionLink workflowDefinitionLink =
195                 workflowDefinitionLinkLocalService.getWorkflowDefinitionLink(
196                     companyId, groupId, className);
197 
198             String workflowDefinitionName =
199                 workflowDefinitionLink.getWorkflowDefinitionName();
200             int workflowDefinitionVersion =
201                 workflowDefinitionLink.getWorkflowDefinitionVersion();
202 
203             Map<String, Serializable> context =
204                 new HashMap<String, Serializable>();
205 
206             context.put(ContextConstants.COMPANY_ID, companyId);
207             context.put(ContextConstants.GROUP_ID, groupId);
208             context.put(ContextConstants.ENTRY_CLASS_NAME, className);
209             context.put(ContextConstants.ENTRY_CLASS_PK, classPK);
210 
211             WorkflowHandler workflowHandler =
212                 WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
213 
214             context.put(ContextConstants.ENTRY_TYPE, workflowHandler.getType());
215 
216             WorkflowInstance workflowInstance =
217                 WorkflowInstanceManagerUtil.startWorkflowInstance(
218                     companyId, userId, workflowDefinitionName,
219                     workflowDefinitionVersion, null, context);
220 
221             addWorkflowInstanceLink(
222                 userId, companyId, groupId, className, classPK,
223                 workflowInstance.getWorkflowInstanceId());
224         }
225         catch (NoSuchWorkflowDefinitionLinkException nswdle) {
226             return;
227         }
228     }
229 
230     public void updateClassPK(
231             long companyId, long groupId, String className, long oldClassPK,
232             long newClassPK)
233         throws PortalException, SystemException {
234 
235         List<WorkflowInstanceLink> workflowInstanceLinks =
236             getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
237 
238         for (WorkflowInstanceLink workflowInstanceLink :
239                 workflowInstanceLinks) {
240 
241             WorkflowInstance workflowInstance =
242                 WorkflowInstanceManagerUtil.getWorkflowInstance(
243                     workflowInstanceLink.getCompanyId(),
244                     workflowInstanceLink.getWorkflowInstanceId());
245 
246             workflowInstanceLink.setClassPK(newClassPK);
247 
248             workflowInstanceLinkPersistence.update(
249                 workflowInstanceLink, false);
250 
251             Map<String, Serializable> context =
252                 new HashMap<String, Serializable>(
253                     workflowInstance.getContext());
254 
255             context.put(ContextConstants.ENTRY_CLASS_PK, newClassPK);
256 
257             WorkflowInstanceManagerUtil.updateContext(
258                 workflowInstanceLink.getCompanyId(),
259                 workflowInstanceLink.getWorkflowInstanceId(), context);
260         }
261     }
262 
263 }