package envoy.server.data; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; 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.
* It will be referenced as "database message" to clarify between the different * message objects.
*
* Project: envoy-server-standalone
* File: Message.java
* Created: 02.01.2020
* * @author Kai S. K. Engelbart * @since Envoy Server Standalone v0.1-alpha */ @Entity @Table(name = "messages") @NamedQueries( { @NamedQuery( query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT", name = "getUnreadMessages" ) } ) public class Message { @Id private long id; @ManyToOne(cascade = CascadeType.PERSIST) private User sender; @ManyToOne(cascade = CascadeType.PERSIST) private User recipient; @Temporal(TemporalType.TIMESTAMP) private Date creationDate; @Temporal(TemporalType.TIMESTAMP) private Date receivedDate; @Temporal(TemporalType.TIMESTAMP) private Date readDate; private String text; private envoy.data.Message.MessageStatus status; private byte[] attachment; private 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 persMan = PersistenceManager.getInstance(); id = message.getId(); status = message.getStatus(); text = message.getText(); creationDate = message.getCreationDate(); receivedDate = message.getReceivedDate(); readDate = message.getReadDate(); sender = persMan.getUserById(message.getSenderId()); recipient = persMan.getUserById(message.getRecipientId()); forwarded = message.isForwarded(); // TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET } /** * 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 toCommonMessage() { // TODO: Attachment envoy.data.Message message = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text) .setDate(creationDate) .setStatus(status) .setForwarded(forwarded) .build(); message.setReceivedDate(receivedDate); message.setReadDate(readDate); return message; } /** * @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 User 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 Date getCreationDate() { return creationDate; } /** * @param creationDate the creation date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getCreationDate() */ public void setCreationDate(Date 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 Date getReceivedDate() { return receivedDate; } /** * @param receivedDate the received date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getReceivedDate() */ public void setReceivedDate(Date 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 Date getReadDate() { return readDate; } /** * @param readDate the read date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getReadDate() */ public void setReadDate(Date 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 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; } }