View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  /**
54   * This class provides a service that build messages and deliver the Url to display them
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       * Private constructor
75       */
76      private AdminMessageService(  )
77      {
78      }
79  
80      /**
81       * Returns the Url that display the given message
82       * @param request The HttpRequest
83       * @param strMessageKey The message key
84       * @return The Url of the JSP that display the message
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       * Returns the Url that display the given message
93       * @param request The HttpRequest
94       * @param strMessageKey The message key
95       * @param nMessageType The message type
96       * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message
105      * @return The Url of the JSP that display the message
106      * @param messageArgs Message arguments
107      * @param request The HttpRequest
108      * @param strMessageKey The message key
109      * @param nMessageType The message type
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      * Returns the Url that display the given message
119      * @return The Url of the JSP that display the message
120      * @param strTarget Target
121      * @param request The HttpRequest
122      * @param strMessageKey The message key
123      * @param strUrl The Url of the OK button
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      * Returns the Url that display the given message
133      * @return The Url of the JSP that display the message
134      * @param strTarget The url target if not "_self"
135      * @param request The HttpRequest
136      * @param strMessageKey The message key
137      * @param strUrl The Url of the Ok button
138      * @param nMessageType The message type
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      * Returns the Url that display the given message
148      * @return The Url of the JSP that display the message
149      * @param request The HttpRequest
150      * @param strMessageKey The message key
151      * @param strUrl The Url of the Ok button
152      * @param nMessageType The message type
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      * Returns the Url that display the given message
162      * @return The Url of the JSP that display the message
163      * @param request The HttpRequest
164      * @param strMessageKey The message key
165      * @param strUrl The Url of the Ok button
166      * @param nMessageType The message type
167      * @param requestParameters a collection of parameters
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      * Returns the Url that display the given message
177      * @return The Url of the JSP that display the message
178      * @param messageArgs Message Arguments
179      * @param request The HttpRequest
180      * @param strMessageKey The message key
181      * @param strUrl The Url of the Ok button
182      * @param nMessageType The message type
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      * Returns the Url that display the given message
192      * @return The Url of the JSP that display the message
193      * @param messageArgs Message Arguments
194      * @param strTarget The url target if not "_self"
195      * @param strTitleKey The title key
196      * @param nMessageType The message type
197      * @param request The HttpRequest
198      * @param strMessageKey The message key
199      * @param strUrl The Url of the Ok button
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      * Returns the Url that display the given message
209      * @return The Url of the JSP that display the message
210      * @param messageArgs Message Arguments
211      * @param strTarget The url target if not "_self"
212      * @param strTitleKey The title key
213      * @param nMessageType The message type
214      * @param request The HttpRequest
215      * @param strMessageKey The message key
216      * @param strUrl The Url of the Ok button
217      * @param requestParameters a collection of parameters
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      * Returns the Url that display the given message
228      * @param <T> The type of the bean that has been validated
229      * @param request The HTTP request
230      * @param strMessageKey The message key
231      * @param constraintViolations The set of violations
232      * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message
243      * @param request The HTTP request
244      * @param strMessageKey The message key
245      * @param errors The set of violations
246      * @return The Url of the JSP that display the message
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      * Returns the Url that display the given message.
257      *
258      * @param request The HttpRequest
259      * @param strMessageKey The message key
260      * @param messageArgs Message Arguments
261      * @param strTitleKey The title key
262      * @param strUrl The Url of the Ok button
263      * @param strTarget The url target if not "_self"
264      * @param nMessageType The message type
265      * @param requestParameters a collection of parameters
266      * @param strBackUrl the str back url
267      * @return The Url of the JSP that display the message
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      * Returns the message associated to the current request
284      * @param request The HttpRequest
285      * @return The message associated to the current request
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      * Return relative url for the admin message jsp.
297      * This method does not generate admin message.
298      * @return The relative url
299      */
300     public static String getMessageRelativeUrl(  )
301     {
302         return JSP_ADMIN_MESSAGE;
303     }
304 
305     /**
306      * Store a message into the current session
307      * @param request The HTTP request
308      * @param message The message to store
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      * Build the message url
318      * @param request The HTTP request
319      * @return The Url
320      */
321     private static String getUrl( HttpServletRequest request )
322     {
323         return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
324     }
325 
326     /**
327      * Returns a default title for the message box
328      * @param nMessageType The message type
329      * @return The default title
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      * Returns if the cancel button should be displayed or not according the message type
373      * @param nMessageType The message type
374      * @return True if the button should be displayed, otherwise false
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      * Format a list of item to include in a message
399      * @param list The list of item as string
400      * @param locale The current locale
401      * @return The formatted list
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      * Format a set of constraints violations as en error list.
415      * @param <T> The type of the object
416      * @param request The HTTP request
417      * @param constraintViolations The set of violations
418      * @return The formatted errors list as an object array
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      * Format a set of constraints violations as en error list.
434      * @param <T> The type of the bean that has been validated
435      * @param request The HTTP request
436      * @param errors The set of violations
437      * @return The formatted errors list as an object array
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 }