Add movieMax constraint check on date selection
This commit is contained in:
parent
6f94624e89
commit
c506e9860b
4 changed files with 47 additions and 7 deletions
|
@ -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<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) {
|
||||
return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName());
|
||||
}
|
||||
|
|
|
@ -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<Movie> 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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Date> 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() {
|
||||
|
|
Reference in a new issue