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 fr.paris.lutece.plugins.workflowcore.business.action.Action;
37 import fr.paris.lutece.plugins.workflowcore.business.state.State;
38 import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.service.plugin.PluginService;
41 import fr.paris.lutece.portal.service.spring.SpringContextService;
42 import fr.paris.lutece.portal.service.util.AppException;
43 import fr.paris.lutece.portal.service.util.AppLogService;
44 import fr.paris.lutece.util.ReferenceList;
45 import fr.paris.lutece.util.sql.TransactionManager;
46
47 import org.apache.commons.lang.StringUtils;
48
49 import org.springframework.beans.factory.BeanDefinitionStoreException;
50 import org.springframework.beans.factory.CannotLoadBeanClassException;
51 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
52
53 import java.util.Collection;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Map;
57
58 import javax.servlet.http.HttpServletRequest;
59
60
61
62
63
64
65
66 public final class WorkflowService
67 {
68 private static final String PLUGIN_WORKFLOW_NAME = "workflow";
69 private static final String BEAN_WORKFLOW_PROVIDER = "workflow.workflowProvider";
70 private static volatile WorkflowService _singleton;
71 private boolean _bServiceAvailable = true;
72 private IWorkflowService _service;
73 private IWorkflowProvider _provider;
74
75
76
77
78 private WorkflowService( )
79 {
80 try
81 {
82 _service = SpringContextService.getBean( fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService.BEAN_SERVICE );
83 _provider = SpringContextService.getBean( BEAN_WORKFLOW_PROVIDER );
84 _bServiceAvailable = ( _service != null ) && ( _provider != null );
85 }
86 catch ( BeanDefinitionStoreException e )
87 {
88 _bServiceAvailable = false;
89 }
90 catch ( NoSuchBeanDefinitionException e )
91 {
92 _bServiceAvailable = false;
93 }
94 catch ( CannotLoadBeanClassException e )
95 {
96 _bServiceAvailable = false;
97 }
98 }
99
100
101
102
103
104 public static WorkflowService getInstance( )
105 {
106 if ( _singleton == null )
107 {
108 _singleton = new WorkflowService( );
109 }
110
111 return _singleton;
112 }
113
114
115
116
117
118
119
120
121
122
123 public boolean isAvailable( )
124 {
125
126 return _bServiceAvailable && ( _service != null ) && ( _provider != null ) &&
127 PluginService.isPluginEnable( PLUGIN_WORKFLOW_NAME );
128 }
129
130
131
132
133
134
135
136
137
138
139
140 public Collection<Action> getActions( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
141 {
142 if ( isAvailable( ) )
143 {
144 Collection<Action> listActions = _service.getActions( nIdResource, strResourceType, nIdWorkflow );
145
146 return _provider.getActions( listActions, user );
147 }
148
149 return null;
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 public Map<Integer, List<Action>> getActions( List<Integer> listIdResource, String strResourceType,
164 Integer nIdExternalParentId, int nIdWorkflow, AdminUser user )
165 {
166 if ( isAvailable( ) )
167 {
168 Map<Integer, List<Action>> mapActions = _service.getActions( listIdResource, strResourceType,
169 nIdExternalParentId, nIdWorkflow );
170
171 return _provider.getActions( mapActions, user );
172 }
173
174 return null;
175 }
176
177
178
179
180
181
182
183
184 public boolean isDisplayTasksForm( int nIdAction, Locale locale )
185 {
186 return isAvailable( ) ? _service.isDisplayTasksForm( nIdAction, locale ) : false;
187 }
188
189
190
191
192
193
194
195
196
197
198
199 public void doProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId,
200 HttpServletRequest request, Locale locale, boolean bIsAutomatic )
201 {
202 if ( isAvailable( ) &&
203 canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic ) )
204 {
205 TransactionManager.beginTransaction( null );
206
207 try
208 {
209 String strUserAccessCode = bIsAutomatic ? null : _provider.getUserAccessCode( request );
210 _service.doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale,
211 bIsAutomatic, strUserAccessCode );
212 TransactionManager.commitTransaction( null );
213 }
214 catch ( Exception e )
215 {
216 TransactionManager.rollBack( null );
217 throw new AppException( e.getMessage( ), e );
218 }
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow,
232 HttpServletRequest request, Locale locale )
233 {
234 return isAvailable( )
235 ? _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale ) : null;
236 }
237
238
239
240
241
242
243
244
245
246
247
248 @Deprecated
249 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow,
250 HttpServletRequest request, Locale locale, String strTemplate )
251 {
252 if( ! isAvailable() )
253 {
254 return null;
255 }
256 try
257 {
258 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, strTemplate );
259 }
260 catch( NoSuchMethodError ex )
261 {
262 AppLogService.error( "You are using a too old Workflow provider version. Please upgrade.");
263 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale );
264 }
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278 public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow,
279 HttpServletRequest request, Locale locale, Map<String, Object> model, String strTemplate )
280 {
281 if( ! isAvailable() )
282 {
283 return null;
284 }
285 try
286 {
287 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, model, strTemplate );
288 }
289 catch( NoSuchMethodError ex )
290 {
291 AppLogService.error( "You are using a too old Workflow provider version. Please upgrade.");
292 return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale );
293 }
294 }
295
296
297
298
299
300
301
302
303
304
305
306 public String getDocumentHistoryXml( int nIdResource, String strResourceType, int nIdWorkflow,
307 HttpServletRequest request, Locale locale )
308 {
309 return isAvailable( )
310 ? _provider.getDocumentHistoryXml( nIdResource, strResourceType, nIdWorkflow, request, locale ) : null;
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public String doSaveTasksForm( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId,
326 HttpServletRequest request, Locale locale )
327 {
328 if ( isAvailable( ) )
329 {
330 String strError = _provider.doValidateTasksForm( nIdResource, strResourceType, nIdAction, request, locale );
331
332 if ( StringUtils.isNotBlank( strError ) )
333 {
334 return strError;
335 }
336
337 doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, false );
338 }
339
340 return null;
341 }
342
343
344
345
346
347
348
349
350
351
352 public List<Integer> getResourceIdListByIdState( int nIdState, String strResourceType )
353 {
354 if ( isAvailable( ) )
355 {
356 return _service.getResourceIdListByIdState( nIdState, strResourceType );
357 }
358
359 return null;
360 }
361
362
363
364
365
366
367 public void doRemoveWorkFlowResource( int nIdResource, String strResourceType )
368 {
369 if ( isAvailable( ) )
370 {
371 TransactionManager.beginTransaction( null );
372
373 try
374 {
375 _service.doRemoveWorkFlowResource( nIdResource, strResourceType );
376 TransactionManager.commitTransaction( null );
377 }
378 catch ( Exception e )
379 {
380 TransactionManager.rollBack( null );
381 throw new AppException( e.getMessage( ), e );
382 }
383 }
384 }
385
386
387
388
389
390
391
392 public void doRemoveWorkFlowResourceByListId( List<Integer> lListIdResource, String strResourceType,
393 Integer nIdWorflow )
394 {
395 if ( isAvailable( ) )
396 {
397 TransactionManager.beginTransaction( null );
398
399 try
400 {
401 _service.doRemoveWorkFlowResourceByListId( lListIdResource, strResourceType, nIdWorflow );
402 TransactionManager.commitTransaction( null );
403 }
404 catch ( Exception e )
405 {
406 TransactionManager.rollBack( null );
407 throw new AppException( e.getMessage( ), e );
408 }
409 }
410 }
411
412
413
414
415
416
417
418
419
420
421
422 public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction,
423 HttpServletRequest request, Locale locale )
424 {
425 return isAvailable( )
426 ? _provider.getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale ) : null;
427 }
428
429
430
431
432
433
434
435
436
437
438 public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
439 {
440 return isAvailable( ) && _provider.isAuthorized( nIdResource, strResourceType, nIdWorkflow, user );
441 }
442
443
444
445
446
447
448
449
450
451
452
453 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState,
454 Integer nExternalParentId, AdminUser user )
455 {
456 return isAvailable( )
457 ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, user )
458 : null;
459 }
460
461
462
463
464
465
466
467
468
469
470
471 public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow,
472 List<Integer> lListIdWorkflowState, Integer nExternalParentId, AdminUser user )
473 {
474 return isAvailable( )
475 ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId,
476 user ) : null;
477 }
478
479
480
481
482
483
484
485 public ReferenceList getWorkflowsEnabled( AdminUser user, Locale locale )
486 {
487 return isAvailable( ) ? _provider.getWorkflowsEnabled( user, locale ) : null;
488 }
489
490
491
492
493
494
495
496 public Collection<State> getAllStateByWorkflow( int nIdWorkflow, AdminUser user )
497 {
498 if ( isAvailable( ) )
499 {
500 Collection<State> listStates = _service.getAllStateByWorkflow( nIdWorkflow );
501
502 return _provider.getAllStateByWorkflow( listStates, user );
503 }
504
505 return null;
506 }
507
508
509
510
511
512
513
514
515
516
517 public State getState( int nIdResource, String strResourceType, int nIdWorkflow, Integer nIdExternalParentId )
518 {
519 if ( isAvailable( ) )
520 {
521 State state = null;
522 TransactionManager.beginTransaction( null );
523
524 try
525 {
526 state = _service.getState( nIdResource, strResourceType, nIdWorkflow, nIdExternalParentId );
527 TransactionManager.commitTransaction( null );
528 }
529 catch ( Exception e )
530 {
531 TransactionManager.rollBack( null );
532 throw new AppException( e.getMessage( ), e );
533 }
534
535 return state;
536 }
537
538 return null;
539 }
540
541
542
543
544
545
546
547
548 public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow,
549 Integer nExternalParentId )
550 {
551 if ( isAvailable( ) )
552 {
553 TransactionManager.beginTransaction( null );
554
555 try
556 {
557 _service.executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId );
558 TransactionManager.commitTransaction( null );
559 }
560 catch ( Exception e )
561 {
562 TransactionManager.rollBack( null );
563 throw new AppException( e.getMessage( ), e );
564 }
565 }
566 }
567
568
569
570
571
572
573 public List<Action> getMassActions( int nIdWorkflow )
574 {
575 return isAvailable( ) ? _service.getMassActions( nIdWorkflow ) : null;
576 }
577
578
579
580
581
582
583
584
585
586
587
588 public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId,
589 HttpServletRequest request, boolean bIsAutomatic )
590 {
591 if ( isAvailable( ) )
592 {
593 if ( _service.canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId ) )
594 {
595 if ( bIsAutomatic )
596 {
597 return true;
598 }
599
600 return _provider.canProcessAction( nIdAction, request );
601 }
602 }
603
604 return false;
605 }
606
607
608
609
610
611
612
613
614
615
616
617 public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState,
618 Integer nIdExternalParent, Locale locale )
619 {
620 if ( isAvailable( ) )
621 {
622 TransactionManager.beginTransaction( null );
623
624 try
625 {
626 _service.doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent,
627 locale );
628 TransactionManager.commitTransaction( null );
629 }
630 catch ( Exception e )
631 {
632 TransactionManager.rollBack( null );
633 throw new AppException( e.getMessage( ), e );
634 }
635 }
636 }
637 }