diff --git a/src/main/java/DB/Competition.java b/src/main/java/DB/Competition.java index b30c2b1..8392c7a 100644 --- a/src/main/java/DB/Competition.java +++ b/src/main/java/DB/Competition.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; @@ -154,6 +155,21 @@ public class Competition extends Table { return list; } + public ArrayList getTableMovies(Date date) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT M.name FROM Movie M INNER JOIN Projection P on M.name = P.MovieName WHERE M.CompetitionName = ? AND P.startDate = ? GROUP BY M.name"); + ps.setString(1, name); + ps.setDate(2, date); + 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 boolean addRoom(Room room) { return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName()); } diff --git a/src/main/java/DB/Movie.java b/src/main/java/DB/Movie.java index 3c56731..5066ccc 100644 --- a/src/main/java/DB/Movie.java +++ b/src/main/java/DB/Movie.java @@ -4,10 +4,8 @@ import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Time; +import java.sql.*; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Map; @@ -94,6 +92,13 @@ public class Movie extends Table { return list; } + public boolean validateDate(LocalDate date) { + Competition competition = getCompetition(); + int moviesMax = competition.getMovieMax(); + ArrayList movies = competition.getTableMovies(Date.valueOf(date)); + return moviesMax <= 0 || movies.stream().filter(m -> m.equals(this)).toArray().length != 0 || movies.size() < moviesMax; + } + @Override public boolean delete() { return false; diff --git a/src/main/java/DB/Table.java b/src/main/java/DB/Table.java index ff595e7..862019b 100644 --- a/src/main/java/DB/Table.java +++ b/src/main/java/DB/Table.java @@ -4,6 +4,7 @@ import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import java.util.Map; +import java.util.Objects; public abstract class Table { final protected String tableName; @@ -51,4 +52,17 @@ public abstract class Table { public String toString() { return checkValue; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Table table = (Table) o; + return tableName.equals(table.tableName) && checkValue.equals(table.checkValue); + } + + @Override + public int hashCode() { + return Objects.hash(tableName, checkValue); + } } diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 5ada2de..c8dfe9d 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.IntStream; public class ProjectionHandler extends JDialog { private JPanel contentPane; @@ -52,9 +53,11 @@ public class ProjectionHandler extends JDialog { if (agenda.getDate() != null || agenda.getSlot() != null) { updateMovies(); updateDates(); - dayComboBox.getModel().setSelectedItem(agenda.getDate()); + if (IntStream.range(0, dayComboBox.getItemCount()).mapToObj(d -> dayComboBox.getItemAt(d)).filter(d -> d.equals(agenda.getDate())).toArray().length == 1) + dayComboBox.getModel().setSelectedItem(agenda.getDate()); updateSlots(); - slotComboBox.getModel().setSelectedItem(agenda.getSlot()); + if (IntStream.range(0, slotComboBox.getItemCount()).mapToObj(s -> slotComboBox.getItemAt(s)).filter(s -> s.equals(agenda.getSlot())).toArray().length == 1) + slotComboBox.getModel().setSelectedItem(agenda.getSlot()); updateRooms(); } } @@ -136,13 +139,15 @@ public class ProjectionHandler extends JDialog { slotComboBox.removeAllItems(); roomComboBox.removeAllItems(); Competition competition = (Competition) competitionComboBox.getSelectedItem(); + Movie movie = (Movie) filmComboBox.getSelectedItem(); Set dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet()); LocalDate[] localDates = Config.getStartDate().datesUntil(Config.getEndDate()).toArray(LocalDate[]::new); if (competition.getDays() != 0 && dates.size() >= competition.getDays() && !(projectionType == ProjectionType.EDIT && projection.getCompetition().getName().equals(competition.getName()))) localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new); for (LocalDate date : localDates) - dayComboBox.addItem(date); + if (movie == null || movie.validateDate(date)) + dayComboBox.addItem(date); } private void updateSlots() {