Compare commits
No commits in common. "a424c04937a93a77a3276411a415c275b890a90d" and "cb18e4755789c0fd92367a6d707febe110ca7fdb" have entirely different histories.
a424c04937
...
cb18e47557
@ -1,87 +1,27 @@
|
|||||||
package at.ionas999.questioncatalog.controller;
|
package at.ionas999.questioncatalog.controller;
|
||||||
|
|
||||||
import at.ionas999.questioncatalog.model.Question;
|
import at.ionas999.questioncatalog.model.Question;
|
||||||
import at.ionas999.questioncatalog.model.QuestionCatalog;
|
|
||||||
import at.ionas999.questioncatalog.services.QuestionCatalogService;
|
|
||||||
import at.ionas999.questioncatalog.services.QuestionService;
|
import at.ionas999.questioncatalog.services.QuestionService;
|
||||||
import at.ionas999.questioncatalog.services.ServiceBase;
|
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.event.ActionEvent;
|
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.ListView;
|
||||||
|
import javafx.scene.control.TextArea;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ViewController {
|
public class ViewController{
|
||||||
@FXML
|
public Button editButton;
|
||||||
private ComboBox selectBox;
|
public Button deleteButton;
|
||||||
@FXML
|
public TextField answerField;
|
||||||
private Button editButton;
|
public ListView questionListView;
|
||||||
@FXML
|
public TextField questionField;
|
||||||
private Button deleteButton;
|
|
||||||
@FXML
|
|
||||||
private TextField answerField;
|
|
||||||
@FXML
|
|
||||||
private ListView questionListView;
|
|
||||||
@FXML
|
|
||||||
private TextField questionField;
|
|
||||||
|
|
||||||
private Question currentQuestion = null;
|
|
||||||
private ObservableList<QuestionCatalog> catalogs;
|
|
||||||
private ObservableList<Question> questions;
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void initialize() throws SQLException {
|
private void initialize() throws SQLException {
|
||||||
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
|
List<Question> questions = QuestionService.GetQuestionsFromCatalog(1);
|
||||||
selectBox.setItems(catalogs);
|
System.out.println(questions);
|
||||||
addObserverToSelectBox();
|
|
||||||
addListenerToListView();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addListenerToListView() {
|
|
||||||
questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
|
|
||||||
currentQuestion = (Question) newValue;
|
|
||||||
if (newValue != null) {
|
|
||||||
Question q = (Question) newValue;
|
|
||||||
questionField.setText(q.getQuestion());
|
|
||||||
answerField.setText(q.getAnswer());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addObserverToSelectBox() {
|
|
||||||
selectBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
|
||||||
if (newValue == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
setQuestionListView(((QuestionCatalog) newValue).getId());
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setQuestionListView(int catalogId) throws SQLException {
|
|
||||||
this.questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalogId));
|
|
||||||
questionListView.setItems(questions);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
private void deleteQuestion(ActionEvent actionEvent) {
|
|
||||||
questions.remove(currentQuestion);
|
|
||||||
QuestionService.DeleteQuestion(currentQuestion.getId());
|
|
||||||
this.questionListView.refresh();
|
|
||||||
this.currentQuestion = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
private void editQuestion(ActionEvent actionEvent) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,17 +32,4 @@ public class Question {
|
|||||||
public void setAnswer(String answer) {
|
public void setAnswer(String answer) {
|
||||||
this.answer = answer;
|
this.answer = answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setQuestionCatalogId(int questionCatalogId) {
|
|
||||||
this.questionCatalogId = questionCatalogId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getId()
|
|
||||||
{
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return question;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,8 +11,7 @@ public class QuestionCatalog {
|
|||||||
private ArrayList<Question> questions;
|
private ArrayList<Question> questions;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public QuestionCatalog(int id, String name, ArrayList<Question> questions) {
|
public QuestionCatalog(String name, ArrayList<Question> questions) {
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.questions = questions;
|
this.questions = questions;
|
||||||
}
|
}
|
||||||
@ -21,20 +20,8 @@ public class QuestionCatalog {
|
|||||||
return FXCollections.observableList(questions);
|
return FXCollections.observableList(questions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setQuestions(ArrayList<Question> questions){
|
|
||||||
this.questions = questions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getId(){
|
|
||||||
return this.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,75 +0,0 @@
|
|||||||
package at.ionas999.questioncatalog.services;
|
|
||||||
|
|
||||||
import at.ionas999.questioncatalog.model.Question;
|
|
||||||
import at.ionas999.questioncatalog.model.QuestionCatalog;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class QuestionCatalogService {
|
|
||||||
public static boolean 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.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int id = GetLastInsertedCatalogWithoutQuestions(catalog.getName()).getId();
|
|
||||||
for(Question q : catalog.getQuestions()) {
|
|
||||||
q.setQuestionCatalogId(id);
|
|
||||||
QuestionService.AddQuestionToDb(q);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static QuestionCatalog GetLastInsertedCatalogWithoutQuestions(String name) throws SQLException {
|
|
||||||
String stmtString = "select id, name from QUESTIONCATALOG order by ID desc fetch first row only";
|
|
||||||
QuestionCatalog catalog;
|
|
||||||
try (Connection connection = ServiceBase.GetJDBCConnection()) {
|
|
||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
|
|
||||||
ResultSet rs = statement.executeQuery();
|
|
||||||
List<Question> questions = new ArrayList<Question>();
|
|
||||||
rs.next();
|
|
||||||
catalog = new QuestionCatalog(
|
|
||||||
rs.getInt(1),
|
|
||||||
rs.getString(2),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return catalog;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
|
|
||||||
String stmtString = "select id, name from QUESTIONCATALOG";
|
|
||||||
|
|
||||||
try (Connection connection = ServiceBase.GetJDBCConnection()) {
|
|
||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
|
|
||||||
ResultSet rs = statement.executeQuery();
|
|
||||||
ArrayList<QuestionCatalog> catalogs = new ArrayList<>();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
|
||||||
QuestionCatalog catalog = new QuestionCatalog(
|
|
||||||
rs.getInt(1),
|
|
||||||
rs.getString(2),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
catalogs.add(catalog);
|
|
||||||
}
|
|
||||||
|
|
||||||
return catalogs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -26,7 +26,7 @@ public class QuestionService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
|
public static List<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
|
||||||
String stmtString = "select id, questionCatalogId, question, answer from Question where questionCatalogId = ?";
|
String stmtString = "select id, questionCatalogId, question, answer from Question where questionCatalogId = ?";
|
||||||
|
|
||||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||||
@ -34,7 +34,7 @@ public class QuestionService {
|
|||||||
try (PreparedStatement statement = connection.prepareStatement(stmtString)){
|
try (PreparedStatement statement = connection.prepareStatement(stmtString)){
|
||||||
statement.setInt(1, catalogId);
|
statement.setInt(1, catalogId);
|
||||||
ResultSet rs = statement.executeQuery();
|
ResultSet rs = statement.executeQuery();
|
||||||
ArrayList<Question> questions = new ArrayList<Question>();
|
List<Question> questions = new ArrayList<Question>();
|
||||||
|
|
||||||
while (rs.next()){
|
while (rs.next()){
|
||||||
Question question = new Question(
|
Question question = new Question(
|
||||||
@ -50,20 +50,4 @@ public class QuestionService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean DeleteQuestion(int id){
|
|
||||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
|
||||||
String insertStmt = "delete from QUESTION where ID = ?";
|
|
||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
|
|
||||||
statement.setInt(1, id);
|
|
||||||
statement.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (SQLException _){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,7 @@ public abstract class ServiceBase {
|
|||||||
create table QuestionCatalog
|
create table QuestionCatalog
|
||||||
(
|
(
|
||||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
|
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
|
||||||
name varchar(10) not null unique
|
name varchar(10) not null
|
||||||
)
|
)
|
||||||
""";
|
""";
|
||||||
try (Statement stmt = connection.createStatement()) {
|
try (Statement stmt = connection.createStatement()) {
|
||||||
|
|||||||
@ -7,15 +7,6 @@
|
|||||||
fx:controller="at.ionas999.questioncatalog.controller.ViewController"
|
fx:controller="at.ionas999.questioncatalog.controller.ViewController"
|
||||||
prefHeight="400.0" prefWidth="600.0">
|
prefHeight="400.0" prefWidth="600.0">
|
||||||
|
|
||||||
<top>
|
|
||||||
<HBox alignment="TOP_RIGHT" spacing="10.0">
|
|
||||||
<padding>
|
|
||||||
<Insets top="10.0" right="10.0" bottom="0.0" left="10.0"/>
|
|
||||||
</padding>
|
|
||||||
<ComboBox fx:id="selectBox" promptText="Select an Catalog" />
|
|
||||||
</HBox>
|
|
||||||
</top>
|
|
||||||
|
|
||||||
<left>
|
<left>
|
||||||
<ListView fx:id="questionListView" prefWidth="200.0" />
|
<ListView fx:id="questionListView" prefWidth="200.0" />
|
||||||
</left>
|
</left>
|
||||||
@ -28,8 +19,8 @@
|
|||||||
<TextField fx:id="questionField" editable="false" />
|
<TextField fx:id="questionField" editable="false" />
|
||||||
<TextField fx:id="answerField" promptText="Answer" editable="false" />
|
<TextField fx:id="answerField" promptText="Answer" editable="false" />
|
||||||
<HBox spacing="10.0" alignment="BOTTOM_RIGHT">
|
<HBox spacing="10.0" alignment="BOTTOM_RIGHT">
|
||||||
<Button fx:id="deleteButton" onAction="#deleteQuestion" text="Delete" />
|
<Button fx:id="deleteButton" text="Delete" />
|
||||||
<Button fx:id="editButton" text="Edit" onAction="#editQuestion" />
|
<Button fx:id="editButton" text="Edit" />
|
||||||
</HBox>
|
</HBox>
|
||||||
</VBox>
|
</VBox>
|
||||||
</center>
|
</center>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user