From 4490eff40c65faf10fec95eee749379517854930 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 17 Nov 2020 00:19:24 +0100 Subject: [PATCH] [WIP] Now it is possible to get all subtitles from the database (including language and films) and to print them through the logger. Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MoviesQuoteBot.java | 5 ++ .../louis/database/SubtitleManager.java | 49 +++++++++++++++++-- .../vallat/louis/subtitles/dao/Subtitle.java | 28 +++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/subtitles/dao/Subtitle.java diff --git a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java index 810d376..43b839e 100644 --- a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.database.DBManager; +import xyz.vallat.louis.database.SubtitleManager; import xyz.vallat.louis.discord.DiscordManager; import xyz.vallat.louis.sockets.CommandsClient; import xyz.vallat.louis.sockets.CommandsServer; @@ -94,8 +95,12 @@ public class MoviesQuoteBot { } private static void reimportAllSubtitleLines() { + logger.info("Reimporting all subtitle lines."); CommandsClient.checkVersions(); CommandsClient.setMaintenanceMode(true); + SubtitleManager.getSubtitles().forEach(subtitle -> + logger.debug("[{}] - '{}' in {}.", subtitle.getId(), subtitle.getMovie(), subtitle.getLang().getEnglish()) + ); CommandsClient.setMaintenanceMode(false); System.exit(0); } diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleManager.java b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java index 04df528..0b338f2 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleManager.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleManager.java @@ -4,11 +4,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.omdb.objects.Movie; +import xyz.vallat.louis.subtitles.dao.Lang; +import xyz.vallat.louis.subtitles.dao.Subtitle; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; public final class SubtitleManager { @@ -33,6 +34,46 @@ public final class SubtitleManager { return -1; } + public static List getSubtitles() { + logger.debug("Getting all subtitles in database."); + List subtitles = new ArrayList<>(); + try (Connection connection = DBManager.getConnection()) { + try (Statement stmt = connection.createStatement()) { + String query = """ + SELECT films.id AS film_id, imdb_id, title, year, film_type, poster_link, + languages.id AS language_id, alpha3_b, alpha3_t, alpha2, english, french, subtitles.id AS subtitle_id + FROM subtitles INNER JOIN films ON subtitles.film_id = films.id + INNER JOIN languages ON subtitles.language_id = languages.id ORDER BY subtitles.id;"""; + stmt.executeQuery(query); + while (stmt.getResultSet().next()) { + ResultSet set = stmt.getResultSet(); + Movie movie = new Movie( + set.getString("title"), + set.getString("imdb_id"), + set.getString("film_type"), + set.getString("poster_link") + ); + movie.setId(set.getInt("film_id")); + if (set.getObject("year") == null) movie.setYear(set.getInt("year")); + Lang lang = new Lang( + set.getInt("language_id"), + set.getString("alpha3_b"), + set.getString("alpha3_t"), + set.getString("alpha2"), + set.getString("english"), + set.getString("french") + ); + subtitles.add(new Subtitle(set.getInt("subtitle_id"), movie, lang)); + } + return subtitles; + } + } catch (SQLException e) { + logger.error("Could not connect to the database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + return null; + } + public static int getNumberOfSubtitlesByGuild(String guild_id) { logger.debug("Getting the number of subtitles in database from guild '{}'.", guild_id); try (Connection connection = DBManager.getConnection()) { diff --git a/src/main/java/xyz/vallat/louis/subtitles/dao/Subtitle.java b/src/main/java/xyz/vallat/louis/subtitles/dao/Subtitle.java new file mode 100644 index 0000000..c0e00f7 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/subtitles/dao/Subtitle.java @@ -0,0 +1,28 @@ +package xyz.vallat.louis.subtitles.dao; + +import xyz.vallat.louis.omdb.objects.Movie; + +public class Subtitle { + + private final int id; + private final Movie movie; + private final Lang lang; + + public Subtitle(int id, Movie movie, Lang lang) { + this.id = id; + this.movie = movie; + this.lang = lang; + } + + public Movie getMovie() { + return movie; + } + + public Lang getLang() { + return lang; + } + + public int getId() { + return id; + } +}