diff --git a/core/src/main/java/se/su/dsv/scipro/mail/MailEvent.java b/core/src/main/java/se/su/dsv/scipro/mail/MailEvent.java index 200d362663..a8b99efd4d 100755 --- a/core/src/main/java/se/su/dsv/scipro/mail/MailEvent.java +++ b/core/src/main/java/se/su/dsv/scipro/mail/MailEvent.java @@ -1,5 +1,6 @@ package se.su.dsv.scipro.mail; +import jakarta.persistence.CollectionTable; import jakarta.persistence.GenerationType; import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.system.DomainObject; @@ -19,61 +20,80 @@ import jakarta.persistence.Lob; import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import java.util.*; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; @Entity @Table(name = "mail_event") @Cacheable(false) public class MailEvent extends DomainObject { public static final int STRING_MAX_LENGTH = 255; + + // ---------------------------------------------------------------------------------- + // Basic JPA-mappings + // ---------------------------------------------------------------------------------- @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @JoinTable( - name = "mail_event_recipients", - joinColumns = @JoinColumn(name = "mail_event_id"), - inverseJoinColumns = @JoinColumn(name = "recipients_id")) - @ManyToMany(fetch = FetchType.EAGER) - private Set<User> recipients = new HashSet<>(); - - @ElementCollection - private Set<String> nonUserRecipients = new HashSet<>(); - - @Column(length = STRING_MAX_LENGTH) @Basic(optional = false) + @Column(name = "subject", length = STRING_MAX_LENGTH) private String subject; - @Column(length = STRING_MAX_LENGTH) @Basic(optional = true) + @Column(name = "from_name", length = STRING_MAX_LENGTH) private String fromName; - @Column(length = STRING_MAX_LENGTH) @Basic(optional = true) + @Column(name = "from_email", length = STRING_MAX_LENGTH) private String fromEmail; - @Lob @Basic(optional = false) + @Column(name = "message_body") + @Lob private String messageBody; @Basic(optional = false) + @Column(name = "sent") private boolean sent = false; @Basic(optional = true) + @Column(name = "message_id") private String messageID; + // ---------------------------------------------------------------------------------- + // JPA-mappings of foreign keys in this table (mail_event) referencing other + // tables. + // ---------------------------------------------------------------------------------- @ManyToOne(optional = true) - @JoinColumn(name = "attachment_reference_id") + @JoinColumn(name = "attachment_file_reference_id", referencedColumnName = "id") private FileReference attachment; + // ---------------------------------------------------------------------------------- + // @ManyToMany JPA-mappings + // ---------------------------------------------------------------------------------- + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "mail_event_recipient", + joinColumns = @JoinColumn(name = "mail_event_id", referencedColumnName = "id"), + inverseJoinColumns = @JoinColumn(name = "recipient_id", referencedColumnName = "id")) + private Set<User> recipients = new HashSet<>(); + + @ElementCollection + @CollectionTable(name = "mail_event_non_user_recipient", + joinColumns = @JoinColumn(name = "mail_event_id", referencedColumnName = "id")) + @Column(name = "non_user_recipient") + private Set<String> nonUserRecipients = new HashSet<>(); + + // ---------------------------------------------------------------------------------- + // Constructors + // ---------------------------------------------------------------------------------- public MailEvent() { } - public MailEvent( - final String subject, - final String messageBody, - final User recipient, - final String fromName, + public MailEvent(final String subject, final String messageBody, final User recipient, final String fromName, final String fromEmail) { this.subject = subject; this.messageBody = messageBody; @@ -82,13 +102,8 @@ public class MailEvent extends DomainObject { this.fromEmail = fromEmail; } - public MailEvent( - final String subject, - final String messageBody, - final Collection<User> recipients, - final String fromName, - final String fromEmail - ) { + public MailEvent(final String subject, final String messageBody, final Collection<User> recipients, + final String fromName, final String fromEmail) { this.subject = subject; this.messageBody = messageBody; this.recipients.addAll(recipients); @@ -96,35 +111,124 @@ public class MailEvent extends DomainObject { this.fromEmail = fromEmail; } - public boolean isSent() { - return sent; + // ---------------------------------------------------------------------------------- + // Properties (Getters and Setters) + // ---------------------------------------------------------------------------------- + @Override + public Long getId() { + return this.id; } - public void markSent(final String messageID) { - this.messageID = messageID; - this.sent = true; + public void setId(Long id) { + this.id = id; } - public Set<User> getRecipients() { - return recipients; - } - - public void setMessageBody(String messageBody) { - this.messageBody = messageBody; + public String getSubject() { + return this.subject; } public void setSubject(String subject) { this.subject = subject; } + public String getFromName() { + return this.fromName; + } + public void setFromName(String fromName) { this.fromName = fromName; } + public String getFromEmail() { + return this.fromEmail; + } + public void setFromEmail(String fromEmail) { this.fromEmail = fromEmail; } + public String getMessageBody() { + return this.messageBody; + } + + public void setMessageBody(String messageBody) { + this.messageBody = messageBody; + } + + public boolean isSent() { + return sent; + } + + public void setSent(boolean sent) { + this.sent = sent; + } + + public String getMessageID() { + return this.messageID; + } + + public void setMessageID(String messageID) { + this.messageID = messageID; + } + + public FileReference getAttachment() { + return this.attachment; + } + + public void setAttachment(FileReference attachment) { + this.attachment = attachment; + } + + public Set<User> getRecipients() { + return recipients; + } + + public void setRecipients(Set<User> recipients) { + this.recipients = recipients; + } + + public Set<String> getNonUserRecipients() { + return this.nonUserRecipients; + } + + public void setNonUserRecipients(Set<String> nonUserRecipients) { + this.nonUserRecipients = nonUserRecipients; + } + + // ---------------------------------------------------------------------------------- + // Methods Common To All Objects + // ---------------------------------------------------------------------------------- + @Override + public boolean equals(final Object o) { + if (o == this) return true; + if (!(o instanceof MailEvent)) return false; + final MailEvent other = (MailEvent) o; + return other.canEqual(this) + && Objects.equals(this.getId(), other.getId()); + } + + @Override + public int hashCode() { + return Objects.hashCode(this.getId()); + } + + @Override + public String toString() { + return "MailEvent(id=" + this.getId() + ", recipients=" + this.getRecipients() + + ", nonUserRecipients=" + this.getNonUserRecipients() + ", subject=" + + this.getSubject() + ", fromName=" + this.getFromName() + + ", fromEmail=" + this.getFromEmail() + ", messageBody=" + this.getMessageBody() + + ", sent=" + this.isSent() + ", messageID=" + this.getMessageID() + + ", attachment=" + this.getAttachment() + ")"; + } + + // ---------------------------------------------------------------------------------- + // Other Methods + // ---------------------------------------------------------------------------------- + protected boolean canEqual(final Object other) { + return other instanceof MailEvent; + } + public void addRecipients(final Iterable<Recipient> recipients) { for (Recipient recipient : recipients) { recipient.accept(new RecipientVisitor<Boolean>() { @@ -141,83 +245,8 @@ public class MailEvent extends DomainObject { } } - @Override - public Long getId() { - return this.id; - } - - public Set<String> getNonUserRecipients() { - return this.nonUserRecipients; - } - - public String getSubject() { - return this.subject; - } - - public String getFromName() { - return this.fromName; - } - - public String getFromEmail() { - return this.fromEmail; - } - - public String getMessageBody() { - return this.messageBody; - } - - public String getMessageID() { - return this.messageID; - } - - public FileReference getAttachment() { - return this.attachment; - } - - public void setId(Long id) { - this.id = id; - } - - public void setRecipients(Set<User> recipients) { - this.recipients = recipients; - } - - public void setNonUserRecipients(Set<String> nonUserRecipients) { - this.nonUserRecipients = nonUserRecipients; - } - - public void setSent(boolean sent) { - this.sent = sent; - } - - public void setMessageID(String messageID) { + public void markSent(final String messageID) { this.messageID = messageID; - } - - public void setAttachment(FileReference attachment) { - this.attachment = attachment; - } - - @Override - public String toString() { - return "MailEvent(id=" + this.getId() + ", recipients=" + this.getRecipients() + ", nonUserRecipients=" + this.getNonUserRecipients() + ", subject=" + this.getSubject() + ", fromName=" + this.getFromName() + ", fromEmail=" + this.getFromEmail() + ", messageBody=" + this.getMessageBody() + ", sent=" + this.isSent() + ", messageID=" + this.getMessageID() + ", attachment=" + this.getAttachment() + ")"; - } - - @Override - public boolean equals(final Object o) { - if (o == this) return true; - if (!(o instanceof MailEvent)) return false; - final MailEvent other = (MailEvent) o; - return other.canEqual(this) - && Objects.equals(this.getId(), other.getId()); - } - - protected boolean canEqual(final Object other) { - return other instanceof MailEvent; - } - - @Override - public int hashCode() { - return Objects.hashCode(this.getId()); + this.sent = true; } } diff --git a/core/src/main/java/se/su/dsv/scipro/match/Idea.java b/core/src/main/java/se/su/dsv/scipro/match/Idea.java index 30a9029388..e2772b539b 100755 --- a/core/src/main/java/se/su/dsv/scipro/match/Idea.java +++ b/core/src/main/java/se/su/dsv/scipro/match/Idea.java @@ -125,7 +125,7 @@ public class Idea extends DomainObject { private User creator; @OneToOne(optional = true, cascade = CascadeType.ALL) - @JoinColumn(name = "match_id", referencedColumnName = "id") + @JoinColumn(name = "latest_match_id", referencedColumnName = "id") @QueryInit({"supervisor.user", "supervisor.unit"}) private Match match; diff --git a/core/src/main/resources/db/migration/V389__harmonize_table_attribute_name.sql b/core/src/main/resources/db/migration/V389__harmonize_table_attribute_name.sql index 2af85aa93a..55688a4701 100644 --- a/core/src/main/resources/db/migration/V389__harmonize_table_attribute_name.sql +++ b/core/src/main/resources/db/migration/V389__harmonize_table_attribute_name.sql @@ -2821,6 +2821,71 @@ alter table `plagiarism_request` foreign key (receiver_user_id) references user (id) on delete cascade on update cascade; +/* + * Step 20: mail_event, mail_event_recipients and MailEvent_nonUserRecipients + */ + +-- table: mail_event + +alter table `mail_event` drop foreign key `FK_mail_event_attachment`; + +alter table `mail_event` drop key `FK_mail_event_attachment`; + +alter table `mail_event` change `version` `version` int(4) not null default 0 after `last_modified`; +alter table `mail_event` change `subject` `subject` varchar(255) not null after `version`; +alter table `mail_event` change `fromName` `from_name` varchar(255) default null after `subject`; +alter table `mail_event` change `fromEmail` `from_email` varchar(255) default null after `from_name`; +alter table `mail_event` change `messageBody` `message_body` longtext not null after `from_email`; +alter table `mail_event` change `sent` `sent` tinyint(1) not null default 0 after `message_body`; +alter table `mail_event` change `messageID` `message_id` varchar(255) default null after `sent`; +alter table `mail_event` change `notificationEventType` `notification_event_type` varchar(255) default null after `message_id`; +alter table `mail_event` change `attachment_reference_id` `attachment_file_reference_id` bigint(20) default null after `notification_event_type`; + +alter table `mail_event` + add constraint fk_mail_event_attachment_file_reference_id + foreign key (attachment_file_reference_id) references file_reference (id) + on delete cascade on update cascade; + +-- table: mail_event_recipients + +alter table `mail_event_recipients` drop foreign key `FK_mail_event_recipients_user`; +alter table `mail_event_recipients` drop foreign key `FK41091C7FE7F98C6`; + +alter table `mail_event_recipients` drop key `FK41091C7B286D1B0`; +alter table `mail_event_recipients` drop key `FK41091C7FE7F98C6`; + +alter table `mail_event_recipients` drop primary key; + +rename table `mail_event_recipients` to `mail_event_recipient`; + +alter table `mail_event_recipient` change `recipients_id` `recipient_id` bigint(20) not null after `mail_event_id`; + +alter table `mail_event_recipient` add primary key (mail_event_id, recipient_id); + +alter table `mail_event_recipient` + add constraint fk_mail_event_recipient_mail_event_id + foreign key (mail_event_id) references mail_event (id) + on delete cascade on update cascade; + +alter table `mail_event_recipient` + add constraint fk_mail_event_recipient_recipient_id + foreign key (recipient_id) references user (id) + on delete cascade on update cascade; + +-- table: MailEvent_nonUserRecipients + +alter table `MailEvent_nonUserRecipients` drop foreign key `FKD7F8996D0814DF5`; +alter table `MailEvent_nonUserRecipients` drop key `FKD7F8996D0814DF5`; + +rename table `MailEvent_nonUserRecipients` to `mail_event_non_user_recipient`; + +alter table `mail_event_non_user_recipient` change `MailEvent_id` `mail_event_id` bigint(20) not null; +alter table `mail_event_non_user_recipient` change `nonUserRecipients` `non_user_recipient` varchar(255) default null; + +alter table `mail_event_non_user_recipient` + add constraint fk_mail_event_non_user_recipient_mail_event_id + foreign key (mail_event_id) references mail_event (id) + on delete cascade on update cascade; /* Useful SQL