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