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 public void doRemoveWorkFlowResource( int nIdResource, String strResourceType )
503 {
504 if ( isAvailable( ) )
505 {
506 TransactionManager.beginTransaction( null );
507
508 try
509 {
510 _service.doRemoveWorkFlowResource( nIdResource, strResourceType );
511 TransactionManager.commitTransaction( null );
512 }
513 catch( Exception e )
514 {
515 TransactionManager.rollBack( null );
516 throw new AppException( e.getMessage( ), e );
517 }
518 }
519 }
520
521
522
523
524
525
526
527
528
529
530
531 public void doRemoveWorkFlowResourceByListId( List<Integer> lListIdResource, String strResourceType, Integer nIdWorflow )
532 {
533 if ( isAvailable( ) )
534 {
535 TransactionManager.beginTransaction( null );
536
537 try
538 {
539 _service.doRemoveWorkFlowResourceByListId( lListIdResource, strResourceType, nIdWorflow );
540 TransactionManager.commitTransaction( null );
541 }
542 catch( Exception e )
543 {
544 TransactionManager.rollBack( null );
545 throw new AppException( e.getMessage( ), e );
546 }
547 }
548 }
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567 @Deprecated
568 public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale )
569 {
570 return getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, null );
571
572 }
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592 public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale, User user )
593 {
594 return isAvailable( ) ? _provider.getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, user ) : null;
595 }
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610 public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, User user )
611 {
612 return isAvailable( ) && _provider.isAuthorized( nIdResource, strResourceType, nIdWorkflow, user );
613 }
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629 @Deprecated
630 public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
631 {
632 return isAuthorized( nIdResource, strResourceType, nIdWorkflow, (User) user );
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, User user )
651 {
652 return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, user ) : null;
653 }
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671 @Deprecated
672 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, AdminUser user )
673 {
674 return getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, (User) user );
675
676 }
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
694 User user )
695 {
696 return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, user ) : null;
697 }
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715 @Deprecated
716 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
717 AdminUser user )
718 {
719 return getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, (User) user );
720 }
721
722
723
724
725
726
727
728
729
730
731 public ReferenceList getWorkflowsEnabled( User user, Locale locale )
732 {
733 return isAvailable( ) ? _provider.getWorkflowsEnabled( user, locale ) : null;
734 }
735
736
737
738
739
740
741
742
743
744
745
746 @Deprecated
747 public ReferenceList getWorkflowsEnabled( AdminUser user, Locale locale )
748 {
749 return getWorkflowsEnabled( (User) user, locale );
750 }
751
752
753
754
755
756
757
758
759
760
761 public Collection<State> getAllStateByWorkflow( int nIdWorkflow, User user )
762 {
763 if ( isAvailable( ) )
764 {
765 Collection<State> listStates = _service.getAllStateByWorkflow( nIdWorkflow );
766
767 return _provider.getAllStateByWorkflow( listStates, user );
768 }
769
770 return null;
771 }
772
773
774
775
776
777
778
779
780
781
782
783 @Deprecated
784 public Collection<State> getAllStateByWorkflow( int nIdWorkflow, AdminUser user )
785 {
786 return getAllStateByWorkflow( nIdWorkflow, (User) user );
787 }
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802 public State getState( int nIdResource, String strResourceType, int nIdWorkflow, Integer nIdExternalParentId )
803 {
804 if ( isAvailable( ) )
805 {
806 State state = null;
807 TransactionManager.beginTransaction( null );
808
809 try
810 {
811 state = _service.getState( nIdResource, strResourceType, nIdWorkflow, nIdExternalParentId );
812 TransactionManager.commitTransaction( null );
813 }
814 catch( Exception e )
815 {
816 TransactionManager.rollBack( null );
817 throw new AppException( e.getMessage( ), e );
818 }
819
820 return state;
821 }
822
823 return null;
824 }
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839 @Deprecated
840 public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId )
841 {
842 executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, null );
843 }
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859 public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId, User user )
860 {
861 if ( isAvailable( ) )
862 {
863 TransactionManager.beginTransaction( null );
864
865 try
866 {
867 _service.executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, user );
868 TransactionManager.commitTransaction( null );
869
870 registerResourceEvent( nIdResource, strResourceType );
871 }
872 catch( Exception e )
873 {
874 TransactionManager.rollBack( null );
875 throw new AppException( e.getMessage( ), e );
876 }
877 }
878 }
879
880
881
882
883
884
885
886
887 public List<Action> getMassActions( int nIdWorkflow )
888 {
889 return isAvailable( ) ? _service.getMassActions( nIdWorkflow ) : null;
890 }
891
892
893
894
895
896
897
898
899
900
901 public Collection<Action> getMassActions( int nIdWorkflow, int nIdState, User user )
902 {
903 if ( !isAvailable( ) )
904 {
905 return null;
906 }
907
908 Collection<Action> listActions = _service.getMassActions( nIdWorkflow, nIdState );
909
910 return _provider.getAuthorizedActions( listActions, user );
911 }
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931 @Deprecated
932 public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
933 boolean bIsAutomatic )
934 {
935 return canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic, null );
936 }
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957 public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
958 boolean bIsAutomatic, User user )
959 {
960 if ( isAvailable( ) && _service.canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId ) )
961 {
962 if ( bIsAutomatic )
963 {
964 return true;
965 }
966
967 return _provider.canProcessAction( nIdResource, strResourceType, nIdAction, request, user );
968 }
969
970 return false;
971 }
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989 @Deprecated
990 public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale )
991 {
992 doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, null );
993 }
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale, User user )
1013 {
1014 if ( isAvailable( ) )
1015 {
1016 TransactionManager.beginTransaction( null );
1017
1018 try
1019 {
1020 _service.doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, user );
1021 TransactionManager.commitTransaction( null );
1022
1023 registerResourceEvent( nIdResource, strResourceType );
1024 }
1025 catch( Exception e )
1026 {
1027 TransactionManager.rollBack( null );
1028 throw new AppException( e.getMessage( ), e );
1029 }
1030 }
1031 }
1032
1033
1034
1035
1036
1037
1038
1039 private void registerResourceEvent( int nIdResource, String strResourceType )
1040 {
1041 ResourceEventsourceEvent.html#ResourceEvent">ResourceEvent formResponseEvent = new ResourceEvent( );
1042 formResponseEvent.setIdResource( String.valueOf( nIdResource ) );
1043 formResponseEvent.setTypeResource( strResourceType );
1044
1045 ResourceEventManager.fireUpdatedResource( formResponseEvent );
1046 }
1047 }