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.message;
35
36 import fr.paris.lutece.portal.service.template.AppTemplateService;
37 import fr.paris.lutece.portal.service.util.AppPathService;
38 import fr.paris.lutece.util.ErrorMessage;
39 import fr.paris.lutece.util.html.HtmlTemplate;
40
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Locale;
44 import java.util.Map;
45 import java.util.Set;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpSession;
49
50 import javax.validation.ConstraintViolation;
51
52
53
54
55 public final class AdminMessageService
56 {
57 private static final String ATTRIBUTE_MESSAGE = "LUTECE_ADMIN_MESSAGE";
58 private static final String JSP_ADMIN_MESSAGE = "jsp/admin/AdminMessage.jsp";
59 private static final String JSP_BACK = "javascript:history.go(-1)";
60 private static final String TARGET_SELF = "_self";
61 private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
62 private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
63 private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
64 private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
65 private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
66 private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
67 private static final String TEMPLATE_FORMAT_LIST = "admin/util/message_list.html";
68 private static final String MARK_MESSAGES_LIST = "messages_list";
69 private static final String TEMPLATE_ERRORS_LIST = "admin/util/errors_list.html";
70 private static final String MARK_ERRORS_LIST = "errors_list";
71
72
73
74
75 private AdminMessageService( )
76 {
77 }
78
79
80
81
82
83
84
85
86
87
88 public static String getMessageUrl( HttpServletRequest request, String strMessageKey )
89 {
90 return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_INFO );
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, int nMessageType )
105 {
106 return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, nMessageType );
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType )
123 {
124 return getMessageUrl( request, strMessageKey, messageArgs, null, JSP_BACK, TARGET_SELF, nMessageType );
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget )
141 {
142 return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, AdminMessage.TYPE_INFO );
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget, int nMessageType )
161 {
162 return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, nMessageType );
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType )
179 {
180 return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType );
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType, Map requestParameters )
199 {
200 return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType, requestParameters );
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strUrl, int nMessageType )
219 {
220 return getMessageUrl( request, strMessageKey, messageArgs, null, strUrl, "", nMessageType );
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
243 String strTarget, int nMessageType )
244 {
245 return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
270 String strTarget, int nMessageType, Map<String, Object> requestParameters )
271 {
272 return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, requestParameters, null );
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 public static <T> String getMessageUrl( HttpServletRequest request, String strMessageKey, Set<ConstraintViolation<T>> constraintViolations )
289 {
290 return getMessageUrl( request, strMessageKey, formatConstraintViolations( request, constraintViolations ), null, JSP_BACK, TARGET_SELF,
291 AdminMessage.TYPE_ERROR );
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, List<? extends ErrorMessage> errors )
306 {
307 return getMessageUrl( request, strMessageKey, formatValidationErrors( request, errors ), null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_ERROR );
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
334 String strTarget, int nMessageType, Map<String, Object> requestParameters, String strBackUrl )
335 {
336 String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
337 boolean bCancelButton = getCancelButton( nMessageType );
338 AdminMessagee/message/AdminMessage.html#AdminMessage">AdminMessage message = new AdminMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType, bCancelButton, requestParameters,
339 strBackUrl );
340 setMessage( request, message );
341
342 return getUrl( request );
343 }
344
345
346
347
348
349
350
351
352 public static AdminMessage getMessage( HttpServletRequest request )
353 {
354 HttpSession session = request.getSession( true );
355 return (AdminMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
356 }
357
358
359
360
361
362
363 public static String getMessageRelativeUrl( )
364 {
365 return JSP_ADMIN_MESSAGE;
366 }
367
368
369
370
371
372
373
374
375
376 private static void setMessage( HttpServletRequest request, AdminMessage message )
377 {
378 HttpSession session = request.getSession( true );
379 session.setAttribute( ATTRIBUTE_MESSAGE, message );
380 }
381
382
383
384
385
386
387
388
389 private static String getUrl( HttpServletRequest request )
390 {
391 return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
392 }
393
394
395
396
397
398
399
400
401 private static String getDefaultTitle( int nMessageType )
402 {
403 String strTitleKey;
404
405 switch( nMessageType )
406 {
407 case AdminMessage.TYPE_QUESTION:
408 strTitleKey = PROPERTY_TITLE_QUESTION;
409
410 break;
411
412 case AdminMessage.TYPE_ERROR:
413 strTitleKey = PROPERTY_TITLE_ERROR;
414
415 break;
416
417 case AdminMessage.TYPE_WARNING:
418 strTitleKey = PROPERTY_TITLE_WARNING;
419
420 break;
421
422 case AdminMessage.TYPE_CONFIRMATION:
423 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
424
425 break;
426
427 case AdminMessage.TYPE_STOP:
428 strTitleKey = PROPERTY_TITLE_STOP;
429
430 break;
431
432 default:
433 strTitleKey = PROPERTY_TITLE_DEFAULT;
434
435 break;
436 }
437
438 return strTitleKey;
439 }
440
441
442
443
444
445
446
447
448 private static boolean getCancelButton( int nMessageType )
449 {
450 boolean bCancel;
451
452 switch( nMessageType )
453 {
454 case AdminMessage.TYPE_QUESTION:
455 case AdminMessage.TYPE_CONFIRMATION:
456 bCancel = true;
457
458 break;
459
460 default:
461 bCancel = false;
462
463 break;
464 }
465
466 return bCancel;
467 }
468
469
470
471
472
473
474
475
476
477
478 public static String getFormattedList( List<String> list, Locale locale )
479 {
480 Map<String, List<String>> model = new HashMap<>( );
481 model.put( MARK_MESSAGES_LIST, list );
482
483 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORMAT_LIST, locale, model );
484
485 return template.getHtml( );
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499 private static <T> Object [ ] formatConstraintViolations( HttpServletRequest request, Set<ConstraintViolation<T>> constraintViolations )
500 {
501 Map<String, Object> model = new HashMap<>( );
502 model.put( MARK_ERRORS_LIST, constraintViolations );
503
504 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
505 return new String [ ] {
506 template.getHtml( )
507 };
508 }
509
510
511
512
513
514
515
516
517
518
519
520
521 private static Object [ ] formatValidationErrors( HttpServletRequest request, List<? extends ErrorMessage> errors )
522 {
523 Map<String, Object> model = new HashMap<>( );
524 model.put( MARK_ERRORS_LIST, errors );
525
526 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
527 return new String [ ] {
528 template.getHtml( )
529 };
530 }
531 }