SiteMessageService.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.i18n.I18nService;
  36. import fr.paris.lutece.util.url.UrlItem;

  37. import java.util.Map;

  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpSession;

  40. /**
  41.  * This class provides a service that build messages
  42.  */
  43. public final class SiteMessageService
  44. {
  45.     private static final String ATTRIBUTE_MESSAGE = "LUTECE_PORTAL_MESSAGE";
  46.     private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
  47.     private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
  48.     private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
  49.     private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
  50.     private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
  51.     private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
  52.     private static final String PARAMETER_SITE_MESSAGE = "sitemessage";
  53.     private static final String PARAMETER_SITE_MESSAGE_VALUE = "true";

  54.     /**
  55.      * Private constructor
  56.      */
  57.     private SiteMessageService( )
  58.     {
  59.     }

  60.     /**
  61.      * Set the INFO message, store it in session and throw a LuteceSiteMessageException
  62.      *
  63.      * @param request
  64.      *            The HttpRequest
  65.      * @param strMessageKey
  66.      *            The message key
  67.      * @throws SiteMessageException
  68.      *             occurs when a site message need to be displayed
  69.      */
  70.     public static void setMessage( HttpServletRequest request, String strMessageKey ) throws SiteMessageException
  71.     {
  72.         setMessage( request, strMessageKey, null, null, null, null, SiteMessage.TYPE_INFO );
  73.     }

  74.     /**
  75.      * Set the message, store it in session and throw a LuteceSiteMessageException
  76.      *
  77.      * @param request
  78.      *            The HttpRequest
  79.      * @param strMessageKey
  80.      *            The message key
  81.      * @param nMessageType
  82.      *            The message type
  83.      * @throws SiteMessageException
  84.      *             occurs when a site message need to be displayed
  85.      */
  86.     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType ) throws SiteMessageException
  87.     {
  88.         setMessage( request, strMessageKey, null, null, null, null, nMessageType );
  89.     }

  90.     /**
  91.      * Set the message, store it in session and throw a LuteceSiteMessageException
  92.      *
  93.      * @param request
  94.      *            The HttpRequest
  95.      * @param strMessageKey
  96.      *            The message key
  97.      * @param nMessageType
  98.      *            The message type
  99.      * @param strUrl
  100.      *            The url of the Ok button
  101.      * @throws SiteMessageException
  102.      *             occurs when a site message need to be displayed
  103.      */
  104.     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl ) throws SiteMessageException
  105.     {
  106.         setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType );
  107.     }

  108.     /**
  109.      * Set the message, store it in session and throw a LuteceSiteMessageException
  110.      *
  111.      * @param request
  112.      *            The HttpRequest
  113.      * @param strMessageKey
  114.      *            The message key
  115.      * @param nMessageType
  116.      *            The message type
  117.      * @param strUrl
  118.      *            The url of the Ok button
  119.      * @param requestParameters
  120.      *            The request parameters as a map
  121.      * @throws SiteMessageException
  122.      *             occurs when a site message need to be displayed
  123.      */
  124.     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl, Map<String, Object> requestParameters )
  125.             throws SiteMessageException
  126.     {
  127.         setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType, requestParameters );
  128.     }

  129.     /**
  130.      * Set the message, store it in session and throw a LuteceSiteMessageException
  131.      *
  132.      * @param request
  133.      *            The HttpRequest
  134.      * @param strMessageKey
  135.      *            The message key
  136.      * @param strTitleKey
  137.      *            The title key
  138.      * @param nMessageType
  139.      *            The message type
  140.      * @throws SiteMessageException
  141.      *             occurs when a site message need to be displayed
  142.      */
  143.     public static void setMessage( HttpServletRequest request, String strMessageKey, String strTitleKey, int nMessageType ) throws SiteMessageException
  144.     {
  145.         setMessage( request, strMessageKey, null, strTitleKey, null, null, nMessageType );
  146.     }

  147.     /**
  148.      * Set the message, store it in session and throw a LuteceSiteMessageException
  149.      *
  150.      * @param request
  151.      *            The HttpRequest
  152.      * @param strMessageKey
  153.      *            The message key
  154.      * @param messageArgs
  155.      *            Message Arguments
  156.      * @param strTitleKey
  157.      *            The title key
  158.      * @param nMessageType
  159.      *            The message type
  160.      * @throws SiteMessageException
  161.      *             occurs when a site message need to be displayed
  162.      */
  163.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, int nMessageType )
  164.             throws SiteMessageException
  165.     {
  166.         setMessage( request, strMessageKey, messageArgs, strTitleKey, null, null, nMessageType );
  167.     }

  168.     /**
  169.      * Set the message, store it in session and throw a LuteceSiteMessageException
  170.      *
  171.      * @param request
  172.      *            The HttpRequest
  173.      * @param strMessageKey
  174.      *            The message key
  175.      * @param messageArgs
  176.      *            Message Arguments
  177.      * @param nMessageType
  178.      *            The message type
  179.      * @throws SiteMessageException
  180.      *             occurs when a site message need to be displayed
  181.      */
  182.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType ) throws SiteMessageException
  183.     {
  184.         setMessage( request, strMessageKey, messageArgs, null, null, null, nMessageType );
  185.     }

  186.     /**
  187.      * Set the message, store it in session and throw a LuteceSiteMessageException
  188.      *
  189.      * @param request
  190.      *            The HttpRequest
  191.      * @param strMessageKey
  192.      *            The message key
  193.      * @param messageArgs
  194.      *            Message arguments
  195.      * @param strTitleKey
  196.      *            The title key
  197.      * @param nMessageType
  198.      *            The message type
  199.      * @param strUrl
  200.      *            The URL
  201.      * @throws SiteMessageException
  202.      *             occurs when a site message need to be displayed
  203.      */
  204.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType, String strUrl,
  205.             String strTitleKey ) throws SiteMessageException
  206.     {
  207.         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
  208.     }

  209.     /**
  210.      * Set the message, store it in session and throw a LuteceSiteMessageException
  211.      *
  212.      * @param request
  213.      *            The HttpRequest
  214.      * @param strMessageKey
  215.      *            The message key
  216.      * @param messageArgs
  217.      *            Message Arguments
  218.      * @param strUrl
  219.      *            The forward URL
  220.      * @param strTitleKey
  221.      *            The title key
  222.      * @param nMessageType
  223.      *            The message type
  224.      * @throws SiteMessageException
  225.      *             occurs when a site message need to be displayed
  226.      */
  227.     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl, String strTitleKey,
  228.             Object [ ] messageArgs ) throws SiteMessageException
  229.     {
  230.         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
  231.     }

  232.     /**
  233.      * Set the message, store it in session and throw a LuteceSiteMessageException
  234.      *
  235.      * @param request
  236.      *            The HttpRequest
  237.      * @param strMessageKey
  238.      *            The message key
  239.      * @param messageArgs
  240.      *            Message Arguments
  241.      * @param strTitleKey
  242.      *            The title key
  243.      * @param strUrl
  244.      *            The Url of the Ok button
  245.      * @param strTarget
  246.      *            The url target if not "_self"
  247.      * @param nMessageType
  248.      *            The message type
  249.      * @throws SiteMessageException
  250.      *             occurs when a site message need to be displayed
  251.      */
  252.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  253.             String strTarget, int nMessageType ) throws SiteMessageException
  254.     {
  255.         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
  256.     }

  257.     /**
  258.      * Set the message, store it in session and throw a LuteceSiteMessageException
  259.      *
  260.      * @param request
  261.      *            The HttpRequest
  262.      * @param strMessageKey
  263.      *            The message key
  264.      * @param messageArgs
  265.      *            Message Arguments
  266.      * @param strTitleKey
  267.      *            The title key
  268.      * @param strUrl
  269.      *            The Url of the Ok button
  270.      * @param strTarget
  271.      *            The url target if not "_self"
  272.      * @param nMessageType
  273.      *            The message type
  274.      * @param requestParameters
  275.      *            The request parameters
  276.      * @throws SiteMessageException
  277.      *             occurs when a site message need to be displayed
  278.      */
  279.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  280.             String strTarget, int nMessageType, Map<String, Object> requestParameters ) throws SiteMessageException
  281.     {
  282.         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, requestParameters, null );
  283.     }

  284.     /**
  285.      * Set the message, store it in session and throw a LuteceSiteMessageException
  286.      *
  287.      * @param request
  288.      *            The HttpRequest
  289.      * @param strMessageKey
  290.      *            The message key
  291.      * @param messageArgs
  292.      *            Message Arguments
  293.      * @param strTitleKey
  294.      *            The title key
  295.      * @param strUrl
  296.      *            The Url of the Ok button
  297.      * @param strTarget
  298.      *            The url target if not "_self"
  299.      * @param nMessageType
  300.      *            The message type
  301.      * @param requestParameters
  302.      *            The request parameters
  303.      * @param strBackUrl
  304.      *            The Url of back button
  305.      * @throws SiteMessageException
  306.      *             occurs when a site message need to be displayed
  307.      */
  308.     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
  309.             String strTarget, int nMessageType, Map<String, Object> requestParameters, String strBackUrl ) throws SiteMessageException
  310.     {
  311.         String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
  312.         SiteMessage message = new SiteMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType, getTypeButton( nMessageType, strUrl ),
  313.                 requestParameters, strBackUrl );
  314.         setMessage( request, message );

  315.         throw new SiteMessageException( );
  316.     }

  317.     /**
  318.      * Set the custom message, store it in session and throw a LuteceSiteMessageException
  319.      *
  320.      * @param request
  321.      *            The HttpRequest
  322.      * @param title
  323.      *            The title
  324.      * @param strUrl
  325.      *            The Url of the Ok button
  326.      * @param text
  327.      *            The message
  328.      * @param nMessageType
  329.      *            The message type
  330.      * @param strBackUrl
  331.      *            The Url of back button
  332.      * @throws SiteMessageException
  333.      */
  334.     public static void setCustomMessage( HttpServletRequest request, String title, String text, String strUrl, int nMessageType, String strBackUrl )
  335.             throws SiteMessageException
  336.     {
  337.         String strTitle = title != null ? title : I18nService.getLocalizedString( getDefaultTitle( nMessageType ), request.getLocale( ) );
  338.         SiteMessage message = new CustomSiteMessage( strTitle, text, strUrl, nMessageType, getTypeButton( nMessageType, strBackUrl ), strBackUrl );

  339.         setMessage( request, message );

  340.         throw new SiteMessageException( );
  341.     }

  342.     /**
  343.      * Set the custom message, store it in session and throw a LuteceSiteMessageException
  344.      *
  345.      * @param request
  346.      *            The HttpRequest
  347.      * @param strText
  348.      *            The message
  349.      * @param nMessageType
  350.      *            The message type
  351.      * @throws SiteMessageException
  352.      */
  353.     public static void setCustomMessage( HttpServletRequest request, String strText, int nMessageType ) throws SiteMessageException
  354.     {
  355.         setCustomMessage( request, null, strText, null, nMessageType, null );
  356.     }

  357.     /**
  358.      * Set the custom message, store it in session and throw a LuteceSiteMessageException
  359.      *
  360.      * @param request
  361.      *            The HttpRequest
  362.      * @param strText
  363.      *            The message
  364.      * @param strTitle
  365.      *            The title
  366.      * @param nMessageType
  367.      *            The message type
  368.      * @throws SiteMessageException
  369.      */
  370.     public static void setCustomMessage( HttpServletRequest request, String strTitle, String strText, int nMessageType ) throws SiteMessageException
  371.     {
  372.         setCustomMessage( request, strTitle, strText, null, nMessageType, null );
  373.     }

  374.     /**
  375.      * Returns the message associated to the current request
  376.      *
  377.      * @param request
  378.      *            The HttpRequest
  379.      * @return The message associated to the current request
  380.      */
  381.     public static SiteMessage getMessage( HttpServletRequest request )
  382.     {
  383.         HttpSession session = request.getSession( true );
  384.         return (SiteMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
  385.     }

  386.     /**
  387.      * Store a message into the current session
  388.      *
  389.      * @param request
  390.      *            The HTTP request
  391.      * @param message
  392.      *            The message to store
  393.      */
  394.     private static void setMessage( HttpServletRequest request, SiteMessage message )
  395.     {
  396.         HttpSession session = request.getSession( true );
  397.         session.setAttribute( ATTRIBUTE_MESSAGE, message );
  398.     }

  399.     /**
  400.      * Delete the message in session
  401.      *
  402.      * @param request
  403.      *            The HTTP request
  404.      */
  405.     public static void cleanMessageSession( HttpServletRequest request )
  406.     {
  407.         HttpSession session = request.getSession( true );
  408.         session.removeAttribute( ATTRIBUTE_MESSAGE );
  409.     }

  410.     /**
  411.      * Set the site message url with parameters if necessary
  412.      *
  413.      * @param strRequestUrl
  414.      *            The Request url
  415.      * @return The message url
  416.      */
  417.     public static String setSiteMessageUrl( String strRequestUrl )
  418.     {
  419.         UrlItem urlItem = new UrlItem( strRequestUrl );
  420.         urlItem.addParameter( PARAMETER_SITE_MESSAGE, PARAMETER_SITE_MESSAGE_VALUE );

  421.         return urlItem.getUrl( );
  422.     }

  423.     /**
  424.      * Returns a default title for the message box
  425.      *
  426.      * @param nMessageType
  427.      *            The message type
  428.      * @return The default title
  429.      */
  430.     private static String getDefaultTitle( int nMessageType )
  431.     {
  432.         String strTitleKey;

  433.         switch( nMessageType )
  434.         {
  435.             case SiteMessage.TYPE_QUESTION:
  436.                 strTitleKey = PROPERTY_TITLE_QUESTION;

  437.                 break;

  438.             case SiteMessage.TYPE_ERROR:
  439.                 strTitleKey = PROPERTY_TITLE_ERROR;

  440.                 break;

  441.             case SiteMessage.TYPE_WARNING:
  442.                 strTitleKey = PROPERTY_TITLE_WARNING;

  443.                 break;

  444.             case SiteMessage.TYPE_CONFIRMATION:
  445.                 strTitleKey = PROPERTY_TITLE_CONFIRMATION;

  446.                 break;

  447.             case SiteMessage.TYPE_STOP:
  448.                 strTitleKey = PROPERTY_TITLE_STOP;

  449.                 break;

  450.             default:
  451.                 strTitleKey = PROPERTY_TITLE_DEFAULT;

  452.                 break;
  453.         }

  454.         return strTitleKey;
  455.     }

  456.     /**
  457.      * Returns if the cancel button should be displayed or not according the message type
  458.      *
  459.      * @param nMessageType
  460.      *            The message type
  461.      * @param strUrl
  462.      *            The url of the Ok button
  463.      * @return the type of button
  464.      */
  465.     private static int getTypeButton( int nMessageType, String strUrl )
  466.     {
  467.         /*
  468.          * ------------------------------------- * Type url defined no url * TYPE_INFO none back * TYPE_QUESTION cancel back(?) * TYPE_ERROR none back *
  469.          * TYPE_WARNING none back * TYPE_CONFIRMATION cancel back * TYPE_STOP none back * -------------------------------------
  470.          */
  471.         int nTypeButton;

  472.         if ( ( strUrl != null ) && !strUrl.equals( "" ) )
  473.         {
  474.             switch( nMessageType )
  475.             {
  476.                 case SiteMessage.TYPE_QUESTION:
  477.                 case SiteMessage.TYPE_CONFIRMATION:
  478.                     nTypeButton = SiteMessage.TYPE_BUTTON_CANCEL;

  479.                     break;

  480.                 default:
  481.                     nTypeButton = SiteMessage.TYPE_BUTTON_HIDDEN;

  482.                     break;
  483.             }
  484.         }
  485.         else
  486.         {
  487.             nTypeButton = SiteMessage.TYPE_BUTTON_BACK;
  488.         }

  489.         return nTypeButton;
  490.     }
  491. }