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, false);
074    
075                    return workflowInstanceLink;
076            }
077    
078            @Override
079            public void deleteWorkflowInstanceLink(
080                            long companyId, long groupId, String className, long classPK)
081                    throws PortalException, SystemException {
082    
083                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
084                            companyId, groupId, className, classPK);
085    
086                    if (workflowInstanceLink == null) {
087                            return;
088                    }
089    
090                    deleteWorkflowInstanceLink(workflowInstanceLink);
091            }
092    
093            @Override
094            public WorkflowInstanceLink deleteWorkflowInstanceLink(
095                            WorkflowInstanceLink workflowInstanceLink)
096                    throws PortalException, SystemException {
097    
098                    super.deleteWorkflowInstanceLink(workflowInstanceLink);
099    
100                    subscriptionLocalService.deleteSubscriptions(
101                            workflowInstanceLink.getCompanyId(),
102                            WorkflowInstance.class.getName(),
103                            workflowInstanceLink.getWorkflowInstanceId());
104    
105                    WorkflowInstanceManagerUtil.deleteWorkflowInstance(
106                            workflowInstanceLink.getCompanyId(),
107                            workflowInstanceLink.getWorkflowInstanceId());
108    
109                    return workflowInstanceLink;
110            }
111    
112            @Override
113            public void deleteWorkflowInstanceLinks(
114                            long companyId, long groupId, String className, long classPK)
115                    throws PortalException, SystemException {
116    
117                    List<WorkflowInstanceLink> workflowInstanceLinks =
118                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
119    
120                    for (WorkflowInstanceLink workflowInstanceLink :
121                                    workflowInstanceLinks) {
122    
123                            deleteWorkflowInstanceLink(workflowInstanceLink);
124                    }
125            }
126    
127            @Override
128            public WorkflowInstanceLink fetchWorkflowInstanceLink(
129                            long companyId, long groupId, String className, long classPK)
130                    throws SystemException {
131    
132                    List<WorkflowInstanceLink> workflowInstanceLinks =
133                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
134    
135                    if (!workflowInstanceLinks.isEmpty()) {
136                            return workflowInstanceLinks.get(0);
137                    }
138                    else {
139                            return null;
140                    }
141            }
142    
143            @Override
144            public String getState(
145                            long companyId, long groupId, String className, long classPK)
146                    throws PortalException, SystemException {
147    
148                    WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
149                            companyId, groupId, className, classPK);
150    
151                    WorkflowInstance workflowInstance =
152                            WorkflowInstanceManagerUtil.getWorkflowInstance(
153                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
154    
155                    return workflowInstance.getState();
156            }
157    
158            @Override
159            public WorkflowInstanceLink getWorkflowInstanceLink(
160                            long companyId, long groupId, String className, long classPK)
161                    throws PortalException, SystemException {
162    
163                    List<WorkflowInstanceLink> workflowInstanceLinks =
164                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
165    
166                    if (workflowInstanceLinks.isEmpty()) {
167                            throw new NoSuchWorkflowInstanceLinkException();
168                    }
169                    else {
170                            return workflowInstanceLinks.get(0);
171                    }
172            }
173    
174            @Override
175            public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
176                            long companyId, long groupId, String className, long classPK)
177                    throws SystemException {
178    
179                    long classNameId = PortalUtil.getClassNameId(className);
180    
181                    return workflowInstanceLinkPersistence.findByG_C_C_C(
182                            groupId, companyId, classNameId, classPK);
183            }
184    
185            @Override
186            public boolean hasWorkflowInstanceLink(
187                            long companyId, long groupId, String className, long classPK)
188                    throws SystemException {
189    
190                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
191                            companyId, groupId, className, classPK);
192    
193                    if (workflowInstanceLink != null) {
194                            return true;
195                    }
196    
197                    return false;
198            }
199    
200            @Override
201            public boolean isEnded(
202                            long companyId, long groupId, String className, long classPK)
203                    throws PortalException, SystemException {
204    
205                    WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
206                            companyId, groupId, className, classPK);
207    
208                    if (workflowInstanceLink == null) {
209                            return false;
210                    }
211    
212                    WorkflowInstance workflowInstance =
213                            WorkflowInstanceManagerUtil.getWorkflowInstance(
214                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
215    
216                    if (workflowInstance.getEndDate() != null) {
217                            return true;
218                    }
219    
220                    return false;
221            }
222    
223            @Override
224            public void startWorkflowInstance(
225                            long companyId, long groupId, long userId, String className,
226                            long classPK, Map<String, Serializable> workflowContext)
227                    throws PortalException, SystemException {
228    
229                    if (!WorkflowThreadLocal.isEnabled()) {
230                            return;
231                    }
232    
233                    if (userId == 0) {
234                            userId = userLocalService.getDefaultUserId(companyId);
235                    }
236    
237                    WorkflowHandler workflowHandler =
238                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
239    
240                    WorkflowDefinitionLink workflowDefinitionLink =
241                            workflowHandler.getWorkflowDefinitionLink(
242                                    companyId, groupId, classPK);
243    
244                    String workflowDefinitionName =
245                            workflowDefinitionLink.getWorkflowDefinitionName();
246                    int workflowDefinitionVersion =
247                            workflowDefinitionLink.getWorkflowDefinitionVersion();
248    
249                    if (workflowContext != null) {
250                            workflowContext = new HashMap<String, Serializable>(
251                                    workflowContext);
252                    }
253                    else {
254                            workflowContext = new HashMap<String, Serializable>();
255                    }
256    
257                    workflowContext.put(
258                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
259                    workflowContext.put(
260                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
261                    workflowContext.put(
262                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
263                    workflowContext.put(
264                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
265                    workflowContext.put(
266                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
267                            workflowHandler.getType(LocaleUtil.getDefault()));
268    
269                    WorkflowInstance workflowInstance =
270                            WorkflowInstanceManagerUtil.startWorkflowInstance(
271                                    companyId, groupId, userId, workflowDefinitionName,
272                                    workflowDefinitionVersion, null, workflowContext);
273    
274                    addWorkflowInstanceLink(
275                            userId, companyId, groupId, className, classPK,
276                            workflowInstance.getWorkflowInstanceId());
277            }
278    
279            @Override
280            public void updateClassPK(
281                            long companyId, long groupId, String className, long oldClassPK,
282                            long newClassPK)
283                    throws PortalException, SystemException {
284    
285                    if (!WorkflowThreadLocal.isEnabled()) {
286                            return;
287                    }
288    
289                    List<WorkflowInstanceLink> workflowInstanceLinks =
290                            getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
291    
292                    for (WorkflowInstanceLink workflowInstanceLink :
293                                    workflowInstanceLinks) {
294    
295                            WorkflowInstance workflowInstance =
296                                    WorkflowInstanceManagerUtil.getWorkflowInstance(
297                                            workflowInstanceLink.getCompanyId(),
298                                            workflowInstanceLink.getWorkflowInstanceId());
299    
300                            workflowInstanceLink.setClassPK(newClassPK);
301    
302                            workflowInstanceLinkPersistence.update(workflowInstanceLink, false);
303    
304                            Map<String, Serializable> workflowContext =
305                                    new HashMap<String, Serializable>(
306                                            workflowInstance.getWorkflowContext());
307    
308                            workflowContext.put(
309                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
310                                    String.valueOf(newClassPK));
311    
312                            WorkflowInstanceManagerUtil.updateWorkflowContext(
313                                    workflowInstanceLink.getCompanyId(),
314                                    workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
315                    }
316            }
317    
318    }