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.WorkflowInstance;
23  import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.model.WorkflowDefinitionLink;
26  import com.liferay.portal.model.WorkflowInstanceLink;
27  import com.liferay.portal.service.base.WorkflowInstanceLinkLocalServiceBaseImpl;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.io.Serializable;
31  
32  import java.util.Date;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * <a href="WorkflowInstanceLinkLocalServiceImpl.java.html"><b><i>View Source
38   * </i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Bruno Farache
42   */
43  public class WorkflowInstanceLinkLocalServiceImpl
44      extends WorkflowInstanceLinkLocalServiceBaseImpl {
45  
46      public WorkflowInstanceLink addWorkflowInstanceLink(
47              long userId, long companyId, long groupId, String className,
48              long classPK, long workflowInstanceId)
49          throws PortalException, SystemException {
50  
51          User user = userPersistence.findByPrimaryKey(userId);
52          long classNameId = PortalUtil.getClassNameId(className);
53          Date now = new Date();
54  
55          long workflowInstanceLinkId = counterLocalService.increment();
56  
57          WorkflowInstanceLink workflowInstanceLink =
58              workflowInstanceLinkPersistence.create(workflowInstanceLinkId);
59  
60          workflowInstanceLink.setCreateDate(now);
61          workflowInstanceLink.setModifiedDate(now);
62          workflowInstanceLink.setUserId(userId);
63          workflowInstanceLink.setUserName(user.getFullName());
64          workflowInstanceLink.setGroupId(groupId);
65          workflowInstanceLink.setCompanyId(companyId);
66          workflowInstanceLink.setClassNameId(classNameId);
67          workflowInstanceLink.setClassPK(classPK);
68          workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);
69  
70          workflowInstanceLinkPersistence.update(workflowInstanceLink, false);
71  
72          return workflowInstanceLink;
73      }
74  
75      public void deleteWorkflowInstanceLink(
76              long companyId, long groupId, String className, long classPK)
77          throws PortalException, SystemException {
78  
79          try {
80              WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
81                  companyId, groupId, className, classPK);
82  
83              deleteWorkflowInstanceLink(workflowInstanceLink);
84  
85              WorkflowInstanceManagerUtil.deleteWorkflowInstance(
86                  companyId, workflowInstanceLink.getWorkflowInstanceId());
87          }
88          catch (NoSuchWorkflowInstanceLinkException nswile) {
89          }
90      }
91  
92      public String getState(
93              long companyId, long groupId, String className, long classPK)
94          throws PortalException, SystemException {
95  
96          WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
97              companyId, groupId, className, classPK);
98  
99          WorkflowInstance workflowInstance =
100             WorkflowInstanceManagerUtil.getWorkflowInstance(
101                 companyId, workflowInstanceLink.getWorkflowInstanceId());
102 
103         return workflowInstance.getState();
104     }
105 
106     public WorkflowInstanceLink getWorkflowInstanceLink(
107             long companyId, long groupId, String className, long classPK)
108         throws PortalException, SystemException {
109 
110         long classNameId = PortalUtil.getClassNameId(className);
111 
112         return workflowInstanceLinkPersistence.findByG_C_C_C(
113             groupId, companyId, classNameId, classPK);
114     }
115 
116     public boolean hasWorkflowInstanceLink(
117             long companyId, long groupId, String className, long classPK)
118         throws PortalException, SystemException {
119 
120         try {
121             getWorkflowInstanceLink(companyId, groupId, className, classPK);
122 
123             return true;
124         }
125         catch (NoSuchWorkflowInstanceLinkException nswile) {
126             return false;
127         }
128     }
129 
130     public void startWorkflowInstance(
131             long companyId, long groupId, long userId, String className,
132             long classPK)
133         throws PortalException, SystemException {
134 
135         try {
136             WorkflowDefinitionLink workflowDefinitionLink =
137                 workflowDefinitionLinkLocalService.getWorkflowDefinitionLink(
138                     companyId, groupId, className);
139 
140             String workflowDefinitionName =
141                 workflowDefinitionLink.getWorkflowDefinitionName();
142             int workflowDefinitionVersion =
143                 workflowDefinitionLink.getWorkflowDefinitionVersion();
144 
145             Map<String, Serializable> context =
146                 new HashMap<String, Serializable>();
147 
148             context.put(ContextConstants.COMPANY_ID, companyId);
149             context.put(ContextConstants.GROUP_ID, groupId);
150             context.put(ContextConstants.ENTRY_CLASS_NAME, className);
151             context.put(ContextConstants.ENTRY_CLASS_PK, classPK);
152 
153             WorkflowInstance workflowInstance =
154                 WorkflowInstanceManagerUtil.startWorkflowInstance(
155                     companyId, userId, workflowDefinitionName,
156                     workflowDefinitionVersion, null, context);
157 
158             addWorkflowInstanceLink(
159                 userId, companyId, groupId, className, classPK,
160                 workflowInstance.getWorkflowInstanceId());
161         }
162         catch (NoSuchWorkflowDefinitionLinkException nswdle) {
163             return;
164         }
165     }
166 
167 }