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