SiteMessage.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 java.io.Serializable;

  37. import java.text.MessageFormat;

  38. import java.util.HashMap;
  39. import java.util.Locale;
  40. import java.util.Map;
  41. import java.util.Map.Entry;

  42. /**
  43.  * The class provides a bean to hold message box informations
  44.  */
  45. public class SiteMessage implements Serializable
  46. {
  47.     public static final int TYPE_INFO = 0;
  48.     public static final int TYPE_QUESTION = 1;
  49.     public static final int TYPE_ERROR = 2;
  50.     public static final int TYPE_WARNING = 3;
  51.     public static final int TYPE_CONFIRMATION = 4;
  52.     public static final int TYPE_STOP = 5;
  53.     public static final int TYPE_BUTTON_HIDDEN = 0;
  54.     public static final int TYPE_BUTTON_BACK = 1;
  55.     public static final int TYPE_BUTTON_CANCEL = 2;
  56.     private static final long serialVersionUID = -34775038853250525L;
  57.     private String _strTextKey;
  58.     private String _strTitleKey;
  59.     private String _strUrl;
  60.     private String _strTarget;
  61.     private int _nTypeButton;
  62.     private int _nType;
  63.     private String [ ] _messageArgs;
  64.     private Map<String, String> _requestParameters;
  65.     private String _strBackUrl;

  66.     /**
  67.      *
  68.      * @param strTextKey
  69.      *            I18n key for the message body
  70.      * @param messageArgs
  71.      *            Arguments for the strTextKey or null
  72.      * @param strTitleKey
  73.      *            I18n key for the message title
  74.      * @param strUrl
  75.      *            The url for the Ok button
  76.      * @param strTarget
  77.      *            Target for the form (_blank, _self, ...)
  78.      * @param nType
  79.      *            Message type (TYPE_INFO, TYPE_QUESTION, ...)
  80.      * @param nTypeButton
  81.      *            Type of Cancel button
  82.      * @param requestParameters
  83.      *            Request parameters as a Map
  84.      * @param strBackUrl
  85.      *            the back url
  86.      */
  87.     public SiteMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, int nTypeButton,
  88.             Map<String, Object> requestParameters, String strBackUrl )
  89.     {
  90.         _strTextKey = strTextKey;
  91.         _strTitleKey = strTitleKey;
  92.         _strUrl = strUrl;
  93.         _strTarget = strTarget;
  94.         _nType = nType;
  95.         _nTypeButton = nTypeButton;
  96.         _strBackUrl = strBackUrl;

  97.         // Object message conversion into String values
  98.         if ( messageArgs != null )
  99.         {
  100.             _messageArgs = new String [ messageArgs.length];

  101.             for ( int i = 0; i < messageArgs.length; i++ )
  102.             {
  103.                 _messageArgs [i] = ( messageArgs [i] == null ) ? null : messageArgs [i].toString( );
  104.             }
  105.         }

  106.         // Object message conversion into String values for map of parameters
  107.         if ( requestParameters != null )
  108.         {
  109.             _requestParameters = new HashMap<>( );

  110.             for ( Entry<String, Object> entry : requestParameters.entrySet( ) )
  111.             {
  112.                 _requestParameters.put( entry.getKey( ), ( entry.getValue( ) == null ) ? null : entry.getValue( ).toString( ) );
  113.             }
  114.         }
  115.     }

  116.     /**
  117.      * Get the type of message
  118.      *
  119.      * @return The message type
  120.      */
  121.     public int getType( )
  122.     {
  123.         return _nType;
  124.     }

  125.     /**
  126.      * Returns the localized text of the message
  127.      *
  128.      * @param locale
  129.      *            The current locale
  130.      * @return The localized text of the message
  131.      */
  132.     public String getText( Locale locale )
  133.     {
  134.         String strText = I18nService.getLocalizedString( _strTextKey, locale );

  135.         if ( _messageArgs != null )
  136.         {
  137.             strText = MessageFormat.format( strText, (Object [ ]) _messageArgs );
  138.         }

  139.         return strText;
  140.     }

  141.     /**
  142.      * Returns the localized text of the message
  143.      *
  144.      * @param locale
  145.      *            The current locale
  146.      * @return The localized text of the message
  147.      */
  148.     public String getTitle( Locale locale )
  149.     {
  150.         return I18nService.getLocalizedString( _strTitleKey, locale );
  151.     }

  152.     /**
  153.      * Returns the Url of the message box Ok button
  154.      *
  155.      * @return the Url of the Ok button
  156.      */
  157.     public String getUrl( )
  158.     {
  159.         return _strUrl;
  160.     }

  161.     /**
  162.      * Returns the Url of the message box Ok button
  163.      *
  164.      * @return the Url of the Ok button
  165.      */
  166.     public String getTarget( )
  167.     {
  168.         return _strTarget;
  169.     }

  170.     /**
  171.      *
  172.      * @return type of button
  173.      */
  174.     public int getTypeButton( )
  175.     {
  176.         return _nTypeButton;
  177.     }

  178.     /**
  179.      *
  180.      * @param nTypeButton
  181.      *            The Type of cancel button
  182.      */
  183.     public void setTypeButton( int nTypeButton )
  184.     {
  185.         _nTypeButton = nTypeButton;
  186.     }

  187.     /**
  188.      *
  189.      * @return the request parameters.
  190.      */
  191.     public Map<String, String> getRequestParameters( )
  192.     {
  193.         return _requestParameters;
  194.     }

  195.     /**
  196.      * set the back url
  197.      *
  198.      * @param strBackUrl
  199.      *            the back url
  200.      */
  201.     public void setBackUrl( String strBackUrl )
  202.     {
  203.         this._strBackUrl = strBackUrl;
  204.     }

  205.     /**
  206.      *
  207.      * @return back url
  208.      */
  209.     public String getBackUrl( )
  210.     {
  211.         return _strBackUrl;
  212.     }
  213. }