diff --git a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java index 1b4f7b1..f61adee 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java @@ -1,23 +1,27 @@ package at.ionas999.questioncatalog.controller; import at.ionas999.questioncatalog.Utils; +import at.ionas999.questioncatalog.model.Question; import at.ionas999.questioncatalog.model.QuestionCatalog; import at.ionas999.questioncatalog.services.QuestionCatalogService; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleObjectProperty; +import at.ionas999.questioncatalog.services.QuestionService; 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 javafx.scene.control.*; +import javafx.stage.FileChooser; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; public class UploadController { + public TextArea csvContentArea; @FXML private Button deleteCatalogButton; @FXML @@ -35,37 +39,49 @@ public class UploadController { 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); + 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) + QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); + if (c == null) { return; - + } + 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(); } -} + + + @FXML + private void onImportBtnClick(ActionEvent actionEvent) { + QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); + String csvContent = csvContentArea.getText(); + String[] lines = csvContent.split("\n"); + + for(String line : lines) + { + String[] segments = line.split(";"); + Question q = new Question(-1, segments[0], segments[1], c.getId()); + QuestionService.AddQuestionToDb(q); + } + csvContentArea.setText(""); + } +} \ No newline at end of file diff --git a/src/main/resources/at/ionas999/questioncatalog/upload.fxml b/src/main/resources/at/ionas999/questioncatalog/upload.fxml index 3766dff..def5f63 100644 --- a/src/main/resources/at/ionas999/questioncatalog/upload.fxml +++ b/src/main/resources/at/ionas999/questioncatalog/upload.fxml @@ -23,11 +23,11 @@