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
56 public final class AdminMessageService
57 {
58 private static final String ATTRIBUTE_MESSAGE = "LUTECE_ADMIN_MESSAGE";
59 private static final String JSP_ADMIN_MESSAGE = "jsp/admin/AdminMessage.jsp";
60 private static final String JSP_BACK = "javascript:history.go(-1)";
61 private static final String TARGET_SELF = "_self";
62 private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
63 private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
64 private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
65 private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
66 private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
67 private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
68 private static final String TEMPLATE_FORMAT_LIST = "admin/util/message_list.html";
69 private static final String MARK_MESSAGES_LIST = "messages_list";
70 private static final String TEMPLATE_ERRORS_LIST = "admin/util/errors_list.html";
71 private static final String MARK_ERRORS_LIST = "errors_list";
72
73
74
75
76 private AdminMessageService( )
77 {
78 }
79
80
81
82
83
84
85
86 public static String getMessageUrl( HttpServletRequest request, String strMessageKey )
87 {
88 return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_INFO );
89 }
90
91
92
93
94
95
96
97
98 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, int nMessageType )
99 {
100 return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, nMessageType );
101 }
102
103
104
105
106
107
108
109
110
111 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
112 int nMessageType )
113 {
114 return getMessageUrl( request, strMessageKey, messageArgs, null, JSP_BACK, TARGET_SELF, nMessageType );
115 }
116
117
118
119
120
121
122
123
124
125 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl,
126 String strTarget )
127 {
128 return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, AdminMessage.TYPE_INFO );
129 }
130
131
132
133
134
135
136
137
138
139
140 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl,
141 String strTarget, int nMessageType )
142 {
143 return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, nMessageType );
144 }
145
146
147
148
149
150
151
152
153
154 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl,
155 int nMessageType )
156 {
157 return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType );
158 }
159
160
161
162
163
164
165
166
167
168
169 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl,
170 int nMessageType, Map requestParameters )
171 {
172 return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType, requestParameters );
173 }
174
175
176
177
178
179
180
181
182
183
184 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
185 String strUrl, int nMessageType )
186 {
187 return getMessageUrl( request, strMessageKey, messageArgs, null, strUrl, "", nMessageType );
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
202 String strTitleKey, String strUrl, String strTarget, int nMessageType )
203 {
204 return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
220 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters )
221 {
222 return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType,
223 requestParameters, null );
224 }
225
226
227
228
229
230
231
232
233
234 public static <T> String getMessageUrl( HttpServletRequest request, String strMessageKey,
235 Set<ConstraintViolation<T>> constraintViolations )
236 {
237 return getMessageUrl( request, strMessageKey, formatConstraintViolations( request, constraintViolations ),
238 null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_ERROR );
239 }
240
241
242
243
244
245
246
247
248 public static String getMessageUrl( HttpServletRequest request, String strMessageKey,
249 List<?extends ErrorMessage> errors )
250 {
251 return getMessageUrl( request, strMessageKey, formatValidationErrors( request, errors ), null, JSP_BACK,
252 TARGET_SELF, AdminMessage.TYPE_ERROR );
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,
270 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters,
271 String strBackUrl )
272 {
273 String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
274 boolean bCancelButton = getCancelButton( nMessageType );
275 AdminMessage message = new AdminMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType,
276 bCancelButton, requestParameters, strBackUrl );
277 setMessage( request, message );
278
279 return getUrl( request );
280 }
281
282
283
284
285
286
287 public static AdminMessage getMessage( HttpServletRequest request )
288 {
289 HttpSession session = request.getSession( true );
290 AdminMessage message = (AdminMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
291
292 return message;
293 }
294
295
296
297
298
299
300 public static String getMessageRelativeUrl( )
301 {
302 return JSP_ADMIN_MESSAGE;
303 }
304
305
306
307
308
309
310 private static void setMessage( HttpServletRequest request, AdminMessage message )
311 {
312 HttpSession session = request.getSession( true );
313 session.setAttribute( ATTRIBUTE_MESSAGE, message );
314 }
315
316
317
318
319
320
321 private static String getUrl( HttpServletRequest request )
322 {
323 return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
324 }
325
326
327
328
329
330
331 private static String getDefaultTitle( int nMessageType )
332 {
333 String strTitleKey;
334
335 switch ( nMessageType )
336 {
337 case AdminMessage.TYPE_QUESTION:
338 strTitleKey = PROPERTY_TITLE_QUESTION;
339
340 break;
341
342 case AdminMessage.TYPE_ERROR:
343 strTitleKey = PROPERTY_TITLE_ERROR;
344
345 break;
346
347 case AdminMessage.TYPE_WARNING:
348 strTitleKey = PROPERTY_TITLE_WARNING;
349
350 break;
351
352 case AdminMessage.TYPE_CONFIRMATION:
353 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
354
355 break;
356
357 case AdminMessage.TYPE_STOP:
358 strTitleKey = PROPERTY_TITLE_STOP;
359
360 break;
361
362 default:
363 strTitleKey = PROPERTY_TITLE_DEFAULT;
364
365 break;
366 }
367
368 return strTitleKey;
369 }
370
371
372
373
374
375
376 private static boolean getCancelButton( int nMessageType )
377 {
378 boolean bCancel;
379
380 switch ( nMessageType )
381 {
382 case AdminMessage.TYPE_QUESTION:
383 case AdminMessage.TYPE_CONFIRMATION:
384 bCancel = true;
385
386 break;
387
388 default:
389 bCancel = false;
390
391 break;
392 }
393
394 return bCancel;
395 }
396
397
398
399
400
401
402
403 public static String getFormattedList( List<String> list, Locale locale )
404 {
405 Map<String, List<String>> model = new HashMap<String, List<String>>( );
406 model.put( MARK_MESSAGES_LIST, list );
407
408 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORMAT_LIST, locale, model );
409
410 return template.getHtml( );
411 }
412
413
414
415
416
417
418
419
420 private static <T> Object[] formatConstraintViolations( HttpServletRequest request,
421 Set<ConstraintViolation<T>> constraintViolations )
422 {
423 Map<String, Object> model = new HashMap<String, Object>( );
424 model.put( MARK_ERRORS_LIST, constraintViolations );
425
426 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
427 String[] formatedErrors = { template.getHtml( ) };
428
429 return formatedErrors;
430 }
431
432
433
434
435
436
437
438
439 private static <T> Object[] formatValidationErrors( HttpServletRequest request, List<?extends ErrorMessage> errors )
440 {
441 Map<String, Object> model = new HashMap<String, Object>( );
442 model.put( MARK_ERRORS_LIST, errors );
443
444 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
445 String[] formatedErrors = { template.getHtml( ) };
446
447 return formatedErrors;
448 }
449 }