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