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.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.WorkflowEngineManagerUtil;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.model.WorkflowDefinitionLink;
25  import com.liferay.portal.service.base.WorkflowDefinitionLinkLocalServiceBaseImpl;
26  import com.liferay.portal.util.PortalUtil;
27  
28  import java.util.Date;
29  
30  /**
31   * <a href="WorkflowDefinitionLinkLocalServiceImpl.java.html"><b><i>View Source
32   * </i></b></a>
33   *
34   * @author Jorge Ferrer
35   * @author Bruno Farache
36   * @author Brian Wing Shun Chan
37   * @author Juan Fernández
38   */
39  public class WorkflowDefinitionLinkLocalServiceImpl
40      extends WorkflowDefinitionLinkLocalServiceBaseImpl {
41  
42      public WorkflowDefinitionLink addWorkflowDefinitionLink(
43              long userId, long companyId, long groupId, String className,
44              String workflowDefinitionName, int workflowDefinitionVersion)
45          throws PortalException, SystemException {
46  
47          User user = userPersistence.findByPrimaryKey(userId);
48          long classNameId = PortalUtil.getClassNameId(className);
49          Date now = new Date();
50  
51          long workflowDefinitionLinkId = counterLocalService.increment();
52  
53          WorkflowDefinitionLink workflowDefinitionLink =
54              workflowDefinitionLinkPersistence.create(workflowDefinitionLinkId);
55  
56          workflowDefinitionLink.setCreateDate(now);
57          workflowDefinitionLink.setModifiedDate(now);
58          workflowDefinitionLink.setUserId(userId);
59          workflowDefinitionLink.setUserName(user.getFullName());
60          workflowDefinitionLink.setGroupId(groupId);
61          workflowDefinitionLink.setCompanyId(companyId);
62          workflowDefinitionLink.setClassNameId(classNameId);
63          workflowDefinitionLink.setWorkflowDefinitionName(
64              workflowDefinitionName);
65          workflowDefinitionLink.setWorkflowDefinitionVersion(
66              workflowDefinitionVersion);
67  
68          workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
69  
70          return workflowDefinitionLink;
71      }
72  
73      public void deleteWorkflowDefinitionLink(
74              long companyId, long groupId, String className)
75          throws PortalException, SystemException {
76  
77          try {
78              WorkflowDefinitionLink workflowDefinitionLink =
79                  getWorkflowDefinitionLink(companyId, groupId, className);
80  
81              deleteWorkflowDefinitionLink(workflowDefinitionLink);
82          }
83          catch (NoSuchWorkflowDefinitionLinkException nswdle) {
84          }
85      }
86  
87      public WorkflowDefinitionLink getDefaultWorkflowDefinitionLink(
88              long companyId, String className)
89          throws PortalException, SystemException {
90  
91          if (!WorkflowEngineManagerUtil.isDeployed()) {
92              throw new NoSuchWorkflowDefinitionLinkException();
93          }
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          return workflowDefinitionLinkPersistence.findByG_C_C(
98              WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId);
99      }
100 
101     public WorkflowDefinitionLink getWorkflowDefinitionLink(
102             long companyId, long groupId, String className)
103         throws PortalException, SystemException {
104 
105         return getWorkflowDefinitionLink(companyId, groupId, className, false);
106     }
107 
108     public WorkflowDefinitionLink getWorkflowDefinitionLink(
109             long companyId, long groupId, String className, boolean strict)
110         throws PortalException, SystemException {
111 
112         if (!WorkflowEngineManagerUtil.isDeployed()) {
113             throw new NoSuchWorkflowDefinitionLinkException();
114         }
115 
116         long classNameId = PortalUtil.getClassNameId(className);
117 
118         WorkflowDefinitionLink workflowDefinitionLink = null;
119 
120         if (groupId > 0) {
121             Group group = groupLocalService.getGroup(groupId);
122 
123             if (group.isLayout()) {
124                 groupId = group.getParentGroupId();
125             }
126 
127             workflowDefinitionLink =
128                 workflowDefinitionLinkPersistence.fetchByG_C_C(
129                     groupId, companyId, classNameId);
130         }
131 
132         if (!strict && (workflowDefinitionLink == null)) {
133             workflowDefinitionLink =
134                 workflowDefinitionLinkPersistence.fetchByG_C_C(
135                     WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId);
136         }
137 
138         if (workflowDefinitionLink == null) {
139             throw new NoSuchWorkflowDefinitionLinkException(
140                 "No workflow for groupId=" + groupId + ", companyId=" +
141                     companyId + " and classNameId=" + classNameId);
142         }
143 
144         return workflowDefinitionLink;
145     }
146 
147     public int getWorkflowDefinitionLinksCount(
148             long companyId, String workflowDefinitionName,
149             int workflowDefinitionVersion)
150         throws SystemException{
151 
152         if (!WorkflowEngineManagerUtil.isDeployed()) {
153             return 0;
154         }
155 
156         return workflowDefinitionLinkPersistence.countByC_W_W(
157             companyId, workflowDefinitionName, workflowDefinitionVersion);
158     }
159 
160     public boolean hasWorkflowDefinitionLink(
161             long companyId, long groupId, String className)
162         throws PortalException, SystemException {
163 
164         if (!WorkflowEngineManagerUtil.isDeployed()) {
165             return false;
166         }
167 
168         try {
169             getWorkflowDefinitionLink(companyId, groupId, className);
170 
171             return true;
172         }
173         catch (NoSuchWorkflowDefinitionLinkException nswdle) {
174             return false;
175         }
176     }
177 
178     public WorkflowDefinitionLink updateWorkflowDefinitionLink(
179             long userId, long companyId, long groupId, String className,
180             String workflowDefinitionName, int workflowDefinitionVersion)
181         throws PortalException, SystemException {
182 
183         User user = userPersistence.findByPrimaryKey(userId);
184         long classNameId = PortalUtil.getClassNameId(className);
185         Date now = new Date();
186 
187         WorkflowDefinitionLink workflowDefinitionLink =
188             workflowDefinitionLinkPersistence.fetchByG_C_C(
189                 groupId, companyId, classNameId);
190 
191         if (workflowDefinitionLink == null) {
192             workflowDefinitionLink = addWorkflowDefinitionLink(
193                 userId, companyId, groupId, className, workflowDefinitionName,
194                 workflowDefinitionVersion);
195         }
196 
197         workflowDefinitionLink.setModifiedDate(now);
198         workflowDefinitionLink.setUserId(userId);
199         workflowDefinitionLink.setUserName(user.getFullName());
200         workflowDefinitionLink.setGroupId(groupId);
201         workflowDefinitionLink.setCompanyId(companyId);
202         workflowDefinitionLink.setClassNameId(classNameId);
203         workflowDefinitionLink.setWorkflowDefinitionName(
204             workflowDefinitionName);
205         workflowDefinitionLink.setWorkflowDefinitionVersion(
206             workflowDefinitionVersion);
207 
208         workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
209 
210         return workflowDefinitionLink;
211     }
212 
213 }