Skip to content
Snippets Groups Projects
Commit 4f8d3c9b authored by Kevin Thaller's avatar Kevin Thaller
Browse files

bug fix no duplicate (multiple) courses in CourseModificationController +...

bug fix no duplicate (multiple) courses in CourseModificationController + CoursesOfStudyDetailsController anymore
parent 99ae2123
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,12 @@
<list default="true" id="ce56effb-683b-43e9-9335-7e3aa5d26c29" name="Changes" comment="fix SwitchWindowHelper to work with all event types">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/JpaService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/JpaService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/CourseModificationController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/CourseModificationController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/CoursesOfStudyDetailsController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/CoursesOfStudyDetailsController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/SwitchWindowHelper.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/SwitchWindowHelper.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/StudentCoursesGradesController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/StudentCoursesGradesController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/StudentDetailsController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/gui/StudentDetailsController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/CoursesOfStudy.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/CoursesOfStudy.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/Enrollment.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/Enrollment.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/Student.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/de/thdeg/grademanager/model/Student.java" afterDir="false" />
</list>
......@@ -140,7 +143,14 @@
<option name="project" value="LOCAL" />
<updated>1655647132967</updated>
</task>
<option name="localTasksCounter" value="8" />
<task id="LOCAL-00008" summary="fix SwitchWindowHelper to work with all event types">
<created>1655652253362</created>
<option name="number" value="00008" />
<option name="presentableId" value="LOCAL-00008" />
<option name="project" value="LOCAL" />
<updated>1655652253362</updated>
</task>
<option name="localTasksCounter" value="9" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......@@ -174,6 +184,7 @@
<MESSAGE value="bugfix in course modification option" />
<MESSAGE value="created new window for student courses and grades" />
<MESSAGE value="add StudentStats Screen + Controller" />
<option name="LAST_COMMIT_MESSAGE" value="add StudentStats Screen + Controller" />
<MESSAGE value="fix SwitchWindowHelper to work with all event types" />
<option name="LAST_COMMIT_MESSAGE" value="fix SwitchWindowHelper to work with all event types" />
</component>
</project>
\ No newline at end of file
package de.thdeg.grademanager;
import de.thdeg.grademanager.model.Course;
import de.thdeg.grademanager.model.CoursesOfStudy;
import de.thdeg.grademanager.model.Student;
import jakarta.persistence.*;
......@@ -12,31 +13,25 @@ import java.util.function.Function;
* You can configure the database connection in the
* {@link /src/main/resources/META-INF/persistence.xml} file.
*/
public class JpaService
{
public class JpaService {
private static JpaService instance;
private EntityManagerFactory entityManagerFactory;
private JpaService()
{
private JpaService() {
entityManagerFactory = Persistence.createEntityManagerFactory("jpa-hibernate-notenmanager");
}
public static synchronized JpaService getInstance()
{
public static synchronized JpaService getInstance() {
return instance == null ? instance = new JpaService() : instance;
}
public void closeResource()
{
if(entityManagerFactory != null)
{
public void closeResource() {
if (entityManagerFactory != null) {
entityManagerFactory.close();
}
}
public EntityManagerFactory getEntityManagerFactory()
{
public EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
......@@ -49,43 +44,65 @@ public class JpaService
* @param <T> The function's return type.
* @return the value returned by the specified function.
*/
public <T> T runInTransaction(Function <EntityManager, T> function)
{
public <T> T runInTransaction(Function<EntityManager, T> function) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
boolean isPersisted = false;
entityTransaction.begin();
try
{
try {
T returnValue = function.apply(entityManager);
isPersisted = true;
return returnValue;
}
finally
{
if(isPersisted)
{
} finally {
if (isPersisted) {
entityTransaction.commit();
}
else
{
} else {
entityTransaction.rollback();
}
}
}
public List<Student> getStudentsFromDb(){
public List<Student> getStudentsFromDb() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT q FROM Student q", Student.class);
Query query = entityManager.createQuery("SELECT q FROM Student q", Student.class);
return query.getResultList();
}
public List<CoursesOfStudy> getCoursesOfStudyFromDb(){
public List<CoursesOfStudy> getCoursesOfStudyFromDb() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT q FROM CoursesOfStudy q", CoursesOfStudy.class);
return query.getResultList();
}
public List<Course> getCoursesForCoursesOfStudyFromDb(CoursesOfStudy coursesOfStudy) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT q FROM Course q WHERE q.coursesOfStudy=:coursesOfStudy");
query.setParameter("coursesOfStudy", coursesOfStudy);
return query.getResultList();
}
public Student getStudentFromDb(int id){
EntityManager entityManager = entityManagerFactory.createEntityManager();
//Query query = entityManager.find(student, Student.class);
Query query = entityManager.createQuery("SELECT p FROM Student p WHERE p.id=:id");
query.setParameter("id", id);
return (Student) query.getSingleResult();
}
public Course getCourseFromDb(int id){
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT p FROM Course p WHERE p.id=:id");
query.setParameter("id", id);
return (Course) query.getSingleResult();
}
public CoursesOfStudy getCoursesOfStudyFromDb(int id){
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("SELECT p FROM CoursesOfStudy p WHERE p.id=:id");
query.setParameter("id", id);
return (CoursesOfStudy) query.getSingleResult();
}
}
......@@ -84,8 +84,10 @@ public class CourseModificationController {
courseType = courseTypeComboBox.getValue();
examType = examTypeComboBox.getValue();
try {
CourseModificationController.coursesOfStudy.addCourse(new Course(
JpaService jpaService = JpaService.getInstance();
coursesOfStudy = jpaService.getCoursesOfStudyFromDb(coursesOfStudy.getId());
jpaService.runInTransaction(entityManager -> {entityManager.detach(coursesOfStudy); return null;});
coursesOfStudy.addCourse(new Course(
name.getText(),
semester.getSemester(),
courseType.getCourseType(),
......@@ -95,14 +97,15 @@ public class CourseModificationController {
false)); //TODO: Add UI-Element for isCredited
JpaService jpaService = JpaService.getInstance();
System.out.println(coursesOfStudy.getCourses());//Important: Hibernate throws warning (bug) if this line is not here
jpaService.runInTransaction(entityManager -> {
entityManager.merge(CourseModificationController.coursesOfStudy);
entityManager.flush();
entityManager.merge(coursesOfStudy);
return null;
});
SwitchWindowHelper.switchTo("CoursesOfStudy Details", event);
newCourse = true;
} catch (NumberFormatException e) {
warning.setText("Bitte ganze Zahlen eingeben für SWS und ECTS.");
Alert alert = new Alert(Alert.AlertType.ERROR);
......@@ -120,7 +123,10 @@ public class CourseModificationController {
}
} else {
if (!name.getText().isBlank() && !sws.getText().isBlank() && !ects.getText().isBlank()) {
JpaService jpaService = JpaService.getInstance();
CoursesOfStudy cos = jpaService.getCoursesOfStudyFromDb(coursesOfStudy.getId());
course = jpaService.getCourseFromDb(course.getId());
jpaService.runInTransaction(entityManager -> {entityManager.detach(cos); return null;});
course.setName(name.getText());
if(semesterComboBox.getValue() != null){
semester = semesterComboBox.getValue();
......@@ -136,13 +142,10 @@ public class CourseModificationController {
examType = examTypeComboBox.getValue();
course.setExamType(examType.getExamType());
}
JpaService jpaService = JpaService.getInstance();
jpaService.runInTransaction(entityManager -> {entityManager.merge(coursesOfStudy); return null;});
cos.updateCourse(course);
jpaService.runInTransaction(entityManager -> {entityManager.merge(cos); return null;});
initialize();
newCourse = true;
SwitchWindowHelper.switchTo("CoursesOfStudy Details", event);
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
......
......@@ -56,7 +56,9 @@ public class CoursesOfStudyDetailsController {
fieldOfStudy.setText(coursesOfStudy.getFieldOfStudy());
fees.setText(Integer.toString(coursesOfStudy.getFees()));
courseList.setAll(coursesOfStudy.getCourses());
JpaService jpaService = JpaService.getInstance();
courseList.setAll(jpaService.getCoursesForCoursesOfStudyFromDb(coursesOfStudy));
if (!courseList.isEmpty()) {
courseListView.setItems(courseList);
}
......
package de.thdeg.grademanager.gui;
import de.thdeg.grademanager.JpaService;
import de.thdeg.grademanager.model.Course;
import de.thdeg.grademanager.model.CoursesOfStudy;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.List;
public class Main extends Application {
......
......@@ -2,14 +2,11 @@ package de.thdeg.grademanager.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
@Entity
@Table(name = "coursesOfStudy")
public class CoursesOfStudy
{
public class CoursesOfStudy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
......@@ -50,13 +47,11 @@ public class CoursesOfStudy
@JoinColumn(name = "courses_of_study_id")
private CoursesOfStudy coursesOfStudy;
*/
public CoursesOfStudy()
{
public CoursesOfStudy() {
}
public CoursesOfStudy(String name, String degree, int duration, int fees, String fieldOfStudy)
{
public CoursesOfStudy(String name, String degree, int duration, int fees, String fieldOfStudy) {
this.name = name;
this.degree = degree;
this.duration = duration;
......@@ -65,8 +60,7 @@ public class CoursesOfStudy
}
@Override
public boolean equals(Object o)
{
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CoursesOfStudy that = (CoursesOfStudy) o;
......@@ -74,8 +68,7 @@ public class CoursesOfStudy
}
@Override
public int hashCode()
{
public int hashCode() {
return Objects.hash(id);
}
......@@ -84,75 +77,75 @@ public class CoursesOfStudy
return name;
}
public int getId()
{
public int getId() {
return id;
}
public void setId(int id)
{
public void setId(int id) {
this.id = id;
}
public String getName()
{
public String getName() {
return name;
}
public void setName(String name)
{
public void setName(String name) {
this.name = name;
}
public String getDegree()
{
public String getDegree() {
return degree;
}
public void setDegree(String degree)
{
public void setDegree(String degree) {
this.degree = degree;
}
public int getDuration()
{
public int getDuration() {
return duration;
}
public void setDuration(int duration)
{
public void setDuration(int duration) {
this.duration = duration;
}
public int getFees()
{
public int getFees() {
return fees;
}
public void setFees(int fees)
{
public void setFees(int fees) {
this.fees = fees;
}
public String getFieldOfStudy()
{
public String getFieldOfStudy() {
return fieldOfStudy;
}
public void setFieldOfStudy(String fieldOfStudy)
{
public void setFieldOfStudy(String fieldOfStudy) {
this.fieldOfStudy = fieldOfStudy;
}
public void addCourse(Course course)
{
public void addCourse(Course course) {
courses.add(course);
course.setCoursesOfStudy(this);
}
public void removeCourse(Course course)
{
public void removeCourse(Course course) {
courses.remove(course);
course.setCoursesOfStudy(null);
}
public void updateCourse(Course course){
int index = findCourseID(course);
courses.set(index, course);
}
private int findCourseID(Course course){
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getId() == course.getId()){
return i;
}
}
return -1;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment