Skip to content

Commit

Permalink
[#12946] Initialise Entities for Course Structure (#12948)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
domlimm and weiquu authored Apr 4, 2024
1 parent 75b8af1 commit d372790
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 29 deletions.
22 changes: 3 additions & 19 deletions src/main/java/teammates/storage/sqlentity/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,12 +36,12 @@ public class Course extends BaseEntity {
@Column(nullable = false)
private String institute;

@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
private List<CourseStructure> courseStructures = new ArrayList<>();

@OneToMany(mappedBy = "course")
private List<FeedbackSession> feedbackSessions = new ArrayList<>();

@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
private List<Section> sections = new ArrayList<>();

@UpdateTimestamp
private Instant updatedAt;

Expand Down Expand Up @@ -70,13 +69,6 @@ public List<String> getInvalidityInfo() {
return errors;
}

/**
* Adds a section to the Course.
*/
public void addSection(Section section) {
this.sections.add(section);
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -117,14 +109,6 @@ public void setFeedbackSessions(List<FeedbackSession> feedbackSessions) {
this.feedbackSessions = feedbackSessions;
}

public List<Section> getSections() {
return sections;
}

public void setSections(List<Section> sections) {
this.sections = sections;
}

public Instant getUpdatedAt() {
return updatedAt;
}
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/teammates/storage/sqlentity/CourseStructure.java
Original file line number Diff line number Diff line change
@@ -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<Section> 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<String> getInvalidityInfo() {
List<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/teammates/storage/sqlentity/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/teammates/storage/sqlentity/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;

/**
Expand All @@ -21,6 +22,9 @@ public class Student extends User {
@Column(nullable = false)
private String comments;

@ManyToMany(mappedBy = "teamId")
private List<Team> teams;

protected Student() {
// required by Hibernate
}
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/teammates/storage/sqlentity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -31,8 +34,11 @@ public class Team extends BaseEntity {
@JoinColumn(name = "sectionId")
private Section section;

@OneToMany(mappedBy = "team")
private List<User> users;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "TeamToStudentMaps",
joinColumns = { @JoinColumn(name = "teamId") },
inverseJoinColumns = { @JoinColumn(name = "studentId") })
Set<Student> students = new HashSet<>();

@Column(nullable = false)
private String name;
Expand All @@ -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
Expand Down Expand Up @@ -95,12 +100,12 @@ public void setSection(Section section) {
this.section = section;
}

public List<User> getUsers() {
return users;
public Set<Student> getStudents() {
return students;
}

public void setUsers(List<User> users) {
this.users = users;
public void setStudents(Set<Student> students) {
this.students = students;
}

public String getName() {
Expand All @@ -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 + "]";
}

Expand Down
72 changes: 72 additions & 0 deletions src/main/java/teammates/storage/sqlentity/TeamToStudentMap.java
Original file line number Diff line number Diff line change
@@ -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<String> getInvalidityInfo() {
List<String> 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();
}
}
1 change: 1 addition & 0 deletions src/main/java/teammates/storage/sqlentity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d372790

Please sign in to comment.