AdminMessage.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.Locale;
  39. import java.util.Map;

  40. /**
  41.  * The class provides a bean to hold message box informations
  42.  */
  43. public class AdminMessage implements Serializable
  44. {
  45.     public static final int TYPE_INFO = 0;
  46.     public static final int TYPE_QUESTION = 1;
  47.     public static final int TYPE_ERROR = 2;
  48.     public static final int TYPE_WARNING = 3;
  49.     public static final int TYPE_CONFIRMATION = 4;
  50.     public static final int TYPE_STOP = 5;
  51.     private static final long serialVersionUID = 1924932226627941151L;
  52.     private String _strTextKey;
  53.     private String _strTitleKey;
  54.     private String _strUrl;
  55.     private String _strTarget;
  56.     private int _nType;
  57.     private boolean _bCancel;
  58.     private Object [ ] _messageArgs;
  59.     private Map<String, Object> _requestParameters;
  60.     private String _strBackUrl;

  61.     /**
  62.      * Creates a new instance of AppMessage
  63.      *
  64.      * @param strTextKey
  65.      *            The message Key
  66.      * @param strUrl
  67.      *            The default Button URL
  68.      * @param messageArgs
  69.      *            The message arguments
  70.      * @param strTitleKey
  71.      *            The Title key
  72.      * @param strTarget
  73.      *            The target
  74.      * @param nType
  75.      *            The message Type
  76.      * @param bCancelButton
  77.      *            Add a Cancel Button
  78.      */
  79.     public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton )
  80.     {
  81.         buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, null, null );
  82.     }

  83.     /**
  84.      * Creates a new instance of AppMessage with request parameters
  85.      *
  86.      * @param strTextKey
  87.      *            The message Key
  88.      * @param strUrl
  89.      *            The default Button URL
  90.      * @param messageArgs
  91.      *            The message arguments
  92.      * @param strTitleKey
  93.      *            The Title key
  94.      * @param strTarget
  95.      *            The target
  96.      * @param nType
  97.      *            The message Type
  98.      * @param bCancelButton
  99.      *            Add a Cancel Button
  100.      * @param requestParameters
  101.      *            The Request parameters in a map
  102.      */
  103.     public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton,
  104.             Map<String, Object> requestParameters )
  105.     {
  106.         buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, requestParameters, null );
  107.     }

  108.     /**
  109.      * Creates a new instance of AppMessage with request parameters
  110.      *
  111.      * @param strTextKey
  112.      *            The message Key
  113.      * @param strUrl
  114.      *            The default Button URL
  115.      * @param messageArgs
  116.      *            The message arguments
  117.      * @param strTitleKey
  118.      *            The Title key
  119.      * @param strTarget
  120.      *            The target
  121.      * @param nType
  122.      *            The message Type
  123.      * @param bCancelButton
  124.      *            Add a Cancel Button
  125.      * @param requestParameters
  126.      *            The Request parameters in a map
  127.      * @param strBackUrl
  128.      *            the back url
  129.      */
  130.     public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton,
  131.             Map<String, Object> requestParameters, String strBackUrl )
  132.     {
  133.         buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, requestParameters, strBackUrl );
  134.     }

  135.     /**
  136.      * Build a new admin message
  137.      *
  138.      * @param strTextKey
  139.      *            The message Key
  140.      * @param strUrl
  141.      *            The default Button URL
  142.      * @param messageArgs
  143.      *            The message arguments
  144.      * @param strTitleKey
  145.      *            The Title key
  146.      * @param strTarget
  147.      *            The target
  148.      * @param nType
  149.      *            The message Type
  150.      * @param bCancelButton
  151.      *            Add a Cancel Button
  152.      * @param requestParameters
  153.      *            The Request parameters in a map
  154.      * @param strBackUrl
  155.      *            the back url
  156.      */
  157.     private void buildAdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType,
  158.             boolean bCancelButton, Map<String, Object> requestParameters, String strBackUrl )
  159.     {
  160.         _strTextKey = strTextKey;
  161.         _strTitleKey = strTitleKey;
  162.         _strUrl = strUrl;
  163.         _strTarget = strTarget;
  164.         _nType = nType;
  165.         _bCancel = bCancelButton;
  166.         _messageArgs = messageArgs;
  167.         _requestParameters = requestParameters;
  168.         _strBackUrl = strBackUrl;
  169.     }

  170.     /**
  171.      * Return the type of message
  172.      *
  173.      * @return the type message
  174.      */
  175.     public int getType( )
  176.     {
  177.         return _nType;
  178.     }

  179.     /**
  180.      * Return if the cancel button is display
  181.      *
  182.      * @return true if the cancel button is display
  183.      */
  184.     public boolean isCancel( )
  185.     {
  186.         return _bCancel;
  187.     }

  188.     /**
  189.      * Set the display of cancel button
  190.      *
  191.      * @param bCancel
  192.      *            the new bCancel
  193.      */
  194.     public void setCancel( boolean bCancel )
  195.     {
  196.         _bCancel = bCancel;
  197.     }

  198.     /**
  199.      * Returns the localized text of the message
  200.      *
  201.      * @param locale
  202.      *            The current locale
  203.      * @return The localized text of the message
  204.      */
  205.     public String getText( Locale locale )
  206.     {
  207.         String strText = I18nService.getLocalizedString( _strTextKey, locale );

  208.         if ( _messageArgs != null )
  209.         {
  210.             strText = MessageFormat.format( strText, _messageArgs );
  211.         }

  212.         return strText;
  213.     }

  214.     /**
  215.      * Returns the localized text of the message
  216.      *
  217.      * @param locale
  218.      *            The current locale
  219.      * @return The localized text of the message
  220.      */
  221.     public String getTitle( Locale locale )
  222.     {
  223.         return I18nService.getLocalizedString( _strTitleKey, locale );
  224.     }

  225.     /**
  226.      * Returns the Url of the message box Ok button
  227.      *
  228.      * @return the Url of the Ok button
  229.      */
  230.     public String getUrl( )
  231.     {
  232.         return _strUrl;
  233.     }

  234.     /**
  235.      * Returns the Url of the message box Ok button
  236.      *
  237.      * @return the Url of the Ok button
  238.      */
  239.     public String getTarget( )
  240.     {
  241.         return _strTarget;
  242.     }

  243.     /**
  244.      * Return the request parameters
  245.      *
  246.      * @return the request parameters
  247.      */
  248.     public Map<String, Object> getRequestParameters( )
  249.     {
  250.         return _requestParameters;
  251.     }

  252.     /**
  253.      * set the back url
  254.      *
  255.      * @param strBackUrl
  256.      *            the back url
  257.      */
  258.     public void setBackUrl( String strBackUrl )
  259.     {
  260.         this._strBackUrl = strBackUrl;
  261.     }

  262.     /**
  263.      *
  264.      * @return back url
  265.      */
  266.     public String getBackUrl( )
  267.     {
  268.         return _strBackUrl;
  269.     }
  270. }