implemente the import without error handeling

This commit is contained in:
Jonas Hinterdorfer 2025-05-12 21:17:47 +02:00
parent a87c20208e
commit 4fcda75290
3 changed files with 55 additions and 41 deletions

View File

@ -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<QuestionCatalog> catalogs;
private ObjectProperty<QuestionCatalog> 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("");
}
}

View File

@ -23,11 +23,11 @@
</padding>
<Label text="Select Catalog:"/>
<ListView fx:id="catalogListViw" VBox.vgrow="ALWAYS"/>
<HBox spacing="5">
<TextField fx:id="newCatalogField" promptText="New Catalog Name" HBox.hgrow="ALWAYS"/>
<Button fx:id="addCatalogButton" text="+" onAction="#onAddNewCatalogBtnClick"/>
<Button fx:id="deleteCatalogButton" text="-" onAction="#onDeleteCatalogBtnClick"/>
</HBox>
<HBox spacing="5">
<TextField fx:id="newCatalogField" promptText="New Catalog Name" HBox.hgrow="ALWAYS"/>
<Button fx:id="addCatalogButton" text="+" onAction="#onAddNewCatalogBtnClick"/>
<Button fx:id="deleteCatalogButton" text="-" onAction="#onDeleteCatalogBtnClick"/>
</HBox>
</VBox>
</left>
@ -36,10 +36,9 @@
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
<Label text="Upload CSV File"/>
<Button fx:id="uploadButton" text="Choose File"/>
<Label fx:id="fileNameLabel" text="No file selected"/>
<Button fx:id="importButton" text="Import Questions" disable="true"/>
<Label text="Paste CSV Content Below:"/>
<TextArea fx:id="csvContentArea" promptText="Paste your CSV content here..." prefHeight="200.0"/>
<Button fx:id="importButton" text="Import Questions" onAction="#onImportBtnClick"/>
</VBox>
</center>
</BorderPane>

View File

@ -1,35 +1,34 @@
<?import javafx.geometry.Insets?>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="at.ionas999.questioncatalog.controller.ViewController"
prefHeight="400.0" prefWidth="600.0">
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.12" xmlns:fx="http://javafx.com/fxml/1" fx:controller="at.ionas999.questioncatalog.controller.ViewController">
<top>
<HBox alignment="TOP_RIGHT" spacing="10.0">
<padding>
<Insets top="10.0" right="10.0" bottom="0.0" left="10.0"/>
<Insets bottom="0.0" left="10.0" right="10.0" top="10.0" />
</padding>
<ComboBox fx:id="selectBox" promptText="Select an Catalog" />
</HBox>
</top>
<left>
<ListView fx:id="questionListView" prefWidth="200.0" />
<ListView fx:id="questionListView" prefHeight="364.0" prefWidth="209.0" />
</left>
<center>
<VBox spacing="10.0">
<padding>
<Insets top="10.0" right="10.0" bottom="10.0" left="10.0"/>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<TextField fx:id="questionField" editable="false" />
<TextField fx:id="answerField" promptText="Answer" editable="false" />
<HBox spacing="10.0" alignment="BOTTOM_RIGHT">
<TextField fx:id="answerField" editable="false" promptText="Answer" />
<HBox alignment="BOTTOM_RIGHT" spacing="10.0">
<Button fx:id="deleteButton" onAction="#deleteQuestion" text="Delete" />
<Button fx:id="editButton" text="Edit" onAction="#editQuestion" />
<Button fx:id="editButton" onAction="#editQuestion" text="Edit" />
</HBox>
</VBox>
</center>