Skip to content
Snippets Groups Projects
  • Kevin Thaller's avatar
    75cae095
    big update: · 75cae095
    Kevin Thaller authored
    -add toString() for Course, Student and CoursesOfStudy
    -fix null-pointer-exception in detailControllers + SwitchWindowHelper
    -add CoursesOfStudy Column in Student
    -add find-Methods to JpaService
    -connect Gui to DB
    75cae095
    History
    big update:
    Kevin Thaller authored
    -add toString() for Course, Student and CoursesOfStudy
    -fix null-pointer-exception in detailControllers + SwitchWindowHelper
    -add CoursesOfStudy Column in Student
    -add find-Methods to JpaService
    -connect Gui to DB
CourseModificationController.java 4.54 KiB
package de.thdeg.grademanager.gui;

import de.thdeg.grademanager.JpaService;
import de.thdeg.grademanager.model.Course;
import de.thdeg.grademanager.model.CoursesOfStudy;
import de.thdeg.grademanager.model.enumeration.*;
import de.thdeg.grademanager.model.enumeration.CourseType;
import de.thdeg.grademanager.model.enumeration.ExamType;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.io.IOException;
import java.util.Optional;

public class CourseModificationController {
    @FXML
    protected ComboBox<Semester> semesterComboBox;
    @FXML
    protected ComboBox<CourseType> courseTypeComboBox;
    @FXML
    protected ComboBox<ExamType> examTypeComboBox;
    ObservableList<Semester> bachelorSemesterList = FXCollections.observableArrayList(Semester.values());
    ObservableList<CourseType> courseTypeList = FXCollections.observableArrayList(CourseType.values());
    ObservableList<ExamType> examTypeList = FXCollections.observableArrayList(ExamType.values());


    private static CoursesOfStudy coursesOfStudy;

    public static void setCoursesOfStudy(CoursesOfStudy coursesOfStudy) {
        CourseModificationController.coursesOfStudy = coursesOfStudy;
    }

    @FXML
    protected void initialize() {
        semesterComboBox.setItems(bachelorSemesterList);
        courseTypeComboBox.setItems(courseTypeList);
        examTypeComboBox.setItems(examTypeList);
    }


    @FXML
    protected TextField name;
    @FXML
    protected Semester semester;
    @FXML
    protected CourseType courseType;
    @FXML
    protected TextField sws;
    @FXML
    protected TextField ects;
    @FXML
    protected ExamType examType;
    @FXML
    protected TextField warning;


    @FXML
    protected void onSaveButtonClick(ActionEvent event) throws IOException {
        if (!name.getText().isBlank() && semesterComboBox.getValue() != null &&
                courseTypeComboBox.getValue() != null && !sws.getText().isBlank() && !ects.getText().isBlank() &&
                examTypeComboBox.getValue() != null) {
            semester = semesterComboBox.getValue();
            courseType = courseTypeComboBox.getValue();
            examType = examTypeComboBox.getValue();
            try {

                CourseModificationController.coursesOfStudy.addCourse(new Course(
                        name.getText(),
                        semester.getSemester(),
                        courseType.getCourseType(),
                        Integer.parseInt(sws.getText()),
                        Double.parseDouble(ects.getText()),
                        examType.getExamType(),
                        false)); //TODO: Add UI-Element for isCredited


                JpaService jpaService = JpaService.getInstance();
                jpaService.runInTransaction(entityManager -> {
                    entityManager.merge(CourseModificationController.coursesOfStudy);
                    entityManager.flush();
                    return null;
                });

                SwitchWindowHelper.switchTo("CoursesOfStudy Details", event);
            } catch (NumberFormatException e) {
                warning.setText("Bitte ganze Zahlen eingeben für SWS und ECTS.");
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Fehler");
                alert.setHeaderText("Bitte ganze Zahlen eingeben für SWS und ECTS.");
                Optional<ButtonType> result = alert.showAndWait();
            }

        } else {
            warning.setText("Bitte alle Felder ausfüllen.");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Fehler");
            alert.setHeaderText("Bitte alle Felder ausfüllen.");
            Optional<ButtonType> result = alert.showAndWait();
        }
    }

    @FXML
    protected void abortAndSwitchToCoursesOfStudyDetails(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Bestätigung erforderlich");
        alert.setHeaderText("Daten wurden nicht gespeichert. Änderungen verwerfen?");

        ButtonType buttonTypeOne = new ButtonType("Ja");
        ButtonType buttonTypeCancel = new ButtonType("Abbrechen", ButtonBar.ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeOne) {
            SwitchWindowHelper.switchTo("CoursesOfStudy Details", event);
        }
    }
}