Switched to mysql implementation and added persistence for students.

This commit is contained in:
Andrew Lalis 2019-04-19 16:17:09 +02:00 committed by andrewlalis
parent df92a2c2d5
commit d9d9a51707
12 changed files with 166 additions and 60 deletions

View File

@ -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();
}
}

View File

@ -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<StudentTeam> 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();
}

View File

@ -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<TeachingAssistantTeam> 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<Person> 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<Student> getStudents() {
List<Student> 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');

View File

@ -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<Team> teams;
/**
* The list of courses that this person belongs to.
*/
@ManyToMany(
fetch = FetchType.LAZY,
mappedBy = "participants"
)
private List<Course> 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<Course> 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.

View File

@ -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<Student> {
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<Student> {
*/
public StudentTeam() {}
@Override
public void addMember(Student person) {
this.getMembers().add(person);
public List<Student> getStudents() {
List<Person> people = super.getMembers();
List<Student> students = new ArrayList<>();
people.forEach(person -> {
students.add((Student) person);
});
return students;
}
}

View File

@ -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<TeachingAssistant> {
public class TeachingAssistantTeam extends Team {
/**
* The role that this teaching assistant team plays.
@ -33,8 +35,10 @@ public class TeachingAssistantTeam extends Team<TeachingAssistant> {
*/
public TeachingAssistantTeam() {}
@Override
public void addMember(TeachingAssistant person) {
this.getMembers().add(person);
public List<TeachingAssistant> getTeachingAssistants() {
List<Person> people = super.getMembers();
List<TeachingAssistant> teachingAssistants = new ArrayList<>(people.size());
people.forEach(person -> teachingAssistants.add((TeachingAssistant) person));
return teachingAssistants;
}
}

View File

@ -17,7 +17,7 @@ import java.util.List;
@Inheritance(
strategy = InheritanceType.JOINED
)
public abstract class Team<P extends Person> extends BasicEntity {
public abstract class Team extends BasicEntity {
/**
* The list of members in this group.
@ -30,7 +30,7 @@ public abstract class Team<P extends Person> extends BasicEntity {
joinColumns = @JoinColumn(name = "team_id"),
inverseJoinColumns = @JoinColumn(name = "person_id")
)
protected List<P> members;
protected List<Person> 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<P extends Person> 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<P> people) {
for (P person : people) {
public void addMembers(List<Person> 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<P extends Person> extends BasicEntity {
* Gets a list of all members of this team.
* @return A list of all the members in this team.
*/
public List<P> getMembers() {
public List<Person> getMembers() {
return this.members;
}
@ -90,7 +90,7 @@ public abstract class Team<P extends Person> 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();

View File

@ -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<Student, Long> {
/**
* 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<Student> findByStudentNumber(int studentNumber);
}

View File

@ -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<StudentTeam, Long> {
}

View File

@ -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

View File

@ -11,6 +11,7 @@
<th>Name</th>
<th>Code</th>
<th>Created at</th>
<th>Students</th>
</tr>
<tr th:each="course: ${courses}">
<td>
@ -18,6 +19,7 @@
</td>
<td th:text="${course.getCode()}"></td>
<td th:text="${course.getCreatedOn()}"></td>
<td th:text="${course.getStudents().size()}"></td>
</tr>
</table>
</div>

View File

@ -41,6 +41,22 @@
</td>
</tr>
</table>
<h3>All Students (<span th:text="${course.getStudents().size()}"></span>):</h3>
<table>
<tr th:each="student: ${course.getStudents()}">
<td>
<a
th:href="@{/students/{student_id}
(student_id=${student.getId()})}">
Student <span th:text="${student.getId()}"></span>
</a>
</td>
<td th:text="${student.getFullName()}"></td>
<td th:text="${student.getStudentNumber()}"></td>
<td th:text="${student.getEmailAddress()}"></td>
</tr>
</table>
</div>
<div id="sidebar">