/
ActivitiUserWorkflowAdapter.java
1071 lines (870 loc) · 42.8 KB
/
ActivitiUserWorkflowAdapter.java
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.workflow.activiti;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Gateway;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.FormType;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.syncope.common.lib.SyncopeClientException;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.UserPatch;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.to.WorkflowDefinitionTO;
import org.apache.syncope.common.lib.to.WorkflowFormPropertyTO;
import org.apache.syncope.common.lib.to.WorkflowFormTO;
import org.apache.syncope.common.lib.to.WorkflowTaskTO;
import org.apache.syncope.core.provisioning.api.PropagationByResource;
import org.apache.syncope.common.lib.types.ResourceOperation;
import org.apache.syncope.common.lib.types.WorkflowFormPropertyType;
import org.apache.syncope.core.spring.security.AuthContextUtils;
import org.apache.syncope.core.spring.BeanUtils;
import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
import org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException;
import org.apache.syncope.core.persistence.api.dao.NotFoundException;
import org.apache.syncope.core.persistence.api.dao.search.OrderByClause;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.provisioning.api.WorkflowResult;
import org.apache.syncope.core.workflow.activiti.spring.DomainProcessEngine;
import org.apache.syncope.core.workflow.api.WorkflowDefinitionFormat;
import org.apache.syncope.core.workflow.api.WorkflowException;
import org.apache.syncope.core.workflow.java.AbstractUserWorkflowAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
* <a href="http://www.activiti.org/">Activiti</a> based implementation.
*/
public class ActivitiUserWorkflowAdapter extends AbstractUserWorkflowAdapter {
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
protected static final String[] PROPERTY_IGNORE_PROPS = { "type" };
public static final String WF_PROCESS_ID = "userWorkflow";
public static final String USER = "user";
public static final String WF_EXECUTOR = "wfExecutor";
public static final String FORM_SUBMITTER = "formSubmitter";
public static final String USER_TO = "userTO";
public static final String ENABLED = "enabled";
public static final String USER_PATCH = "userPatch";
public static final String EMAIL_KIND = "emailKind";
public static final String TASK = "task";
public static final String TOKEN = "token";
public static final String PASSWORD = "password";
public static final String PROP_BY_RESOURCE = "propByResource";
public static final String PROPAGATE_ENABLE = "propagateEnable";
public static final String ENCRYPTED_PWD = "encryptedPwd";
public static final String TASK_IS_FORM = "taskIsForm";
public static final String MODEL_DATA_JSON_MODEL = "model";
public static final String STORE_PASSWORD = "storePassword";
public static final String EVENT = "event";
@Resource(name = "adminUser")
protected String adminUser;
@Autowired
protected DomainProcessEngine engine;
@Override
public boolean supportsDefinitionEdit() {
return true;
}
@Override
public String getPrefix() {
return "ACT_";
}
protected void throwException(final ActivitiException e, final String defaultMessage) {
if (e.getCause() != null) {
if (e.getCause().getCause() instanceof SyncopeClientException) {
throw (SyncopeClientException) e.getCause().getCause();
} else if (e.getCause().getCause() instanceof ParsingValidationException) {
throw (ParsingValidationException) e.getCause().getCause();
} else if (e.getCause().getCause() instanceof InvalidEntityException) {
throw (InvalidEntityException) e.getCause().getCause();
} else if (e.getCause().getCause().getClass().getName().contains("persistence")) {
throw (RuntimeException) e.getCause().getCause();
}
}
throw new WorkflowException(defaultMessage, e);
}
protected void updateStatus(final User user) {
List<Task> tasks = engine.getTaskService().createTaskQuery().processInstanceId(user.getWorkflowId()).list();
if (tasks.isEmpty() || tasks.size() > 1) {
LOG.warn("While setting user status: unexpected task number ({})", tasks.size());
} else {
user.setStatus(tasks.get(0).getTaskDefinitionKey());
}
}
protected String getFormTask(final User user) {
String result = null;
List<Task> tasks = engine.getTaskService().createTaskQuery().processInstanceId(user.getWorkflowId()).list();
if (tasks.isEmpty() || tasks.size() > 1) {
LOG.debug("While checking if form task: unexpected task number ({})", tasks.size());
} else {
try {
TaskFormData formData = engine.getFormService().getTaskFormData(tasks.get(0).getId());
if (formData != null && !formData.getFormProperties().isEmpty()) {
result = tasks.get(0).getId();
}
} catch (ActivitiException e) {
LOG.warn("Could not get task form data", e);
}
}
return result;
}
protected Set<String> getPerformedTasks(final User user) {
final Set<String> result = new HashSet<>();
for (HistoricActivityInstance task : engine.getHistoryService().createHistoricActivityInstanceQuery().
executionId(user.getWorkflowId()).list()) {
result.add(task.getActivityId());
}
return result;
}
/**
* Saves resources to be propagated and password for later - after form submission - propagation.
*
* @param user user
* @param password password
* @param propByRes current propagation actions against resources
*/
protected void saveForFormSubmit(final User user, final String password, final PropagationByResource propByRes) {
String formTaskId = getFormTask(user);
if (formTaskId != null) {
UserTO userTO = engine.getRuntimeService().getVariable(user.getWorkflowId(), USER_TO, UserTO.class);
if (userTO != null) {
userTO.setKey(user.getKey());
userTO.setCreationDate(user.getCreationDate());
userTO.setLastChangeDate(user.getLastChangeDate());
if (password == null) {
String encryptedPwd = engine.getRuntimeService().
getVariable(user.getWorkflowId(), ENCRYPTED_PWD, String.class);
if (encryptedPwd != null) {
userTO.setPassword(decrypt(encryptedPwd));
}
} else {
userTO.setPassword(password);
}
engine.getRuntimeService().setVariable(user.getWorkflowId(), USER_TO, userTO);
}
// SYNCOPE-238: This is needed to simplify the task query in this.getForms()
engine.getTaskService().setVariableLocal(formTaskId, TASK_IS_FORM, Boolean.TRUE);
engine.getRuntimeService().setVariable(user.getWorkflowId(), PROP_BY_RESOURCE, propByRes);
if (propByRes != null) {
propByRes.clear();
}
if (password != null) {
engine.getRuntimeService().setVariable(user.getWorkflowId(), ENCRYPTED_PWD, encrypt(password));
}
}
}
public <T> T getVariable(final String executionId, final String variableName, final Class<T> variableClass) {
return engine.getRuntimeService().getVariable(executionId, variableName, variableClass);
}
public void setVariable(final String executionId, final String variableName, final Object value) {
engine.getRuntimeService().setVariable(executionId, variableName, value);
}
@Override
protected WorkflowResult<Pair<String, Boolean>> doCreate(
final UserTO userTO,
final boolean disablePwdPolicyCheck,
final Boolean enabled,
final boolean storePassword) {
Map<String, Object> variables = new HashMap<>();
variables.put(WF_EXECUTOR, AuthContextUtils.getUsername());
variables.put(USER_TO, userTO);
variables.put(ENABLED, enabled);
variables.put(STORE_PASSWORD, storePassword);
ProcessInstance processInstance = null;
try {
processInstance = engine.getRuntimeService().startProcessInstanceByKey(WF_PROCESS_ID, variables);
} catch (ActivitiException e) {
throwException(e, "While starting " + WF_PROCESS_ID + " instance");
}
User user = engine.getRuntimeService().getVariable(processInstance.getProcessInstanceId(), USER, User.class);
Boolean updatedEnabled =
engine.getRuntimeService().getVariable(processInstance.getProcessInstanceId(), ENABLED, Boolean.class);
if (updatedEnabled != null) {
user.setSuspended(!updatedEnabled);
}
// this will make UserValidator not to consider password policies at all
if (disablePwdPolicyCheck) {
user.removeClearPassword();
}
updateStatus(user);
user = userDAO.save(user);
Boolean propagateEnable = engine.getRuntimeService().getVariable(
processInstance.getProcessInstanceId(), PROPAGATE_ENABLE, Boolean.class);
if (propagateEnable == null) {
propagateEnable = enabled;
}
PropagationByResource propByRes = new PropagationByResource();
propByRes.set(ResourceOperation.CREATE, userDAO.findAllResourceKeys(user.getKey()));
saveForFormSubmit(user, userTO.getPassword(), propByRes);
Set<String> tasks = getPerformedTasks(user);
return new WorkflowResult<>(Pair.of(user.getKey(), propagateEnable), propByRes, tasks);
}
protected Set<String> doExecuteTask(final User user, final String task, final Map<String, Object> moreVariables) {
Set<String> preTasks = getPerformedTasks(user);
Map<String, Object> variables = new HashMap<>();
variables.put(WF_EXECUTOR, AuthContextUtils.getUsername());
variables.put(TASK, task);
// using BeanUtils to access all user's properties and trigger lazy loading - we are about to
// serialize a User instance for availability within workflow tasks, and this breaks transactions
BeanUtils.copyProperties(user, entityFactory.newEntity(User.class));
variables.put(USER, user);
if (moreVariables != null && !moreVariables.isEmpty()) {
variables.putAll(moreVariables);
}
if (StringUtils.isBlank(user.getWorkflowId())) {
throw new WorkflowException(new NotFoundException("Empty workflow id for " + user));
}
List<Task> tasks = engine.getTaskService().createTaskQuery().processInstanceId(user.getWorkflowId()).list();
if (tasks.size() == 1) {
try {
engine.getTaskService().complete(tasks.get(0).getId(), variables);
} catch (ActivitiException e) {
throwException(e, "While completing task '" + tasks.get(0).getName() + "' for " + user);
}
} else {
LOG.warn("Expected a single task, found {}", tasks.size());
}
Set<String> postTasks = getPerformedTasks(user);
postTasks.removeAll(preTasks);
postTasks.add(task);
return postTasks;
}
@Override
protected WorkflowResult<String> doActivate(final User user, final String token) {
Set<String> tasks = doExecuteTask(user, "activate", Collections.singletonMap(TOKEN, (Object) token));
updateStatus(user);
User updated = userDAO.save(user);
return new WorkflowResult<>(updated.getKey(), null, tasks);
}
@Override
protected WorkflowResult<Pair<UserPatch, Boolean>> doUpdate(final User user, final UserPatch userPatch) {
Set<String> tasks = doExecuteTask(user, "update", Collections.singletonMap(USER_PATCH, (Object) userPatch));
updateStatus(user);
User updated = userDAO.save(user);
PropagationByResource propByRes = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROP_BY_RESOURCE, PropagationByResource.class);
saveForFormSubmit(
updated, userPatch.getPassword() == null ? null : userPatch.getPassword().getValue(), propByRes);
Boolean propagateEnable = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROPAGATE_ENABLE, Boolean.class);
return new WorkflowResult<>(Pair.of(userPatch, propagateEnable), propByRes, tasks);
}
@Override
public WorkflowResult<String> requestCertify(final User user) {
String authUser = AuthContextUtils.getUsername();
engine.getRuntimeService().setVariable(user.getWorkflowId(), FORM_SUBMITTER, authUser);
Set<String> performedTasks = doExecuteTask(user, "request-certify", null);
PropagationByResource propByRes = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROP_BY_RESOURCE, PropagationByResource.class);
saveForFormSubmit(user, null, propByRes);
return new WorkflowResult<>(user.getKey(), null, performedTasks);
}
@Override
protected WorkflowResult<String> doSuspend(final User user) {
Set<String> performedTasks = doExecuteTask(user, "suspend", null);
updateStatus(user);
User updated = userDAO.save(user);
return new WorkflowResult<>(updated.getKey(), null, performedTasks);
}
@Override
protected WorkflowResult<String> doReactivate(final User user) {
Set<String> performedTasks = doExecuteTask(user, "reactivate", null);
updateStatus(user);
User updated = userDAO.save(user);
return new WorkflowResult<>(updated.getKey(), null, performedTasks);
}
@Override
protected void doRequestPasswordReset(final User user) {
Map<String, Object> variables = new HashMap<>(2);
variables.put(USER_TO, dataBinder.getUserTO(user, true));
variables.put(EVENT, "requestPasswordReset");
doExecuteTask(user, "requestPasswordReset", variables);
userDAO.save(user);
}
@Override
protected WorkflowResult<Pair<UserPatch, Boolean>> doConfirmPasswordReset(
final User user, final String token, final String password) {
Map<String, Object> variables = new HashMap<>(4);
variables.put(TOKEN, token);
variables.put(PASSWORD, password);
variables.put(USER_TO, dataBinder.getUserTO(user, true));
variables.put(EVENT, "confirmPasswordReset");
Set<String> tasks = doExecuteTask(user, "confirmPasswordReset", variables);
userDAO.save(user);
PropagationByResource propByRes = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROP_BY_RESOURCE, PropagationByResource.class);
UserPatch updatedPatch = engine.getRuntimeService().getVariable(
user.getWorkflowId(), USER_PATCH, UserPatch.class);
Boolean propagateEnable = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROPAGATE_ENABLE, Boolean.class);
return new WorkflowResult<>(Pair.of(updatedPatch, propagateEnable), propByRes, tasks);
}
@Override
protected void doDelete(final User user) {
doExecuteTask(user, "delete", null);
PropagationByResource propByRes = new PropagationByResource();
propByRes.set(ResourceOperation.DELETE, userDAO.findAllResourceKeys(user.getKey()));
saveForFormSubmit(user, null, propByRes);
if (engine.getRuntimeService().createProcessInstanceQuery().
processInstanceId(user.getWorkflowId()).active().list().isEmpty()) {
userDAO.delete(user.getKey());
if (!engine.getHistoryService().createHistoricProcessInstanceQuery().
processInstanceId(user.getWorkflowId()).list().isEmpty()) {
engine.getHistoryService().deleteHistoricProcessInstance(user.getWorkflowId());
}
} else {
updateStatus(user);
userDAO.save(user);
}
}
@Override
public WorkflowResult<String> execute(final UserTO userTO, final String taskId) {
User user = userDAO.authFind(userTO.getKey());
Map<String, Object> variables = new HashMap<>();
variables.put(USER_TO, userTO);
Set<String> performedTasks = doExecuteTask(user, taskId, variables);
updateStatus(user);
User updated = userDAO.save(user);
PropagationByResource propByRes = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROP_BY_RESOURCE, PropagationByResource.class);
saveForFormSubmit(user, userTO.getPassword(), propByRes);
return new WorkflowResult<>(updated.getKey(), null, performedTasks);
}
protected WorkflowFormPropertyType fromActivitiFormType(final FormType activitiFormType) {
WorkflowFormPropertyType result = WorkflowFormPropertyType.String;
if ("string".equals(activitiFormType.getName())) {
result = WorkflowFormPropertyType.String;
}
if ("long".equals(activitiFormType.getName())) {
result = WorkflowFormPropertyType.Long;
}
if ("enum".equals(activitiFormType.getName())) {
result = WorkflowFormPropertyType.Enum;
}
if ("date".equals(activitiFormType.getName())) {
result = WorkflowFormPropertyType.Date;
}
if ("boolean".equals(activitiFormType.getName())) {
result = WorkflowFormPropertyType.Boolean;
}
return result;
}
protected WorkflowFormTO getFormTO(final Task task) {
return getFormTO(task, engine.getFormService().getTaskFormData(task.getId()));
}
protected WorkflowFormTO getFormTO(final Task task, final TaskFormData fd) {
WorkflowFormTO formTO =
getFormTO(task.getProcessInstanceId(), task.getId(), fd.getFormKey(), fd.getFormProperties());
BeanUtils.copyProperties(task, formTO);
return formTO;
}
protected WorkflowFormTO getFormTO(final HistoricTaskInstance task) {
final List<HistoricFormPropertyEntity> props = new ArrayList<>();
for (HistoricDetail historicDetail
: engine.getHistoryService().createHistoricDetailQuery().taskId(task.getId()).list()) {
if (historicDetail instanceof HistoricFormPropertyEntity) {
props.add((HistoricFormPropertyEntity) historicDetail);
}
}
WorkflowFormTO formTO = getHistoricFormTO(
task.getProcessInstanceId(), task.getId(), task.getFormKey(), props);
BeanUtils.copyProperties(task, formTO);
HistoricActivityInstance historicActivityInstance = engine.getHistoryService().
createHistoricActivityInstanceQuery().
executionId(task.getExecutionId()).activityType("userTask").activityName(task.getName()).singleResult();
if (historicActivityInstance != null) {
formTO.setCreateTime(historicActivityInstance.getStartTime());
formTO.setDueDate(historicActivityInstance.getEndTime());
}
return formTO;
}
protected WorkflowFormTO getHistoricFormTO(
final String processInstanceId,
final String taskId,
final String formKey,
final List<HistoricFormPropertyEntity> props) {
WorkflowFormTO formTO = new WorkflowFormTO();
User user = userDAO.findByWorkflowId(processInstanceId);
if (user == null) {
throw new NotFoundException("User with workflow id " + processInstanceId);
}
formTO.setUsername(user.getUsername());
formTO.setTaskId(taskId);
formTO.setKey(formKey);
formTO.setUserTO(engine.getRuntimeService().getVariable(processInstanceId, USER_TO, UserTO.class));
formTO.setUserPatch(engine.getRuntimeService().getVariable(processInstanceId, USER_PATCH, UserPatch.class));
for (HistoricFormPropertyEntity prop : props) {
WorkflowFormPropertyTO propertyTO = new WorkflowFormPropertyTO();
propertyTO.setId(prop.getPropertyId());
propertyTO.setName(prop.getPropertyId());
propertyTO.setValue(prop.getPropertyValue());
formTO.getProperties().add(propertyTO);
}
return formTO;
}
@SuppressWarnings("unchecked")
protected WorkflowFormTO getFormTO(
final String processInstanceId,
final String taskId,
final String formKey,
final List<FormProperty> properties) {
WorkflowFormTO formTO = new WorkflowFormTO();
User user = userDAO.findByWorkflowId(processInstanceId);
if (user == null) {
throw new NotFoundException("User with workflow id " + processInstanceId);
}
formTO.setUsername(user.getUsername());
formTO.setTaskId(taskId);
formTO.setKey(formKey);
formTO.setUserTO(engine.getRuntimeService().getVariable(processInstanceId, USER_TO, UserTO.class));
formTO.setUserPatch(engine.getRuntimeService().getVariable(processInstanceId, USER_PATCH, UserPatch.class));
for (FormProperty fProp : properties) {
WorkflowFormPropertyTO propertyTO = new WorkflowFormPropertyTO();
BeanUtils.copyProperties(fProp, propertyTO, PROPERTY_IGNORE_PROPS);
propertyTO.setType(fromActivitiFormType(fProp.getType()));
if (propertyTO.getType() == WorkflowFormPropertyType.Date) {
propertyTO.setDatePattern((String) fProp.getType().getInformation("datePattern"));
}
if (propertyTO.getType() == WorkflowFormPropertyType.Enum) {
propertyTO.getEnumValues().putAll((Map<String, String>) fProp.getType().getInformation("values"));
}
formTO.getProperties().add(propertyTO);
}
return formTO;
}
@Transactional(readOnly = true)
@Override
public Pair<Integer, List<WorkflowFormTO>> getForms(
final int page, final int size, final List<OrderByClause> orderByClauses) {
String authUser = AuthContextUtils.getUsername();
return adminUser.equals(authUser)
? getForms(engine.getTaskService().createTaskQuery().
taskVariableValueEquals(TASK_IS_FORM, Boolean.TRUE), page, size, orderByClauses)
: getForms(engine.getTaskService().createTaskQuery().
taskVariableValueEquals(TASK_IS_FORM, Boolean.TRUE).
taskCandidateOrAssigned(authUser), page, size, orderByClauses);
}
protected Pair<Integer, List<WorkflowFormTO>> getForms(
final TaskQuery query, final int page, final int size, final List<OrderByClause> orderByClauses) {
TaskQuery sortedQuery = query;
for (OrderByClause clause : orderByClauses) {
boolean ack = true;
switch (clause.getField().trim()) {
case "taskId":
sortedQuery = sortedQuery.orderByTaskId();
break;
case "createTime":
sortedQuery = sortedQuery.orderByTaskCreateTime();
break;
case "dueDate":
sortedQuery = sortedQuery.orderByTaskDueDate();
break;
case "assignee":
sortedQuery = sortedQuery.orderByTaskAssignee();
break;
default:
LOG.warn("Form sort request by {}: unsupported, ignoring", clause.getField().trim());
ack = false;
}
if (ack) {
if (clause.getDirection() == OrderByClause.Direction.ASC) {
sortedQuery = sortedQuery.asc();
} else {
sortedQuery = sortedQuery.desc();
}
}
}
List<WorkflowFormTO> result = new ArrayList<>();
for (Task task : sortedQuery.listPage(size * (page <= 0 ? 0 : page - 1), size)) {
if (task instanceof HistoricTaskInstance) {
result.add(getFormTO((HistoricTaskInstance) task));
} else {
result.add(getFormTO(task));
}
}
return Pair.of((int) query.count(), result);
}
@Override
public WorkflowFormTO getForm(final String workflowId) {
Task task;
try {
task = engine.getTaskService().createTaskQuery().processInstanceId(workflowId).singleResult();
} catch (ActivitiException e) {
throw new WorkflowException("While reading form for workflow instance " + workflowId, e);
}
TaskFormData formData;
try {
formData = engine.getFormService().getTaskFormData(task.getId());
} catch (ActivitiException e) {
LOG.debug("No form found for task {}", task.getId(), e);
formData = null;
}
WorkflowFormTO result = null;
if (formData != null && !formData.getFormProperties().isEmpty()) {
result = getFormTO(task);
}
return result;
}
protected Pair<Task, TaskFormData> checkTask(final String taskId, final String authUser) {
Task task;
try {
task = engine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
throw new ActivitiException("NULL result");
}
} catch (ActivitiException e) {
throw new NotFoundException("Activiti Task " + taskId, e);
}
TaskFormData formData;
try {
formData = engine.getFormService().getTaskFormData(task.getId());
} catch (ActivitiException e) {
throw new NotFoundException("Form for Activiti Task " + taskId, e);
}
if (!adminUser.equals(authUser)) {
User user = userDAO.findByUsername(authUser);
if (user == null) {
throw new NotFoundException("Syncope User " + authUser);
}
}
return Pair.of(task, formData);
}
@Override
public WorkflowFormTO claimForm(final String taskId) {
String authUser = AuthContextUtils.getUsername();
Pair<Task, TaskFormData> checked = checkTask(taskId, authUser);
if (!adminUser.equals(authUser)) {
List<Task> tasksForUser = engine.getTaskService().createTaskQuery().taskId(taskId).taskCandidateUser(
authUser).list();
if (tasksForUser.isEmpty()) {
throw new WorkflowException(
new IllegalArgumentException(authUser + " is not candidate for task " + taskId));
}
}
boolean hasAssignees = IterableUtils.matchesAny(engine.getTaskService().getIdentityLinksForTask(taskId),
new Predicate<IdentityLink>() {
@Override
public boolean evaluate(final IdentityLink identityLink) {
return IdentityLinkType.ASSIGNEE.equals(identityLink.getType());
}
});
if (hasAssignees) {
try {
engine.getTaskService().unclaim(taskId);
} catch (ActivitiException e) {
throw new WorkflowException("While unclaiming task " + taskId, e);
}
}
Task task;
try {
engine.getTaskService().claim(taskId, authUser);
task = engine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
} catch (ActivitiException e) {
throw new WorkflowException("While reading task " + taskId, e);
}
return getFormTO(task, checked.getValue());
}
@Override
public WorkflowFormTO unclaimForm(final String taskId) {
String authUser = AuthContextUtils.getUsername();
Pair<Task, TaskFormData> checked = checkTask(taskId, authUser);
Task task;
try {
engine.getTaskService().unclaim(taskId);
task = engine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
} catch (ActivitiException e) {
throw new WorkflowException("While unclaiming task " + taskId, e);
}
return getFormTO(task, checked.getValue());
}
private Map<String, String> getPropertiesForSubmit(final WorkflowFormTO form) {
Map<String, String> props = new HashMap<>();
for (WorkflowFormPropertyTO prop : form.getProperties()) {
if (prop.isWritable()) {
props.put(prop.getId(), prop.getValue());
}
}
return Collections.unmodifiableMap(props);
}
@Override
public WorkflowResult<UserPatch> submitForm(final WorkflowFormTO form) {
String authUser = AuthContextUtils.getUsername();
Pair<Task, TaskFormData> checked = checkTask(form.getTaskId(), authUser);
if (!checked.getKey().getAssignee().equals(authUser)) {
throw new WorkflowException(new IllegalArgumentException("Task " + form.getTaskId() + " assigned to "
+ checked.getKey().getAssignee() + " but submitted by " + authUser));
}
User user = userDAO.findByWorkflowId(checked.getKey().getProcessInstanceId());
if (user == null) {
throw new NotFoundException("User with workflow id " + checked.getKey().getProcessInstanceId());
}
Set<String> preTasks = getPerformedTasks(user);
try {
engine.getFormService().submitTaskFormData(form.getTaskId(), getPropertiesForSubmit(form));
engine.getRuntimeService().setVariable(user.getWorkflowId(), FORM_SUBMITTER, authUser);
} catch (ActivitiException e) {
throwException(e, "While submitting form for task " + form.getTaskId());
}
Set<String> postTasks = getPerformedTasks(user);
postTasks.removeAll(preTasks);
postTasks.add(form.getTaskId());
updateStatus(user);
User updated = userDAO.save(user);
// see if there is any propagation to be done
PropagationByResource propByRes = engine.getRuntimeService().getVariable(
user.getWorkflowId(), PROP_BY_RESOURCE, PropagationByResource.class);
// fetch - if available - the encrypted password
String clearPassword = null;
String encryptedPwd = engine.getRuntimeService().getVariable(user.getWorkflowId(), ENCRYPTED_PWD, String.class);
if (StringUtils.isNotBlank(encryptedPwd)) {
clearPassword = decrypt(encryptedPwd);
}
// supports approval chains
saveForFormSubmit(user, clearPassword, propByRes);
UserPatch userPatch = engine.getRuntimeService().getVariable(user.getWorkflowId(), USER_PATCH, UserPatch.class);
if (userPatch == null) {
userPatch = new UserPatch();
userPatch.setKey(updated.getKey());
userPatch.setPassword(new PasswordPatch.Builder().onSyncope(true).value(clearPassword).build());
if (propByRes != null) {
userPatch.getPassword().getResources().addAll(propByRes.get(ResourceOperation.CREATE));
}
}
return new WorkflowResult<>(userPatch, propByRes, postTasks);
}
protected void navigateAvailableTasks(final FlowElement flow, final List<String> availableTasks) {
if (flow instanceof Gateway) {
for (SequenceFlow subflow : ((Gateway) flow).getOutgoingFlows()) {
navigateAvailableTasks(subflow, availableTasks);
}
} else if (flow instanceof SequenceFlow) {
availableTasks.add(((SequenceFlow) flow).getTargetRef());
} else {
LOG.debug("Unexpected flow found: {}", flow);
}
}
@Override
public List<WorkflowTaskTO> getAvailableTasks(final String workflowId) {
List<String> availableTasks = new ArrayList<>();
try {
Task currentTask = engine.getTaskService().createTaskQuery().processInstanceId(workflowId).singleResult();
Process process = engine.getRepositoryService().
getBpmnModel(getProcessDefinitionByKey(WF_PROCESS_ID).getId()).getProcesses().get(0);
for (FlowElement flowElement : process.getFlowElements()) {
if (flowElement instanceof SequenceFlow) {
SequenceFlow sequenceFlow = (SequenceFlow) flowElement;
if (sequenceFlow.getSourceRef().equals(currentTask.getTaskDefinitionKey())) {
FlowElement target = process.getFlowElementRecursive(sequenceFlow.getTargetRef());
navigateAvailableTasks(target, availableTasks);
}
}
}
} catch (ActivitiException e) {
throw new WorkflowException("While reading available tasks for workflow instance " + workflowId, e);
}
return CollectionUtils.collect(availableTasks, new Transformer<String, WorkflowTaskTO>() {
@Override
public WorkflowTaskTO transform(final String input) {
WorkflowTaskTO workflowTaskTO = new WorkflowTaskTO();
workflowTaskTO.setName(input);
return workflowTaskTO;
}
}, new ArrayList<WorkflowTaskTO>());
}
protected Model getModel(final ProcessDefinition procDef) {
try {
Model model = engine.getRepositoryService().createModelQuery().
deploymentId(procDef.getDeploymentId()).singleResult();
if (model == null) {
throw new NotFoundException("Could not find Model for deployment " + procDef.getDeploymentId());
}
return model;
} catch (Exception e) {
throw new WorkflowException("While accessing process " + procDef.getKey(), e);
}
}
@Override
public List<WorkflowDefinitionTO> getDefinitions() {
try {
return CollectionUtils.collect(
engine.getRepositoryService().createProcessDefinitionQuery().latestVersion().list(),
new Transformer<ProcessDefinition, WorkflowDefinitionTO>() {
@Override
public WorkflowDefinitionTO transform(final ProcessDefinition procDef) {
WorkflowDefinitionTO defTO = new WorkflowDefinitionTO();
defTO.setKey(procDef.getKey());
defTO.setName(procDef.getName());
try {
defTO.setModelId(getModel(procDef).getId());
} catch (NotFoundException e) {
LOG.warn("No model found for definition {}, ignoring", procDef.getDeploymentId(), e);
}
defTO.setMain(WF_PROCESS_ID.equals(procDef.getKey()));
return defTO;
}
}, new ArrayList<WorkflowDefinitionTO>());
} catch (ActivitiException e) {
throw new WorkflowException("While listing available process definitions", e);
}
}
protected ProcessDefinition getProcessDefinitionByKey(final String key) {
try {
return engine.getRepositoryService().createProcessDefinitionQuery().
processDefinitionKey(key).latestVersion().singleResult();
} catch (ActivitiException e) {
throw new WorkflowException("While accessing process " + key, e);
}
}
protected ProcessDefinition getProcessDefinitionByDeploymentId(final String deploymentId) {
try {
return engine.getRepositoryService().createProcessDefinitionQuery().
deploymentId(deploymentId).latestVersion().singleResult();
} catch (ActivitiException e) {
throw new WorkflowException("While accessing deployment " + deploymentId, e);
}
}
protected void exportProcessModel(final String key, final OutputStream os) {
Model model = getModel(getProcessDefinitionByKey(key));
try {
ObjectNode modelNode = (ObjectNode) OBJECT_MAPPER.readTree(model.getMetaInfo());
modelNode.put(ModelDataJsonConstants.MODEL_ID, model.getId());
modelNode.replace(MODEL_DATA_JSON_MODEL,
OBJECT_MAPPER.readTree(engine.getRepositoryService().getModelEditorSource(model.getId())));
os.write(modelNode.toString().getBytes());
} catch (IOException e) {
LOG.error("While exporting workflow definition {}", model.getId(), e);
}
}
protected void exportProcessResource(final String deploymentId, final String resourceName, final OutputStream os) {
try (InputStream procDefIS = engine.getRepositoryService().getResourceAsStream(deploymentId, resourceName)) {
IOUtils.copy(procDefIS, os);
} catch (IOException e) {
LOG.error("While exporting {}", resourceName, e);
}
}
@Override
public void exportDefinition(final String key, final WorkflowDefinitionFormat format, final OutputStream os) {
switch (format) {
case JSON:
exportProcessModel(key, os);
break;
case XML:
default: