package edu.spbstu.hoteldb.handlers;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import edu.spbstu.hoteldb.interfaces.SQLQuery;
import edu.spbstu.hoteldb.sql.HistorySearchQuery;
public class HistorySearchEventHandler implements EventHandler<ActionEvent> {
private final TextArea results;
private final Connection connect;
private List<Object> args;
public HistorySearchEventHandler(TextArea results, Connection connect, List<Object> args) {
this.results = results;
this.connect = connect;
this.args = args;
}
@Override
public void handle(ActionEvent arg0) {
List<Object> arg = new LinkedList<Object>();
LocalDate from = ((DatePicker)args.get(0)).getValue();
LocalDate to = ((DatePicker)args.get(1)).getValue();
if (from == null || to == null) {
results.appendText("undefined from date or to date\n");
return;
}
if (from.compareTo(to) >= 0) {
results.appendText("from date >= to date\n");
return;
}
arg.add(from.toString()); arg.add(to.toString());
try {
SQLQuery query = new HistorySearchQuery(connect, results);
query.prepareQuery(arg);
query.doQuery();
query.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}