diff --git a/src/main/java/DB/Movie.java b/src/main/java/DB/Movie.java index b16064c..3c56731 100644 --- a/src/main/java/DB/Movie.java +++ b/src/main/java/DB/Movie.java @@ -66,6 +66,20 @@ public class Movie extends Table { return list; } + static public ArrayList getAvailable(Competition competition) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM Movie WHERE CompetitionName = ?"); + ps.setString(1, competition.getName()); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Movie(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + public ArrayList getProjections() { ArrayList list = new ArrayList<>(); try { diff --git a/src/main/java/DB/Projection.java b/src/main/java/DB/Projection.java index 776ee49..c4cddc6 100644 --- a/src/main/java/DB/Projection.java +++ b/src/main/java/DB/Projection.java @@ -18,12 +18,13 @@ public class Projection extends Table { this.id = id; } - public Projection(Date startDate, Competition competition, Room room, Movie movie) { + public Projection(Date startDate, Competition competition, Room room, Movie movie, Slot slot) { super("Projection", "id", Map.ofEntries( Map.entry("startDate", startDate.toString()), Map.entry("CompetitionName", competition.getName()), Map.entry("RoomName", room.getName()), - Map.entry("MovieName", movie.getName()) + Map.entry("MovieName", movie.getName()), + Map.entry("SlotId", Integer.toString(slot.getId())) )); this.id = Integer.parseInt(super.checkValue); } @@ -44,6 +45,10 @@ public class Projection extends Table { return get(Movie.class, "MovieName"); } + public Slot getSlot() { + return get(Slot.class, "SlotId"); + } + public boolean setStartDate(Date startDate) { return set("startDate", startDate.toString()); } @@ -73,6 +78,20 @@ public class Projection extends Table { return list; } + static public ArrayList getAvailable(Competition competition) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE CompetitionName = ?"); + ps.setString(1, competition.getName()); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + @Override public boolean delete() { return false; diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index bcd354c..6eca641 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -4,6 +4,7 @@ import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -47,6 +48,22 @@ public class Room extends Table { return list; } + static public ArrayList getAvailable(Movie movie, Slot slot, Date date) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT R.name FROM Room R INNER JOIN CompetitionRoom CR on R.name = CR.RoomName LEFT JOIN Projection P on R.name = P.RoomName LEFT JOIN Slot S on S.id = P.SlotId WHERE CR.CompetitionName = ? AND NOT (S.id = ? AND P.startDate = ?) OR P.id IS NULL"); + ps.setString(1, movie.getCompetition().getName()); + ps.setInt(2, slot.getId()); + ps.setDate(3, date); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Room(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + public ArrayList getCompetitions() { ArrayList list = new ArrayList<>(); try { diff --git a/src/main/java/DB/Slot.java b/src/main/java/DB/Slot.java index 982f105..9bad054 100644 --- a/src/main/java/DB/Slot.java +++ b/src/main/java/DB/Slot.java @@ -3,10 +3,7 @@ package DB; import Exceptions.NotFoundInTable; import lombok.Getter; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.Map; @@ -26,8 +23,8 @@ public class Slot extends Table { this.id = Integer.parseInt(super.checkValue); } - public Date getStartTime() { - return get(Date.class, "startTime"); + public Time getStartTime() { + return get(Time.class, "startTime"); } public Competition getCompetition() { @@ -42,6 +39,20 @@ public class Slot extends Table { return set("startTime", startTime.toString()); } + public ArrayList getProjections() { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE SlotId = ?"); + ps.setInt(1, id); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + static public ArrayList getAll() { ArrayList list = new ArrayList<>(); try { @@ -54,4 +65,19 @@ public class Slot extends Table { } return list; } + + static public ArrayList getAvailable(Competition competition, Date date) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT S.id FROM Slot S LEFT JOIN Projection P ON S.id = P.SlotId WHERE S.CompetitionName = ? AND P.startDate != ? OR P.id IS NULL"); + ps.setString(1, competition.getName()); + ps.setDate(2, date); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Slot(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } }