implemeted deleted and adding a new question catalog
This commit is contained in:
parent
d294716316
commit
a87c20208e
@ -3,10 +3,13 @@ package at.ionas999.questioncatalog;
|
|||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.control.Alert;
|
||||||
|
import javafx.scene.control.ButtonType;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
public static void switchScenes(String fxmlFileName, Stage stage) throws IOException {
|
public static void switchScenes(String fxmlFileName, Stage stage) throws IOException {
|
||||||
@ -20,4 +23,20 @@ public class Utils {
|
|||||||
public static Stage getStageFromActionEven(ActionEvent actionEvent){
|
public static Stage getStageFromActionEven(ActionEvent actionEvent){
|
||||||
return (Stage) ((javafx.scene.Node) actionEvent.getSource()).getScene().getWindow();
|
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<ButtonType> result = alert.showAndWait();
|
||||||
|
|
||||||
|
return result.filter(buttonType -> buttonType == yesButton).isPresent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,71 @@
|
|||||||
package at.ionas999.questioncatalog.controller;
|
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.Button;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class UploadController {
|
public class UploadController {
|
||||||
public ListView catalogListView;
|
@FXML
|
||||||
public TextField newCatalogField;
|
private Button deleteCatalogButton;
|
||||||
public Button addCatalogButton;
|
@FXML
|
||||||
public ListView catalogListViw;
|
private TextField newCatalogField;
|
||||||
public Button uploadButton;
|
@FXML
|
||||||
public Label fileNameLabel;
|
private Button addCatalogButton;
|
||||||
public Button importButton;
|
@FXML
|
||||||
|
private ListView catalogListViw;
|
||||||
|
@FXML
|
||||||
|
private Button uploadButton;
|
||||||
|
@FXML
|
||||||
|
private Label fileNameLabel;
|
||||||
|
@FXML
|
||||||
|
private Button importButton;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static at.ionas999.questioncatalog.Utils.showConfirmationButton;
|
||||||
|
|
||||||
public class ViewController {
|
public class ViewController {
|
||||||
@FXML
|
@FXML
|
||||||
private ComboBox selectBox;
|
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<ButtonType> result = alert.showAndWait();
|
|
||||||
|
|
||||||
return result.filter(buttonType -> buttonType == yesButton).isPresent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class QuestionCatalog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ObservableList<Question> getQuestions() {
|
public ObservableList<Question> getQuestions() {
|
||||||
return FXCollections.observableList(questions);
|
return questions == null ? null : FXCollections.observableList(questions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setQuestions(ArrayList<Question> questions){
|
public void setQuestions(ArrayList<Question> questions){
|
||||||
|
|||||||
@ -11,22 +11,26 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class QuestionCatalogService {
|
public class QuestionCatalogService {
|
||||||
public static boolean AddQuestionCatalogToDb(QuestionCatalog catalog) throws SQLException {
|
public static QuestionCatalog AddQuestionCatalogToDb(QuestionCatalog catalog) throws SQLException {
|
||||||
try (Connection connection = ServiceBase.GetJDBCConnection()) {
|
try (Connection connection = ServiceBase.GetJDBCConnection()) {
|
||||||
String insertStmt = "insert into QUESTIONCATALOG (NAME) VALUES (?)";
|
String insertStmt = "insert into QUESTIONCATALOG (NAME) VALUES (?)";
|
||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement(insertStmt)) {
|
try (PreparedStatement statement = connection.prepareStatement(insertStmt)) {
|
||||||
statement.setString(2, catalog.getName());
|
statement.setString(1, catalog.getName());
|
||||||
statement.execute();
|
statement.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int id = GetLastInsertedCatalogWithoutQuestions(catalog.getName()).getId();
|
QuestionCatalog c = GetLastInsertedCatalogWithoutQuestions(catalog.getName());
|
||||||
|
|
||||||
|
if(catalog.getQuestions() != null)
|
||||||
|
{
|
||||||
for(Question q : catalog.getQuestions()) {
|
for(Question q : catalog.getQuestions()) {
|
||||||
q.setQuestionCatalogId(id);
|
q.setQuestionCatalogId(c.getId());
|
||||||
QuestionService.AddQuestionToDb(q);
|
QuestionService.AddQuestionToDb(q);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +41,6 @@ public class QuestionCatalogService {
|
|||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
|
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
|
||||||
ResultSet rs = statement.executeQuery();
|
ResultSet rs = statement.executeQuery();
|
||||||
List<Question> questions = new ArrayList<Question>();
|
|
||||||
rs.next();
|
rs.next();
|
||||||
catalog = new QuestionCatalog(
|
catalog = new QuestionCatalog(
|
||||||
rs.getInt(1),
|
rs.getInt(1),
|
||||||
@ -49,7 +52,21 @@ public class QuestionCatalogService {
|
|||||||
}
|
}
|
||||||
return catalog;
|
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<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
|
public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
|
||||||
String stmtString = "select id, name from QUESTIONCATALOG";
|
String stmtString = "select id, name from QUESTIONCATALOG";
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,7 @@ public abstract class ServiceBase {
|
|||||||
questionCatalogId integer,
|
questionCatalogId integer,
|
||||||
question varchar(100) not null,
|
question varchar(100) not null,
|
||||||
answer 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()) {
|
try (Statement stmt = connection.createStatement()) {
|
||||||
|
|||||||
@ -25,7 +25,8 @@
|
|||||||
<ListView fx:id="catalogListViw" VBox.vgrow="ALWAYS"/>
|
<ListView fx:id="catalogListViw" VBox.vgrow="ALWAYS"/>
|
||||||
<HBox spacing="5">
|
<HBox spacing="5">
|
||||||
<TextField fx:id="newCatalogField" promptText="New Catalog Name" HBox.hgrow="ALWAYS"/>
|
<TextField fx:id="newCatalogField" promptText="New Catalog Name" HBox.hgrow="ALWAYS"/>
|
||||||
<Button fx:id="addCatalogButton" text="+"/>
|
<Button fx:id="addCatalogButton" text="+" onAction="#onAddNewCatalogBtnClick"/>
|
||||||
|
<Button fx:id="deleteCatalogButton" text="-" onAction="#onDeleteCatalogBtnClick"/>
|
||||||
</HBox>
|
</HBox>
|
||||||
</VBox>
|
</VBox>
|
||||||
</left>
|
</left>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user