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