at.spardat.enterprise.exc.Notification Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: Notification.java 2609 2008-06-23 12:50:01Z gub $
package at.spardat.enterprise.exc;
import java.text.MessageFormat;
import java.util.Locale;
/**
* This is a default implementation of INotification, see {@link INotification}.
*
* The constructors expect a message in the form of a java.text.MessageFormat compliant
* string with optional parameters. Besides providing a message text, a code may be set using
* setCode(). The code may be used to discriminate different notifications.
* See below for a recommendation on how to structure the code.
*
* The message must be set in the constructor, additional attributes are set using the set-
* methods that may be chained, see examples below. If the set methods are not called,
* the constructed Notification defaults to type T_ERROR with
* reaction R_OK. The default code is zero, the default short message
* is the empty string.
*
* An instance of this class cannot be thrown since it is not an exception. If you
* want to use this class for exceptional purposes, use a subclass of BaseException,
* either AppException or SysException
* instead. These classes work much the same and effectively are wrapper around this class.
*
* The following example creates a error notification with code 4711 and one supplied parameter:
*
* new Notification("Limit of {0} exceeded", "123").setCode(4711)
*
*
* Here is a question where the recipient should be forced to answer with
* YES or NO:
*
* new Notification("Limit of {0} exceeded. \nDo u want to proceed?", "123")
* .setType (Notification.T_QUESTION)
* .setReaction (Notification.R_YES_NO)
*
*
* @author YSD, 21.05.2003 18:30:37
*/
public class Notification implements INotification, java.io.Serializable {
/**
* The code of this notification. Defaults to 0 if not set.
*/
private int code_;
/**
* The message of this notification.
*/
private String message_;
/**
* @see INotification#getReaction()
*/
private int reaction_ = R_OK;
/**
* @see INotification#getShortMessage()
*/
private String shortMessage_;
/**
* @see INotification#getType()
*/
private int type_ = T_ERROR;
/**
* Increase this if u make non compatible changes
*/
private static final long serialVersionUID = 1L;
/**
* Constructs and sets the message of this from a two
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 first message parameter
* @param param2 second message parameter
*/
public Notification (String messageFmt, Object param1, Object param2) {
this (messageFmt, null, new Object[]{param1,param2});
}
/**
* Constructs and sets the message of this from a one
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 message parameter
*/
public Notification (String messageFmt, Object param1) {
this (messageFmt, null, new Object[]{param1});
}
/**
* Constructs and sets the message to the provided value.
*
* @param message message text.
*/
public Notification (String message) {
this (message, null, null);
}
/**
* Constructs and sets the message from a format string as
* defined in java.text.MessageFormat. The required parameters
* must be contained in the params array.
*
* @param message format string as defined in java.text.MessageFormat
* or plain text that is not a MessageFormat, depending on params
* @param l a java.util.Locale to format locale
* dependent data types or null if the
* params do not contain local specifics.
* @param params the parameters of the message. If params is null,
* message is not subject to MessageFormat-processing. If params
* is not null, message must be compliant to
* java.text.MessageFormat.
*/
public Notification (String message, Locale l, Object[] params) {
setMessage (message, l, params);
}
/**
* Sets the message from a format string as
* defined in java.text.MessageFormat. The required parameters
* must be contained in the params array.
*
* @param message format string as defined in java.text.MessageFormat
* or plain text that is not a MessageFormat, depending on params
* @param l a java.util.Locale to format locale
* dependent data types or null if the
* params do not contain local specifics.
* @param params the parameters of the message. If params is null,
* message is not subject to MessageFormat-processing. If params
* is not null, message must be compliant to
* java.text.MessageFormat.
* @return this
*/
public Notification setMessage (String message, Locale l, Object[] params) {
if (message == null) {
message_ = "";
return this;
}
if (params == null) {
message_ = message;
} else {
try {
MessageFormat mf = new MessageFormat (message);
if (l != null) mf.setLocale(l);
message_ = mf.format (params);
} catch (Exception ex) {
message_ = ex.toString();
}
}
return this;
}
/**
* Sets the message of this from a one
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 message parameter
* @param param2 message parameter
* @return this
*/
public Notification setMessage (String messageFmt, Object param1, Object param2) {
setMessage (messageFmt, null, new Object[]{param1, param2});
return this;
}
/**
* Sets the message of this from a one
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 message parameter
* @return this
*/
public Notification setMessage (String messageFmt, Object param1) {
setMessage (messageFmt, null, new Object[]{param1});
return this;
}
/**
* Constructs and sets the message to the provided value.
*
* @param message text to set
* @return this
*/
public Notification setMessage (String message) {
setMessage (message, null, null);
return this;
}
/**
* Sets the code. The code must be greater than zero.
* @see #getCode()
*/
public Notification setCode (int code) {
if (code <= 0) throw new IllegalArgumentException();
code_ = code;
return this;
}
/**
* Returns the code set. The code should be used to discrimitate amongst
* different kinds of notifications.
*/
public int getCode () {
return code_;
}
/**
* Sets one or a bit-or combination of the reaction constants
* of INotification.
*
* @see INotification#getReaction()
* @return this
*/
public Notification setReaction (int reaction) {
reaction_ = reaction;
return this;
}
/**
* Sets the short message.
*
* @see INotification#getShortMessage()
*/
public Notification setShortMessage (String shortMessage) {
shortMessage_ = shortMessage;
return this;
}
/**
* Sets the type which may be one of the T_-constants.
*
* @see INotification#getType()
* @return this
*/
public Notification setType (int type) {
type_ = type;
return this;
}
/**
* @see at.spardat.enterprise.exc.INotification#getType()
*/
public int getType() {
return type_;
}
/**
* @see at.spardat.enterprise.exc.INotification#getReaction()
*/
public int getReaction() {
return reaction_;
}
/**
* @see at.spardat.enterprise.exc.INotification#getShortMessage()
*/
public String getShortMessage() {
return shortMessage_ == null ? "" : shortMessage_;
}
/**
* @see at.spardat.enterprise.exc.INotification#getMessage()
*/
public String getMessage() {
return message_ == null ? "" : message_;
}
public String toString() {
return code_ +" "+ message_;
}
}