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.util.url.UrlItem;
37
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpSession;
42
43
44 /**
45 * This class provides a service that build messages
46 */
47 public final class SiteMessageService
48 {
49 private static final String ATTRIBUTE_MESSAGE = "LUTECE_PORTAL_MESSAGE";
50 private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
51 private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
52 private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
53 private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
54 private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
55 private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
56 private static final String PARAMETER_SITE_MESSAGE = "sitemessage";
57 private static final String PARAMETER_SITE_MESSAGE_VALUE = "true";
58
59 /**
60 * Private constructor
61 */
62 private SiteMessageService( )
63 {
64 }
65
66 /**
67 * Set the INFO message, store it in session and throw a
68 * LuteceSiteMessageException
69 *
70 * @param request The HttpRequest
71 * @param strMessageKey The message key
72 * @throws SiteMessageException occurs when a site message need to be
73 * displayed
74 */
75 public static void setMessage( HttpServletRequest request, String strMessageKey )
76 throws SiteMessageException
77 {
78 setMessage( request, strMessageKey, null, null, null, null, SiteMessage.TYPE_INFO );
79 }
80
81 /**
82 * Set the message, store it in session and throw a
83 * LuteceSiteMessageException
84 *
85 * @param request The HttpRequest
86 * @param strMessageKey The message key
87 * @param nMessageType The message type
88 * @throws SiteMessageException occurs when a site message need to be
89 * displayed
90 */
91 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType )
92 throws SiteMessageException
93 {
94 setMessage( request, strMessageKey, null, null, null, null, nMessageType );
95 }
96
97 /**
98 * Set the message, store it in session and throw a
99 * LuteceSiteMessageException
100 *
101 * @param request The HttpRequest
102 * @param strMessageKey The message key
103 * @param nMessageType The message type
104 * @param strUrl The url of the Ok button
105 * @throws SiteMessageException occurs when a site message need to be
106 * displayed
107 */
108 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl )
109 throws SiteMessageException
110 {
111 setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType );
112 }
113
114 /**
115 * Set the message, store it in session and throw a
116 * LuteceSiteMessageException
117 *
118 * @param request The HttpRequest
119 * @param strMessageKey The message key
120 * @param nMessageType The message type
121 * @param strUrl The url of the Ok button
122 * @param requestParameters The request parameters as a map
123 * @throws SiteMessageException occurs when a site message need to be
124 * displayed
125 */
126 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl,
127 Map<String, Object> requestParameters ) throws SiteMessageException
128 {
129 setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType, requestParameters );
130 }
131
132 /**
133 * Set the message, store it in session and throw a
134 * LuteceSiteMessageException
135 *
136 * @param request The HttpRequest
137 * @param strMessageKey The message key
138 * @param strTitleKey The title key
139 * @param nMessageType The message type
140 * @throws SiteMessageException occurs when a site message need to be
141 * displayed
142 */
143 public static void setMessage( HttpServletRequest request, String strMessageKey, String strTitleKey,
144 int nMessageType ) throws SiteMessageException
145 {
146 setMessage( request, strMessageKey, null, strTitleKey, null, null, nMessageType );
147 }
148
149 /**
150 * Set the message, store it in session and throw a
151 * LuteceSiteMessageException
152 *
153 * @param request The HttpRequest
154 * @param strMessageKey The message key
155 * @param messageArgs Message Arguments
156 * @param strTitleKey The title key
157 * @param nMessageType The message type
158 * @throws SiteMessageException occurs when a site message need to be
159 * displayed
160 */
161 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
162 String strTitleKey, int nMessageType ) throws SiteMessageException
163 {
164 setMessage( request, strMessageKey, messageArgs, strTitleKey, null, null, nMessageType );
165 }
166
167 /**
168 * Set the message, store it in session and throw a
169 * LuteceSiteMessageException
170 *
171 * @param request The HttpRequest
172 * @param strMessageKey The message key
173 * @param messageArgs Message Arguments
174 * @param nMessageType The message type
175 * @throws SiteMessageException occurs when a site message need to be
176 * displayed
177 */
178 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
179 int nMessageType ) throws SiteMessageException
180 {
181 setMessage( request, strMessageKey, messageArgs, null, null, null, nMessageType );
182 }
183
184 /**
185 * Set the message, store it in session and throw a
186 * LuteceSiteMessageException
187 *
188 * @param request The HttpRequest
189 * @param strMessageKey The message key
190 * @param messageArgs Message arguments
191 * @param strTitleKey The title key
192 * @param nMessageType The message type
193 * @param strUrl The URL
194 * @throws SiteMessageException occurs when a site message need to be
195 * displayed
196 */
197 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
198 int nMessageType, String strUrl, String strTitleKey )
199 throws SiteMessageException
200 {
201 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
202 }
203
204 /**
205 * Set the message, store it in session and throw a
206 * LuteceSiteMessageException
207 *
208 * @param request The HttpRequest
209 * @param strMessageKey The message key
210 * @param messageArgs Message Arguments
211 * @param strUrl The forward URL
212 * @param strTitleKey The title key
213 * @param nMessageType The message type
214 * @throws SiteMessageException occurs when a site message need to be
215 * displayed
216 */
217 public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl,
218 String strTitleKey, Object[] messageArgs ) throws SiteMessageException
219 {
220 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
221 }
222
223 /**
224 * Set the message, store it in session and throw a
225 * LuteceSiteMessageException
226 *
227 * @param request The HttpRequest
228 * @param strMessageKey The message key
229 * @param messageArgs Message Arguments
230 * @param strTitleKey The title key
231 * @param strUrl The Url of the Ok button
232 * @param strTarget The url target if not "_self"
233 * @param nMessageType The message type
234 * @throws SiteMessageException occurs when a site message need to be
235 * displayed
236 */
237 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
238 String strTitleKey, String strUrl, String strTarget, int nMessageType )
239 throws SiteMessageException
240 {
241 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
242 }
243
244 /**
245 * Set the message, store it in session and throw a
246 * LuteceSiteMessageException
247 *
248 * @param request The HttpRequest
249 * @param strMessageKey The message key
250 * @param messageArgs Message Arguments
251 * @param strTitleKey The title key
252 * @param strUrl The Url of the Ok button
253 * @param strTarget The url target if not "_self"
254 * @param nMessageType The message type
255 * @param requestParameters The request parameters
256 * @throws SiteMessageException occurs when a site message need to be
257 * displayed
258 */
259 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
260 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters )
261 throws SiteMessageException
262 {
263 setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType,
264 requestParameters, null );
265 }
266
267 /**
268 * Set the message, store it in session and throw a
269 * LuteceSiteMessageException
270 *
271 * @param request The HttpRequest
272 * @param strMessageKey The message key
273 * @param messageArgs Message Arguments
274 * @param strTitleKey The title key
275 * @param strUrl The Url of the Ok button
276 * @param strTarget The url target if not "_self"
277 * @param nMessageType The message type
278 * @param requestParameters The request parameters
279 * @param strBackUrl The Url of back button
280 * @throws SiteMessageException occurs when a site message need to be
281 * displayed
282 */
283 public static void setMessage( HttpServletRequest request, String strMessageKey, Object[] messageArgs,
284 String strTitleKey, String strUrl, String strTarget, int nMessageType, Map<String, Object> requestParameters,
285 String strBackUrl ) throws SiteMessageException
286 {
287 String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
288 SiteMessage message = new SiteMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType,
289 getTypeButton( nMessageType, strUrl ), requestParameters, strBackUrl );
290 setMessage( request, message );
291
292 throw new SiteMessageException( );
293 }
294
295 /**
296 * Returns the message associated to the current request
297 * @param request The HttpRequest
298 * @return The message associated to the current request
299 */
300 public static SiteMessage getMessage( HttpServletRequest request )
301 {
302 HttpSession session = request.getSession( true );
303 SiteMessage message = (SiteMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
304
305 return message;
306 }
307
308 /**
309 * Store a message into the current session
310 * @param request The HTTP request
311 * @param message The message to store
312 */
313 private static void setMessage( HttpServletRequest request, SiteMessage message )
314 {
315 HttpSession session = request.getSession( true );
316 session.setAttribute( ATTRIBUTE_MESSAGE, message );
317 }
318
319 /**
320 * Delete the message in session
321 *
322 * @param request The HTTP request
323 */
324 public static void cleanMessageSession( HttpServletRequest request )
325 {
326 HttpSession session = request.getSession( true );
327 session.removeAttribute( ATTRIBUTE_MESSAGE );
328 }
329
330 /**
331 * Set the site message url with parameters if necessary
332 *
333 * @param strRequestUrl The Request url
334 * @return The message url
335 */
336 public static String setSiteMessageUrl( String strRequestUrl )
337 {
338 UrlItem urlItem = new UrlItem( strRequestUrl );
339 urlItem.addParameter( PARAMETER_SITE_MESSAGE, PARAMETER_SITE_MESSAGE_VALUE );
340
341 return urlItem.getUrl( );
342 }
343
344 /**
345 * Returns a default title for the message box
346 * @param nMessageType The message type
347 * @return The default title
348 */
349 private static String getDefaultTitle( int nMessageType )
350 {
351 String strTitleKey;
352
353 switch ( nMessageType )
354 {
355 case SiteMessage.TYPE_QUESTION:
356 strTitleKey = PROPERTY_TITLE_QUESTION;
357
358 break;
359
360 case SiteMessage.TYPE_ERROR:
361 strTitleKey = PROPERTY_TITLE_ERROR;
362
363 break;
364
365 case SiteMessage.TYPE_WARNING:
366 strTitleKey = PROPERTY_TITLE_WARNING;
367
368 break;
369
370 case SiteMessage.TYPE_CONFIRMATION:
371 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
372
373 break;
374
375 case SiteMessage.TYPE_STOP:
376 strTitleKey = PROPERTY_TITLE_STOP;
377
378 break;
379
380 default:
381 strTitleKey = PROPERTY_TITLE_DEFAULT;
382
383 break;
384 }
385
386 return strTitleKey;
387 }
388
389 /**
390 * Returns if the cancel button should be displayed or not according the
391 * message type
392 * @param nMessageType The message type
393 * @param strUrl The url of the Ok button
394 * @return the type of button
395 */
396 private static int getTypeButton( int nMessageType, String strUrl )
397 {
398 /*
399 * ------------------------------------- *
400 * Type url defined no url *
401 * TYPE_INFO none back *
402 * TYPE_QUESTION cancel back(?) *
403 * TYPE_ERROR none back *
404 * TYPE_WARNING none back *
405 * TYPE_CONFIRMATION cancel back *
406 * TYPE_STOP none back *
407 * -------------------------------------
408 */
409 int nTypeButton;
410
411 if ( ( strUrl != null ) && !strUrl.equals( "" ) )
412 {
413 switch ( nMessageType )
414 {
415 case SiteMessage.TYPE_QUESTION:
416 case SiteMessage.TYPE_CONFIRMATION:
417 nTypeButton = SiteMessage.TYPE_BUTTON_CANCEL;
418
419 break;
420
421 default:
422 nTypeButton = SiteMessage.TYPE_BUTTON_HIDDEN;
423
424 break;
425 }
426 }
427 else
428 {
429 nTypeButton = SiteMessage.TYPE_BUTTON_BACK;
430 }
431
432 return nTypeButton;
433 }
434 }