001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ObjectValuePair;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.kernel.workflow.WorkflowEngineManagerUtil;
028    import com.liferay.portal.model.Group;
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            public WorkflowDefinitionLink addWorkflowDefinitionLink(
048                            long userId, long companyId, long groupId, String className,
049                            long classPK, long typePK, String workflowDefinitionName,
050                            int workflowDefinitionVersion)
051                    throws PortalException, SystemException {
052    
053                    User user = userPersistence.findByPrimaryKey(userId);
054                    long classNameId = PortalUtil.getClassNameId(className);
055                    Date now = new Date();
056    
057                    long workflowDefinitionLinkId = counterLocalService.increment();
058    
059                    WorkflowDefinitionLink workflowDefinitionLink =
060                            workflowDefinitionLinkPersistence.create(workflowDefinitionLinkId);
061    
062                    workflowDefinitionLink.setCreateDate(now);
063                    workflowDefinitionLink.setModifiedDate(now);
064                    workflowDefinitionLink.setUserId(userId);
065                    workflowDefinitionLink.setUserName(user.getFullName());
066                    workflowDefinitionLink.setGroupId(groupId);
067                    workflowDefinitionLink.setCompanyId(companyId);
068                    workflowDefinitionLink.setClassNameId(classNameId);
069                    workflowDefinitionLink.setClassPK(classPK);
070                    workflowDefinitionLink.setTypePK(typePK);
071                    workflowDefinitionLink.setWorkflowDefinitionName(
072                            workflowDefinitionName);
073                    workflowDefinitionLink.setWorkflowDefinitionVersion(
074                            workflowDefinitionVersion);
075    
076                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
077    
078                    return workflowDefinitionLink;
079            }
080    
081            public void deleteWorkflowDefinitionLink(
082                            long companyId, long groupId, String className, long classPK,
083                            long typePK)
084                    throws PortalException, SystemException {
085    
086                    try {
087                            WorkflowDefinitionLink workflowDefinitionLink =
088                                    getWorkflowDefinitionLink(
089                                            companyId, groupId, className, classPK, typePK, true);
090    
091                            deleteWorkflowDefinitionLink(workflowDefinitionLink);
092                    }
093                    catch (NoSuchWorkflowDefinitionLinkException nswdle) {
094                    }
095            }
096    
097            public WorkflowDefinitionLink getDefaultWorkflowDefinitionLink(
098                            long companyId, String className, long classPK, long typePK)
099                    throws PortalException, SystemException {
100    
101                    if (!WorkflowEngineManagerUtil.isDeployed()) {
102                            throw new NoSuchWorkflowDefinitionLinkException();
103                    }
104    
105                    long classNameId = PortalUtil.getClassNameId(className);
106    
107                    return workflowDefinitionLinkPersistence.findByG_C_C_C_T(
108                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId,
109                            classPK, typePK);
110            }
111    
112            public WorkflowDefinitionLink getWorkflowDefinitionLink(
113                            long companyId, long groupId, String className, long classPK,
114                            long typePK)
115                    throws PortalException, SystemException {
116    
117                    return getWorkflowDefinitionLink(
118                            companyId, groupId, className, classPK, typePK, false);
119            }
120    
121            public WorkflowDefinitionLink getWorkflowDefinitionLink(
122                            long companyId, long groupId, String className, long classPK,
123                            long typePK, boolean strict)
124                    throws PortalException, SystemException {
125    
126                    if (!WorkflowEngineManagerUtil.isDeployed()) {
127                            throw new NoSuchWorkflowDefinitionLinkException();
128                    }
129    
130                    long classNameId = PortalUtil.getClassNameId(className);
131    
132                    WorkflowDefinitionLink workflowDefinitionLink = null;
133    
134                    if (groupId > 0) {
135                            Group group = groupLocalService.getGroup(groupId);
136    
137                            if (group.isLayout()) {
138                                    groupId = group.getParentGroupId();
139                            }
140                    }
141    
142                    workflowDefinitionLink =
143                            workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
144                                    groupId, companyId, classNameId, classPK, typePK);
145    
146                    if (!strict && (workflowDefinitionLink == null)) {
147                            workflowDefinitionLink =
148                                    workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
149                                            WorkflowConstants.DEFAULT_GROUP_ID, companyId, classNameId,
150                                            classPK, typePK);
151                    }
152    
153                    if (workflowDefinitionLink == null) {
154                            throw new NoSuchWorkflowDefinitionLinkException(
155                                    "No workflow for groupId=" + groupId + ", companyId=" +
156                                            companyId + " and classNameId=" + classNameId);
157                    }
158    
159                    return workflowDefinitionLink;
160            }
161    
162            public int getWorkflowDefinitionLinksCount(
163                            long companyId, String workflowDefinitionName,
164                            int workflowDefinitionVersion)
165                    throws SystemException{
166    
167                    if (!WorkflowEngineManagerUtil.isDeployed()) {
168                            return 0;
169                    }
170    
171                    return workflowDefinitionLinkPersistence.countByC_W_W(
172                            companyId, workflowDefinitionName, workflowDefinitionVersion);
173            }
174    
175            @Skip
176            public boolean hasWorkflowDefinitionLink(
177                            long companyId, long groupId, String className)
178                    throws PortalException, SystemException {
179    
180                    return hasWorkflowDefinitionLink(companyId, groupId, className, 0);
181            }
182    
183            @Skip
184            public boolean hasWorkflowDefinitionLink(
185                            long companyId, long groupId, String className, long classPK)
186                    throws PortalException, SystemException {
187    
188                    return hasWorkflowDefinitionLink(
189                            companyId, groupId, className, classPK, 0);
190            }
191    
192            @Skip
193            public boolean hasWorkflowDefinitionLink(
194                            long companyId, long groupId, String className, long classPK,
195                            long typePK)
196                    throws PortalException, SystemException {
197    
198                    if (!WorkflowEngineManagerUtil.isDeployed()) {
199                            return false;
200                    }
201    
202                    try {
203                            workflowDefinitionLinkLocalService.getWorkflowDefinitionLink(
204                                    companyId, groupId, className, classPK, typePK);
205    
206                            return true;
207                    }
208                    catch (NoSuchWorkflowDefinitionLinkException nswdle) {
209                            return false;
210                    }
211            }
212    
213            public void updateWorkflowDefinitionLink(
214                            long userId, long companyId, long groupId, String className,
215                            long classPK, long typePK, String workflowDefinition)
216                    throws PortalException, SystemException {
217    
218                    if (Validator.isNull(workflowDefinition)) {
219                            deleteWorkflowDefinitionLink(
220                                    companyId, groupId, className, classPK, typePK);
221                    }
222                    else {
223                            String[] workflowDefinitionParts = StringUtil.split(
224                                    workflowDefinition, CharPool.AT);
225    
226                            String workflowDefinitionName = workflowDefinitionParts[0];
227                            int workflowDefinitionVersion = GetterUtil.getInteger(
228                                    workflowDefinitionParts[1]);
229    
230                            updateWorkflowDefinitionLink(
231                                    userId, companyId, groupId, className, classPK, typePK,
232                                    workflowDefinitionName, workflowDefinitionVersion);
233                    }
234            }
235    
236            public WorkflowDefinitionLink updateWorkflowDefinitionLink(
237                            long userId, long companyId, long groupId, String className,
238                            long classPK, long typePK, String workflowDefinitionName,
239                            int workflowDefinitionVersion)
240                    throws PortalException, SystemException {
241    
242                    User user = userPersistence.findByPrimaryKey(userId);
243                    long classNameId = PortalUtil.getClassNameId(className);
244                    Date now = new Date();
245    
246                    WorkflowDefinitionLink workflowDefinitionLink =
247                            workflowDefinitionLinkPersistence.fetchByG_C_C_C_T(
248                                    groupId, companyId, classNameId, classPK, typePK);
249    
250                    if (workflowDefinitionLink == null) {
251                            workflowDefinitionLink = addWorkflowDefinitionLink(
252                                    userId, companyId, groupId, className, classPK, typePK,
253                                    workflowDefinitionName, workflowDefinitionVersion);
254                    }
255    
256                    workflowDefinitionLink.setModifiedDate(now);
257                    workflowDefinitionLink.setUserId(userId);
258                    workflowDefinitionLink.setUserName(user.getFullName());
259                    workflowDefinitionLink.setGroupId(groupId);
260                    workflowDefinitionLink.setCompanyId(companyId);
261                    workflowDefinitionLink.setClassNameId(classNameId);
262                    workflowDefinitionLink.setClassPK(classPK);
263                    workflowDefinitionLink.setTypePK(typePK);
264                    workflowDefinitionLink.setWorkflowDefinitionName(
265                            workflowDefinitionName);
266                    workflowDefinitionLink.setWorkflowDefinitionVersion(
267                            workflowDefinitionVersion);
268    
269                    workflowDefinitionLinkPersistence.update(workflowDefinitionLink, false);
270    
271                    return workflowDefinitionLink;
272            }
273    
274            public void updateWorkflowDefinitionLinks(
275                            long userId, long companyId, long groupId, String className,
276                            long classPK,
277                            List<ObjectValuePair<Long, String>> workflowDefinitions)
278                    throws PortalException, SystemException {
279    
280                    for (ObjectValuePair<Long, String> workflowDefinition :
281                                    workflowDefinitions) {
282    
283                            long typePK = workflowDefinition.getKey();
284                            String workflowDefinitionName = workflowDefinition.getValue();
285    
286                            if (Validator.isNull(workflowDefinitionName)) {
287                                    deleteWorkflowDefinitionLink(
288                                            companyId, groupId, className, classPK, typePK);
289                            }
290                            else {
291                                    updateWorkflowDefinitionLink(
292                                            userId, companyId, groupId, className, classPK, typePK,
293                                            workflowDefinitionName);
294                            }
295                    }
296            }
297    
298    }