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.NoSuchWorkflowInstanceLinkException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.kernel.workflow.WorkflowHandler;
023    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowInstance;
025    import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.model.WorkflowDefinitionLink;
029    import com.liferay.portal.model.WorkflowInstanceLink;
030    import com.liferay.portal.service.base.WorkflowInstanceLinkLocalServiceBaseImpl;
031    import com.liferay.portal.util.PortalUtil;
032    
033    import java.io.Serializable;
034    
035    import java.util.Date;
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Map;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Bruno Farache
043     * @author Marcellus Tavares
044     */
045    public class WorkflowInstanceLinkLocalServiceImpl
046            extends WorkflowInstanceLinkLocalServiceBaseImpl {
047    
048            @Override
049            public WorkflowInstanceLink addWorkflowInstanceLink(
050                            long userId, long companyId, long groupId, String className,
051                            long classPK, long workflowInstanceId)
052                    throws PortalException, SystemException {
053    
054                    User user = userPersistence.findByPrimaryKey(userId);
055                    long classNameId = PortalUtil.getClassNameId(className);
056                    Date now = new Date();
057    
058                    long workflowInstanceLinkId = counterLocalService.increment();
059    
060                    WorkflowInstanceLink workflowInstanceLink =
061                            workflowInstanceLinkPersistence.create(workflowInstanceLinkId);
062    
063                    workflowInstanceLink.setCreateDate(now);
064                    workflowInstanceLink.setModifiedDate(now);
065                    workflowInstanceLink.setUserId(userId);
066                    workflowInstanceLink.setUserName(user.getFullName());
067                    workflowInstanceLink.setGroupId(groupId);
068                    workflowInstanceLink.setCompanyId(companyId);
069                    workflowInstanceLink.setClassNameId(classNameId);
070                    workflowInstanceLink.setClassPK(classPK);
071                    workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);
072    
073                    workflowInstanceLinkPersistence.update(workflowInstanceLink);
074    
075                    return workflowInstanceLink;
076            }
077    
078            @Override
079            public WorkflowInstanceLink deleteWorkflowInstanceLink(
080                            long workflowInstanceLinkId)
081                    throws PortalException, SystemException {
082    
083                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
084                            workflowInstanceLinkId);
085    
086                    return deleteWorkflowInstanceLink(workflowInstanceLink);
087            }
088    
089            @Override
090            public WorkflowInstanceLink deleteWorkflowInstanceLink(
091                            long companyId, long groupId, String className, long classPK)
092                    throws PortalException, SystemException {
093    
094                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
095                            companyId, groupId, className, classPK);
096    
097                    return deleteWorkflowInstanceLink(workflowInstanceLink);
098            }
099    
100            @Override
101            public WorkflowInstanceLink deleteWorkflowInstanceLink(
102                            WorkflowInstanceLink workflowInstanceLink)
103                    throws PortalException, SystemException {
104    
105                    if (workflowInstanceLink == null) {
106                            return null;
107                    }
108    
109                    super.deleteWorkflowInstanceLink(workflowInstanceLink);
110    
111                    subscriptionLocalService.deleteSubscriptions(
112                            workflowInstanceLink.getCompanyId(),
113                            WorkflowInstance.class.getName(),
114                            workflowInstanceLink.getWorkflowInstanceId());
115    
116                    WorkflowInstanceManagerUtil.deleteWorkflowInstance(
117                            workflowInstanceLink.getCompanyId(),
118                            workflowInstanceLink.getWorkflowInstanceId());
119    
120                    return workflowInstanceLink;
121            }
122    
123            @Override
124            public void deleteWorkflowInstanceLinks(
125                            long companyId, long groupId, String className, long classPK)
126                    throws PortalException, SystemException {
127    
128                    List<WorkflowInstanceLink> workflowInstanceLinks =
129                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
130    
131                    for (WorkflowInstanceLink workflowInstanceLink :
132                                    workflowInstanceLinks) {
133    
134                            deleteWorkflowInstanceLink(workflowInstanceLink);
135                    }
136            }
137    
138            @Override
139            public WorkflowInstanceLink fetchWorkflowInstanceLink(
140                            long companyId, long groupId, String className, long classPK)
141                    throws SystemException {
142    
143                    List<WorkflowInstanceLink> workflowInstanceLinks =
144                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
145    
146                    if (!workflowInstanceLinks.isEmpty()) {
147                            return workflowInstanceLinks.get(0);
148                    }
149                    else {
150                            return null;
151                    }
152            }
153    
154            @Override
155            public String getState(
156                            long companyId, long groupId, String className, long classPK)
157                    throws PortalException, SystemException {
158    
159                    WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
160                            companyId, groupId, className, classPK);
161    
162                    WorkflowInstance workflowInstance =
163                            WorkflowInstanceManagerUtil.getWorkflowInstance(
164                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
165    
166                    return workflowInstance.getState();
167            }
168    
169            @Override
170            public WorkflowInstanceLink getWorkflowInstanceLink(
171                            long companyId, long groupId, String className, long classPK)
172                    throws PortalException, SystemException {
173    
174                    List<WorkflowInstanceLink> workflowInstanceLinks =
175                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
176    
177                    if (workflowInstanceLinks.isEmpty()) {
178                            throw new NoSuchWorkflowInstanceLinkException();
179                    }
180                    else {
181                            return workflowInstanceLinks.get(0);
182                    }
183            }
184    
185            @Override
186            public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
187                            long companyId, long groupId, String className, long classPK)
188                    throws SystemException {
189    
190                    long classNameId = PortalUtil.getClassNameId(className);
191    
192                    return workflowInstanceLinkPersistence.findByG_C_C_C(
193                            groupId, companyId, classNameId, classPK);
194            }
195    
196            @Override
197            public boolean hasWorkflowInstanceLink(
198                            long companyId, long groupId, String className, long classPK)
199                    throws SystemException {
200    
201                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
202                            companyId, groupId, className, classPK);
203    
204                    if (workflowInstanceLink != null) {
205                            return true;
206                    }
207    
208                    return false;
209            }
210    
211            @Override
212            public boolean isEnded(
213                            long companyId, long groupId, String className, long classPK)
214                    throws PortalException, SystemException {
215    
216                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
217                            companyId, groupId, className, classPK);
218    
219                    if (workflowInstanceLink == null) {
220                            return false;
221                    }
222    
223                    WorkflowInstance workflowInstance =
224                            WorkflowInstanceManagerUtil.getWorkflowInstance(
225                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
226    
227                    if (workflowInstance.getEndDate() != null) {
228                            return true;
229                    }
230    
231                    return false;
232            }
233    
234            @Override
235            public void startWorkflowInstance(
236                            long companyId, long groupId, long userId, String className,
237                            long classPK, Map<String, Serializable> workflowContext)
238                    throws PortalException, SystemException {
239    
240                    if (!WorkflowThreadLocal.isEnabled()) {
241                            return;
242                    }
243    
244                    if (userId == 0) {
245                            userId = userLocalService.getDefaultUserId(companyId);
246                    }
247    
248                    WorkflowHandler workflowHandler =
249                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
250    
251                    WorkflowDefinitionLink workflowDefinitionLink =
252                            workflowHandler.getWorkflowDefinitionLink(
253                                    companyId, groupId, classPK);
254    
255                    String workflowDefinitionName =
256                            workflowDefinitionLink.getWorkflowDefinitionName();
257                    int workflowDefinitionVersion =
258                            workflowDefinitionLink.getWorkflowDefinitionVersion();
259    
260                    if (workflowContext != null) {
261                            workflowContext = new HashMap<String, Serializable>(
262                                    workflowContext);
263                    }
264                    else {
265                            workflowContext = new HashMap<String, Serializable>();
266                    }
267    
268                    workflowContext.put(
269                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
270                    workflowContext.put(
271                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
272                    workflowContext.put(
273                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
274                    workflowContext.put(
275                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
276                    workflowContext.put(
277                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
278                            workflowHandler.getType(LocaleUtil.getDefault()));
279    
280                    WorkflowInstance workflowInstance =
281                            WorkflowInstanceManagerUtil.startWorkflowInstance(
282                                    companyId, groupId, userId, workflowDefinitionName,
283                                    workflowDefinitionVersion, null, workflowContext);
284    
285                    addWorkflowInstanceLink(
286                            userId, companyId, groupId, className, classPK,
287                            workflowInstance.getWorkflowInstanceId());
288            }
289    
290            @Override
291            public void updateClassPK(
292                            long companyId, long groupId, String className, long oldClassPK,
293                            long newClassPK)
294                    throws PortalException, SystemException {
295    
296                    if (!WorkflowThreadLocal.isEnabled()) {
297                            return;
298                    }
299    
300                    List<WorkflowInstanceLink> workflowInstanceLinks =
301                            getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
302    
303                    for (WorkflowInstanceLink workflowInstanceLink :
304                                    workflowInstanceLinks) {
305    
306                            WorkflowInstance workflowInstance =
307                                    WorkflowInstanceManagerUtil.getWorkflowInstance(
308                                            workflowInstanceLink.getCompanyId(),
309                                            workflowInstanceLink.getWorkflowInstanceId());
310    
311                            workflowInstanceLink.setClassPK(newClassPK);
312    
313                            workflowInstanceLinkPersistence.update(workflowInstanceLink);
314    
315                            Map<String, Serializable> workflowContext =
316                                    new HashMap<String, Serializable>(
317                                            workflowInstance.getWorkflowContext());
318    
319                            workflowContext.put(
320                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
321                                    String.valueOf(newClassPK));
322    
323                            WorkflowInstanceManagerUtil.updateWorkflowContext(
324                                    workflowInstanceLink.getCompanyId(),
325                                    workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
326                    }
327            }
328    
329    }