com.xlrit.gears.base.model.Mail Maven / Gradle / Ivy
package com.xlrit.gears.base.model;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.xlrit.gears.base.content.ContentRef;
import com.xlrit.gears.base.meta.*;
import jakarta.persistence.*;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
/*
MAIL in MAILS =
from_address : text
to_address : text
subject : text
body : text multiline
content_type : text optional // default 'text/plain'
attachments : multiple file optional
*/
@EntityType(typeName = "MAIL", collectionName = "MAILS")
@Attributes({
"fromAddress",
"toAddress",
"subject",
"body",
"contentType",
"attachments",
"publisher",
"publishedAt"
})
@Entity
@Table(name = "SYS_MAIL")
public class Mail implements Identifiable, Publishable {
@Getter
@Id @Column(length = 26)
private String id;
@Getter @Setter
@Attribute(formalName = "from_address", type = DataType.TEXT, label = "From address")
@Basic @Column(name = "from_address_")
private String fromAddress;
@Getter @Setter
@Attribute(formalName = "to_address", type = DataType.TEXT, label = "To address")
@Basic @Column(name = "to_address_")
private String toAddress;
@Getter @Setter
@Attribute(formalName = "subject", type = DataType.TEXT, label = "Subject")
@Basic @Column(name = "subject_")
private String subject;
@Getter @Setter
@Attribute(formalName = "body", type = DataType.TEXT, label = "Body")
@Trait("multiline")
@Basic @Column(name = "body_", length = 32700)
private String body;
@Getter @Setter
@Attribute(formalName = "content_type", type = DataType.TEXT, label = "Content type")
@Basic @Column(name = "content_type_")
private String contentType;
@Getter @Setter
@Attribute(formalName = "ATTACHMENTS", type = DataType.ENTITY, label = "Attachments")
@Basic @Column(name = "attachments_")
@JdbcTypeCode(SqlTypes.JSON)
private List attachments;
@Getter @Setter
@Basic @Column(name = "invalid_")
private boolean invalid;
@Getter @Setter
@Basic @Column(name = "attempts_")
private int attempts;
@Getter @Setter
@Basic @Column(name = "last_attempt_")
private OffsetDateTime lastAttempt;
@Getter @Setter
@Attribute(formalName = "PUBLISHER", type = DataType.ENTITY, label = "Publisher")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "publisher_id_")
private User publisher;
@Getter @Setter
@Attribute(formalName = "published_at", type = DataType.DATETIME, label = "Published at")
@Basic @Column(name = "published_at_")
private OffsetDateTime publishedAt;
public Mail() {}
public Mail(String id) {
this.id = id;
this.attachments = new ArrayList<>();
}
@Displayed
public String defaultDisplayed() {
return "fromAddress: " + fromAddress +
", toAddress: " + toAddress +
", subject: " + subject +
", attachments: " + (attachments.isEmpty() ? "none" : attachments.size());
}
@Override
public boolean equals(Object obj) {
if (this.id == null) return super.equals(obj);
if (!(obj instanceof Mail that)) return false;
return Objects.equals(this.id, that.getId());
}
@Override
public int hashCode() {
return Objects.hash(this.id);
}
@Override
public String toString() {
return "Mail[" + defaultDisplayed() + "]";
}
}