This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/server/src/main/java/envoy/server/data/Message.java

291 lines
8.4 KiB
Java
Executable File

package envoy.server.data;
import static envoy.data.Message.MessageStatus.*;
import java.time.LocalDateTime;
import javax.persistence.*;
import envoy.data.Attachment;
import envoy.data.Attachment.AttachmentType;
import envoy.data.Message.MessageStatus;
import envoy.data.MessageBuilder;
/**
* This class serves as a way to let Hibernate communicate with the server
* without bringing the dependency of JPA/Hibernate into the client.<br>
* It will be referenced as "database message" to clarify between the different
* message objects.<br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Message.java</strong><br>
* Created: <strong>02.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@Table(name = "messages")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQuery(
name = Message.getPending,
query = "SELECT m FROM Message m WHERE (m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) "
+ "OR (m.sender = :user) AND ((m.status = envoy.data.Message$MessageStatus.RECEIVED) AND (m.receivedDate > :lastSeen)"
+ "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))"
)
public class Message {
/**
* Named query retrieving pending messages for a user (parameter {@code :user})
* which was last seen after a specific date (parameter {@code :lastSeen}).
*
* @since Envoy Server Standalone v0.1-beta
*/
public static final String getPending = "Message.getPending";
@Id
protected long id;
@ManyToOne
@JoinColumn
protected User sender;
@ManyToOne
@JoinColumn
protected Contact recipient;
@Column(name = "creation_date")
protected LocalDateTime creationDate;
@Column(name = "received_date")
protected LocalDateTime receivedDate;
@Column(name = "read_date")
protected LocalDateTime readDate;
protected String text;
protected envoy.data.Message.MessageStatus status;
protected AttachmentType attachmentType;
protected byte[] attachment;
protected boolean forwarded;
/**
* The constructor for a database object.
*
* @since Envoy Server Standalone v0.1-alpha
*/
public Message() {}
/**
* Constructs a database message from a common message.
*
* @param message the {@link envoy.data.Message} to convert into a database
* {@link Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public Message(envoy.data.Message message) {
PersistenceManager persistenceManager = PersistenceManager.getInstance();
id = message.getID();
status = message.getStatus();
text = message.getText();
creationDate = message.getCreationDate();
receivedDate = message.getReceivedDate();
readDate = message.getReadDate();
sender = persistenceManager.getUserByID(message.getSenderID());
recipient = persistenceManager.getContactByID(message.getRecipientID());
forwarded = message.isForwarded();
if (message.hasAttachment()) {
attachment = message.getAttachment().getData();
attachmentType = message.getAttachment().getType();
}
}
/**
* Converts this message into an instance of {@link envoy.data.Message}.
*
* @return a {@link envoy.data.Message} containing the same values as this
* message
* @since Envoy Server Standalone v0.1-alpha
*/
public envoy.data.Message toCommon() {
return prepareBuilder().build();
}
/**
* @return a message builder containing the state of this message
* @since Envoy Server Standalone v0.1-beta
*/
MessageBuilder prepareBuilder() {
var builder = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(
text)
.setCreationDate(creationDate)
.setReceivedDate(receivedDate)
.setReadDate(readDate)
.setStatus(status)
.setForwarded(forwarded);
if (attachment != null) builder.setAttachment(new Attachment(attachment, attachmentType));
return builder;
}
/**
* Sets the message status to {@link MessageStatus#RECEIVED} and sets the
* current time stamp as the received date.
*
* @since Envoy Server Standalone v0.1-beta
*/
public void received() {
receivedDate = LocalDateTime.now();
status = RECEIVED;
}
/**
* Sets the message status to {@link MessageStatus#READ} and sets the
* current time stamp as the read date.
*
* @since Envoy Server Standalone v0.1-beta
*/
public void read() {
readDate = LocalDateTime.now();
status = READ;
}
/**
* @return the id of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public long getID() { return id; }
/**
* @param id the id to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getID()
*/
public void setId(long id) { this.id = id; }
/**
* @return the sender of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public User getSender() { return sender; }
/**
* @param sender the sender to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getSender()
*/
public void setSender(User sender) { this.sender = sender; }
/**
* @return the recipient of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public Contact getRecipient() { return recipient; }
/**
* @param recipient the recipient to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getRecipient()
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
/**
* @return the date at which a {link envoy.data.Message} has been created
* @since Envoy Server Standalone v0.1-alpha
*/
public LocalDateTime getCreationDate() { return creationDate; }
/**
* @param creationDate the creation date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getCreationDate()
*/
public void setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; }
/**
* @return the date at which a {link envoy.data.Message} has been received by
* the server
* @since Envoy Server Standalone v0.1-alpha
*/
public LocalDateTime getReceivedDate() { return receivedDate; }
/**
* @param receivedDate the received date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getReceivedDate()
*/
public void setReceivedDate(LocalDateTime receivedDate) { this.receivedDate = receivedDate; }
/**
* @return the date at which a {link envoy.data.Message} has been read
* @since Envoy Server Standalone v0.1-alpha
*/
public LocalDateTime getReadDate() { return readDate; }
/**
* @param readDate the read date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getReadDate()
*/
public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; }
/**
* @return the status of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public envoy.data.Message.MessageStatus getStatus() { return status; }
/**
* @param status the new status of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public void setStatus(envoy.data.Message.MessageStatus status) { this.status = status; }
/**
* @return the text content of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public String getText() { return text; }
/**
* @param text the new text content of a {@link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public void setText(String text) { this.text = text; }
/**
* @return the attachment of a {@link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public byte[] getAttachment() { return attachment; }
/**
* @param attachment the new attachment
* @since Envoy Server Standalone v0.1-alpha
*/
public void setAttachment(byte[] attachment) { this.attachment = attachment; }
/**
* @return the attachmentType
* @since Envoy Server Standalone v0.1-beta
*/
public AttachmentType getAttachmentType() { return attachmentType; }
/**
* @param attachmentType the attachmentType to set
* @since Envoy Server Standalone v0.1-beta
*/
public void setAttachmentType(AttachmentType attachmentType) { this.attachmentType = attachmentType; }
/**
* @return whether this message is a forwarded message
* @since Envoy Server Standalone v0.1-alpha
*/
public boolean isForwarded() { return forwarded; }
/**
* @param forwarded this message should be a forwarded message.
* @since Envoy Server Standalone v0.1-alpha
*/
public void setForwarded(boolean forwarded) { this.forwarded = forwarded; }
}