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.Locale;
43  import java.util.Map;
44  
45  /**
46   * The class provides a bean to hold message box informations
47   */
48  public class AdminMessage implements Serializable
49  {
50      public static final int TYPE_INFO = 0;
51      public static final int TYPE_QUESTION = 1;
52      public static final int TYPE_ERROR = 2;
53      public static final int TYPE_WARNING = 3;
54      public static final int TYPE_CONFIRMATION = 4;
55      public static final int TYPE_STOP = 5;
56      private static final long serialVersionUID = 1924932226627941151L;
57      private String _strTextKey;
58      private String _strTitleKey;
59      private String _strUrl;
60      private String _strTarget;
61      private int _nType;
62      private boolean _bCancel;
63      private Object [ ] _messageArgs;
64      private Map<String, Object> _requestParameters;
65      private String _strBackUrl;
66  
67      /**
68       * Creates a new instance of AppMessage
69       * 
70       * @param strTextKey
71       *            The message Key
72       * @param strUrl
73       *            The default Button URL
74       * @param messageArgs
75       *            The message arguments
76       * @param strTitleKey
77       *            The Title key
78       * @param strTarget
79       *            The target
80       * @param nType
81       *            The message Type
82       * @param bCancelButton
83       *            Add a Cancel Button
84       */
85      public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton )
86      {
87          buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, null, null );
88      }
89  
90      /**
91       * Creates a new instance of AppMessage with request parameters
92       * 
93       * @param strTextKey
94       *            The message Key
95       * @param strUrl
96       *            The default Button URL
97       * @param messageArgs
98       *            The message arguments
99       * @param strTitleKey
100      *            The Title key
101      * @param strTarget
102      *            The target
103      * @param nType
104      *            The message Type
105      * @param bCancelButton
106      *            Add a Cancel Button
107      * @param requestParameters
108      *            The Request parameters in a map
109      */
110     public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton,
111             Map<String, Object> requestParameters )
112     {
113         buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, requestParameters, null );
114     }
115 
116     /**
117      * Creates a new instance of AppMessage with request parameters
118      * 
119      * @param strTextKey
120      *            The message Key
121      * @param strUrl
122      *            The default Button URL
123      * @param messageArgs
124      *            The message arguments
125      * @param strTitleKey
126      *            The Title key
127      * @param strTarget
128      *            The target
129      * @param nType
130      *            The message Type
131      * @param bCancelButton
132      *            Add a Cancel Button
133      * @param requestParameters
134      *            The Request parameters in a map
135      * @param strBackUrl
136      *            the back url
137      */
138     public AdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType, boolean bCancelButton,
139             Map<String, Object> requestParameters, String strBackUrl )
140     {
141         buildAdminMessage( strTextKey, messageArgs, strTitleKey, strUrl, strTarget, nType, bCancelButton, requestParameters, strBackUrl );
142     }
143 
144     /**
145      * Build a new admin message
146      *
147      * @param strTextKey
148      *            The message Key
149      * @param strUrl
150      *            The default Button URL
151      * @param messageArgs
152      *            The message arguments
153      * @param strTitleKey
154      *            The Title key
155      * @param strTarget
156      *            The target
157      * @param nType
158      *            The message Type
159      * @param bCancelButton
160      *            Add a Cancel Button
161      * @param requestParameters
162      *            The Request parameters in a map
163      * @param strBackUrl
164      *            the back url
165      */
166     private void buildAdminMessage( String strTextKey, Object [ ] messageArgs, String strTitleKey, String strUrl, String strTarget, int nType,
167             boolean bCancelButton, Map<String, Object> requestParameters, String strBackUrl )
168     {
169         _strTextKey = strTextKey;
170         _strTitleKey = strTitleKey;
171         _strUrl = strUrl;
172         _strTarget = strTarget;
173         _nType = nType;
174         _bCancel = bCancelButton;
175         _messageArgs = messageArgs;
176         _requestParameters = requestParameters;
177         _strBackUrl = strBackUrl;
178     }
179 
180     /**
181      * Return the type of message
182      * 
183      * @return the type message
184      */
185     public int getType( )
186     {
187         return _nType;
188     }
189 
190     /**
191      * Return if the cancel button is display
192      * 
193      * @return true if the cancel button is display
194      */
195     public boolean isCancel( )
196     {
197         return _bCancel;
198     }
199 
200     /**
201      * Set the display of cancel button
202      * 
203      * @param bCancel
204      *            the new bCancel
205      */
206     public void setCancel( boolean bCancel )
207     {
208         _bCancel = bCancel;
209     }
210 
211     /**
212      * Returns the localized text of the message
213      * 
214      * @param locale
215      *            The current locale
216      * @return The localized text of the message
217      */
218     public String getText( Locale locale )
219     {
220         String strText = I18nService.getLocalizedString( _strTextKey, locale );
221 
222         if ( _messageArgs != null )
223         {
224             strText = MessageFormat.format( strText, _messageArgs );
225         }
226 
227         return strText;
228     }
229 
230     /**
231      * Returns the localized text of the message
232      * 
233      * @param locale
234      *            The current locale
235      * @return The localized text of the message
236      */
237     public String getTitle( Locale locale )
238     {
239         return I18nService.getLocalizedString( _strTitleKey, locale );
240     }
241 
242     /**
243      * Returns the Url of the message box Ok button
244      * 
245      * @return the Url of the Ok button
246      */
247     public String getUrl( )
248     {
249         return _strUrl;
250     }
251 
252     /**
253      * Returns the Url of the message box Ok button
254      * 
255      * @return the Url of the Ok button
256      */
257     public String getTarget( )
258     {
259         return _strTarget;
260     }
261 
262     /**
263      * Return the request parameters
264      * 
265      * @return the request parameters
266      */
267     public Map<String, Object> getRequestParameters( )
268     {
269         return _requestParameters;
270     }
271 
272     /**
273      * set the back url
274      * 
275      * @param strBackUrl
276      *            the back url
277      */
278     public void setBackUrl( String strBackUrl )
279     {
280         this._strBackUrl = strBackUrl;
281     }
282 
283     /**
284      *
285      * @return back url
286      */
287     public String getBackUrl( )
288     {
289         return _strBackUrl;
290     }
291 }