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.util.url.UrlItem;
37
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpSession;
42
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 public static void setMessage( HttpServletRequest request, String strMessageKey )
76 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 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType )
92 throws SiteMessageException
93 {
94 setMessage( request, strMessageKey, null, null, null, null, nMessageType );
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl )
109 throws SiteMessageException
110 {
111 setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType );
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl,
127 Map<String, Object> requestParameters ) throws SiteMessageException
128 {
129 setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType, requestParameters );
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 public static void setMessage( HttpServletRequest request, String strMessageKey, String strTitleKey,
144 int nMessageType ) throws SiteMessageException
145 {
146 setMessage( request, strMessageKey, null, strTitleKey, null, null, nMessageType );
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
162 String strTitleKey, int nMessageType ) throws SiteMessageException
163 {
164 setMessage( request, strMessageKey, messageArgs, strTitleKey, null, null, nMessageType );
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
179 int nMessageType ) throws SiteMessageException
180 {
181 setMessage( request, strMessageKey, messageArgs, null, null, null, nMessageType );
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
198 int nMessageType, String strUrl, String strTitleKey )
199 throws SiteMessageException
200 {
201 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl,
218 String strTitleKey, Object[] messageArgs ) 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 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
238 String strTitleKey, String strUrl, String strTarget, int nMessageType )
239 throws SiteMessageException
240 {
241 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
260 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters )
261 throws SiteMessageException
262 {
263 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType,
264 requestParameters, null );
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
284 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters,
285 String strBackUrl ) throws SiteMessageException
286 {
287 String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
288 SiteMessage message = new SiteMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType,
289 getTypeButton( nMessageType, strUrl ), requestParameters, strBackUrl );
290 setMessage( request, message );
291
292 throw new SiteMessageException( );
293 }
294
295
296
297
298
299
300 public static SiteMessage getMessage( HttpServletRequest request )
301 {
302 HttpSession session = request.getSession( true );
303 SiteMessage message = (SiteMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
304
305 return message;
306 }
307
308
309
310
311
312
313 private static void setMessage( HttpServletRequest request, SiteMessage message )
314 {
315 HttpSession session = request.getSession( true );
316 session.setAttribute( ATTRIBUTE_MESSAGE, message );
317 }
318
319
320
321
322
323
324 public static void cleanMessageSession( HttpServletRequest request )
325 {
326 HttpSession session = request.getSession( true );
327 session.removeAttribute( ATTRIBUTE_MESSAGE );
328 }
329
330
331
332
333
334
335
336 public static String setSiteMessageUrl( String strRequestUrl )
337 {
338 UrlItem urlItem = new UrlItem( strRequestUrl );
339 urlItem.addParameter( PARAMETER_SITE_MESSAGE, PARAMETER_SITE_MESSAGE_VALUE );
340
341 return urlItem.getUrl( );
342 }
343
344
345
346
347
348
349 private static String getDefaultTitle( int nMessageType )
350 {
351 String strTitleKey;
352
353 switch ( nMessageType )
354 {
355 case SiteMessage.TYPE_QUESTION:
356 strTitleKey = PROPERTY_TITLE_QUESTION;
357
358 break;
359
360 case SiteMessage.TYPE_ERROR:
361 strTitleKey = PROPERTY_TITLE_ERROR;
362
363 break;
364
365 case SiteMessage.TYPE_WARNING:
366 strTitleKey = PROPERTY_TITLE_WARNING;
367
368 break;
369
370 case SiteMessage.TYPE_CONFIRMATION:
371 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
372
373 break;
374
375 case SiteMessage.TYPE_STOP:
376 strTitleKey = PROPERTY_TITLE_STOP;
377
378 break;
379
380 default:
381 strTitleKey = PROPERTY_TITLE_DEFAULT;
382
383 break;
384 }
385
386 return strTitleKey;
387 }
388
389
390
391
392
393
394
395
396 private static int getTypeButton( int nMessageType, String strUrl )
397 {
398
399
400
401
402
403
404
405
406
407
408
409 int nTypeButton;
410
411 if ( ( strUrl != null ) && !strUrl.equals( "" ) )
412 {
413 switch ( nMessageType )
414 {
415 case SiteMessage.TYPE_QUESTION:
416 case SiteMessage.TYPE_CONFIRMATION:
417 nTypeButton = SiteMessage.TYPE_BUTTON_CANCEL;
418
419 break;
420
421 default:
422 nTypeButton = SiteMessage.TYPE_BUTTON_HIDDEN;
423
424 break;
425 }
426 }
427 else
428 {
429 nTypeButton = SiteMessage.TYPE_BUTTON_BACK;
430 }
431
432 return nTypeButton;
433 }
434 }