From d9d9a51707cc7e2ca1364dad14fad053322d0744 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Fri, 19 Apr 2019 16:17:09 +0200 Subject: [PATCH] Switched to mysql implementation and added persistence for students. --- .../config/DataSourceConfig.java | 34 ----------------- .../controllers/courses/ImportStudents.java | 37 +++++++++++++++++-- .../model/Course.java | 32 ++++++++++++++++ .../model/people/Person.java | 25 ++++++++++++- .../model/people/teams/StudentTeam.java | 15 ++++++-- .../people/teams/TeachingAssistantTeam.java | 12 ++++-- .../model/people/teams/Team.java | 18 ++++----- .../model/repositories/StudentRepository.java | 17 +++++++++ .../repositories/StudentTeamRepository.java | 7 ++++ src/main/resources/application.properties | 11 ++++-- src/main/resources/templates/courses.html | 2 + .../resources/templates/courses/entity.html | 16 ++++++++ 12 files changed, 166 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/DataSourceConfig.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentRepository.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentTeamRepository.java diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/DataSourceConfig.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/DataSourceConfig.java deleted file mode 100644 index f6b0a94..0000000 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/config/DataSourceConfig.java +++ /dev/null @@ -1,34 +0,0 @@ -package nl.andrewlalis.teaching_assistant_assistant.config; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.jdbc.DataSourceBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; - -import javax.sql.DataSource; - -/** - * Configures the data source for this application. - */ -@Configuration -public class DataSourceConfig { - - private static final String USERNAME = "root"; - private static final String PASSWORD = "root"; - private static final String DB_HOST = "localhost"; - private static final String DB_NAME = "teaching_assistant_assistant"; - - @ConfigurationProperties(prefix = "spring.datasource") - @Bean - @Primary - public DataSource getDataSource() { - return DataSourceBuilder - .create() - .url("jdbc:h2:~/" + DB_NAME) - .username(USERNAME) - .password(PASSWORD) - .build(); - } - -} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/ImportStudents.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/ImportStudents.java index 02819db..b858d4c 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/ImportStudents.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/ImportStudents.java @@ -3,7 +3,8 @@ package nl.andrewlalis.teaching_assistant_assistant.controllers.courses; import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.StudentTeam; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; -import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentTeamRepository; import nl.andrewlalis.teaching_assistant_assistant.util.team_importing.StudentTeamImporter; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -25,11 +26,14 @@ public class ImportStudents { private CourseRepository courseRepository; - private TeamRepository teamRepository; + private StudentTeamRepository studentTeamRepository; - protected ImportStudents(CourseRepository courseRepository, TeamRepository teamRepository) { + private StudentRepository studentRepository; + + protected ImportStudents(CourseRepository courseRepository, StudentTeamRepository studentTeamRepository, StudentRepository studentRepository) { this.courseRepository = courseRepository; - this.teamRepository = teamRepository; + this.studentTeamRepository = studentTeamRepository; + this.studentRepository = studentRepository; } @GetMapping("/courses/{code}/import_students") @@ -50,8 +54,33 @@ public class ImportStudents { return "redirect:/courses"; } + Course course = optionalCourse.get(); + try { List studentTeams = StudentTeamImporter.importFromCSV(file.getInputStream(), optionalCourse.get()); + + // Save all the new students first, then save all the teams they belong to. + for (StudentTeam team : studentTeams) { + team.getMembers().forEach(student -> student.assignToCourse(course)); + this.studentRepository.saveAll(team.getStudents()); + team.setCourse(course); + } + + studentTeams.forEach(team -> { + team.getMembers().forEach(student -> { + student.assignToCourse(course); + course.addParticipant(student); + }); + team.setCourse(course); + course.addStudentTeam(team); + + //this.studentRepository.saveAll((team.getMembers())); + }); + + //this.studentTeamRepository.saveAll(studentTeams); + + this.courseRepository.save(course); + } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java index 483b294..fd1879d 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java @@ -1,6 +1,8 @@ package nl.andrewlalis.teaching_assistant_assistant.model; import nl.andrewlalis.teaching_assistant_assistant.model.assignments.Assignment; +import nl.andrewlalis.teaching_assistant_assistant.model.people.Person; +import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.StudentTeam; import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.TeachingAssistantTeam; @@ -57,6 +59,19 @@ public class Course extends BasicEntity { ) private List teachingAssistantTeams; + /** + * The list of all participants in this course, both teaching assistants and students. + */ + @ManyToMany( + cascade = CascadeType.ALL + ) + @JoinTable( + name = "course_participants", + joinColumns = @JoinColumn(name = "course_id"), + inverseJoinColumns = @JoinColumn(name = "person_id") + ) + private List participants; + /** * Default constructor for JPA. */ @@ -64,6 +79,7 @@ public class Course extends BasicEntity { this.assignments = new ArrayList<>(); this.studentTeams = new ArrayList<>(); this.teachingAssistantTeams = new ArrayList<>(); + this.participants = new ArrayList<>(); } /** @@ -85,6 +101,12 @@ public class Course extends BasicEntity { this.teachingAssistantTeams.add(team); } + public void addParticipant(Person person) { + if (!this.participants.contains(person)) { + this.participants.add(person); + } + } + /* Getters and Setters */ @@ -109,6 +131,16 @@ public class Course extends BasicEntity { return teachingAssistantTeams; } + public List getStudents() { + List students = new ArrayList<>(); + this.participants.forEach(participant -> { + if (participant instanceof Student) { + students.add((Student) participant); + } + }); + return students; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(this.getName()).append('\n'); diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java index 1029403..7909a7f 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java @@ -1,6 +1,7 @@ package nl.andrewlalis.teaching_assistant_assistant.model.people; import nl.andrewlalis.teaching_assistant_assistant.model.BasicEntity; +import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.Team; import javax.persistence.*; @@ -41,11 +42,21 @@ public abstract class Person extends BasicEntity { ) private List teams; + /** + * The list of courses that this person belongs to. + */ + @ManyToMany( + fetch = FetchType.LAZY, + mappedBy = "participants" + ) + private List courses; + /** * Default constructor for JPA. */ protected Person () { this.teams = new ArrayList<>(); + this.courses = new ArrayList<>(); } /** @@ -64,7 +75,15 @@ public abstract class Person extends BasicEntity { } public void assignToTeam(Team team) { - this.teams.add(team); + if (!this.teams.contains(team)) { + this.teams.add(team); + } + } + + public void assignToCourse(Course course) { + if (!this.courses.contains(course)) { + this.courses.add(course); + } } /* @@ -91,6 +110,10 @@ public abstract class Person extends BasicEntity { return this.githubUsername; } + public List getCourses() { + return this.courses; + } + /** * Determines if two Persons are equal. They are considered equal when all of the basic identifying information * about the person is the same, regardless of case. diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/StudentTeam.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/StudentTeam.java index 70fddce..f4be0fc 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/StudentTeam.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/StudentTeam.java @@ -1,18 +1,20 @@ package nl.andrewlalis.teaching_assistant_assistant.model.people.teams; import nl.andrewlalis.teaching_assistant_assistant.model.assignments.grades.AssignmentGrade; +import nl.andrewlalis.teaching_assistant_assistant.model.people.Person; import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.OneToMany; +import java.util.ArrayList; import java.util.List; /** * A group of students. */ @Entity -public class StudentTeam extends Team { +public class StudentTeam extends Team { /** * The list of assignment grades which this student group has received. @@ -27,8 +29,13 @@ public class StudentTeam extends Team { */ public StudentTeam() {} - @Override - public void addMember(Student person) { - this.getMembers().add(person); + public List getStudents() { + List people = super.getMembers(); + List students = new ArrayList<>(); + people.forEach(person -> { + students.add((Student) person); + }); + return students; } + } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/TeachingAssistantTeam.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/TeachingAssistantTeam.java index 5361bc1..830a89f 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/TeachingAssistantTeam.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/TeachingAssistantTeam.java @@ -1,6 +1,7 @@ package nl.andrewlalis.teaching_assistant_assistant.model.people.teams; import nl.andrewlalis.teaching_assistant_assistant.model.assignments.grades.SectionGrade; +import nl.andrewlalis.teaching_assistant_assistant.model.people.Person; import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant; import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistantRole; @@ -8,13 +9,14 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; +import java.util.ArrayList; import java.util.List; /** * A group of teaching assistants. */ @Entity -public class TeachingAssistantTeam extends Team { +public class TeachingAssistantTeam extends Team { /** * The role that this teaching assistant team plays. @@ -33,8 +35,10 @@ public class TeachingAssistantTeam extends Team { */ public TeachingAssistantTeam() {} - @Override - public void addMember(TeachingAssistant person) { - this.getMembers().add(person); + public List getTeachingAssistants() { + List people = super.getMembers(); + List teachingAssistants = new ArrayList<>(people.size()); + people.forEach(person -> teachingAssistants.add((TeachingAssistant) person)); + return teachingAssistants; } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/Team.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/Team.java index 82a43f7..0e81635 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/Team.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/teams/Team.java @@ -17,7 +17,7 @@ import java.util.List; @Inheritance( strategy = InheritanceType.JOINED ) -public abstract class Team

extends BasicEntity { +public abstract class Team extends BasicEntity { /** * The list of members in this group. @@ -30,7 +30,7 @@ public abstract class Team

extends BasicEntity { joinColumns = @JoinColumn(name = "team_id"), inverseJoinColumns = @JoinColumn(name = "person_id") ) - protected List

members; + protected List members; /** * The course that this team belongs to. A team cannot exist on its own, it must belong to a course. @@ -47,23 +47,23 @@ public abstract class Team

extends BasicEntity { this.members = new ArrayList<>(); } - public void addMember(P person) { + public void addMember(Person person) { if (!this.containsMember(person)) { this.members.add(person); } } - public void addMembers(List

people) { - for (P person : people) { + public void addMembers(List people) { + for (Person person : people) { this.addMember(person); } } - public boolean containsMember(P person) { + public boolean containsMember(Person person) { return this.members.contains(person); } - public void removeMember(P person) { + public void removeMember(Person person) { this.members.remove(person); } @@ -79,7 +79,7 @@ public abstract class Team

extends BasicEntity { * Gets a list of all members of this team. * @return A list of all the members in this team. */ - public List

getMembers() { + public List getMembers() { return this.members; } @@ -90,7 +90,7 @@ public abstract class Team

extends BasicEntity { @Override public String toString() { StringBuilder sb = new StringBuilder(); - for (P p : this.getMembers()) { + for (Person p : this.getMembers()) { sb.append(p.getFullName()).append(", "); } return sb.toString(); diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentRepository.java new file mode 100644 index 0000000..3f86bb7 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentRepository.java @@ -0,0 +1,17 @@ +package nl.andrewlalis.teaching_assistant_assistant.model.repositories; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; +import org.springframework.data.repository.CrudRepository; + +import java.util.Optional; + +public interface StudentRepository extends CrudRepository { + + /** + * Tries to find a student by its unique student number. + * @param studentNumber The student number to search for. + * @return An optional Student object. + */ + public Optional findByStudentNumber(int studentNumber); + +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentTeamRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentTeamRepository.java new file mode 100644 index 0000000..f0b5250 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/StudentTeamRepository.java @@ -0,0 +1,7 @@ +package nl.andrewlalis.teaching_assistant_assistant.model.repositories; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.StudentTeam; +import org.springframework.data.repository.CrudRepository; + +public interface StudentTeamRepository extends CrudRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e12669e..57daaf2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,10 @@ -spring.jpa.hibernate.ddl-auto=create -#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect - -#spring.jpa.properties.hibernate.show_sql=true +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.url=jdbc:mysql://localhost:3306/teaching_assistant_assistant?serverTimezone=UTC +# +#spring.jpa.properties.hibernate.show_sql=false #spring.jpa.properties.hibernate.use_sql_comments=true #spring.jpa.properties.hibernate.format_sql=true #spring.jpa.properties.hibernate.type=trace diff --git a/src/main/resources/templates/courses.html b/src/main/resources/templates/courses.html index d6f8562..42b41e7 100644 --- a/src/main/resources/templates/courses.html +++ b/src/main/resources/templates/courses.html @@ -11,6 +11,7 @@ Name Code Created at + Students @@ -18,6 +19,7 @@ + diff --git a/src/main/resources/templates/courses/entity.html b/src/main/resources/templates/courses/entity.html index 511b5b4..4933903 100644 --- a/src/main/resources/templates/courses/entity.html +++ b/src/main/resources/templates/courses/entity.html @@ -41,6 +41,22 @@ + +

All Students ():

+ + + + + + + +
+ + Student + +