83 lines
2.8 KiB
Java
83 lines
2.8 KiB
Java
package DB;
|
|
|
|
import Exceptions.NotFoundInTable;
|
|
import lombok.Getter;
|
|
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
import java.util.Map;
|
|
|
|
public class Slot extends Table {
|
|
@Getter final private int id;
|
|
|
|
public Slot(int id) throws NotFoundInTable {
|
|
super("Slot", "id", Integer.toString(id));
|
|
this.id = id;
|
|
}
|
|
|
|
public Slot(Date startTime, Competition competition) {
|
|
super("Slot", "id", Map.ofEntries(
|
|
Map.entry("startTime", startTime.toString()),
|
|
Map.entry("CompetitionName", competition.getName())
|
|
));
|
|
this.id = Integer.parseInt(super.checkValue);
|
|
}
|
|
|
|
public Time getStartTime() {
|
|
return get(Time.class, "startTime");
|
|
}
|
|
|
|
public Competition getCompetition() {
|
|
return get(Competition.class, "CompetitionName");
|
|
}
|
|
|
|
public boolean setCompetition(Competition competition) {
|
|
return set("CompetitionName", competition.getName());
|
|
}
|
|
|
|
public boolean setStartTime(Date startTime) {
|
|
return set("startTime", startTime.toString());
|
|
}
|
|
|
|
public ArrayList<Projection> getProjections() {
|
|
ArrayList<Projection> 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<Slot> getAll() {
|
|
ArrayList<Slot> list = new ArrayList<>();
|
|
try {
|
|
PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot");
|
|
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;
|
|
}
|
|
|
|
static public ArrayList<Slot> getAvailable(Competition competition, Date date) {
|
|
ArrayList<Slot> 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;
|
|
}
|
|
}
|