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

203 lines
5.6 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.*;
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;
@ManyToOne(cascade = { CascadeType.PERSIST })
private User sender;
@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 envoy.data.Message.MessageStatus status;
private String text;
private byte[] attachment;
2020-01-03 16:21:35 +01:00
/**
* The constructor for a database object
*
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public Message() {}
// TODO: everything except ID
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();
sender = persMan.getUserById(message.getSenderId());
recipient = persMan.getUserById(message.getRecipientId());
// 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
/**
* @return a database {@link Message} converted into an
* {@link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
2020-01-02 17:50:56 +01:00
public envoy.data.Message toCommonMessage() {
// TODO: Attachment, dates
return new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text).setDate(creationDate).setStatus(status).build();
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
*/
2020-01-02 17:50:56 +01:00
public long getId() { return id; }
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-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; }
}