package edu.spbstu.hoteldb.handlers;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import edu.spbstu.hoteldb.interfaces.SQLQuery;
import edu.spbstu.hoteldb.sql.SearchRoomQuery;
public class SearchRoomEventHandler implements EventHandler<ActionEvent> {
private final TextArea results;
private final TextField tfpcount;
private final TextField tfcost;
private final CheckBox chckbxdesc;
private final ChoiceBox<String> chbbar;
private Connection connect;
public SearchRoomEventHandler(Connection connection, TextArea ta,
TextField tfpc, TextField tfc, CheckBox chbd, ChoiceBox<String> chbb) {
results = ta;
tfpcount = tfpc;
tfcost = tfc;
chckbxdesc = chbd;
chbbar = chbb;
connect = connection;
}
@Override
public void handle(ActionEvent arg0) {
SQLQuery query = new SearchRoomQuery(connect, results);
LinkedList<Object> args = new LinkedList<Object>();
Integer pc = Integer.MAX_VALUE;
Integer cost = Integer.MAX_VALUE;
try {
pc = Integer.parseInt(tfpcount.getText());
} catch (NumberFormatException e) {
// ignore
}
try {
cost = Integer.parseInt(tfcost.getText());
} catch (NumberFormatException e) {
// ignore
}
args.addLast(pc);
args.addLast(cost);
args.addLast(new Boolean(chckbxdesc.isSelected()));
args.addLast(chbbar.getValue());
try {
query.prepareQuery(args);
query.doQuery();
query.close();
} catch (SQLException e) {
results.appendText(e.getMessage() + '\n');
e.printStackTrace();
}
}
}