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