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