AdminMessageService.java

  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. import fr.paris.lutece.portal.service.template.AppTemplateService;
  36. import fr.paris.lutece.portal.service.util.AppPathService;
  37. import fr.paris.lutece.util.ErrorMessage;
  38. import fr.paris.lutece.util.html.HtmlTemplate;

  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Locale;
  42. import java.util.Map;
  43. import java.util.Set;

  44. import javax.servlet.http.HttpServletRequest;
  45. import javax.servlet.http.HttpSession;

  46. import javax.validation.ConstraintViolation;

  47. /**
  48.  * This class provides a service that build messages and deliver the Url to display them
  49.  */
  50. public final class AdminMessageService
  51. {
  52.     private static final String ATTRIBUTE_MESSAGE = "LUTECE_ADMIN_MESSAGE";
  53.     private static final String JSP_ADMIN_MESSAGE = "jsp/admin/AdminMessage.jsp";
  54.     private static final String JSP_BACK = "javascript:history.go(-1)";
  55.     private static final String TARGET_SELF = "_self";
  56.     private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
  57.     private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
  58.     private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
  59.     private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
  60.     private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
  61.     private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
  62.     private static final String TEMPLATE_FORMAT_LIST = "admin/util/message_list.html";
  63.     private static final String MARK_MESSAGES_LIST = "messages_list";
  64.     private static final String TEMPLATE_ERRORS_LIST = "admin/util/errors_list.html";
  65.     private static final String MARK_ERRORS_LIST = "errors_list";

  66.     /**
  67.      * Private constructor
  68.      */
  69.     private AdminMessageService( )
  70.     {
  71.     }

  72.     /**
  73.      * Returns the Url that display the given message
  74.      *
  75.      * @param request
  76.      *            The HttpRequest
  77.      * @param strMessageKey
  78.      *            The message key
  79.      * @return The Url of the JSP that display the message
  80.      */
  81.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey )
  82.     {
  83.         return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_INFO );
  84.     }

  85.     /**
  86.      * Returns the Url that display the given message
  87.      *
  88.      * @param request
  89.      *            The HttpRequest
  90.      * @param strMessageKey
  91.      *            The message key
  92.      * @param nMessageType
  93.      *            The message type
  94.      * @return The Url of the JSP that display the message
  95.      */
  96.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, int nMessageType )
  97.     {
  98.         return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, nMessageType );
  99.     }

  100.     /**
  101.      * Returns the Url that display the given message
  102.      *
  103.      * @return The Url of the JSP that display the message
  104.      * @param messageArgs
  105.      *            Message arguments
  106.      * @param request
  107.      *            The HttpRequest
  108.      * @param strMessageKey
  109.      *            The message key
  110.      * @param nMessageType
  111.      *            The message type
  112.      */
  113.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType )
  114.     {
  115.         return getMessageUrl( request, strMessageKey, messageArgs, null, JSP_BACK, TARGET_SELF, nMessageType );
  116.     }

  117.     /**
  118.      * Returns the Url that display the given message
  119.      *
  120.      * @return The Url of the JSP that display the message
  121.      * @param strTarget
  122.      *            Target
  123.      * @param request
  124.      *            The HttpRequest
  125.      * @param strMessageKey
  126.      *            The message key
  127.      * @param strUrl
  128.      *            The Url of the OK button
  129.      */
  130.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget )
  131.     {
  132.         return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, AdminMessage.TYPE_INFO );
  133.     }

  134.     /**
  135.      * Returns the Url that display the given message
  136.      *
  137.      * @return The Url of the JSP that display the message
  138.      * @param strTarget
  139.      *            The url target if not "_self"
  140.      * @param request
  141.      *            The HttpRequest
  142.      * @param strMessageKey
  143.      *            The message key
  144.      * @param strUrl
  145.      *            The Url of the Ok button
  146.      * @param nMessageType
  147.      *            The message type
  148.      */
  149.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget, int nMessageType )
  150.     {
  151.         return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, nMessageType );
  152.     }

  153.     /**
  154.      * Returns the Url that display the given message
  155.      *
  156.      * @return The Url of the JSP that display the message
  157.      * @param request
  158.      *            The HttpRequest
  159.      * @param strMessageKey
  160.      *            The message key
  161.      * @param strUrl
  162.      *            The Url of the Ok button
  163.      * @param nMessageType
  164.      *            The message type
  165.      */
  166.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType )
  167.     {
  168.         return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType );
  169.     }

  170.     /**
  171.      * Returns the Url that display the given message
  172.      *
  173.      * @return The Url of the JSP that display the message
  174.      * @param request
  175.      *            The HttpRequest
  176.      * @param strMessageKey
  177.      *            The message key
  178.      * @param strUrl
  179.      *            The Url of the Ok button
  180.      * @param nMessageType
  181.      *            The message type
  182.      * @param requestParameters
  183.      *            a collection of parameters
  184.      */
  185.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType, Map requestParameters )
  186.     {
  187.         return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType, requestParameters );
  188.     }

  189.     /**
  190.      * Returns the Url that display the given message
  191.      *
  192.      * @return The Url of the JSP that display the message
  193.      * @param messageArgs
  194.      *            Message Arguments
  195.      * @param request
  196.      *            The HttpRequest
  197.      * @param strMessageKey
  198.      *            The message key
  199.      * @param strUrl
  200.      *            The Url of the Ok button
  201.      * @param nMessageType
  202.      *            The message type
  203.      */
  204.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strUrl, int nMessageType )
  205.     {
  206.         return getMessageUrl( request, strMessageKey, messageArgs, null, strUrl, "", nMessageType );
  207.     }

  208.     /**
  209.      * Returns the Url that display the given message
  210.      *
  211.      * @return The Url of the JSP that display the message
  212.      * @param messageArgs
  213.      *            Message Arguments
  214.      * @param strTarget
  215.      *            The url target if not "_self"
  216.      * @param strTitleKey
  217.      *            The title key
  218.      * @param nMessageType
  219.      *            The message type
  220.      * @param request
  221.      *            The HttpRequest
  222.      * @param strMessageKey
  223.      *            The message key
  224.      * @param strUrl
  225.      *            The Url of the Ok button
  226.      */
  227.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  228.             String strTarget, int nMessageType )
  229.     {
  230.         return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
  231.     }

  232.     /**
  233.      * Returns the Url that display the given message
  234.      *
  235.      * @return The Url of the JSP that display the message
  236.      * @param messageArgs
  237.      *            Message Arguments
  238.      * @param strTarget
  239.      *            The url target if not "_self"
  240.      * @param strTitleKey
  241.      *            The title key
  242.      * @param nMessageType
  243.      *            The message type
  244.      * @param request
  245.      *            The HttpRequest
  246.      * @param strMessageKey
  247.      *            The message key
  248.      * @param strUrl
  249.      *            The Url of the Ok button
  250.      * @param requestParameters
  251.      *            a collection of parameters
  252.      */
  253.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  254.             String strTarget, int nMessageType, Map<String, Object> requestParameters )
  255.     {
  256.         return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, requestParameters, null );
  257.     }

  258.     /**
  259.      * Returns the Url that display the given message
  260.      *
  261.      * @param <T>
  262.      *            The type of the bean that has been validated
  263.      * @param request
  264.      *            The HTTP request
  265.      * @param strMessageKey
  266.      *            The message key
  267.      * @param constraintViolations
  268.      *            The set of violations
  269.      * @return The Url of the JSP that display the message
  270.      */
  271.     public static <T> String getMessageUrl( HttpServletRequest request, String strMessageKey, Set<ConstraintViolation<T>> constraintViolations )
  272.     {
  273.         return getMessageUrl( request, strMessageKey, formatConstraintViolations( request, constraintViolations ), null, JSP_BACK, TARGET_SELF,
  274.                 AdminMessage.TYPE_ERROR );
  275.     }

  276.     /**
  277.      * Returns the Url that display the given message
  278.      *
  279.      * @param request
  280.      *            The HTTP request
  281.      * @param strMessageKey
  282.      *            The message key
  283.      * @param errors
  284.      *            The set of violations
  285.      * @return The Url of the JSP that display the message
  286.      */
  287.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, List<? extends ErrorMessage> errors )
  288.     {
  289.         return getMessageUrl( request, strMessageKey, formatValidationErrors( request, errors ), null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_ERROR );
  290.     }

  291.     /**
  292.      * Returns the Url that display the given message.
  293.      *
  294.      * @param request
  295.      *            The HttpRequest
  296.      * @param strMessageKey
  297.      *            The message key
  298.      * @param messageArgs
  299.      *            Message Arguments
  300.      * @param strTitleKey
  301.      *            The title key
  302.      * @param strUrl
  303.      *            The Url of the Ok button
  304.      * @param strTarget
  305.      *            The url target if not "_self"
  306.      * @param nMessageType
  307.      *            The message type
  308.      * @param requestParameters
  309.      *            a collection of parameters
  310.      * @param strBackUrl
  311.      *            the str back url
  312.      * @return The Url of the JSP that display the message
  313.      */
  314.     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  315.             String strTarget, int nMessageType, Map<String, Object> requestParameters, String strBackUrl )
  316.     {
  317.         String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
  318.         boolean bCancelButton = getCancelButton( nMessageType );
  319.         AdminMessage message = new AdminMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType, bCancelButton, requestParameters,
  320.                 strBackUrl );
  321.         setMessage( request, message );

  322.         return getUrl( request );
  323.     }

  324.     /**
  325.      * Returns the message associated to the current request
  326.      *
  327.      * @param request
  328.      *            The HttpRequest
  329.      * @return The message associated to the current request
  330.      */
  331.     public static AdminMessage getMessage( HttpServletRequest request )
  332.     {
  333.         HttpSession session = request.getSession( true );
  334.         return (AdminMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
  335.     }

  336.     /**
  337.      * Return relative url for the admin message jsp. This method does not generate admin message.
  338.      *
  339.      * @return The relative url
  340.      */
  341.     public static String getMessageRelativeUrl( )
  342.     {
  343.         return JSP_ADMIN_MESSAGE;
  344.     }

  345.     /**
  346.      * Store a message into the current session
  347.      *
  348.      * @param request
  349.      *            The HTTP request
  350.      * @param message
  351.      *            The message to store
  352.      */
  353.     private static void setMessage( HttpServletRequest request, AdminMessage message )
  354.     {
  355.         HttpSession session = request.getSession( true );
  356.         session.setAttribute( ATTRIBUTE_MESSAGE, message );
  357.     }

  358.     /**
  359.      * Build the message url
  360.      *
  361.      * @param request
  362.      *            The HTTP request
  363.      * @return The Url
  364.      */
  365.     private static String getUrl( HttpServletRequest request )
  366.     {
  367.         return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
  368.     }

  369.     /**
  370.      * Returns a default title for the message box
  371.      *
  372.      * @param nMessageType
  373.      *            The message type
  374.      * @return The default title
  375.      */
  376.     private static String getDefaultTitle( int nMessageType )
  377.     {
  378.         String strTitleKey;

  379.         switch( nMessageType )
  380.         {
  381.             case AdminMessage.TYPE_QUESTION:
  382.                 strTitleKey = PROPERTY_TITLE_QUESTION;

  383.                 break;

  384.             case AdminMessage.TYPE_ERROR:
  385.                 strTitleKey = PROPERTY_TITLE_ERROR;

  386.                 break;

  387.             case AdminMessage.TYPE_WARNING:
  388.                 strTitleKey = PROPERTY_TITLE_WARNING;

  389.                 break;

  390.             case AdminMessage.TYPE_CONFIRMATION:
  391.                 strTitleKey = PROPERTY_TITLE_CONFIRMATION;

  392.                 break;

  393.             case AdminMessage.TYPE_STOP:
  394.                 strTitleKey = PROPERTY_TITLE_STOP;

  395.                 break;

  396.             default:
  397.                 strTitleKey = PROPERTY_TITLE_DEFAULT;

  398.                 break;
  399.         }

  400.         return strTitleKey;
  401.     }

  402.     /**
  403.      * Returns if the cancel button should be displayed or not according the message type
  404.      *
  405.      * @param nMessageType
  406.      *            The message type
  407.      * @return True if the button should be displayed, otherwise false
  408.      */
  409.     private static boolean getCancelButton( int nMessageType )
  410.     {
  411.         boolean bCancel;

  412.         switch( nMessageType )
  413.         {
  414.             case AdminMessage.TYPE_QUESTION:
  415.             case AdminMessage.TYPE_CONFIRMATION:
  416.                 bCancel = true;

  417.                 break;

  418.             default:
  419.                 bCancel = false;

  420.                 break;
  421.         }

  422.         return bCancel;
  423.     }

  424.     /**
  425.      * Format a list of item to include in a message
  426.      *
  427.      * @param list
  428.      *            The list of item as string
  429.      * @param locale
  430.      *            The current locale
  431.      * @return The formatted list
  432.      */
  433.     public static String getFormattedList( List<String> list, Locale locale )
  434.     {
  435.         Map<String, List<String>> model = new HashMap<>( );
  436.         model.put( MARK_MESSAGES_LIST, list );

  437.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORMAT_LIST, locale, model );

  438.         return template.getHtml( );
  439.     }

  440.     /**
  441.      * Format a set of constraints violations as en error list.
  442.      *
  443.      * @param <T>
  444.      *            The type of the object
  445.      * @param request
  446.      *            The HTTP request
  447.      * @param constraintViolations
  448.      *            The set of violations
  449.      * @return The formatted errors list as an object array
  450.      */
  451.     private static <T> Object [ ] formatConstraintViolations( HttpServletRequest request, Set<ConstraintViolation<T>> constraintViolations )
  452.     {
  453.         Map<String, Object> model = new HashMap<>( );
  454.         model.put( MARK_ERRORS_LIST, constraintViolations );

  455.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
  456.         return new String [ ] {
  457.                 template.getHtml( )
  458.         };
  459.     }

  460.     /**
  461.      * Format a set of constraints violations as en error list.
  462.      *
  463.      * @param <T>
  464.      *            The type of the bean that has been validated
  465.      * @param request
  466.      *            The HTTP request
  467.      * @param errors
  468.      *            The set of violations
  469.      * @return The formatted errors list as an object array
  470.      */
  471.     private static Object [ ] formatValidationErrors( HttpServletRequest request, List<? extends ErrorMessage> errors )
  472.     {
  473.         Map<String, Object> model = new HashMap<>( );
  474.         model.put( MARK_ERRORS_LIST, errors );

  475.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
  476.         return new String [ ] {
  477.                 template.getHtml( )
  478.         };
  479.     }
  480. }