myTaskServiceImpl.java
20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package com.trash.activiti.service.impl;
import org.activiti.api.model.shared.model.VariableInstance;
import org.activiti.api.runtime.shared.NotFoundException;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.impl.TaskImpl;
import org.activiti.api.task.model.payloads.CandidateGroupsPayload;
import org.activiti.api.task.model.payloads.CandidateUsersPayload;
import org.activiti.api.task.model.payloads.ClaimTaskPayload;
import org.activiti.api.task.model.payloads.CompleteTaskPayload;
import org.activiti.api.task.model.payloads.CreateTaskPayload;
import org.activiti.api.task.model.payloads.CreateTaskVariablePayload;
import org.activiti.api.task.model.payloads.DeleteTaskPayload;
import org.activiti.api.task.model.payloads.GetTaskVariablesPayload;
import org.activiti.api.task.model.payloads.GetTasksPayload;
import org.activiti.api.task.model.payloads.ReleaseTaskPayload;
import org.activiti.api.task.model.payloads.SaveTaskPayload;
import org.activiti.api.task.model.payloads.UpdateTaskPayload;
import org.activiti.api.task.model.payloads.UpdateTaskVariablePayload;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.api.task.runtime.conf.TaskRuntimeConfiguration;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.TaskQuery;
import org.activiti.runtime.api.impl.TaskRuntimeHelper;
import org.activiti.runtime.api.model.impl.APITaskConverter;
import org.activiti.runtime.api.model.impl.APIVariableInstanceConverter;
import org.activiti.runtime.api.query.impl.PageImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import com.trash.activiti.service.myTaskService;
import com.trash.common.utils.SecurityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@PreAuthorize("hasRole('ACTIVITI_USER')")
@Service
public class myTaskServiceImpl implements myTaskService {
private final TaskService taskService;
private final APITaskConverter taskConverter;
private final APIVariableInstanceConverter variableInstanceConverter;
private final TaskRuntimeConfiguration configuration;
private final UserGroupManager userGroupManager;
private final SecurityManager securityManager;
private final TaskRuntimeHelper taskRuntimeHelper;
public myTaskServiceImpl(TaskService taskService,
UserGroupManager userGroupManager,
SecurityManager securityManager,
APITaskConverter taskConverter,
APIVariableInstanceConverter variableInstanceConverter,
TaskRuntimeConfiguration configuration,
TaskRuntimeHelper taskRuntimeHelper) {
this.taskService = taskService;
this.userGroupManager = userGroupManager;
this.securityManager = securityManager;
this.taskConverter = taskConverter;
this.variableInstanceConverter = variableInstanceConverter;
this.configuration = configuration;
this.taskRuntimeHelper = taskRuntimeHelper;
}
@Override
public TaskRuntimeConfiguration configuration() {
return configuration;
}
@Override
public Task task(String taskId) {
org.activiti.engine.task.Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
throw new NotFoundException("ๆชๆพๅฐๅฎกๆนไปปๅก id: " + taskId);
}
return taskConverter.from(task);
}
@Override
public Page<Task> tasks(Pageable pageable) {
return tasks(pageable,null);
}
@Override
public Page<Task> tasks(Pageable pageable,GetTasksPayload getTasksPayload) {
TaskQuery taskQuery = taskService.createTaskQuery();
if (getTasksPayload == null) {
getTasksPayload = TaskPayloadBuilder.tasks().build();
}
if(SecurityUtils.getLoginUser().getUser().getPostIds() == null && SecurityUtils.getLoginUser().getUser().getRoleIds() == null){
taskQuery.taskCandidateOrAssigned(SecurityUtils.getUsername());
}else{
if( SecurityUtils.getLoginUser().getUser().getPostIds() != null && SecurityUtils.getLoginUser().getUser().getRoleIds() != null){
taskQuery.or().taskCandidateGroupIn(SecurityUtils.getLoginUser().getUser().getPostIds());
for(String role : SecurityUtils.getLoginUser().getUser().getRoleIds()){
taskQuery.taskCandidateUser(role);
}
taskQuery.endOr();
}else{
if(SecurityUtils.getLoginUser().getUser().getPostIds() != null){
taskQuery.taskCandidateGroupIn(SecurityUtils.getLoginUser().getUser().getPostIds());
}
if(SecurityUtils.getLoginUser().getUser().getRoleIds() != null){
for(String role : SecurityUtils.getLoginUser().getUser().getRoleIds()){
taskQuery.taskCandidateUser(role);
}
}
}
}
List<org.activiti.engine.task.Task> tt = taskQuery.list();
if (getTasksPayload.getProcessInstanceId() != null) {
taskQuery = taskQuery.processInstanceId(getTasksPayload.getProcessInstanceId());
}
if (getTasksPayload.getParentTaskId() != null) {
taskQuery = taskQuery.taskParentTaskId(getTasksPayload.getParentTaskId());
}
List<Task> tasks = taskConverter.from(taskQuery.listPage(pageable.getStartIndex(),
pageable.getMaxItems()));
return new PageImpl<>(tasks,
Math.toIntExact(taskQuery.count()));
}
@Override
public Task complete(CompleteTaskPayload completeTaskPayload) {
//@TODO: not the most efficient way to return the just completed task, improve
// we might need to create an empty shell with the task ID and Status only
Task task;
String authenticatedUserId = securityManager.getAuthenticatedUserId();
try {
task = task(completeTaskPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot complete task" + completeTaskPayload.getTaskId() + " due he/she cannot access to the task");
}
// validate the the task does have an assignee
if (task.getAssignee() == null || task.getAssignee().isEmpty()) {
throw new IllegalStateException("The task needs to be claimed before trying to complete it");
}
if (!task.getAssignee().equals(authenticatedUserId)) {
throw new IllegalStateException("You cannot complete the task if you are not assigned to it");
}
taskService.complete(completeTaskPayload.getTaskId(),
completeTaskPayload.getVariables(), true);
((TaskImpl) task).setStatus(Task.TaskStatus.COMPLETED);
return task;
}
@Override
public Task claim(ClaimTaskPayload claimTaskPayload) {
// Validate that the task is visible by the currently authorized user
Task task;
try {
task = task(claimTaskPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot claim task" + claimTaskPayload.getTaskId() + " due it is not a candidate for it");
}
// validate the the task doesn't have an assignee
if (task.getAssignee() != null && !task.getAssignee().isEmpty()) {
throw new IllegalStateException("The task was already claimed, the assignee of this task needs to release it first for you to claim it");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
claimTaskPayload.setAssignee(authenticatedUserId);
taskService.claim(claimTaskPayload.getTaskId(),
claimTaskPayload.getAssignee());
return task(claimTaskPayload.getTaskId());
}
@Override
public Task release(ReleaseTaskPayload releaseTaskPayload) {
// Validate that the task is visible by the currently authorized user
Task task;
try {
task = task(releaseTaskPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot claim task" + releaseTaskPayload.getTaskId() + " due it is not a candidate for it");
}
// validate the the task doesn't have an assignee
if (task.getAssignee() == null || task.getAssignee().isEmpty()) {
throw new IllegalStateException("You cannot release a task that is not claimed");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to release task where you are the assignee
if (!task.getAssignee().equals(authenticatedUserId)) {
throw new IllegalStateException("You cannot release a task where you are not the assignee");
}
taskService.unclaim(releaseTaskPayload.getTaskId());
return task(releaseTaskPayload.getTaskId());
}
@Override
public Task update(UpdateTaskPayload updateTaskPayload) {
return taskRuntimeHelper.applyUpdateTaskPayload(false, updateTaskPayload);
}
@Override
public Task delete(DeleteTaskPayload deleteTaskPayload) {
//@TODO: not the most efficient way to return the just deleted task, improve
// we might need to create an empty shell with the task ID and Status only
Task task;
try {
task = task(deleteTaskPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot delete the task" + deleteTaskPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to delete task where you are the assignee or the owner
if ((task.getAssignee() == null || task.getAssignee().isEmpty() || !task.getAssignee().equals(authenticatedUserId)) &&
(task.getOwner() == null || task.getOwner().isEmpty() || !task.getOwner().equals(authenticatedUserId))) {
throw new IllegalStateException("You cannot delete a task where you are not the assignee/owner");
}
TaskImpl deletedTaskData = new TaskImpl(task.getId(),
task.getName(),
Task.TaskStatus.CANCELLED);
if (!deleteTaskPayload.hasReason()) {
deleteTaskPayload.setReason("Task deleted by " + authenticatedUserId);
}
taskService.deleteTask(deleteTaskPayload.getTaskId(),
deleteTaskPayload.getReason(),
true);
return deletedTaskData;
}
@Override
public Task create(CreateTaskPayload createTaskPayload) {
if (createTaskPayload.getName() == null || createTaskPayload.getName().isEmpty()) {
throw new IllegalStateException("You cannot create a task without name");
}
org.activiti.engine.task.Task task = taskService.newTask();
task.setName(createTaskPayload.getName());
task.setDescription(createTaskPayload.getDescription());
task.setDueDate(createTaskPayload.getDueDate());
task.setPriority(createTaskPayload.getPriority());
if (createTaskPayload.getAssignee() != null && !createTaskPayload.getAssignee().isEmpty()) {
task.setAssignee(createTaskPayload.getAssignee());
}
task.setParentTaskId(createTaskPayload.getParentTaskId());
task.setFormKey(createTaskPayload.getFormKey());
task.setOwner(securityManager.getAuthenticatedUserId());
taskService.saveTask(task);
taskService.addCandidateUser(task.getId(),
securityManager.getAuthenticatedUserId());
if (createTaskPayload.getCandidateGroups() != null && !createTaskPayload.getCandidateGroups().isEmpty()) {
for ( String g : createTaskPayload.getCandidateGroups() ) {
taskService.addCandidateGroup(task.getId(),
g);
}
}
if (createTaskPayload.getCandidateUsers() != null && !createTaskPayload.getCandidateUsers().isEmpty()) {
for ( String u : createTaskPayload.getCandidateUsers() ) {
taskService.addCandidateUser(task.getId(),
u);
}
}
return taskConverter.from(task);
}
@Override
public void addCandidateUsers(CandidateUsersPayload candidateUsersPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateUsersPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to add CandidateUsers to the task where you are the assignee
if (!Objects.equals(internalTask.getAssignee(), authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
if (candidateUsersPayload.getCandidateUsers() != null && !candidateUsersPayload.getCandidateUsers().isEmpty()) {
for ( String u : candidateUsersPayload.getCandidateUsers() ) {
taskService.addCandidateUser(internalTask.getId(),
u);
}
}
}
@Override
public void deleteCandidateUsers(CandidateUsersPayload candidateUsersPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateUsersPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateUsersPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to add CandidateUsers to the task where you are the assignee
if (!Objects.equals(internalTask.getAssignee(), authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
if (candidateUsersPayload.getCandidateUsers() != null && !candidateUsersPayload.getCandidateUsers().isEmpty()) {
for ( String u : candidateUsersPayload.getCandidateUsers() ) {
taskService.deleteCandidateUser(internalTask.getId(),
u);
}
}
}
@Override
public void addCandidateGroups(CandidateGroupsPayload candidateGroupsPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateGroupsPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to add CandidateGroups to the task where you are the assignee
if (!Objects.equals(internalTask.getAssignee(), authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
if (candidateGroupsPayload.getCandidateGroups() != null && !candidateGroupsPayload.getCandidateGroups().isEmpty()) {
for ( String g : candidateGroupsPayload.getCandidateGroups() ) {
taskService.addCandidateGroup(internalTask.getId(),
g);
}
}
}
@Override
public void deleteCandidateGroups(CandidateGroupsPayload candidateGroupsPayload) {
org.activiti.engine.task.Task internalTask;
try {
internalTask = taskRuntimeHelper.getInternalTaskWithChecks(candidateGroupsPayload.getTaskId());
} catch (IllegalStateException ex) {
throw new IllegalStateException("The authenticated user cannot update the task" + candidateGroupsPayload.getTaskId() + " due it is not the current assignee");
}
String authenticatedUserId = securityManager.getAuthenticatedUserId();
// validate that you are trying to add CandidateGroups to the task where you are the assignee
if (!Objects.equals(internalTask.getAssignee(), authenticatedUserId)) {
throw new IllegalStateException("You cannot update a task where you are not the assignee");
}
if (candidateGroupsPayload.getCandidateGroups() != null && !candidateGroupsPayload.getCandidateGroups().isEmpty()) {
for ( String g : candidateGroupsPayload.getCandidateGroups() ) {
taskService.deleteCandidateGroup(internalTask.getId(),
g);
}
}
}
@Override
public List<String> userCandidates(String taskId) {
List<IdentityLink> identityLinks = getIdentityLinks(taskId);
List<String> userCandidates = new ArrayList<>();
if (identityLinks != null) {
for ( IdentityLink i : identityLinks ) {
if (i.getUserId() != null) {
if (i.getType().equals(IdentityLinkType.CANDIDATE)) {
userCandidates.add(i.getUserId());
}
}
}
}
return userCandidates;
}
@Override
public List<String> groupCandidates(String taskId) {
List<IdentityLink> identityLinks = getIdentityLinks(taskId);
List<String> groupCandidates = new ArrayList<>();
if (identityLinks != null) {
for ( IdentityLink i : identityLinks ) {
if (i.getGroupId() != null) {
if (i.getType().equals(IdentityLinkType.CANDIDATE)) {
groupCandidates.add(i.getGroupId());
}
}
}
}
return groupCandidates;
}
@Override
public List<VariableInstance> variables(GetTaskVariablesPayload getTaskVariablesPayload) {
taskRuntimeHelper.assertHasAccessToTask(getTaskVariablesPayload.getTaskId());
return variableInstanceConverter.from(taskRuntimeHelper.getInternalTaskVariables(getTaskVariablesPayload.getTaskId()).values());
}
@Override
public void createVariable(CreateTaskVariablePayload createTaskVariablePayload) {
taskRuntimeHelper.createVariable(false, createTaskVariablePayload);
}
@Override
public void updateVariable(UpdateTaskVariablePayload updateTaskVariablePayload) {
taskRuntimeHelper.updateVariable(false, updateTaskVariablePayload);
}
private List<IdentityLink> getIdentityLinks(String taskId) {
String authenticatedUserId = securityManager.getAuthenticatedUserId();
if (authenticatedUserId != null && !authenticatedUserId.isEmpty()) {
List<String> userRoles = userGroupManager.getUserRoles(authenticatedUserId);
List<String> userGroups = userGroupManager.getUserGroups(authenticatedUserId);
org.activiti.engine.task.Task internalTask = taskService.createTaskQuery().taskCandidateOrAssigned(authenticatedUserId,
userGroups).taskId(taskId).singleResult();
if (internalTask == null) {
throw new NotFoundException("Unable to find task for the given id: " + taskId + " for user: " + authenticatedUserId + " (with groups: " + userGroups + " & with roles: " + userRoles + ")");
}
return taskService.getIdentityLinksForTask(taskId);
}
throw new IllegalStateException("There is no authenticated user, we need a user authenticated to find tasks");
}
@Override
public void save(SaveTaskPayload saveTaskPayload) {
taskRuntimeHelper.assertHasAccessToTask(saveTaskPayload.getTaskId());
taskService.setVariablesLocal(saveTaskPayload.getTaskId(),
saveTaskPayload.getVariables());
}
}