1
0
Fork 0

Add movieMax constraint check on date selection

This commit is contained in:
Ethanell 2021-01-18 11:32:05 +01:00
parent 6f94624e89
commit c506e9860b
4 changed files with 47 additions and 7 deletions

View file

@ -4,6 +4,7 @@ import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -154,6 +155,21 @@ public class Competition extends Table {
return list; return list;
} }
public ArrayList<Movie> getTableMovies(Date date) {
ArrayList<Movie> 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) { public boolean addRoom(Room room) {
return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName()); return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName());
} }

View file

@ -4,10 +4,8 @@ import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import java.sql.PreparedStatement; import java.sql.*;
import java.sql.ResultSet; import java.time.LocalDate;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
@ -94,6 +92,13 @@ public class Movie extends Table {
return list; return list;
} }
public boolean validateDate(LocalDate date) {
Competition competition = getCompetition();
int moviesMax = competition.getMovieMax();
ArrayList<Movie> movies = competition.getTableMovies(Date.valueOf(date));
return moviesMax <= 0 || movies.stream().filter(m -> m.equals(this)).toArray().length != 0 || movies.size() < moviesMax;
}
@Override @Override
public boolean delete() { public boolean delete() {
return false; return false;

View file

@ -4,6 +4,7 @@ import Exceptions.AlreadyOnTable;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public abstract class Table { public abstract class Table {
final protected String tableName; final protected String tableName;
@ -51,4 +52,17 @@ public abstract class Table {
public String toString() { public String toString() {
return checkValue; 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);
}
} }

View file

@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ProjectionHandler extends JDialog { public class ProjectionHandler extends JDialog {
private JPanel contentPane; private JPanel contentPane;
@ -52,9 +53,11 @@ public class ProjectionHandler extends JDialog {
if (agenda.getDate() != null || agenda.getSlot() != null) { if (agenda.getDate() != null || agenda.getSlot() != null) {
updateMovies(); updateMovies();
updateDates(); 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(); 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(); updateRooms();
} }
} }
@ -136,13 +139,15 @@ public class ProjectionHandler extends JDialog {
slotComboBox.removeAllItems(); slotComboBox.removeAllItems();
roomComboBox.removeAllItems(); roomComboBox.removeAllItems();
Competition competition = (Competition) competitionComboBox.getSelectedItem(); Competition competition = (Competition) competitionComboBox.getSelectedItem();
Movie movie = (Movie) filmComboBox.getSelectedItem();
Set<Date> dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet()); Set<Date> dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet());
LocalDate[] localDates = Config.getStartDate().datesUntil(Config.getEndDate()).toArray(LocalDate[]::new); 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()))) 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); localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new);
for (LocalDate date : localDates) for (LocalDate date : localDates)
dayComboBox.addItem(date); if (movie == null || movie.validateDate(date))
dayComboBox.addItem(date);
} }
private void updateSlots() { private void updateSlots() {