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.dashboard.admin;
35
36 import fr.paris.lutece.portal.business.dashboard.AdminDashboardFactory;
37 import fr.paris.lutece.portal.business.dashboard.AdminDashboardFilter;
38 import fr.paris.lutece.portal.business.dashboard.AdminDashboardHome;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.service.dashboard.DashboardComponentEntry;
41 import fr.paris.lutece.portal.service.plugin.Plugin;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56
57
58
59 public final class AdminDashboardService
60 {
61
62
63
64 private static final String PROPERTY_COLUMN_COUNT = "admindashboard.columnCount";
65 private static final String ALL = "ALL";
66 private static final int CONSTANTE_FIRST_ORDER = 1;
67 private static final int CONSTANTE_DEFAULT_COLUMN_COUNT = 2;
68 private static AdminDashboardService _singleton = new AdminDashboardService( );
69
70
71
72
73 private AdminDashboardService( )
74 {
75
76 }
77
78
79
80
81
82 public static AdminDashboardService getInstance( )
83 {
84 return _singleton;
85 }
86
87
88
89
90
91 public int getColumnCount( )
92 {
93 return AppPropertiesService.getPropertyInt( PROPERTY_COLUMN_COUNT, CONSTANTE_DEFAULT_COLUMN_COUNT );
94 }
95
96
97
98
99
100 public List<IAdminDashboardComponent> getAllAdminDashboardComponents( )
101 {
102 return AdminDashboardFactory.getAllAdminDashboardComponents( );
103 }
104
105
106
107
108
109
110 public List<IAdminDashboardComponent> getAdminDashboardComponents( int nColumn )
111 {
112 AdminDashboardFilter filter = new AdminDashboardFilter( );
113 filter.setFilterColumn( nColumn );
114
115 List<IAdminDashboardComponent> dashboardComponents = AdminDashboardHome.findByFilter( filter );
116
117 return dashboardComponents;
118 }
119
120
121
122
123
124
125 public void registerDashboardComponent( DashboardComponentEntry entry, Plugin plugin )
126 {
127 try
128 {
129 IAdminDashboardComponent dc = (IAdminDashboardComponent) Class.forName( entry.getComponentClass( ) )
130 .newInstance( );
131
132 dc.setName( entry.getName( ) );
133 dc.setPlugin( plugin );
134
135 boolean bRegistered = AdminDashboardFactory.registerDashboardComponent( dc );
136
137 if ( bRegistered )
138 {
139 AppLogService.info( "New Admin Dashboard Component registered : " + entry.getName( ) );
140 }
141 else
142 {
143 AppLogService.error( " Admin Dashboard Component not registered : " + entry.getName( ) + " : " +
144 entry.getComponentClass( ) );
145 }
146 }
147 catch ( InstantiationException e )
148 {
149 AppLogService.error( "Error registering an Admin DashboardComponent : " + e.getMessage( ), e );
150 }
151 catch ( IllegalAccessException e )
152 {
153 AppLogService.error( "Error registering an Admin DashboardComponent : " + e.getMessage( ), e );
154 }
155 catch ( ClassNotFoundException e )
156 {
157 AppLogService.error( "Error registering an Admin DashboardComponent : " + e.getMessage( ), e );
158 }
159 }
160
161
162
163
164
165
166
167
168 public void doMoveDashboard( IAdminDashboardComponent dashboard, int nOldColumn, int nOldOrder, boolean bCreate )
169 {
170 int nColumn = dashboard.getZone( );
171 int nOrder = dashboard.getOrder( );
172
173
174 AdminDashboardFilter filter = new AdminDashboardFilter( );
175 filter.setFilterColumn( nColumn );
176
177 List<IAdminDashboardComponent> listColumnDashboards = AdminDashboardHome.findByFilter( filter );
178
179 if ( ( listColumnDashboards != null ) && !listColumnDashboards.isEmpty( ) )
180 {
181 if ( AppLogService.isDebugEnabled( ) )
182 {
183 AppLogService.debug( "Reordering admin dashboard column " + dashboard.getZone( ) );
184 }
185
186
187 Collections.sort( listColumnDashboards );
188
189 int nMaxOrder = listColumnDashboards.get( listColumnDashboards.size( ) - 1 ).getOrder( );
190
191 if ( ( nOldColumn == 0 ) || ( nOldColumn != nColumn ) )
192 {
193
194 dashboard.setOrder( nMaxOrder + 1 );
195 }
196 else
197 {
198 if ( nOrder < nOldOrder )
199 {
200 for ( IAdminDashboardComponent dc : listColumnDashboards )
201 {
202 int nCurrentOrder = dc.getOrder( );
203
204 if ( !dc.equals( dashboard ) && ( nCurrentOrder >= nOrder ) && ( nCurrentOrder < nOldOrder ) )
205 {
206 dc.setOrder( nCurrentOrder + 1 );
207 AdminDashboardHome.update( dc );
208 }
209 }
210 }
211 else if ( nOrder > nOldOrder )
212 {
213 for ( IAdminDashboardComponent dc : listColumnDashboards )
214 {
215 int nCurrentOrder = dc.getOrder( );
216
217 if ( !dc.equals( dashboard ) && ( nCurrentOrder <= nOrder ) && ( nCurrentOrder > nOldOrder ) )
218 {
219 dc.setOrder( nCurrentOrder - 1 );
220 AdminDashboardHome.update( dc );
221 }
222 }
223 }
224
225
226 dashboard.setOrder( nOrder );
227 dashboard.setZone( nColumn );
228 }
229 }
230 else
231 {
232 dashboard.setOrder( 1 );
233 }
234
235 if ( bCreate )
236 {
237
238 AdminDashboardHome.create( dashboard );
239 }
240 else
241 {
242
243 AdminDashboardHome.update( dashboard );
244 }
245 }
246
247
248
249
250
251 public List<IAdminDashboardComponent> getNotSetDashboards( )
252 {
253 List<IAdminDashboardComponent> listDashboards = AdminDashboardHome.findAll( );
254 List<IAdminDashboardComponent> listSpringDashboards = getAllAdminDashboardComponents( );
255
256 List<IAdminDashboardComponent> listUnsetDashboards = new ArrayList<IAdminDashboardComponent>( );
257
258 for ( IAdminDashboardComponent dashboard : listSpringDashboards )
259 {
260 if ( !listDashboards.contains( dashboard ) )
261 {
262 listUnsetDashboards.add( dashboard );
263 }
264 }
265
266 return listUnsetDashboards;
267 }
268
269
270
271
272
273 public Map<String, List<IAdminDashboardComponent>> getAllSetDashboards( )
274 {
275 Map<String, List<IAdminDashboardComponent>> mapDashboardComponents = new HashMap<String, List<IAdminDashboardComponent>>( );
276
277 List<IAdminDashboardComponent> listDashboards = AdminDashboardHome.findAll( );
278
279 for ( IAdminDashboardComponent dashboard : listDashboards )
280 {
281 int nColumn = dashboard.getZone( );
282
283 String strColumn = Integer.toString( nColumn );
284
285
286 List<IAdminDashboardComponent> listDashboardsColumn = mapDashboardComponents.get( strColumn );
287
288 if ( listDashboardsColumn == null )
289 {
290
291 listDashboardsColumn = new ArrayList<IAdminDashboardComponent>( );
292 mapDashboardComponents.put( strColumn, listDashboardsColumn );
293 }
294
295
296 listDashboardsColumn.add( dashboard );
297 }
298
299 return mapDashboardComponents;
300 }
301
302
303
304
305
306
307
308
309 public String getDashboardData( AdminUser user, int nColumn, HttpServletRequest request )
310 {
311 StringBuilder sbDashboardData = new StringBuilder( );
312
313 for ( IAdminDashboardComponent dc : getAdminDashboardComponents( nColumn ) )
314 {
315 boolean bRight = ( dc.getRight( ) == null ) || user.checkRight( dc.getRight( ) ) ||
316 dc.getRight( ).equalsIgnoreCase( ALL );
317
318 if ( dc.isEnabled( ) && bRight )
319 {
320 sbDashboardData.append( dc.getDashboardData( user, request ) );
321 }
322 }
323
324 return sbDashboardData.toString( );
325 }
326
327
328
329
330
331 public void doReorderColumn( int nColumn )
332 {
333 int nOrder = CONSTANTE_FIRST_ORDER;
334
335 for ( IAdminDashboardComponent dc : getAdminDashboardComponents( nColumn ) )
336 {
337 dc.setOrder( nOrder++ );
338 AdminDashboardHome.update( dc );
339 }
340 }
341
342
343
344
345
346 public Map<String, Boolean> getOrderedColumnsStatus( )
347 {
348 Map<String, Boolean> mapOrderedStatus = new HashMap<String, Boolean>( );
349 List<Integer> listColumns = AdminDashboardHome.findColumns( );
350
351 for ( Integer nIdColumn : listColumns )
352 {
353 mapOrderedStatus.put( nIdColumn.toString( ), isWellOrdered( nIdColumn ) );
354 }
355
356 return mapOrderedStatus;
357 }
358
359
360
361
362
363
364 private boolean isWellOrdered( int nColumn )
365 {
366 int nOrder = CONSTANTE_FIRST_ORDER;
367
368 for ( IAdminDashboardComponent dc : getAdminDashboardComponents( nColumn ) )
369 {
370 if ( nOrder != dc.getOrder( ) )
371 {
372 return false;
373 }
374
375 nOrder++;
376 }
377
378 return true;
379 }
380 }