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.user.menu;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.ListIterator;
39 import java.util.function.Consumer;
40 import java.util.stream.Collectors;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import fr.paris.lutece.portal.business.user.menu.AdminUserMenuItem;
45 import fr.paris.lutece.portal.business.user.menu.IAdminUserMenuItemProvider;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47
48
49
50
51
52
53 public class AdminUserMenuService
54 {
55 public static final String BEAN_NAME = "adminUserMenuService";
56
57 private final List<IAdminUserMenuItemProvider> _itemProviders = new ArrayList<>( );
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public void addItemProvider( IAdminUserMenuItemProvider itemProvider, String strAfterName, String strBeforeName )
72 {
73 if ( strAfterName != null )
74 {
75 addItemProviderInPosition( itemProvider, strAfterName, ( ListIterator<IAdminUserMenuItemProvider> i ) -> i.add( itemProvider ) );
76 }
77 else
78 if ( strBeforeName != null )
79 {
80 addItemProviderInPosition( itemProvider, strBeforeName, ( ListIterator<IAdminUserMenuItemProvider> i ) -> {
81 if ( i.hasPrevious( ) )
82 {
83 i.previous( );
84 i.add( itemProvider );
85 }
86 else
87 {
88 _itemProviders.add( 0, itemProvider );
89 }
90 } );
91 }
92 else
93 {
94 _itemProviders.add( itemProvider );
95 }
96 AppLogService.info( "New admin user menu item provider registered : {}", itemProvider.getName( ) );
97 }
98
99 private void addItemProviderInPosition( IAdminUserMenuItemProvider itemProvider, String strRefName,
100 Consumer<ListIterator<IAdminUserMenuItemProvider>> adder )
101 {
102 ListIterator<IAdminUserMenuItemProvider> iterator = _itemProviders.listIterator( );
103 boolean bFound = false;
104 while ( iterator.hasNext( ) )
105 {
106 IAdminUserMenuItemProvider anItem = iterator.next( );
107 if ( anItem.getName( ).equals( strRefName ) )
108 {
109 bFound = true;
110 adder.accept( iterator );
111 break;
112 }
113 }
114 if ( !bFound )
115 {
116 AppLogService.error( "Did not find admin user menu item provider named <{}> when registering <{}>", strRefName, itemProvider.getName( ) );
117 _itemProviders.add( itemProvider );
118 }
119 }
120
121
122
123
124
125
126
127
128 public List<AdminUserMenuItem> getItems( final HttpServletRequest request )
129 {
130 return _itemProviders.stream( ).filter( p -> p.isInvoked( request ) ).map( p -> p.getItem( request ) ).collect( Collectors.toList( ) );
131 }
132 }