View Javadoc
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  
36  import fr.paris.lutece.portal.service.i18n.I18nService;
37  
38  import java.io.Serializable;
39  
40  import java.text.MessageFormat;
41  
42  import java.util.HashMap;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.Map.Entry;
46  
47  /**
48   * The class provides a bean to hold message box informations
49   */
50  public class SiteMessage implements Serializable
51  {
52      public static final int TYPE_INFO = 0;
53      public static final int TYPE_QUESTION = 1;
54      public static final int TYPE_ERROR = 2;
55      public static final int TYPE_WARNING = 3;
56      public static final int TYPE_CONFIRMATION = 4;
57      public static final int TYPE_STOP = 5;
58      public static final int TYPE_BUTTON_HIDDEN = 0;
59      public static final int TYPE_BUTTON_BACK = 1;
60      public static final int TYPE_BUTTON_CANCEL = 2;
61      private static final long serialVersionUID = -34775038853250525L;
62      private String _strTextKey;
63      private String _strTitleKey;
64      private String _strUrl;
65      private String _strTarget;
66      private int _nTypeButton;
67      private int _nType;
68      private String [ ] _messageArgs;
69      private Map<String, String> _requestParameters;
70      private String _strBackUrl;
71  
72      /**
73       *
74       * @param strTextKey
75       *            I18n key for the message body
76       * @param messageArgs
77       *            Arguments for the strTextKey or null
78       * @param strTitleKey
79       *            I18n key for the message title
80       * @param strUrl
81       *            The url for the Ok button
82       * @param strTarget
83       *            Target for the form (_blank, _self, ...)
84       * @param nType
85       *            Message type (TYPE_INFO, TYPE_QUESTION, ...)
86       * @param nTypeButton
87       *            Type of Cancel button
88       * @param requestParameters
89       *            Request parameters as a Map
90       * @param strBackUrl
91       *            the back url
92       */
93      public SiteMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, int nTypeButton,
94              Map<String, Object> requestParameters, String strBackUrl )
95      {
96          _strTextKey = strTextKey;
97          _strTitleKey = strTitleKey;
98          _strUrl = strUrl;
99          _strTarget = strTarget;
100         _nType = nType;
101         _nTypeButton = nTypeButton;
102         _strBackUrl = strBackUrl;
103 
104         // Object message conversion into String values
105         if ( messageArgs != null )
106         {
107             _messageArgs = new String [ messageArgs.length];
108 
109             for ( int i = 0; i < messageArgs.length; i++ )
110             {
111                 _messageArgs [i] = ( messageArgs [i] == null ) ? null : messageArgs [i].toString( );
112             }
113         }
114 
115         // Object message conversion into String values for map of parameters
116         if ( requestParameters != null )
117         {
118             _requestParameters = new HashMap<>( );
119 
120             for ( Entry<String, Object> entry : requestParameters.entrySet( ) )
121             {
122                 _requestParameters.put( entry.getKey( ), ( entry.getValue( ) == null ) ? null : entry.getValue( ).toString( ) );
123             }
124         }
125     }
126 
127     /**
128      * Get the type of message
129      *
130      * @return The message type
131      */
132     public int getType( )
133     {
134         return _nType;
135     }
136 
137     /**
138      * Returns the localized text of the message
139      * 
140      * @param locale
141      *            The current locale
142      * @return The localized text of the message
143      */
144     public String getText( Locale locale )
145     {
146         String strText = I18nService.getLocalizedString( _strTextKey, locale );
147 
148         if ( _messageArgs != null )
149         {
150             strText = MessageFormat.format( strText, (Object [ ]) _messageArgs );
151         }
152 
153         return strText;
154     }
155 
156     /**
157      * Returns the localized text of the message
158      * 
159      * @param locale
160      *            The current locale
161      * @return The localized text of the message
162      */
163     public String getTitle( Locale locale )
164     {
165         return I18nService.getLocalizedString( _strTitleKey, locale );
166     }
167 
168     /**
169      * Returns the Url of the message box Ok button
170      * 
171      * @return the Url of the Ok button
172      */
173     public String getUrl( )
174     {
175         return _strUrl;
176     }
177 
178     /**
179      * Returns the Url of the message box Ok button
180      * 
181      * @return the Url of the Ok button
182      */
183     public String getTarget( )
184     {
185         return _strTarget;
186     }
187 
188     /**
189      *
190      * @return type of button
191      */
192     public int getTypeButton( )
193     {
194         return _nTypeButton;
195     }
196 
197     /**
198      *
199      * @param nTypeButton
200      *            The Type of cancel button
201      */
202     public void setTypeButton( int nTypeButton )
203     {
204         _nTypeButton = nTypeButton;
205     }
206 
207     /**
208      *
209      * @return the request parameters.
210      */
211     public Map<String, String> getRequestParameters( )
212     {
213         return _requestParameters;
214     }
215 
216     /**
217      * set the back url
218      * 
219      * @param strBackUrl
220      *            the back url
221      */
222     public void setBackUrl( String strBackUrl )
223     {
224         this._strBackUrl = strBackUrl;
225     }
226 
227     /**
228      *
229      * @return back url
230      */
231     public String getBackUrl( )
232     {
233         return _strBackUrl;
234     }
235 }