View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * This class provides a service that build messages and deliver the Url to display them
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       * Private constructor
74       */
75      private AdminMessageService( )
76      {
77      }
78  
79      /**
80       * Returns the Url that display the given message
81       * 
82       * @param request
83       *            The HttpRequest
84       * @param strMessageKey
85       *            The message key
86       * @return The Url of the JSP that display the message
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       * Returns the Url that display the given message
95       * 
96       * @param request
97       *            The HttpRequest
98       * @param strMessageKey
99       *            The message key
100      * @param nMessageType
101      *            The message type
102      * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message
111      * 
112      * @return The Url of the JSP that display the message
113      * @param messageArgs
114      *            Message arguments
115      * @param request
116      *            The HttpRequest
117      * @param strMessageKey
118      *            The message key
119      * @param nMessageType
120      *            The message type
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      * Returns the Url that display the given message
129      * 
130      * @return The Url of the JSP that display the message
131      * @param strTarget
132      *            Target
133      * @param request
134      *            The HttpRequest
135      * @param strMessageKey
136      *            The message key
137      * @param strUrl
138      *            The Url of the OK button
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      * Returns the Url that display the given message
147      * 
148      * @return The Url of the JSP that display the message
149      * @param strTarget
150      *            The url target if not "_self"
151      * @param request
152      *            The HttpRequest
153      * @param strMessageKey
154      *            The message key
155      * @param strUrl
156      *            The Url of the Ok button
157      * @param nMessageType
158      *            The message type
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      * Returns the Url that display the given message
167      * 
168      * @return The Url of the JSP that display the message
169      * @param request
170      *            The HttpRequest
171      * @param strMessageKey
172      *            The message key
173      * @param strUrl
174      *            The Url of the Ok button
175      * @param nMessageType
176      *            The message type
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      * Returns the Url that display the given message
185      * 
186      * @return The Url of the JSP that display the message
187      * @param request
188      *            The HttpRequest
189      * @param strMessageKey
190      *            The message key
191      * @param strUrl
192      *            The Url of the Ok button
193      * @param nMessageType
194      *            The message type
195      * @param requestParameters
196      *            a collection of parameters
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      * Returns the Url that display the given message
205      * 
206      * @return The Url of the JSP that display the message
207      * @param messageArgs
208      *            Message Arguments
209      * @param request
210      *            The HttpRequest
211      * @param strMessageKey
212      *            The message key
213      * @param strUrl
214      *            The Url of the Ok button
215      * @param nMessageType
216      *            The message type
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      * Returns the Url that display the given message
225      * 
226      * @return The Url of the JSP that display the message
227      * @param messageArgs
228      *            Message Arguments
229      * @param strTarget
230      *            The url target if not "_self"
231      * @param strTitleKey
232      *            The title key
233      * @param nMessageType
234      *            The message type
235      * @param request
236      *            The HttpRequest
237      * @param strMessageKey
238      *            The message key
239      * @param strUrl
240      *            The Url of the Ok button
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      * Returns the Url that display the given message
250      * 
251      * @return The Url of the JSP that display the message
252      * @param messageArgs
253      *            Message Arguments
254      * @param strTarget
255      *            The url target if not "_self"
256      * @param strTitleKey
257      *            The title key
258      * @param nMessageType
259      *            The message type
260      * @param request
261      *            The HttpRequest
262      * @param strMessageKey
263      *            The message key
264      * @param strUrl
265      *            The Url of the Ok button
266      * @param requestParameters
267      *            a collection of parameters
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      * Returns the Url that display the given message
277      * 
278      * @param <T>
279      *            The type of the bean that has been validated
280      * @param request
281      *            The HTTP request
282      * @param strMessageKey
283      *            The message key
284      * @param constraintViolations
285      *            The set of violations
286      * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message
296      * 
297      * @param request
298      *            The HTTP request
299      * @param strMessageKey
300      *            The message key
301      * @param errors
302      *            The set of violations
303      * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message.
312      *
313      * @param request
314      *            The HttpRequest
315      * @param strMessageKey
316      *            The message key
317      * @param messageArgs
318      *            Message Arguments
319      * @param strTitleKey
320      *            The title key
321      * @param strUrl
322      *            The Url of the Ok button
323      * @param strTarget
324      *            The url target if not "_self"
325      * @param nMessageType
326      *            The message type
327      * @param requestParameters
328      *            a collection of parameters
329      * @param strBackUrl
330      *            the str back url
331      * @return The Url of the JSP that display the message
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      * Returns the message associated to the current request
347      * 
348      * @param request
349      *            The HttpRequest
350      * @return The message associated to the current request
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      * Return relative url for the admin message jsp. This method does not generate admin message.
360      * 
361      * @return The relative url
362      */
363     public static String getMessageRelativeUrl( )
364     {
365         return JSP_ADMIN_MESSAGE;
366     }
367 
368     /**
369      * Store a message into the current session
370      * 
371      * @param request
372      *            The HTTP request
373      * @param message
374      *            The message to store
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      * Build the message url
384      * 
385      * @param request
386      *            The HTTP request
387      * @return The Url
388      */
389     private static String getUrl( HttpServletRequest request )
390     {
391         return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
392     }
393 
394     /**
395      * Returns a default title for the message box
396      * 
397      * @param nMessageType
398      *            The message type
399      * @return The default title
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      * Returns if the cancel button should be displayed or not according the message type
443      * 
444      * @param nMessageType
445      *            The message type
446      * @return True if the button should be displayed, otherwise false
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      * Format a list of item to include in a message
471      * 
472      * @param list
473      *            The list of item as string
474      * @param locale
475      *            The current locale
476      * @return The formatted list
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      * Format a set of constraints violations as en error list.
490      * 
491      * @param <T>
492      *            The type of the object
493      * @param request
494      *            The HTTP request
495      * @param constraintViolations
496      *            The set of violations
497      * @return The formatted errors list as an object array
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      * Format a set of constraints violations as en error list.
512      * 
513      * @param <T>
514      *            The type of the bean that has been validated
515      * @param request
516      *            The HTTP request
517      * @param errors
518      *            The set of violations
519      * @return The formatted errors list as an object array
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 }