2019-12-28 21:03:41 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2019-12-30 18:49:48 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2019-12-28 21:03:41 +01:00
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a unique message with a unique, numeric ID. Further metadata
|
|
|
|
* includes the sender and recipient {@link User}s, as well as the creation
|
|
|
|
* date and the current {@link MessageStatus}.<br>
|
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
* File: <strong>Message.java</strong><br>
|
|
|
|
* Created: <strong>28.12.2019</strong><br>
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-28 21:03:41 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2019-12-30 18:49:48 +01:00
|
|
|
* @author Leon Hofmeister
|
2019-12-28 21:03:41 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2019-12-31 10:16:52 +01:00
|
|
|
public class Message implements Serializable {
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* This enumeration defines all possible statuses a {link Message} can have.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-28 17:01:02 +01:00
|
|
|
public enum MessageStatus {
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
|
|
|
* is selected, if a message was sent but not received by the server yet.
|
|
|
|
*/
|
|
|
|
WAITING,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is selected, if a sent message was received by the server.
|
|
|
|
*/
|
|
|
|
SENT,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is selected, if a message was delivered from the server to the recipient, but
|
|
|
|
* has not been read yet.
|
|
|
|
*/
|
|
|
|
RECEIVED,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is selected, if a recipient opened the corresponding chat of said message.
|
|
|
|
*/
|
|
|
|
READ
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:50:04 +01:00
|
|
|
private final long id, senderId, recipientId;
|
2020-03-22 14:52:38 +01:00
|
|
|
private final boolean forwarded;
|
2020-01-02 17:50:04 +01:00
|
|
|
private final Date creationDate;
|
|
|
|
private final String text;
|
|
|
|
private final MessageAttachment<?> attachment;
|
2019-12-30 18:49:48 +01:00
|
|
|
|
2020-01-02 17:50:04 +01:00
|
|
|
private Date receivedDate, readDate;
|
|
|
|
private MessageStatus status;
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2020-03-24 17:29:16 +01:00
|
|
|
private static final long serialVersionUID = 0L;
|
2019-12-31 10:16:52 +01:00
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* Initializes a {@link Message} with values for all of its properties. The use
|
|
|
|
* of this constructor is only intended for the {@link MessageBuilder} class, as
|
|
|
|
* this class provides {@code null} checks and default values for all
|
|
|
|
* properties.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param id unique ID
|
|
|
|
* @param senderId the ID of the user who sends the message
|
|
|
|
* @param recipientId the ID of the user who receives the message
|
|
|
|
* @param creationDate the creation date of the message
|
|
|
|
* @param text the text content of the message
|
|
|
|
* @param attachment the attachment of the message, if present
|
|
|
|
* @param status the current {@link MessageStatus} of the message
|
2020-03-22 14:52:38 +01:00
|
|
|
* @param forwarded whether this message was forwarded
|
2020-01-02 17:50:04 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-03-22 14:52:38 +01:00
|
|
|
Message(long id, long senderId, long recipientId, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status,
|
|
|
|
boolean forwarded) {
|
2020-01-02 17:50:04 +01:00
|
|
|
this.id = id;
|
|
|
|
this.senderId = senderId;
|
|
|
|
this.recipientId = recipientId;
|
2020-01-28 17:32:33 +01:00
|
|
|
this.creationDate = creationDate;
|
2020-01-02 17:50:04 +01:00
|
|
|
this.text = text;
|
|
|
|
this.attachment = attachment;
|
|
|
|
this.status = status;
|
2020-03-22 14:52:38 +01:00
|
|
|
this.forwarded = forwarded;
|
2019-12-28 21:03:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 10:20:35 +01:00
|
|
|
/**
|
|
|
|
* Changes the current {@link MessageStatus} to the next logical status.<br>
|
|
|
|
* <br>
|
|
|
|
* The underlying order is as follows:
|
|
|
|
* <ol>
|
|
|
|
* <li>{@link MessageStatus#WAITING}
|
|
|
|
* <li>{@link MessageStatus#SENT}
|
|
|
|
* <li>{@link MessageStatus#RECEIVED}
|
|
|
|
* <li>{@link MessageStatus#READ}
|
|
|
|
* </ol>
|
|
|
|
*
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void nextStatus() {
|
|
|
|
if (status == MessageStatus.READ) throw new IllegalStateException("Message status READ is already reached");
|
|
|
|
status = MessageStatus.values()[status.ordinal() + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2020-03-24 17:29:16 +01:00
|
|
|
return String.format(
|
|
|
|
"TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded,hasAttachment=%b]",
|
2019-12-31 10:20:35 +01:00
|
|
|
id,
|
2020-01-02 17:50:04 +01:00
|
|
|
senderId,
|
|
|
|
recipientId,
|
|
|
|
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(creationDate),
|
2019-12-31 10:20:35 +01:00
|
|
|
status,
|
2020-01-02 17:50:04 +01:00
|
|
|
text,
|
2020-03-24 17:29:16 +01:00
|
|
|
forwarded,
|
2020-01-02 17:50:04 +01:00
|
|
|
attachment != null);
|
2019-12-31 10:20:35 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
|
|
|
* @return the ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public long getId() { return id; }
|
|
|
|
|
2019-12-31 15:38:07 +01:00
|
|
|
/**
|
|
|
|
* @return the sender ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public long getSenderId() { return senderId; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the recipient ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public long getRecipientId() { return recipientId; }
|
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* @return the date at which this message was created
|
2019-12-28 21:03:41 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-11 14:17:51 +01:00
|
|
|
public Date getCreationDate() { return creationDate; }
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the date at which the message has been received by the sender
|
2019-12-30 18:49:48 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public Date getReceivedDate() { return receivedDate; }
|
2019-12-31 10:16:52 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @param receivedDate the date at which the message has been received by the
|
|
|
|
* sender
|
2019-12-31 10:16:52 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the date at which the message has been read by the sender
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public Date getReadDate() { return readDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @param readDate at which the message has been read by the sender
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public void setReadDate(Date readDate) { this.readDate = readDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the text content of this message
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public String getText() { return text; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the messageAttachment
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public MessageAttachment<?> getAttachment() { return attachment; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the current status of this message
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public MessageStatus getStatus() { return status; }
|
2020-01-11 14:17:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param status the new {@link MessageStatus}, if permitted
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setStatus(MessageStatus status) {
|
|
|
|
if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time");
|
2020-01-28 17:01:02 +01:00
|
|
|
this.status = status;
|
2020-01-11 14:17:51 +01:00
|
|
|
}
|
2020-03-22 14:52:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return whether this message was forwarded
|
|
|
|
* @since Envoy common v0.1-beta
|
|
|
|
*/
|
|
|
|
public boolean isForwarded() { return forwarded; }
|
|
|
|
}
|