001    /**
002     * Copyright (c) 2000-2010 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.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                    try {
082                            WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
083                                    companyId, groupId, className, classPK);
084    
085                            deleteWorkflowInstanceLink(workflowInstanceLink);
086    
087                            WorkflowInstanceManagerUtil.deleteWorkflowInstance(
088                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
089                    }
090                    catch (NoSuchWorkflowInstanceLinkException nswile) {
091                    }
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 String getState(
112                            long companyId, long groupId, String className, long classPK)
113                    throws PortalException, SystemException {
114    
115                    WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
116                            companyId, groupId, className, classPK);
117    
118                    WorkflowInstance workflowInstance =
119                            WorkflowInstanceManagerUtil.getWorkflowInstance(
120                                    companyId, workflowInstanceLink.getWorkflowInstanceId());
121    
122                    return workflowInstance.getState();
123            }
124    
125            public WorkflowInstanceLink getWorkflowInstanceLink(
126                            long companyId, long groupId, String className, long classPK)
127                    throws PortalException, SystemException {
128    
129                    List<WorkflowInstanceLink> workflowInstanceLinks =
130                            getWorkflowInstanceLinks(companyId, groupId, className, classPK);
131    
132                    if (workflowInstanceLinks.isEmpty()) {
133                            throw new NoSuchWorkflowInstanceLinkException();
134                    }
135                    else {
136                            return workflowInstanceLinks.get(0);
137                    }
138            }
139    
140            public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
141                            long companyId, long groupId, String className, long classPK)
142                    throws SystemException {
143    
144                    long classNameId = PortalUtil.getClassNameId(className);
145    
146                    return workflowInstanceLinkPersistence.findByG_C_C_C(
147                            groupId, companyId, classNameId, classPK);
148            }
149    
150            public boolean hasWorkflowInstanceLink(
151                            long companyId, long groupId, String className, long classPK)
152                    throws PortalException, SystemException {
153    
154                    try {
155                            getWorkflowInstanceLink(companyId, groupId, className, classPK);
156    
157                            return true;
158                    }
159                    catch (NoSuchWorkflowInstanceLinkException nswile) {
160                            return false;
161                    }
162            }
163    
164            public boolean isEnded(
165                            long companyId, long groupId, String className, long classPK)
166                    throws PortalException, SystemException {
167    
168                    try {
169                            WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
170                                    companyId, groupId, className, classPK);
171    
172                            WorkflowInstance workflowInstance =
173                                    WorkflowInstanceManagerUtil.getWorkflowInstance(
174                                            companyId, workflowInstanceLink.getWorkflowInstanceId());
175    
176                            if (workflowInstance.getEndDate() != null) {
177                                    return true;
178                            }
179                    }
180                    catch (NoSuchWorkflowInstanceLinkException nswile) {
181                    }
182    
183                    return false;
184            }
185    
186            public void startWorkflowInstance(
187                            long companyId, long groupId, long userId, String className,
188                            long classPK, Map<String, Serializable> workflowContext)
189                    throws PortalException, SystemException {
190    
191                    if (!WorkflowThreadLocal.isEnabled()) {
192                            return;
193                    }
194    
195                    WorkflowDefinitionLink workflowDefinitionLink =
196                            workflowDefinitionLinkLocalService.getWorkflowDefinitionLink(
197                                    companyId, groupId, className);
198    
199                    String workflowDefinitionName =
200                            workflowDefinitionLink.getWorkflowDefinitionName();
201                    int workflowDefinitionVersion =
202                            workflowDefinitionLink.getWorkflowDefinitionVersion();
203    
204                    if (workflowContext != null) {
205                            workflowContext = new HashMap<String, Serializable>(
206                                    workflowContext);
207                    }
208                    else {
209                            workflowContext = new HashMap<String, Serializable>();
210                    }
211    
212                    workflowContext.put(
213                            WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
214                    workflowContext.put(
215                            WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
216                    workflowContext.put(
217                            WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
218                    workflowContext.put(
219                            WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
220    
221                    WorkflowHandler workflowHandler =
222                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
223    
224                    workflowContext.put(
225                            WorkflowConstants.CONTEXT_ENTRY_TYPE,
226                            workflowHandler.getType(LocaleUtil.getDefault()));
227    
228                    WorkflowInstance workflowInstance =
229                            WorkflowInstanceManagerUtil.startWorkflowInstance(
230                                    companyId, groupId, userId, workflowDefinitionName,
231                            workflowDefinitionVersion, null, workflowContext);
232    
233                    addWorkflowInstanceLink(
234                            userId, companyId, groupId, className, classPK,
235                            workflowInstance.getWorkflowInstanceId());
236            }
237    
238            public void updateClassPK(
239                            long companyId, long groupId, String className, long oldClassPK,
240                            long newClassPK)
241                    throws PortalException, SystemException {
242    
243                    if (!WorkflowThreadLocal.isEnabled()) {
244                            return;
245                    }
246    
247                    List<WorkflowInstanceLink> workflowInstanceLinks =
248                            getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
249    
250                    for (WorkflowInstanceLink workflowInstanceLink :
251                                    workflowInstanceLinks) {
252    
253                            WorkflowInstance workflowInstance =
254                                    WorkflowInstanceManagerUtil.getWorkflowInstance(
255                                            workflowInstanceLink.getCompanyId(),
256                                            workflowInstanceLink.getWorkflowInstanceId());
257    
258                            workflowInstanceLink.setClassPK(newClassPK);
259    
260                            workflowInstanceLinkPersistence.update(
261                                    workflowInstanceLink, false);
262    
263                            Map<String, Serializable> workflowContext =
264                                    new HashMap<String, Serializable>(
265                                            workflowInstance.getWorkflowContext());
266    
267                            workflowContext.put(
268                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
269                                    String.valueOf(newClassPK));
270    
271                            WorkflowInstanceManagerUtil.updateWorkflowContext(
272                                    workflowInstanceLink.getCompanyId(),
273                                    workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
274                    }
275            }
276    
277    }