001    /**
002     * Copyright (c) 2000-2013 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.spring.aop.Skip;
021    import com.liferay.portal.kernel.staging.StagingUtil;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.ObjectValuePair;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.kernel.workflow.WorkflowEngineManagerUtil;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.model.WorkflowDefinitionLink;
031    import com.liferay.portal.service.base.WorkflowDefinitionLinkLocalServiceBaseImpl;
032    import com.liferay.portal.util.PortalUtil;
033    
034    import java.util.Date;
035    import java.util.List;
036    
037    /**
038     * @author Jorge Ferrer
039     * @author Bruno Farache
040     * @author Brian Wing Shun Chan
041     * @author Juan Fern??ndez
042     * @author Marcellus Tavares
043     */
044    public class WorkflowDefinitionLinkLocalServiceImpl
045            extends WorkflowDefinitionLinkLocalServiceBaseImpl {
046    
047            @Override
048            public WorkflowDefinitionLink addWorkflowDefinitionLink(
049                            long userId, long companyId, long groupId, String className,
050                            long classPK, long typePK, String workflowDefinitionName,
051                            int workflowDefinitionVersion)
052                    throws PortalException, SystemException {
053    
054                    User user = userPersistence.findByPrimaryKey(userId);
055                    groupId = StagingUtil.getLiveGroupId(groupId);
056                    long classNameId = PortalUtil.getClassNameId(className);
057                    Date now = new Date();
058    
059                    long workflowDefinitionLinkId = counterLocalService.increment();
060    
061                    WorkflowDefinitionLink workflowDefinitionLink =
062                            workflowDefinitionLinkPersistence.create(workflowDefinitionLinkId);
063    
064                    workflowDefinitionLink.setCreateDate(now);
065                    workflowDefinitionLink.setModifiedDate(now);
066                    workflowDefinitionLink.setUserId(userId);
067                    workflowDefinitionLink.setUserName(user.getFullName());
068                    workflowDefinitionLink.setGroupId(groupId);
069                    workflowDefinitionLink.setCompanyId(companyId);
070                    workflowDefinitionLink.setClassNameId(classNameId);
071                    workflowDefinitionLink.setClassPK(classPK);
072                    workflowDefinitionLink.setTypePK(typePK);
073                    workflowDefinitionLink.setWorkflowDefinitionName(
074                            workflowDefinitionName);
075                    workflowDefinitionLink.setWorkflowDefinitionVersion(
076                            workflowDefinitionVersion);
077    
078                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink);
079    
080                    return workflowDefinitionLink;
081            }
082    
083            @Override
084            public void deleteWorkflowDefinitionLink(
085                            long companyId, long groupId, String className, long classPK,
086                            long typePK)
087                    throws PortalException, SystemException {
088    
089                    WorkflowDefinitionLink workflowDefinitionLink =
090                            fetchWorkflowDefinitionLink(
091                                    companyId, groupId, className, classPK, typePK, true);
092    
093                    if (workflowDefinitionLink != null) {
094                            deleteWorkflowDefinitionLink(workflowDefinitionLink);
095                    }
096            }
097    
098            @Override
099            public WorkflowDefinitionLink fetchDefaultWorkflowDefinitionLink(
100                            long companyId, String className, long classPK, long typePK)
101                    throws SystemException {
102    
103                    if (!WorkflowEngineManagerUtil.isDeployed()) {
104                            return null;
105                    }
106    
107                    long classNameId = PortalUtil.getClassNameId(className);
108    
109                    return workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
110                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId, classPK,
111                            typePK);
112            }
113    
114            @Override
115            public WorkflowDefinitionLink fetchWorkflowDefinitionLink(
116                            long companyId, long groupId, String className, long classPK,
117                            long typePK)
118                    throws PortalException, SystemException {
119    
120                    return fetchWorkflowDefinitionLink(
121                            companyId, groupId, className, classPK, typePK, false);
122            }
123    
124            @Override
125            public WorkflowDefinitionLink fetchWorkflowDefinitionLink(
126                            long companyId, long groupId, String className, long classPK,
127                            long typePK, boolean strict)
128                    throws PortalException, SystemException {
129    
130                    if (!WorkflowEngineManagerUtil.isDeployed()) {
131                            return null;
132                    }
133    
134                    groupId = StagingUtil.getLiveGroupId(groupId);
135                    long classNameId = PortalUtil.getClassNameId(className);
136    
137                    WorkflowDefinitionLink workflowDefinitionLink = null;
138    
139                    workflowDefinitionLink =
140                            workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
141                                    groupId, companyId, classNameId, classPK, typePK);
142    
143                    if (!strict && (workflowDefinitionLink == null)) {
144                            workflowDefinitionLink =
145                                    workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
146                                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId,
147                                            classPK, typePK);
148                    }
149    
150                    return workflowDefinitionLink;
151            }
152    
153            @Override
154            public WorkflowDefinitionLink getDefaultWorkflowDefinitionLink(
155                            long companyId, String className, long classPK, long typePK)
156                    throws PortalException, SystemException {
157    
158                    if (!WorkflowEngineManagerUtil.isDeployed()) {
159                            throw new NoSuchWorkflowDefinitionLinkException();
160                    }
161    
162                    long classNameId = PortalUtil.getClassNameId(className);
163    
164                    return workflowDefinitionLinkPersistence.findByG_C_C_C_T(
165                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId, classPK,
166                            typePK);
167            }
168    
169            @Override
170            public WorkflowDefinitionLink getWorkflowDefinitionLink(
171                            long companyId, long groupId, String className, long classPK,
172                            long typePK)
173                    throws PortalException, SystemException {
174    
175                    return getWorkflowDefinitionLink(
176                            companyId, groupId, className, classPK, typePK, false);
177            }
178    
179            @Override
180            public WorkflowDefinitionLink getWorkflowDefinitionLink(
181                            long companyId, long groupId, String className, long classPK,
182                            long typePK, boolean strict)
183                    throws PortalException, SystemException {
184    
185                    if (!WorkflowEngineManagerUtil.isDeployed()) {
186                            throw new NoSuchWorkflowDefinitionLinkException();
187                    }
188    
189                    WorkflowDefinitionLink workflowDefinitionLink =
190                            fetchWorkflowDefinitionLink(
191                                    companyId, groupId, className, classPK, typePK, strict);
192    
193                    if (workflowDefinitionLink == null) {
194                            throw new NoSuchWorkflowDefinitionLinkException(
195                                    "No workflow for groupId=" + groupId + ", companyId=" +
196                                            companyId + " and className=" + className);
197                    }
198    
199                    return workflowDefinitionLink;
200            }
201    
202            @Override
203            public int getWorkflowDefinitionLinksCount(
204                            long companyId, String workflowDefinitionName,
205                            int workflowDefinitionVersion)
206                    throws SystemException {
207    
208                    if (!WorkflowEngineManagerUtil.isDeployed()) {
209                            return 0;
210                    }
211    
212                    return workflowDefinitionLinkPersistence.countByC_W_W(
213                            companyId, workflowDefinitionName, workflowDefinitionVersion);
214            }
215    
216            @Override
217            @Skip
218            public boolean hasWorkflowDefinitionLink(
219                            long companyId, long groupId, String className)
220                    throws PortalException, SystemException {
221    
222                    return hasWorkflowDefinitionLink(companyId, groupId, className, 0);
223            }
224    
225            @Override
226            @Skip
227            public boolean hasWorkflowDefinitionLink(
228                            long companyId, long groupId, String className, long classPK)
229                    throws PortalException, SystemException {
230    
231                    return hasWorkflowDefinitionLink(
232                            companyId, groupId, className, classPK, 0);
233            }
234    
235            @Override
236            @Skip
237            public boolean hasWorkflowDefinitionLink(
238                            long companyId, long groupId, String className, long classPK,
239                            long typePK)
240                    throws PortalException, SystemException {
241    
242                    if (!WorkflowEngineManagerUtil.isDeployed()) {
243                            return false;
244                    }
245    
246                    WorkflowDefinitionLink workflowDefinitionLink =
247                            workflowDefinitionLinkLocalService.fetchWorkflowDefinitionLink(
248                                    companyId, groupId, className, classPK, typePK);
249    
250                    if (workflowDefinitionLink != null) {
251                            return true;
252                    }
253    
254                    return false;
255            }
256    
257            @Override
258            public void updateWorkflowDefinitionLink(
259                            long userId, long companyId, long groupId, String className,
260                            long classPK, long typePK, String workflowDefinition)
261                    throws PortalException, SystemException {
262    
263                    if (Validator.isNull(workflowDefinition)) {
264                            deleteWorkflowDefinitionLink(
265                                    companyId, groupId, className, classPK, typePK);
266                    }
267                    else {
268                            String[] workflowDefinitionParts = StringUtil.split(
269                                    workflowDefinition, CharPool.AT);
270    
271                            String workflowDefinitionName = workflowDefinitionParts[0];
272                            int workflowDefinitionVersion = GetterUtil.getInteger(
273                                    workflowDefinitionParts[1]);
274    
275                            updateWorkflowDefinitionLink(
276                                    userId, companyId, groupId, className, classPK, typePK,
277                                    workflowDefinitionName, workflowDefinitionVersion);
278                    }
279            }
280    
281            @Override
282            public WorkflowDefinitionLink updateWorkflowDefinitionLink(
283                            long userId, long companyId, long groupId, String className,
284                            long classPK, long typePK, String workflowDefinitionName,
285                            int workflowDefinitionVersion)
286                    throws PortalException, SystemException {
287    
288                    User user = userPersistence.findByPrimaryKey(userId);
289                    groupId = StagingUtil.getLiveGroupId(groupId);
290                    long classNameId = PortalUtil.getClassNameId(className);
291                    Date now = new Date();
292    
293                    WorkflowDefinitionLink workflowDefinitionLink =
294                            workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
295                                    groupId, companyId, classNameId, classPK, typePK);
296    
297                    if (workflowDefinitionLink == null) {
298                            workflowDefinitionLink = addWorkflowDefinitionLink(
299                                    userId, companyId, groupId, className, classPK, typePK,
300                                    workflowDefinitionName, workflowDefinitionVersion);
301                    }
302    
303                    workflowDefinitionLink.setModifiedDate(now);
304                    workflowDefinitionLink.setUserId(userId);
305                    workflowDefinitionLink.setUserName(user.getFullName());
306                    workflowDefinitionLink.setGroupId(groupId);
307                    workflowDefinitionLink.setCompanyId(companyId);
308                    workflowDefinitionLink.setClassNameId(classNameId);
309                    workflowDefinitionLink.setClassPK(classPK);
310                    workflowDefinitionLink.setTypePK(typePK);
311                    workflowDefinitionLink.setWorkflowDefinitionName(
312                            workflowDefinitionName);
313                    workflowDefinitionLink.setWorkflowDefinitionVersion(
314                            workflowDefinitionVersion);
315    
316                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink);
317    
318                    return workflowDefinitionLink;
319            }
320    
321            @Override
322            public void updateWorkflowDefinitionLinks(
323                            long userId, long companyId, long groupId, String className,
324                            long classPK,
325                            List<ObjectValuePair<Long, String>> workflowDefinitionOVPs)
326                    throws PortalException, SystemException {
327    
328                    for (ObjectValuePair<Long, String> workflowDefinitionOVP :
329                                    workflowDefinitionOVPs) {
330    
331                            long typePK = workflowDefinitionOVP.getKey();
332                            String workflowDefinitionName = workflowDefinitionOVP.getValue();
333    
334                            if (Validator.isNull(workflowDefinitionName)) {
335                                    deleteWorkflowDefinitionLink(
336                                            companyId, groupId, className, classPK, typePK);
337                            }
338                            else {
339                                    updateWorkflowDefinitionLink(
340                                            userId, companyId, groupId, className, classPK, typePK,
341                                            workflowDefinitionName);
342                            }
343                    }
344            }
345    
346    }