diff --git a/src/main/java/at/ionas999/questioncatalog/Utils.java b/src/main/java/at/ionas999/questioncatalog/Utils.java index 547d91e..42d3b03 100644 --- a/src/main/java/at/ionas999/questioncatalog/Utils.java +++ b/src/main/java/at/ionas999/questioncatalog/Utils.java @@ -3,10 +3,13 @@ package at.ionas999.questioncatalog; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; import javafx.stage.Stage; import javafx.event.ActionEvent; import java.io.IOException; +import java.util.Optional; public class Utils { public static void switchScenes(String fxmlFileName, Stage stage) throws IOException { @@ -20,4 +23,20 @@ public class Utils { public static Stage getStageFromActionEven(ActionEvent actionEvent){ return (Stage) ((javafx.scene.Node) actionEvent.getSource()).getScene().getWindow(); } + public static boolean showConfirmationButton(String content) { + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Confirmation Dialog"); + alert.setHeaderText("Are you sure?"); + alert.setContentText(content); + + ButtonType yesButton = new ButtonType("Yes"); + ButtonType cancelButton = new ButtonType("Cancel"); + + alert.getButtonTypes().setAll(yesButton, cancelButton); + + Optional result = alert.showAndWait(); + + return result.filter(buttonType -> buttonType == yesButton).isPresent(); + } } diff --git a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java index b6a81f0..1b4f7b1 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java @@ -1,16 +1,71 @@ package at.ionas999.questioncatalog.controller; +import at.ionas999.questioncatalog.Utils; +import at.ionas999.questioncatalog.model.QuestionCatalog; +import at.ionas999.questioncatalog.services.QuestionCatalogService; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.TextField; +import java.sql.SQLException; +import java.util.ArrayList; + public class UploadController { - public ListView catalogListView; - public TextField newCatalogField; - public Button addCatalogButton; - public ListView catalogListViw; - public Button uploadButton; - public Label fileNameLabel; - public Button importButton; + @FXML + private Button deleteCatalogButton; + @FXML + private TextField newCatalogField; + @FXML + private Button addCatalogButton; + @FXML + private ListView catalogListViw; + @FXML + private Button uploadButton; + @FXML + private Label fileNameLabel; + @FXML + private Button importButton; + + ObservableList catalogs; + + private ObjectProperty currentCatalog = new SimpleObjectProperty<>(); + + @FXML + private void initialize() throws SQLException { + currentCatalog.bind(catalogListViw.getSelectionModel().selectedItemProperty()); + catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); + catalogListViw.setItems(catalogs); + } + + + @FXML + private void onAddNewCatalogBtnClick(ActionEvent actionEvent) throws SQLException { + String name = newCatalogField.getText(); + QuestionCatalog c = new QuestionCatalog(-1, name, null); + System.out.println(c); + QuestionCatalogService.AddQuestionCatalogToDb(c); + this.catalogs.add(c); + catalogListViw.refresh(); + } + + + @FXML + private void onDeleteCatalogBtnClick(ActionEvent actionEvent) { + QuestionCatalog c = currentCatalog.get(); + boolean result = Utils.showConfirmationButton("Do you want to proceed with deleting a catalog with all questions contained?"); + + if(!result) + return; + + QuestionCatalogService.DeleteQuestionCatalog(c.getId()); + catalogs.remove(c); + catalogListViw.refresh(); + } } diff --git a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java index b0254e0..420430c 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java @@ -16,6 +16,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import static at.ionas999.questioncatalog.Utils.showConfirmationButton; + public class ViewController { @FXML private ComboBox selectBox; @@ -114,20 +116,5 @@ public class ViewController { } - private boolean showConfirmationButton(String content) { - Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.setTitle("Confirmation Dialog"); - alert.setHeaderText("Are you sure?"); - alert.setContentText(content); - - ButtonType yesButton = new ButtonType("Yes"); - ButtonType cancelButton = new ButtonType("Cancel"); - - alert.getButtonTypes().setAll(yesButton, cancelButton); - - Optional result = alert.showAndWait(); - - return result.filter(buttonType -> buttonType == yesButton).isPresent(); - } } diff --git a/src/main/java/at/ionas999/questioncatalog/model/QuestionCatalog.java b/src/main/java/at/ionas999/questioncatalog/model/QuestionCatalog.java index d470aac..798588b 100644 --- a/src/main/java/at/ionas999/questioncatalog/model/QuestionCatalog.java +++ b/src/main/java/at/ionas999/questioncatalog/model/QuestionCatalog.java @@ -18,7 +18,7 @@ public class QuestionCatalog { } public ObservableList getQuestions() { - return FXCollections.observableList(questions); + return questions == null ? null : FXCollections.observableList(questions); } public void setQuestions(ArrayList questions){ diff --git a/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java b/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java index efdb553..3cd46d8 100644 --- a/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java +++ b/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java @@ -11,22 +11,26 @@ import java.util.ArrayList; import java.util.List; public class QuestionCatalogService { - public static boolean AddQuestionCatalogToDb(QuestionCatalog catalog) throws SQLException { + public static QuestionCatalog AddQuestionCatalogToDb(QuestionCatalog catalog) throws SQLException { try (Connection connection = ServiceBase.GetJDBCConnection()) { String insertStmt = "insert into QUESTIONCATALOG (NAME) VALUES (?)"; try (PreparedStatement statement = connection.prepareStatement(insertStmt)) { - statement.setString(2, catalog.getName()); + statement.setString(1, catalog.getName()); statement.execute(); } } - int id = GetLastInsertedCatalogWithoutQuestions(catalog.getName()).getId(); - for(Question q : catalog.getQuestions()) { - q.setQuestionCatalogId(id); - QuestionService.AddQuestionToDb(q); + QuestionCatalog c = GetLastInsertedCatalogWithoutQuestions(catalog.getName()); + + if(catalog.getQuestions() != null) + { + for(Question q : catalog.getQuestions()) { + q.setQuestionCatalogId(c.getId()); + QuestionService.AddQuestionToDb(q); + } } - return true; + return c; } @@ -37,7 +41,6 @@ public class QuestionCatalogService { try (PreparedStatement statement = connection.prepareStatement(stmtString)) { ResultSet rs = statement.executeQuery(); - List questions = new ArrayList(); rs.next(); catalog = new QuestionCatalog( rs.getInt(1), @@ -49,7 +52,21 @@ public class QuestionCatalogService { } return catalog; } + public static boolean DeleteQuestionCatalog(int id){ + try(Connection connection = ServiceBase.GetJDBCConnection()){ + String insertStmt = "delete from QUESTIONCATALOG where ID = ?"; + try (PreparedStatement statement = connection.prepareStatement(insertStmt)){ + statement.setInt(1, id); + statement.execute(); + } + } + catch (SQLException _){ + return false; + } + + return true; + } public static ArrayList GetCatalogsWithoutQuestions() throws SQLException { String stmtString = "select id, name from QUESTIONCATALOG"; diff --git a/src/main/java/at/ionas999/questioncatalog/services/ServiceBase.java b/src/main/java/at/ionas999/questioncatalog/services/ServiceBase.java index 8a3f2e0..088cd1c 100644 --- a/src/main/java/at/ionas999/questioncatalog/services/ServiceBase.java +++ b/src/main/java/at/ionas999/questioncatalog/services/ServiceBase.java @@ -37,7 +37,7 @@ public abstract class ServiceBase { questionCatalogId integer, question varchar(100) not null, answer varchar(100) not null, - constraint fk_questionCatalog foreign key (questionCatalogId) references QuestionCatalog(id) + constraint fk_questionCatalog foreign key (questionCatalogId) references QuestionCatalog(id) on delete cascade ) """; try (Statement stmt = connection.createStatement()) { diff --git a/src/main/resources/at/ionas999/questioncatalog/upload.fxml b/src/main/resources/at/ionas999/questioncatalog/upload.fxml index fd43c05..3766dff 100644 --- a/src/main/resources/at/ionas999/questioncatalog/upload.fxml +++ b/src/main/resources/at/ionas999/questioncatalog/upload.fxml @@ -23,10 +23,11 @@