From d37279086b5a7b4e4a20ec7c6ba725425b362e9f Mon Sep 17 00:00:00 2001 From: Dominic Lim <46486515+domlimm@users.noreply.github.com> Date: Thu, 4 Apr 2024 10:41:42 +0800 Subject: [PATCH] [#12946] Initialise Entities for Course Structure (#12948) * Initialise DDL for Course Structure * Resolve PR comments - Add courseStructures to Course - Remove teamToStudentMaps from Team as superseded by Students --------- Co-authored-by: Wei Qing <48304907+weiquu@users.noreply.github.com> --- .../teammates/storage/sqlentity/Course.java | 22 +---- .../storage/sqlentity/CourseStructure.java | 87 +++++++++++++++++++ .../storage/sqlentity/FeedbackSession.java | 4 + .../teammates/storage/sqlentity/Section.java | 4 + .../teammates/storage/sqlentity/Student.java | 4 + .../teammates/storage/sqlentity/Team.java | 25 +++--- .../storage/sqlentity/TeamToStudentMap.java | 72 +++++++++++++++ .../teammates/storage/sqlentity/User.java | 1 + 8 files changed, 190 insertions(+), 29 deletions(-) create mode 100644 src/main/java/teammates/storage/sqlentity/CourseStructure.java create mode 100644 src/main/java/teammates/storage/sqlentity/TeamToStudentMap.java diff --git a/src/main/java/teammates/storage/sqlentity/Course.java b/src/main/java/teammates/storage/sqlentity/Course.java index 2b0fac83767..9d0e7297681 100644 --- a/src/main/java/teammates/storage/sqlentity/Course.java +++ b/src/main/java/teammates/storage/sqlentity/Course.java @@ -11,7 +11,6 @@ import teammates.common.util.Const; import teammates.common.util.FieldValidator; import teammates.common.util.SanitizationHelper; - import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -37,12 +36,12 @@ public class Course extends BaseEntity { @Column(nullable = false) private String institute; + @OneToMany(mappedBy = "course", cascade = CascadeType.ALL) + private List courseStructures = new ArrayList<>(); + @OneToMany(mappedBy = "course") private List feedbackSessions = new ArrayList<>(); - @OneToMany(mappedBy = "course", cascade = CascadeType.ALL) - private List
sections = new ArrayList<>(); - @UpdateTimestamp private Instant updatedAt; @@ -70,13 +69,6 @@ public List getInvalidityInfo() { return errors; } - /** - * Adds a section to the Course. - */ - public void addSection(Section section) { - this.sections.add(section); - } - public String getId() { return id; } @@ -117,14 +109,6 @@ public void setFeedbackSessions(List feedbackSessions) { this.feedbackSessions = feedbackSessions; } - public List
getSections() { - return sections; - } - - public void setSections(List
sections) { - this.sections = sections; - } - public Instant getUpdatedAt() { return updatedAt; } diff --git a/src/main/java/teammates/storage/sqlentity/CourseStructure.java b/src/main/java/teammates/storage/sqlentity/CourseStructure.java new file mode 100644 index 00000000000..a2c6b252913 --- /dev/null +++ b/src/main/java/teammates/storage/sqlentity/CourseStructure.java @@ -0,0 +1,87 @@ +package teammates.storage.sqlentity; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +import org.hibernate.annotations.UpdateTimestamp; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +/** + * Represents a Course Structure. + */ +@Entity +@Table(name = "CourseStructures") +public class CourseStructure extends BaseEntity { + @Id + private UUID id; + + @ManyToOne + @JoinColumn(name = "courseId") + private Course course; + + @Column(nullable = false) + private String name; + + @OneToMany(mappedBy = "section", cascade = CascadeType.ALL) + private List
sections = new ArrayList<>(); + + @UpdateTimestamp + private Instant updatedAt; + + protected CourseStructure() { + // required by Hibernate + } + + public CourseStructure(UUID id, Course course, String name) { + this.id = id; + this.course = course; + this.name = name; + } + + public UUID getId() { + return this.id; + } + + @Override + public String toString() { + return "CourseStructure [id=" + id + ", course=" + course.getId() + + ", createdAt=" + super.getCreatedAt() + ", updatedAt=" + updatedAt + "]"; + } + + @Override + public List getInvalidityInfo() { + List errors = new ArrayList<>(); + + return errors; + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } else if (this == other) { + return true; + } else if (this.getClass() == other.getClass()) { + CourseStructure otherCourseStructure = (CourseStructure) other; + return Objects.equals(this.getId(), otherCourseStructure.getId()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return this.getId().hashCode(); + } +} \ No newline at end of file diff --git a/src/main/java/teammates/storage/sqlentity/FeedbackSession.java b/src/main/java/teammates/storage/sqlentity/FeedbackSession.java index cf25a3897b5..53c5422cee9 100644 --- a/src/main/java/teammates/storage/sqlentity/FeedbackSession.java +++ b/src/main/java/teammates/storage/sqlentity/FeedbackSession.java @@ -42,6 +42,10 @@ public class FeedbackSession extends BaseEntity { @JoinColumn(name = "courseId") private Course course; + @ManyToOne + @JoinColumn(name = "structureId") + private CourseStructure courseStructure; + @Column(nullable = false) private String name; diff --git a/src/main/java/teammates/storage/sqlentity/Section.java b/src/main/java/teammates/storage/sqlentity/Section.java index 7dba45edd8e..e181543a834 100644 --- a/src/main/java/teammates/storage/sqlentity/Section.java +++ b/src/main/java/teammates/storage/sqlentity/Section.java @@ -31,6 +31,10 @@ public class Section extends BaseEntity { @Id private UUID id; + @ManyToOne + @JoinColumn(name = "structureId") + private CourseStructure courseStructure; + @ManyToOne @JoinColumn(name = "courseId") private Course course; diff --git a/src/main/java/teammates/storage/sqlentity/Student.java b/src/main/java/teammates/storage/sqlentity/Student.java index 3c4601dbe07..ece5419847f 100644 --- a/src/main/java/teammates/storage/sqlentity/Student.java +++ b/src/main/java/teammates/storage/sqlentity/Student.java @@ -10,6 +10,7 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.ManyToMany; import jakarta.persistence.Table; /** @@ -21,6 +22,9 @@ public class Student extends User { @Column(nullable = false) private String comments; + @ManyToMany(mappedBy = "teamId") + private List teams; + protected Student() { // required by Hibernate } diff --git a/src/main/java/teammates/storage/sqlentity/Team.java b/src/main/java/teammates/storage/sqlentity/Team.java index 587d5ceefe6..92cd6e6c503 100644 --- a/src/main/java/teammates/storage/sqlentity/Team.java +++ b/src/main/java/teammates/storage/sqlentity/Team.java @@ -2,20 +2,23 @@ import java.time.Instant; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.UUID; import org.hibernate.annotations.UpdateTimestamp; import teammates.common.util.FieldValidator; - +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToMany; import jakarta.persistence.Table; /** @@ -31,8 +34,11 @@ public class Team extends BaseEntity { @JoinColumn(name = "sectionId") private Section section; - @OneToMany(mappedBy = "team") - private List users; + @ManyToMany(cascade = {CascadeType.ALL}) + @JoinTable(name = "TeamToStudentMaps", + joinColumns = { @JoinColumn(name = "teamId") }, + inverseJoinColumns = { @JoinColumn(name = "studentId") }) + Set students = new HashSet<>(); @Column(nullable = false) private String name; @@ -48,7 +54,6 @@ public Team(Section section, String name) { this.setId(UUID.randomUUID()); this.setSection(section); this.setName(name); - this.setUsers(new ArrayList<>()); } @Override @@ -95,12 +100,12 @@ public void setSection(Section section) { this.section = section; } - public List getUsers() { - return users; + public Set getStudents() { + return students; } - public void setUsers(List users) { - this.users = users; + public void setStudents(Set students) { + this.students = students; } public String getName() { @@ -121,7 +126,7 @@ public void setUpdatedAt(Instant updatedAt) { @Override public String toString() { - return "Team [id=" + id + ", users=" + users + ", name=" + name + return "Team [id=" + id + ", students=" + students + ", name=" + name + ", createdAt=" + getCreatedAt() + ", updatedAt=" + updatedAt + "]"; } diff --git a/src/main/java/teammates/storage/sqlentity/TeamToStudentMap.java b/src/main/java/teammates/storage/sqlentity/TeamToStudentMap.java new file mode 100644 index 00000000000..b8ee17b4a9d --- /dev/null +++ b/src/main/java/teammates/storage/sqlentity/TeamToStudentMap.java @@ -0,0 +1,72 @@ +package teammates.storage.sqlentity; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +import org.hibernate.annotations.UpdateTimestamp; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.Table; + +/** + * Represents a Team to Student Map. + */ +@Entity +@Table(name = "TeamToStudentMaps") +public class TeamToStudentMap extends BaseEntity { + @Id + private UUID id; + + @ManyToMany + @JoinColumn(name = "teamId") + private Team team; + + @ManyToMany + @JoinColumn(name = "studentId") + private Student student; + + @UpdateTimestamp + private Instant updatedAt; + + public UUID getId() { + return this.id; + } + + @Override + public String toString() { + return "TeamToStudentMap [id=" + id + ", team=" + team.getId() + ", student=" + student.getId() + + ", createdAt=" + super.getCreatedAt() + ", updatedAt=" + updatedAt + "]"; + } + + @Override + public List getInvalidityInfo() { + List errors = new ArrayList<>(); + + return errors; + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } else if (this == other) { + return true; + } else if (this.getClass() == other.getClass()) { + TeamToStudentMap otherTeamToStudentMap = (TeamToStudentMap) other; + return Objects.equals(this.id, otherTeamToStudentMap.getId()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return this.getId().hashCode(); + } +} diff --git a/src/main/java/teammates/storage/sqlentity/User.java b/src/main/java/teammates/storage/sqlentity/User.java index 8e4a44bf059..dc18bdcc0fb 100644 --- a/src/main/java/teammates/storage/sqlentity/User.java +++ b/src/main/java/teammates/storage/sqlentity/User.java @@ -45,6 +45,7 @@ public abstract class User extends BaseEntity { @ManyToOne @JoinColumn(name = "teamId") + // TODO: Could be removed as only Students should be part of a team, not Instructors. private Team team; @Column(nullable = false)