From d6ca3718bc43030b18e807c3a9d85959170e3b13 Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 5 Jan 2021 15:58:57 +0100 Subject: [PATCH] Setup add projection --- src/main/java/DB/DB.java | 4 +- src/main/java/DB/Room.java | 2 +- src/main/java/DB/Slot.java | 7 +- src/main/java/DB/Table.java | 5 ++ src/main/java/GUI/ProjectionHandler.form | 30 +++++++- src/main/java/GUI/ProjectionHandler.java | 82 +++++++++++++++++++++- src/main/java/ProjectionPlanning/Main.java | 7 +- 7 files changed, 124 insertions(+), 13 deletions(-) diff --git a/src/main/java/DB/DB.java b/src/main/java/DB/DB.java index 6842933..827c0ca 100644 --- a/src/main/java/DB/DB.java +++ b/src/main/java/DB/DB.java @@ -2,7 +2,6 @@ package DB; import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; -import lombok.Data; import lombok.Getter; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -11,6 +10,7 @@ import org.json.simple.parser.ParseException; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.sql.*; +import java.sql.Date; import java.util.*; public class DB { @@ -90,7 +90,7 @@ public class DB { returnValue = rs.getInt(value); else if (cl == Time.class) returnValue = rs.getTime(value); - else if (cl == Data.class) + else if (cl == Date.class) returnValue = rs.getDate(value); else if (cl == Competition.class) returnValue = new Competition(rs.getString(value)); diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index 6eca641..2943f5b 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -51,7 +51,7 @@ public class Room extends Table { 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"); + 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 GROUP BY R.name"); ps.setString(1, movie.getCompetition().getName()); ps.setInt(2, slot.getId()); ps.setDate(3, date); diff --git a/src/main/java/DB/Slot.java b/src/main/java/DB/Slot.java index 9bad054..7917ea9 100644 --- a/src/main/java/DB/Slot.java +++ b/src/main/java/DB/Slot.java @@ -69,7 +69,7 @@ public class Slot extends Table { 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"); + 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.id IS NULL OR P.startDate != ?"); ps.setString(1, competition.getName()); ps.setDate(2, date); for (ResultSet rs = ps.executeQuery(); rs.next();) @@ -80,4 +80,9 @@ public class Slot extends Table { } return list; } + + @Override + public String toString() { + return getStartTime().toString(); + } } diff --git a/src/main/java/DB/Table.java b/src/main/java/DB/Table.java index 2d18774..ff595e7 100644 --- a/src/main/java/DB/Table.java +++ b/src/main/java/DB/Table.java @@ -46,4 +46,9 @@ public abstract class Table { public boolean delete() { return DB.delete(tableName, check, checkValue); } + + @Override + public String toString() { + return checkValue; + } } diff --git a/src/main/java/GUI/ProjectionHandler.form b/src/main/java/GUI/ProjectionHandler.form index 8f19d6a..b1913d5 100644 --- a/src/main/java/GUI/ProjectionHandler.form +++ b/src/main/java/GUI/ProjectionHandler.form @@ -1,6 +1,6 @@
- + @@ -82,7 +82,7 @@ - + @@ -90,7 +90,7 @@ - + @@ -120,6 +120,30 @@ + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 3d23f5a..ac5b01e 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -1,9 +1,15 @@ package GUI; +import DB.*; + import javax.swing.*; import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import java.awt.event.*; +import java.sql.Date; +import java.time.LocalDate; +import java.util.*; +import java.util.List; +import java.util.stream.Collectors; public class ProjectionHandler extends JDialog { private JPanel contentPane; @@ -21,6 +27,9 @@ public class ProjectionHandler extends JDialog { private JPanel filmPanel; private JLabel filmLabel; private JComboBox filmComboBox; + private JPanel roomPanel; + private JLabel roomLabel; + private JComboBox roomComboBox; private ProjectionType projectionType; public ProjectionHandler(ProjectionType projectionType) { @@ -36,6 +45,13 @@ public class ProjectionHandler extends JDialog { } }); setLocationRelativeTo(null); + updateCompetitions(); + competitionComboBox.addActionListener(actionEvent -> updateMovies()); + filmComboBox.addActionListener(actionEvent -> updateDates()); + dayComboBox.addActionListener(actionEvent -> updateSlots()); + slotComboBox.addActionListener(actionEvent -> updateRooms()); + cancelButton.addActionListener(actionEvent -> dispose()); + confirmButton.addActionListener(actionEvent -> confirm()); } private void handleDialogName() { @@ -59,4 +75,66 @@ public class ProjectionHandler extends JDialog { break; } } + + private void updateCompetitions() { + ArrayList competitions = Competition.getAll(); + if (competitions != null) + for (Competition c : competitions) + competitionComboBox.addItem(c); + } + + private void updateMovies() { + filmComboBox.removeAllItems(); + dayComboBox.removeAllItems(); + slotComboBox.removeAllItems(); + roomComboBox.removeAllItems(); + ArrayList movies = Movie.getAvailable((Competition) competitionComboBox.getSelectedItem()); + if (movies != null) + for (Movie m : movies) + filmComboBox.addItem(m); + } + + private void updateDates() { + dayComboBox.removeAllItems(); + slotComboBox.removeAllItems(); + roomComboBox.removeAllItems(); + Competition competition = (Competition) competitionComboBox.getSelectedItem(); + Set dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet()); + LocalDate[] localDates = LocalDate.of(2021, 5, 11).datesUntil(LocalDate.of(2021, 5, 22)).toArray(LocalDate[]::new); + if (competition.getDays() != 0 && dates.size() >= competition.getDays()) + localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new); + + for (LocalDate date : localDates) + dayComboBox.addItem(date); + } + + private void updateSlots() { + slotComboBox.removeAllItems(); + roomComboBox.removeAllItems(); + if (dayComboBox.getSelectedItem() != null) { + ArrayList slots = Slot.getAvailable((Competition) competitionComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem())); + if (slots != null) + for (Slot s : slots) + slotComboBox.addItem(s); + } + } + + private void updateRooms() { + roomComboBox.removeAllItems(); + if (dayComboBox.getSelectedItem() != null) { + ArrayList rooms = Room.getAvailable((Movie) filmComboBox.getSelectedItem(), (Slot) slotComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem())); + if (rooms != null) + for (Room r : rooms) + roomComboBox.addItem(r); + } + } + + private void confirm() { + for (JComboBox comboBox : Arrays.asList(competitionComboBox, filmComboBox, dayComboBox, slotComboBox, roomComboBox)) + if (comboBox.getSelectedItem() == null) + return; + if (projectionType == ProjectionType.ADD) + new Projection(Date.valueOf((LocalDate) dayComboBox.getSelectedItem()), (Competition) competitionComboBox.getSelectedItem(), (Room) roomComboBox.getSelectedItem(), (Movie) filmComboBox.getSelectedItem(), (Slot) slotComboBox.getSelectedItem()); + dispose(); + } } diff --git a/src/main/java/ProjectionPlanning/Main.java b/src/main/java/ProjectionPlanning/Main.java index f61cc63..ce5d0cd 100644 --- a/src/main/java/ProjectionPlanning/Main.java +++ b/src/main/java/ProjectionPlanning/Main.java @@ -1,12 +1,11 @@ package ProjectionPlanning; import DB.*; -import Exceptions.*; -import java.sql.*; +import GUI.GUI; public class Main { - public static void main(String[] args) throws AlreadyOnTable, NotFoundInTable { - //new GUI(); + public static void main(String[] args) { DB.connect(); + new GUI(); } }