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.web.dashboard;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41
42 import org.apache.commons.lang3.StringUtils;
43
44 import fr.paris.lutece.portal.business.dashboard.AdminDashboardFactory;
45 import fr.paris.lutece.portal.business.dashboard.AdminDashboardHome;
46 import fr.paris.lutece.portal.business.user.AdminUser;
47 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
48 import fr.paris.lutece.portal.service.admin.AdminUserService;
49 import fr.paris.lutece.portal.service.dashboard.admin.AdminDashboardService;
50 import fr.paris.lutece.portal.service.dashboard.admin.IAdminDashboardComponent;
51 import fr.paris.lutece.portal.service.message.AdminMessage;
52 import fr.paris.lutece.portal.service.message.AdminMessageService;
53 import fr.paris.lutece.portal.service.security.SecurityTokenService;
54 import fr.paris.lutece.portal.service.template.AppTemplateService;
55 import fr.paris.lutece.portal.service.util.AppLogService;
56 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
57 import fr.paris.lutece.portal.web.constants.Messages;
58 import fr.paris.lutece.util.ReferenceList;
59 import fr.paris.lutece.util.html.HtmlTemplate;
60 import fr.paris.lutece.util.string.StringUtil;
61
62
63
64
65
66 public class AdminDashboardJspBean extends AdminFeaturesPageJspBean
67 {
68 private static final long serialVersionUID = 5312273100074724058L;
69
70 public static final String TEMPLATE_MANAGE_DASHBOARDS = "/admin/dashboard/admin/manage_dashboards.html";
71
72
73 public static final String RIGHT_MANAGE_ADMINDASHBOARD = "CORE_ADMINDASHBOARD_MANAGEMENT";
74
75
76 private static final String PARAMETER_DASHBOARD_NAME = "dashboard_name";
77 private static final String PARAMETER_DASHBOARD_COLUMN = "dashboard_column";
78 private static final String PARAMETER_DASHBOARD_ORDER = "dashboard_order";
79 private static final String PARAMETER_COLUMN = "column";
80
81
82 private static final String MESSAGE_DASHBOARD_NOT_FOUND = "portal.admindashboard.message.dashboardNotFound";
83
84
85 private static final String MARK_COLUMN_CONTENT_PREFIX = "column_content_";
86 private static final String MARK_MAP_DASHBOARDS = "map_dashboards";
87 private static final String MARK_NOT_SET_DASHBOARDS = "not_set_dashboards";
88 private static final String MARK_COLUMN_COUNT = "column_count";
89 private static final String MARK_LIST_AVAILABLE_COLUMNS = "list_available_columns";
90 private static final String MARK_MAP_AVAILABLE_ORDERS = "map_available_orders";
91 private static final String MARK_MAP_COLUMN_ORDER_STATUS = "map_column_order_status";
92
93
94 private static final String TEMPLATE_VIEW_DASHBOARDS = "/admin/dashboard/admin/view_dashboards.html";
95
96
97 private static final String JSP_MANAGE_DASHBOARDS = "ManageAdminDashboards.jsp";
98 private static final String EMPTY_STRING = "";
99 private transient AdminDashboardService _service = AdminDashboardService.getInstance( );
100
101
102
103
104
105
106
107
108 public String getAdminDashboards( HttpServletRequest request )
109 {
110 AdminUser user = AdminUserService.getAdminUser( request );
111 Map<String, Object> model = new HashMap<>( );
112
113
114 for ( int nColumn = 1; nColumn <= getAdminDashboardService( ).getColumnCount( ); nColumn++ )
115 {
116 String strColumnData = getAdminDashboardService( ).getDashboardData( user, nColumn, request );
117
118 model.put( MARK_COLUMN_CONTENT_PREFIX + nColumn, strColumnData );
119 }
120
121 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_DASHBOARDS, user.getLocale( ), model );
122
123 return template.getHtml( );
124 }
125
126
127
128
129
130
131
132
133 public String getManageDashboards( HttpServletRequest request )
134 {
135 AdminUser user = AdminUserService.getAdminUser( request );
136
137 Map<String, Object> model = new HashMap<>( );
138
139 Map<String, List<IAdminDashboardComponent>> mapAdminDashboards = getAdminDashboardService( ).getAllSetDashboards( );
140 model.put( MARK_MAP_DASHBOARDS, mapAdminDashboards );
141
142 List<IAdminDashboardComponent> listNotSetDashboards = getAdminDashboardService( ).getNotSetDashboards( );
143 model.put( MARK_NOT_SET_DASHBOARDS, listNotSetDashboards );
144
145 model.put( MARK_COLUMN_COUNT, getAdminDashboardService( ).getColumnCount( ) );
146 model.put( MARK_MAP_AVAILABLE_ORDERS, getMapAvailableOrders( ) );
147 model.put( MARK_LIST_AVAILABLE_COLUMNS, getListAvailableColumns( ) );
148 model.put( MARK_MAP_COLUMN_ORDER_STATUS, getAdminDashboardService( ).getOrderedColumnsStatus( ) );
149 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_DASHBOARDS ) );
150
151 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DASHBOARDS, user.getLocale( ), model );
152
153 return getAdminPage( template.getHtml( ) );
154 }
155
156
157
158
159
160
161
162
163
164
165 public String doReorderColumn( HttpServletRequest request ) throws AccessDeniedException
166 {
167 String strColumnName = request.getParameter( PARAMETER_COLUMN );
168
169 if ( StringUtils.isBlank( strColumnName ) )
170 {
171 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
172 }
173
174 int nColumn = 0;
175
176 try
177 {
178 nColumn = Integer.parseInt( strColumnName );
179 }
180 catch( NumberFormatException nfe )
181 {
182 AppLogService.error( "AdminDashboardJspBean.doReorderColumn : {}", nfe.getMessage( ), nfe );
183
184 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
185 }
186 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
187 {
188 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
189 }
190 getAdminDashboardService( ).doReorderColumn( nColumn );
191
192 return JSP_MANAGE_DASHBOARDS;
193 }
194
195
196
197
198
199
200
201
202
203
204 public String doMoveAdminDashboard( HttpServletRequest request ) throws AccessDeniedException
205 {
206 String strDashboardName = request.getParameter( PARAMETER_DASHBOARD_NAME );
207
208 if ( StringUtils.isBlank( strDashboardName ) )
209 {
210 return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
211 }
212
213
214 IAdminDashboardComponent dashboard = AdminDashboardHome.findByPrimaryKey( strDashboardName );
215 int nOldOrder = 0;
216 int nOldColumn = 0;
217 boolean bCreate = false;
218
219 if ( dashboard == null )
220 {
221 bCreate = true;
222
223 if ( AppLogService.isDebugEnabled( ) )
224 {
225 AppLogService.debug( "Dashboard " + strDashboardName + " has no property set. Retrieving from SpringContext" );
226 }
227
228 dashboard = AdminDashboardFactory.getDashboardComponent( strDashboardName );
229
230 if ( dashboard == null )
231 {
232 return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
233 }
234 }
235 else
236 {
237 nOldOrder = dashboard.getOrder( );
238 nOldColumn = dashboard.getZone( );
239 }
240 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
241 {
242 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
243 }
244
245
246 String strOrder = request.getParameter( PARAMETER_DASHBOARD_ORDER );
247 String strColumn = request.getParameter( PARAMETER_DASHBOARD_COLUMN );
248
249 int nOrder = StringUtil.getIntValue( strOrder, -1 );
250 int nColumn = StringUtil.getIntValue( strColumn, -1 );
251
252 dashboard.setOrder( nOrder );
253 dashboard.setZone( nColumn );
254
255 getAdminDashboardService( ).doMoveDashboard( dashboard, nOldColumn, nOldOrder, bCreate );
256
257 return JSP_MANAGE_DASHBOARDS;
258 }
259
260
261
262
263
264
265 private ReferenceList getListAvailableColumns( )
266 {
267 ReferenceListList.html#ReferenceList">ReferenceList refList = new ReferenceList( );
268
269
270 refList.addItem( EMPTY_STRING, EMPTY_STRING );
271
272 for ( int nColumnIndex = 1; nColumnIndex <= getAdminDashboardService( ).getColumnCount( ); nColumnIndex++ )
273 {
274 refList.addItem( nColumnIndex, Integer.toString( nColumnIndex ) );
275 }
276
277 return refList;
278 }
279
280
281
282
283
284
285 private Map<String, ReferenceList> getMapAvailableOrders( )
286 {
287 Map<String, ReferenceList> mapAvailableOrders = new HashMap<>( );
288
289
290 for ( Integer nColumn : AdminDashboardHome.findColumns( ) )
291 {
292
293 mapAvailableOrders.put( nColumn.toString( ), getListAvailableOrders( nColumn ) );
294 }
295
296 return mapAvailableOrders;
297 }
298
299
300
301
302
303
304
305
306 private ReferenceList getListAvailableOrders( int nColumn )
307 {
308 ReferenceListList.html#ReferenceList">ReferenceList refList = new ReferenceList( );
309
310
311 refList.addItem( EMPTY_STRING, EMPTY_STRING );
312
313 int nMaxOrder = AdminDashboardHome.findMaxOrder( nColumn );
314
315 for ( int nOrder = 1; nOrder <= nMaxOrder; nOrder++ )
316 {
317 refList.addItem( nOrder, Integer.toString( nOrder ) );
318 }
319
320 return refList;
321 }
322
323 private AdminDashboardService getAdminDashboardService( )
324 {
325 if ( _service == null )
326 {
327 _service = AdminDashboardService.getInstance( );
328 }
329 return _service;
330 }
331 }