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/src/main/java/envoy/server/data/Message.java

237 lines
6.7 KiB
Java
Raw Normal View History

2020-01-02 17:50:56 +01:00
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;
2020-01-02 17:50:56 +01:00
import envoy.data.MessageBuilder;
/**
2020-01-03 16:21:35 +01:00
* 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>
2020-01-02 17:50:56 +01:00
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Message.java</strong><br>
* Created: <strong>02.01.2020</strong><br>
2020-01-03 16:21:35 +01:00
*
2020-01-02 17:50:56 +01:00
* @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"
) }
)
2020-01-02 17:50:56 +01:00
public class Message {
@Id
private long id;
2020-03-22 11:23:56 +01:00
@ManyToOne(cascade = CascadeType.PERSIST)
private User sender;
2020-03-22 11:23:56 +01:00
@ManyToOne(cascade = CascadeType.PERSIST)
private User recipient;
2020-01-02 17:50:56 +01:00
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
2020-01-02 17:50:56 +01:00
@Temporal(TemporalType.TIMESTAMP)
private Date receivedDate;
2020-01-02 17:50:56 +01:00
@Temporal(TemporalType.TIMESTAMP)
private Date readDate;
2020-01-02 17:50:56 +01:00
private String text;
private envoy.data.Message.MessageStatus status;
2020-01-02 17:50:56 +01:00
private byte[] attachment;
private boolean forwarded;
2020-01-02 17:50:56 +01:00
2020-01-03 16:21:35 +01:00
/**
* The constructor for a database object.
2020-01-03 16:21:35 +01:00
*
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public Message() {}
2020-01-03 16:21:35 +01:00
/**
* Constructs a database message from a common message.
*
2020-01-03 16:21:35 +01:00
* @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) {
2020-02-12 07:10:33 +01:00
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
2020-01-03 16:21:35 +01:00
}
2020-01-02 17:50:56 +01:00
2020-01-03 16:21:35 +01:00
/**
* Converts this message into an instance of {@link envoy.data.Message}.
*
* @return a {@link envoy.data.Message} containing the same values as this
* message
2020-01-03 16:21:35 +01:00
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
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;
2020-01-02 17:50:56 +01:00
}
2020-01-03 16:21:35 +01:00
/**
* @return the id of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
public long getID() { return id; }
2020-01-02 17:50:56 +01:00
2020-01-03 16:21:35 +01:00
/**
* @param id the id to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getID()
2020-01-03 16:21:35 +01:00
*/
2020-01-02 17:50:56 +01:00
public void setId(long id) { this.id = id; }
2020-01-03 16:21:35 +01:00
/**
* @return the sender of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public User getSender() { return sender; }
2020-01-03 16:21:35 +01:00
/**
* @param sender the sender to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getSender()
*/
2020-01-02 17:50:56 +01:00
public void setSender(User sender) { this.sender = sender; }
2020-01-03 16:21:35 +01:00
/**
* @return the recipient of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public User getRecipient() { return recipient; }
2020-01-03 16:21:35 +01:00
/**
* @param recipient the recipient to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getRecipient()
*/
2020-01-02 17:50:56 +01:00
public void setRecipient(User recipient) { this.recipient = recipient; }
2020-01-03 16:21:35 +01:00
/**
* @return the date at which a {link envoy.data.Message} has been created
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public Date getCreationDate() { return creationDate; }
2020-01-03 16:21:35 +01:00
/**
* @param creationDate the creation date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getCreationDate()
*/
2020-01-02 17:50:56 +01:00
public void setCreationDate(Date creationDate) { this.creationDate = creationDate; }
2020-01-03 16:21:35 +01:00
/**
* @return the date at which a {link envoy.data.Message} has been received by
* the server
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public Date getReceivedDate() { return receivedDate; }
2020-01-03 16:21:35 +01:00
/**
* @param receivedDate the received date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getReceivedDate()
*/
2020-01-02 17:50:56 +01:00
public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; }
2020-01-03 16:21:35 +01:00
/**
* @return the date at which a {link envoy.data.Message} has been read
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public Date getReadDate() { return readDate; }
2020-01-03 16:21:35 +01:00
/**
* @param readDate the read date to set
* @since Envoy Server Standalone v0.1-alpha
* @see Message#getReadDate()
*/
2020-01-02 17:50:56 +01:00
public void setReadDate(Date readDate) { this.readDate = readDate; }
2020-01-03 16:21:35 +01:00
/**
* @return the status of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public envoy.data.Message.MessageStatus getStatus() { return status; }
2020-01-03 16:21:35 +01:00
/**
* @param status the new status of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public void setStatus(envoy.data.Message.MessageStatus status) { this.status = status; }
2020-01-03 16:21:35 +01:00
/**
* @return the text content of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public String getText() { return text; }
2020-01-03 16:21:35 +01:00
/**
* @param text the new text content of a {@link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public void setText(String text) { this.text = text; }
2020-01-03 16:21:35 +01:00
/**
* @return the attachment of a {@link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public byte[] getAttachment() { return attachment; }
2020-01-03 16:21:35 +01:00
/**
* @param attachment the new attachment
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
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; }
2020-01-02 17:50:56 +01:00
}