Package net.sf.basedb.core
Class Message
java.lang.Object
net.sf.basedb.core.BasicItem
net.sf.basedb.core.Message
- All Implemented Interfaces:
AccessControlled
,Identifiable
,Nameable
,Removable
public class Message extends BasicItem implements Nameable, Removable
This class represents a message. Messages are normally sent by the system
when a job is completed. It is also possible to send messages between
other users.
- Version:
- 2.0
- Author:
- Nicklas
- Last modified
- $Date: 2021-08-09 14:30:05 +0200 (Mon, 09 Aug 2021) $
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Message.QueryRuntimeFilterImpl
A runtime filter implementation that limits a query to only return messages for the currently logged in user unless the logged in user has generic read permission. -
Field Summary
Fields Modifier and Type Field Description static int
MAX_FROM_LENGTH
The maximum length of the sender name.private static QueryRuntimeFilter
RUNTIME_FILTER
This filter will limit a query to only return messages for the logged in user unless the logged in user has generic read permission.private boolean
sendAsEmail
private boolean
systemCreated
static Item
TYPE
The type of item represented by this class.Fields inherited from interface net.sf.basedb.core.Nameable
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
-
Constructor Summary
Constructors Constructor Description Message(MessageData messageData)
-
Method Summary
Modifier and Type Method Description static long
countUnreadMessages(DbControl dc, User user, boolean cacheResult)
Count the number of unread messages for the specified or logged in user.static Message
getById(DbControl dc, int id)
Get aMessage
item when you know the id.(package private) MessageData
getData()
Get theBasicData
object that holds all data for this item.String
getDescription()
Get the description for the item.String
getFrom()
Get the name of the sender of this message.Job
getJob()
Get theJob
this message is about.String
getName()
Get the name of the item.static Message
getNew(DbControl dc, User to, String fromName, User fromUser, Job job)
Create a newMessage
item.(package private) static Message
getNewSystemMessage(DbControl dc, UserData to, String fromName, UserData fromUser, JobData job)
Create a newMessage
from the system.static ItemQuery<Message>
getQuery(User user)
Get a query configured to retrieve messages for the specified user.User
getRemovedBy()
Get the user that flagged this item for removal.User
getSender()
Get the user that sent this message.Date
getTimeSent()
Get the date and time the message was sent.User
getTo()
Get theUser
this message was sent to.Item
getType()
Get the type of item represented by the object.(package private) void
initPermissions(int granted, int denied)
Read, write an delete permission is granted if the logged in user is the same as the user the message is to.boolean
isRead()
Check if the message has been read or not.boolean
isRemoved()
Check if the removed flag is set for this item.(package private) void
onAfterCommit(Transactional.Action action)
Send the message as an email if requested by the user.void
sendAsEmail()
Send this message as an email.void
setDescription(String description)
Set the description for the item.private void
setFrom(String from)
Set the name of the sender of the message.private void
setJob(JobData job)
void
setName(String name)
Set the name of the item.void
setRead(boolean isRead)
Set read status of the message.void
setRemoved(boolean removed)
Set the removed flag for this item.private void
setSender(UserData sender)
private void
setTo(UserData to)
Set the user this message should be sent to.Methods inherited from class net.sf.basedb.core.BasicItem
addAnnotatableParents, addUsingItems, addUsingItems, breakCircularReferences, checkBatchAnnotatableUsage, checkPermission, equals, getBasicData, getDbControl, getId, getPermissions, getPluginPermissions, getSessionControl, getUsingItems, getVersion, hashCode, hasPermission, isDetached, isInDatabase, isUsed, onAfterInsert, onBeforeCommit, onRollback, setDbControl, setProjectDefaults, toString, validate
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
-
Field Details
-
TYPE
The type of item represented by this class.- See Also:
Item.MESSAGE
,getType()
-
RUNTIME_FILTER
This filter will limit a query to only return messages for the logged in user unless the logged in user has generic read permission. -
systemCreated
private boolean systemCreated -
sendAsEmail
private boolean sendAsEmail -
MAX_FROM_LENGTH
public static final int MAX_FROM_LENGTHThe maximum length of the sender name.
-
-
Constructor Details
-
Message
Message(MessageData messageData)
-
-
Method Details
-
getNew
public static Message getNew(DbControl dc, User to, String fromName, User fromUser, Job job) throws BaseExceptionCreate a newMessage
item.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessto
- The user who should receive the message.fromName
- Name of the sender.fromUser
- The user who sends this message.job
- Which job this message is about.- Returns:
- The new
Message
item - Throws:
BaseException
- If there is an error
-
getNewSystemMessage
static Message getNewSystemMessage(DbControl dc, UserData to, String fromName, UserData fromUser, JobData job)Create a newMessage
from the system. This will not fail due to missing create permission for the logged in user.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessto
- The user who should receive the message.fromName
- Name of the sender.fromUser
- The user who sends this message.job
- Which job this message is about.- Returns:
- The new
Message
item - Throws:
BaseException
- If there is an error
-
getById
public static Message getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseExceptionGet aMessage
item when you know the id.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessid
- The id of the item to load- Returns:
- The
Message
item - Throws:
ItemNotFoundException
- If an item with the specified ID is not foundPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
countUnreadMessages
public static long countUnreadMessages(DbControl dc, User user, boolean cacheResult) throws BaseExceptionCount the number of unread messages for the specified or logged in user. The count doesn't include messages which have been flagged as removed.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessuser
- The user to count the unread messages for, or null to count the messages for the logged in usercacheResult
- If the result should be cached or not- Returns:
- The number of unread messages
- Throws:
BaseException
- If there is an error
-
getQuery
Get a query configured to retrieve messages for the specified user.- Parameters:
user
- The user to retreive messages for, null is allowed if the logged in user has generic read permission for messages in which case all messages will be returned- Returns:
- An
ItemQuery
object - See Also:
User.getMessages()
-
getData
MessageData getData()Description copied from class:BasicItem
Get theBasicData
object that holds all data for this item. -
getType
Description copied from interface:Identifiable
Get the type of item represented by the object. The returned value is one of the values defined in theItem
enumeration.- Specified by:
getType
in interfaceIdentifiable
- Returns:
- A value indicating the type of item
-
getName
Description copied from interface:Nameable
Get the name of the item. -
setName
Description copied from interface:Nameable
Set the name of the item. The name cannot be null and mustn't be longer than the value specified by theNameable.MAX_NAME_LENGTH
constant.- Specified by:
setName
in interfaceNameable
- Parameters:
name
- The new name for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the name is null or longer than specified by theNameable.MAX_NAME_LENGTH
constant
-
getDescription
Description copied from interface:Nameable
Get the description for the item.- Specified by:
getDescription
in interfaceNameable
- Returns:
- A
String
with a description of the item
-
setDescription
public void setDescription(String description) throws PermissionDeniedException, InvalidDataExceptionDescription copied from interface:Nameable
Set the description for the item. The description can be null but mustn't be longer than the value specified by theNameable.MAX_DESCRIPTION_LENGTH
constant.- Specified by:
setDescription
in interfaceNameable
- Parameters:
description
- The new description for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the description longer than specified by theNameable.MAX_DESCRIPTION_LENGTH
constant
-
isRemoved
public boolean isRemoved()Description copied from interface:Removable
Check if the removed flag is set for this item. -
setRemoved
Description copied from interface:Removable
Set the removed flag for this item.- Specified by:
setRemoved
in interfaceRemovable
- Parameters:
removed
- TRUE if the item should be flagged as removed, FALSE otherwise- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.DELETE
permission for setting the flag to TRUE orPermission.WRITE
permission for setting the flag to FALSE
-
getRemovedBy
Description copied from interface:Removable
Get the user that flagged this item for removal.- Specified by:
getRemovedBy
in interfaceRemovable
- Returns:
- A User object, or null if this item has not been flagged
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the userItemNotFoundException
- If the user that removed this item can't be found
-
initPermissions
Read, write an delete permission is granted if the logged in user is the same as the user the message is to.- Overrides:
initPermissions
in classBasicItem
- Parameters:
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclass- Throws:
BaseException
- If the permissions couldn't be initialised
-
onAfterCommit
Send the message as an email if requested by the user.- Overrides:
onAfterCommit
in classBasicItem
- See Also:
Transactional
, Developer documentation: Transactions, Developer documentation: Coding rules and guidelines for item classes
-
getTo
Get theUser
this message was sent to.- Returns:
- The
User
item - Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the userBaseException
- If there is another error
-
setTo
Set the user this message should be sent to.- Throws:
InvalidDataException
- If the user is null
-
getFrom
Get the name of the sender of this message. The sender may be another user or a system process.- See Also:
getSender()
-
setFrom
Set the name of the sender of the message.- Throws:
InvalidDataException
- If the sender is null or longer thanMAX_FROM_LENGTH
-
getSender
Get the user that sent this message.- Returns:
- A
User
object or null if the message was sent by the system or the sending user account has been deleted - Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the senderBaseException
- If there is another error- See Also:
getFrom()
-
setSender
-
getJob
Get theJob
this message is about.- Returns:
- The
Job
item - Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the jobBaseException
- If there is another error
-
setJob
- Throws:
PermissionDeniedException
-
getTimeSent
Get the date and time the message was sent. -
isRead
public boolean isRead()Check if the message has been read or not. -
setRead
Set read status of the message.- Parameters:
isRead
- TRUE to mark the message as read, FALSE to mark it as unread- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission
-
sendAsEmail
public void sendAsEmail()Send this message as an email. This method call is ignored if the server admin has not configured an outgoing SMTP server.- Since:
- 2.16
- See Also:
EmailUtil.isEnabled()
-