// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package booking;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
CREATE TABLE hotels(
    name VARCHAR(50) NOT NULL,
    city VARCHAR(50),
    PRIMARY KEY(name)
);
CREATE TABLE bookings(
    id INTEGER NOT NULL,
    hotel VARCHAR(50) NOT NULL,
    start DATE,
    end DATE,
    PRIMARY KEY(id)
);
INSERT INTO hotels VALUES('A', 'A by');
INSERT INTO hotels VALUES('B', 'B by');
INSERT INTO bookings VALUES(1, 'A', '20141003', '20141005');
INSERT INTO bookings VALUES(2, 'B', '20141003', '20141005');
INSERT INTO bookings VALUES(3, 'B', '20141003', '20141012');
*/

public class DbDataProvider implements DataProvider {
    private static final String DB_CONURL = "jdbc:mysql://localhost/Test";
    private static final String DB_UN = "root";
    private static final String DB_PW = "";
    private static final String SQL_GET_HOTELS = "SELECT name,city FROM hotels";
    private static final String SQL_GET_BOOKINGS = "SELECT hotel,start,end FROM bookings WHERE hotel = ?";
    private Connection con = null;
    private PreparedStatement gethotels = null;
    private PreparedStatement getbookings = null;
    private void openIfNecessary() throws SQLException {
            if(con == null || con.isClosed()) {
                con = DriverManager.getConnection(DB_CONURL, DB_UN, DB_PW);
                gethotels = con.prepareStatement(SQL_GET_HOTELS);
                getbookings = con.prepareStatement(SQL_GET_BOOKINGS);
            }
    }
    @Override
    public List<Hotel> getHotelList(ExceptionHandler exh) {
        List<Hotel> result = new ArrayList<>();
        try {
            openIfNecessary();
            try (ResultSet rs = gethotels.executeQuery()) {
                while(rs.next()) {
                    result.add(new Hotel(rs.getString("name"), rs.getString("city")));
                }
            }
        } catch (SQLException e) {
            exh.processException(e);
        }
        return result;
    }
    @Override
    public List<Booking> getBookingList(String hotelName, ExceptionHandler exh) {
        List<Booking> result = new ArrayList<>();
        try {
            openIfNecessary();
            getbookings.setString(1, hotelName);
            try (ResultSet rs = getbookings.executeQuery()) {
                while(rs.next()) {
                    result.add(new Booking(rs.getString("hotel"), rs.getDate("start"), rs.getDate("end")));
                }
            }
        } catch (SQLException e) {
            exh.processException(e);
        }
        return result;
    }
    @Override
    public void close(ExceptionHandler exh) {
        try {
            getbookings.close();
            gethotels.close();
            con.close();
        } catch (SQLException e) {
            exh.processException(e);
        }
    }
}
