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 package fr.paris.lutece.portal.service.workflow;
35
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Map;
40
41 import javax.servlet.http.HttpServletRequest;
42
43 import org.apache.commons.lang3.StringUtils;
44 import org.springframework.beans.factory.BeanDefinitionStoreException;
45 import org.springframework.beans.factory.CannotLoadBeanClassException;
46 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
47
48 import fr.paris.lutece.api.user.User;
49 import fr.paris.lutece.plugins.workflowcore.business.action.Action;
50 import fr.paris.lutece.plugins.workflowcore.business.state.State;
51 import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
52 import fr.paris.lutece.portal.business.event.ResourceEvent;
53 import fr.paris.lutece.portal.business.user.AdminUser;
54 import fr.paris.lutece.portal.service.event.ResourceEventManager;
55 import fr.paris.lutece.portal.service.plugin.PluginService;
56 import fr.paris.lutece.portal.service.spring.SpringContextService;
57 import fr.paris.lutece.portal.service.util.AppException;
58 import fr.paris.lutece.portal.service.util.AppLogService;
59 import fr.paris.lutece.util.ReferenceList;
60 import fr.paris.lutece.util.sql.TransactionManager;
61
62
63
64
65
66
67 public final class WorkflowService
68 {
69 private static final String PLUGIN_WORKFLOW_NAME = "workflow";
70 private static final String BEAN_WORKFLOW_PROVIDER = "workflow.workflowProvider";
71 private static WorkflowService _singleton;
72 private boolean _bServiceAvailable = true;
73 private IWorkflowService _service;
74 private IWorkflowProvider _provider;
75
76
77
78
79 private WorkflowService( )
80 {
81 try
82 {
83 _service = SpringContextService.getBean( fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService.BEAN_SERVICE );
84 _provider = SpringContextService.getBean( BEAN_WORKFLOW_PROVIDER );
85 _bServiceAvailable = ( _service != null ) && ( _provider != null );
86 }
87 catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
88 {
89 _bServiceAvailable = false;
90 }
91 }
92
93
94
95
96
97
98 public static synchronized WorkflowService getInstance( )
99 {
100 if ( _singleton == null )
101 {
102 _singleton = new WorkflowService( );
103 }
104 return _singleton;
105 }
106
107
108
109
110
111
112
113
114
115
116 public boolean isAvailable( )
117 {
118
119
120 return _bServiceAvailable && ( _service != null ) && ( _provider != null ) && PluginService.isPluginEnable( PLUGIN_WORKFLOW_NAME );
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public Collection<Action> getActions( int nIdResource, String strResourceType, int nIdWorkflow, User user )
137 {
138 if ( isAvailable( ) )
139 {
140 Collection<Action> listActions = _service.getActions( nIdResource, strResourceType, nIdWorkflow );
141
142 return _provider.getActions( nIdResource, strResourceType, listActions, user );
143 }
144
145 return null;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 @Deprecated
163 public Collection<Action> getActions( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
164 {
165 return getActions( nIdResource, strResourceType, nIdWorkflow, (User) user );
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public Map<Integer, List<Action>> getActions( List<Integer> listIdResource, String strResourceType, Integer nIdExternalParentId, int nIdWorkflow,
184 User user )
185 {
186 if ( isAvailable( ) )
187 {
188 Map<Integer, List<Action>> mapActions = _service.getActions( listIdResource, strResourceType, nIdExternalParentId, nIdWorkflow );
189
190 return _provider.getActions( strResourceType, mapActions, user );
191 }
192
193 return null;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 @Deprecated
213 public Map<Integer, List<Action>> getActions( List<Integer> listIdResource, String strResourceType, Integer nIdExternalParentId, int nIdWorkflow,
214 AdminUser user )
215 {
216 return getActions( listIdResource, strResourceType, nIdExternalParentId, nIdWorkflow, (User) user );
217 }
218
219
220
221
222
223
224
225
226
227
228 public boolean isDisplayTasksForm( int nIdAction, Locale locale )
229 {
230 return isAvailable( ) && _service.isDisplayTasksForm( nIdAction, locale );
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 @Deprecated
253 public void doProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
254 boolean bIsAutomatic )
255 {
256 doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, bIsAutomatic, null );
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public void doProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
280 boolean bIsAutomatic, User user )
281 {
282 if ( isAvailable( ) && canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic, user ) )
283 {
284 TransactionManager.beginTransaction( null );
285
286 try
287 {
288 String strUserAccessCode = bIsAutomatic ? null : _provider.getUserAccessCode( request, user );
289 _service.doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, bIsAutomatic, strUserAccessCode, user );
290 TransactionManager.commitTransaction( null );
291
292 registerResourceEvent( nIdResource, strResourceType );
293 }
294 catch( Exception e )
295 {
296 TransactionManager.rollBack( null );
297 throw new AppException( e.getMessage( ), e );
298 }
299 }
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 @Deprecated
319 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale )
320 {
321 return getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, null );
322 }
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale, User user )
342 {
343 return isAvailable( ) ? _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, user ) : null;
344 }
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 @Deprecated
367 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale,
368 Map<String, Object> model, String strTemplate )
369 {
370 return getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, model, strTemplate, null );
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale,
395 Map<String, Object> model, String strTemplate, User user )
396 {
397 if ( !isAvailable( ) )
398 {
399 return null;
400 }
401 try
402 {
403 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, model, strTemplate, user );
404 }
405 catch( NoSuchMethodError ex )
406 {
407 AppLogService.error( "You are using a too old Workflow provider version. Please upgrade." );
408 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, user );
409 }
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430 @Deprecated
431 public String doSaveTasksForm( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
432 Locale locale )
433 {
434 return doSaveTasksForm( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, null );
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 public String doSaveTasksForm( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
457 User user )
458 {
459 if ( isAvailable( ) )
460 {
461 String strError = _provider.doValidateTasksForm( nIdResource, strResourceType, nIdAction, request, locale, user );
462
463 if ( StringUtils.isNotBlank( strError ) )
464 {
465 return strError;
466 }
467
468 doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, false, user );
469 }
470
471 return null;
472 }
473
474
475
476
477
478
479
480
481
482
483
484 public List<Integer> getResourceIdListByIdState( int nIdState, String strResourceType )
485 {
486 if ( isAvailable( ) )
487 {
488 return _service.getResourceIdListByIdState( nIdState, strResourceType );
489 }
490
491 return null;
492 }
493
494
495
496
497
498
499
500
501
502
503
504
505 public List<Integer> getResourceIdListByIdState( int nIdState, String strResourceType, int nExternalParentId )
506 {
507 if ( isAvailable( ) )
508 {
509 return _service.getResourceIdListByIdState( nIdState, strResourceType, nExternalParentId );
510 }
511
512 return null;
513 }
514
515
516
517
518
519
520
521
522
523
524 public void doRemoveWorkFlowResource( int nIdResource, String strResourceType )
525 {
526 if ( isAvailable( ) )
527 {
528 TransactionManager.beginTransaction( null );
529
530 try
531 {
532 _service.doRemoveWorkFlowResource( nIdResource, strResourceType );
533 TransactionManager.commitTransaction( null );
534 }
535 catch( Exception e )
536 {
537 TransactionManager.rollBack( null );
538 throw new AppException( e.getMessage( ), e );
539 }
540 }
541 }
542
543
544
545
546
547
548
549
550
551
552
553 public void doRemoveWorkFlowResourceByListId( List<Integer> lListIdResource, String strResourceType, Integer nIdWorflow )
554 {
555 if ( isAvailable( ) )
556 {
557 TransactionManager.beginTransaction( null );
558
559 try
560 {
561 _service.doRemoveWorkFlowResourceByListId( lListIdResource, strResourceType, nIdWorflow );
562 TransactionManager.commitTransaction( null );
563 }
564 catch( Exception e )
565 {
566 TransactionManager.rollBack( null );
567 throw new AppException( e.getMessage( ), e );
568 }
569 }
570 }
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589 @Deprecated
590 public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale )
591 {
592 return getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, null );
593
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614 public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale, User user )
615 {
616 return isAvailable( ) ? _provider.getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, user ) : null;
617 }
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632 public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, User user )
633 {
634 return isAvailable( ) && _provider.isAuthorized( nIdResource, strResourceType, nIdWorkflow, user );
635 }
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651 @Deprecated
652 public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
653 {
654 return isAuthorized( nIdResource, strResourceType, nIdWorkflow, (User) user );
655 }
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, User user )
673 {
674 return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, user ) : null;
675 }
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693 @Deprecated
694 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, AdminUser user )
695 {
696 return getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, (User) user );
697
698 }
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
716 User user )
717 {
718 return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, user ) : null;
719 }
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 @Deprecated
738 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
739 AdminUser user )
740 {
741 return getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, (User) user );
742 }
743
744
745
746
747
748
749
750
751
752
753 public ReferenceList getWorkflowsEnabled( User user, Locale locale )
754 {
755 return isAvailable( ) ? _provider.getWorkflowsEnabled( user, locale ) : null;
756 }
757
758
759
760
761
762
763
764
765
766
767
768 @Deprecated
769 public ReferenceList getWorkflowsEnabled( AdminUser user, Locale locale )
770 {
771 return getWorkflowsEnabled( (User) user, locale );
772 }
773
774
775
776
777
778
779
780
781
782
783 public Collection<State> getAllStateByWorkflow( int nIdWorkflow, User user )
784 {
785 if ( isAvailable( ) )
786 {
787 Collection<State> listStates = _service.getAllStateByWorkflow( nIdWorkflow );
788
789 return _provider.getAllStateByWorkflow( listStates, user );
790 }
791
792 return null;
793 }
794
795
796
797
798
799
800
801
802
803
804
805 @Deprecated
806 public Collection<State> getAllStateByWorkflow( int nIdWorkflow, AdminUser user )
807 {
808 return getAllStateByWorkflow( nIdWorkflow, (User) user );
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824 public State getState( int nIdResource, String strResourceType, int nIdWorkflow, Integer nIdExternalParentId )
825 {
826 if ( isAvailable( ) )
827 {
828 State state = null;
829 TransactionManager.beginTransaction( null );
830
831 try
832 {
833 state = _service.getState( nIdResource, strResourceType, nIdWorkflow, nIdExternalParentId );
834 TransactionManager.commitTransaction( null );
835 }
836 catch( Exception e )
837 {
838 TransactionManager.rollBack( null );
839 throw new AppException( e.getMessage( ), e );
840 }
841
842 return state;
843 }
844
845 return null;
846 }
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861 @Deprecated
862 public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId )
863 {
864 executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, null );
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881 public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId, User user )
882 {
883 if ( isAvailable( ) )
884 {
885 TransactionManager.beginTransaction( null );
886
887 try
888 {
889 _service.executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, user );
890 TransactionManager.commitTransaction( null );
891
892 registerResourceEvent( nIdResource, strResourceType );
893 }
894 catch( Exception e )
895 {
896 TransactionManager.rollBack( null );
897 throw new AppException( e.getMessage( ), e );
898 }
899 }
900 }
901
902
903
904
905
906
907
908
909 public List<Action> getMassActions( int nIdWorkflow )
910 {
911 return isAvailable( ) ? _service.getMassActions( nIdWorkflow ) : null;
912 }
913
914
915
916
917
918
919
920
921
922
923 public Collection<Action> getMassActions( int nIdWorkflow, int nIdState, User user )
924 {
925 if ( !isAvailable( ) )
926 {
927 return null;
928 }
929
930 Collection<Action> listActions = _service.getMassActions( nIdWorkflow, nIdState );
931
932 return _provider.getAuthorizedActions( listActions, user );
933 }
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953 @Deprecated
954 public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
955 boolean bIsAutomatic )
956 {
957 return canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic, null );
958 }
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979 public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
980 boolean bIsAutomatic, User user )
981 {
982 if ( isAvailable( ) && _service.canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId ) )
983 {
984 if ( bIsAutomatic )
985 {
986 return true;
987 }
988
989 return _provider.canProcessAction( nIdResource, strResourceType, nIdAction, request, user );
990 }
991
992 return false;
993 }
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011 @Deprecated
1012 public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale )
1013 {
1014 doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, null );
1015 }
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034 public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale, User user )
1035 {
1036 if ( isAvailable( ) )
1037 {
1038 TransactionManager.beginTransaction( null );
1039
1040 try
1041 {
1042 _service.doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, user );
1043 TransactionManager.commitTransaction( null );
1044
1045 registerResourceEvent( nIdResource, strResourceType );
1046 }
1047 catch( Exception e )
1048 {
1049 TransactionManager.rollBack( null );
1050 throw new AppException( e.getMessage( ), e );
1051 }
1052 }
1053 }
1054
1055
1056
1057
1058
1059
1060
1061 private void registerResourceEvent( int nIdResource, String strResourceType )
1062 {
1063 ResourceEventsourceEvent.html#ResourceEvent">ResourceEvent formResponseEvent = new ResourceEvent( );
1064 formResponseEvent.setIdResource( String.valueOf( nIdResource ) );
1065 formResponseEvent.setTypeResource( strResourceType );
1066
1067 ResourceEventManager.fireUpdatedResource( formResponseEvent );
1068 }
1069 }