001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchWorkflowDefinitionLinkException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.kernel.workflow.WorkflowEngineManagerUtil;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.model.WorkflowDefinitionLink;
025    import com.liferay.portal.service.base.WorkflowDefinitionLinkLocalServiceBaseImpl;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.Date;
029    
030    /**
031     * @author Jorge Ferrer
032     * @author Bruno Farache
033     * @author Brian Wing Shun Chan
034     * @author Juan Fernández
035     */
036    public class WorkflowDefinitionLinkLocalServiceImpl
037            extends WorkflowDefinitionLinkLocalServiceBaseImpl {
038    
039            public WorkflowDefinitionLink addWorkflowDefinitionLink(
040                            long userId, long companyId, long groupId, String className,
041                            String workflowDefinitionName, int workflowDefinitionVersion)
042                    throws PortalException, SystemException {
043    
044                    User user = userPersistence.findByPrimaryKey(userId);
045                    long classNameId = PortalUtil.getClassNameId(className);
046                    Date now = new Date();
047    
048                    long workflowDefinitionLinkId = counterLocalService.increment();
049    
050                    WorkflowDefinitionLink workflowDefinitionLink =
051                            workflowDefinitionLinkPersistence.create(workflowDefinitionLinkId);
052    
053                    workflowDefinitionLink.setCreateDate(now);
054                    workflowDefinitionLink.setModifiedDate(now);
055                    workflowDefinitionLink.setUserId(userId);
056                    workflowDefinitionLink.setUserName(user.getFullName());
057                    workflowDefinitionLink.setGroupId(groupId);
058                    workflowDefinitionLink.setCompanyId(companyId);
059                    workflowDefinitionLink.setClassNameId(classNameId);
060                    workflowDefinitionLink.setWorkflowDefinitionName(
061                            workflowDefinitionName);
062                    workflowDefinitionLink.setWorkflowDefinitionVersion(
063                            workflowDefinitionVersion);
064    
065                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
066    
067                    return workflowDefinitionLink;
068            }
069    
070            public void deleteWorkflowDefinitionLink(
071                            long companyId, long groupId, String className)
072                    throws PortalException, SystemException {
073    
074                    try {
075                            WorkflowDefinitionLink workflowDefinitionLink =
076                                    getWorkflowDefinitionLink(companyId, groupId, className);
077    
078                            deleteWorkflowDefinitionLink(workflowDefinitionLink);
079                    }
080                    catch (NoSuchWorkflowDefinitionLinkException nswdle) {
081                    }
082            }
083    
084            public WorkflowDefinitionLink getDefaultWorkflowDefinitionLink(
085                            long companyId, String className)
086                    throws PortalException, SystemException {
087    
088                    if (!WorkflowEngineManagerUtil.isDeployed()) {
089                            throw new NoSuchWorkflowDefinitionLinkException();
090                    }
091    
092                    long classNameId = PortalUtil.getClassNameId(className);
093    
094                    return workflowDefinitionLinkPersistence.findByG_C_C(
095                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId);
096            }
097    
098            public WorkflowDefinitionLink getWorkflowDefinitionLink(
099                            long companyId, long groupId, String className)
100                    throws PortalException, SystemException {
101    
102                    return getWorkflowDefinitionLink(companyId, groupId, className, false);
103            }
104    
105            public WorkflowDefinitionLink getWorkflowDefinitionLink(
106                            long companyId, long groupId, String className, boolean strict)
107                    throws PortalException, SystemException {
108    
109                    if (!WorkflowEngineManagerUtil.isDeployed()) {
110                            throw new NoSuchWorkflowDefinitionLinkException();
111                    }
112    
113                    long classNameId = PortalUtil.getClassNameId(className);
114    
115                    WorkflowDefinitionLink workflowDefinitionLink = null;
116    
117                    if (groupId > 0) {
118                            Group group = groupLocalService.getGroup(groupId);
119    
120                            if (group.isLayout()) {
121                                    groupId = group.getParentGroupId();
122                            }
123    
124                            workflowDefinitionLink =
125                                    workflowDefinitionLinkPersistence.fetchByG_C_C(
126                                            groupId, companyId, classNameId);
127                    }
128    
129                    if (!strict && (workflowDefinitionLink == null)) {
130                            workflowDefinitionLink =
131                                    workflowDefinitionLinkPersistence.fetchByG_C_C(
132                                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId);
133                    }
134    
135                    if (workflowDefinitionLink == null) {
136                            throw new NoSuchWorkflowDefinitionLinkException(
137                                    "No workflow for groupId=" + groupId + ", companyId=" +
138                                            companyId + " and classNameId=" + classNameId);
139                    }
140    
141                    return workflowDefinitionLink;
142            }
143    
144            public int getWorkflowDefinitionLinksCount(
145                            long companyId, String workflowDefinitionName,
146                            int workflowDefinitionVersion)
147                    throws SystemException{
148    
149                    if (!WorkflowEngineManagerUtil.isDeployed()) {
150                            return 0;
151                    }
152    
153                    return workflowDefinitionLinkPersistence.countByC_W_W(
154                            companyId, workflowDefinitionName, workflowDefinitionVersion);
155            }
156    
157            public boolean hasWorkflowDefinitionLink(
158                            long companyId, long groupId, String className)
159                    throws PortalException, SystemException {
160    
161                    if (!WorkflowEngineManagerUtil.isDeployed()) {
162                            return false;
163                    }
164    
165                    try {
166                            getWorkflowDefinitionLink(companyId, groupId, className);
167    
168                            return true;
169                    }
170                    catch (NoSuchWorkflowDefinitionLinkException nswdle) {
171                            return false;
172                    }
173            }
174    
175            public WorkflowDefinitionLink updateWorkflowDefinitionLink(
176                            long userId, long companyId, long groupId, String className,
177                            String workflowDefinitionName, int workflowDefinitionVersion)
178                    throws PortalException, SystemException {
179    
180                    User user = userPersistence.findByPrimaryKey(userId);
181                    long classNameId = PortalUtil.getClassNameId(className);
182                    Date now = new Date();
183    
184                    WorkflowDefinitionLink workflowDefinitionLink =
185                            workflowDefinitionLinkPersistence.fetchByG_C_C(
186                                    groupId, companyId, classNameId);
187    
188                    if (workflowDefinitionLink == null) {
189                            workflowDefinitionLink = addWorkflowDefinitionLink(
190                                    userId, companyId, groupId, className, workflowDefinitionName,
191                                    workflowDefinitionVersion);
192                    }
193    
194                    workflowDefinitionLink.setModifiedDate(now);
195                    workflowDefinitionLink.setUserId(userId);
196                    workflowDefinitionLink.setUserName(user.getFullName());
197                    workflowDefinitionLink.setGroupId(groupId);
198                    workflowDefinitionLink.setCompanyId(companyId);
199                    workflowDefinitionLink.setClassNameId(classNameId);
200                    workflowDefinitionLink.setWorkflowDefinitionName(
201                            workflowDefinitionName);
202                    workflowDefinitionLink.setWorkflowDefinitionVersion(
203                            workflowDefinitionVersion);
204    
205                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
206    
207                    return workflowDefinitionLink;
208            }
209    
210    }