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