diff --git a/MoviesQuoteBot.sql b/MoviesQuoteBot.sql index 7df2f0a..eaaf7a7 100644 --- a/MoviesQuoteBot.sql +++ b/MoviesQuoteBot.sql @@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS films imdb_id varchar(10) NOT NULL, title text NOT NULL, release_date date, - type text NOT NULL, + film_type text NOT NULL, season int, episode int, poster_link text, diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index 0bb52b2..2411749 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -12,10 +12,12 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.*; import xyz.vallat.louis.database.DBManager; +import xyz.vallat.louis.database.exceptions.ImportationException; import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.subtitles.OpenSubtitles; import java.io.IOException; +import java.text.ParseException; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index a2461d5..f679e4d 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -10,10 +10,11 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; +import static xyz.vallat.louis.database.FilmManager.initializeFilm; import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded; import static xyz.vallat.louis.database.LanguageManager.initializeLanguages; import static xyz.vallat.louis.database.PropertyManager.initializeProperties; -import static xyz.vallat.louis.database.SubtitleLine.initializeSubtitleLine; +import static xyz.vallat.louis.database.SubtitleLineManager.initializeSubtitleLine; public final class DBManager { @@ -61,25 +62,6 @@ public final class DBManager { } } - private static void initializeFilm(Connection connection) throws SQLException { - logger.debug("Creating film table."); - try (Statement stmt = connection.createStatement()) { - String query = "CREATE TABLE IF NOT EXISTS films\n" + - "(\n" + - " id int GENERATED ALWAYS AS IDENTITY,\n" + - " imdb_id varchar(10) NOT NULL,\n" + - " title text NOT NULL,\n" + - " release_date date,\n" + - " type text NOT NULL,\n" + - " season int,\n" + - " episode int,\n" + - " poster_link text,\n" + - " PRIMARY KEY (id)\n" + - ");"; - stmt.executeUpdate(query); - } - } - private static void initializeSubtitle(Connection connection) throws SQLException { logger.debug("Creating subtitle table."); try (Statement stmt = connection.createStatement()) { diff --git a/src/main/java/xyz/vallat/louis/database/FilmManager.java b/src/main/java/xyz/vallat/louis/database/FilmManager.java new file mode 100644 index 0000000..3dc66d1 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/database/FilmManager.java @@ -0,0 +1,55 @@ +package xyz.vallat.louis.database; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.vallat.louis.codes.ExitCodes; +import xyz.vallat.louis.database.exceptions.ImportationException; +import xyz.vallat.louis.omdb.objects.Movie; + +import java.sql.*; +import java.time.ZoneId; + +public final class FilmManager { + + private static final Logger logger = LoggerFactory.getLogger(FilmManager.class.getCanonicalName()); + + static void initializeFilm(Connection connection) throws SQLException { + logger.debug("Creating film table."); + try (Statement stmt = connection.createStatement()) { + String query = "CREATE TABLE IF NOT EXISTS films\n" + + "(\n" + + " id int GENERATED ALWAYS AS IDENTITY,\n" + + " imdb_id varchar(10) NOT NULL,\n" + + " title text NOT NULL,\n" + + " release_date date,\n" + + " film_type text NOT NULL,\n" + + " season int,\n" + + " episode int,\n" + + " poster_link text,\n" + + " PRIMARY KEY (id)\n" + + ");"; + stmt.executeUpdate(query); + } + } + + public static void importFilm(Movie movie) throws ImportationException { + try (Connection connection = DBManager.getConnection()) { + String insert = "INSERT INTO films(imdb_id, title, release_date, film_type, poster_link) VALUES(?, ?, ?, ?, ?);"; + try (PreparedStatement stmt = connection.prepareStatement(insert)) { + stmt.setString(1, movie.getImdbID()); + stmt.setString(2, movie.getTitle()); + stmt.setDate(3, Date.valueOf(movie.getReleased().atZone(ZoneId.of("UTC")).toLocalDate())); + stmt.setString(4, movie.getType()); + stmt.setString(5, movie.getPoster()); + stmt.executeUpdate(); + } catch (SQLException e) { + logger.error("Error while importing movie: {}", e.getMessage()); + throw new ImportationException(); + } + } catch (SQLException e) { + logger.error("Cannot connect to database right now. Reason: {}", e.getMessage()); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); + } + } + +} diff --git a/src/main/java/xyz/vallat/louis/database/LanguageManager.java b/src/main/java/xyz/vallat/louis/database/LanguageManager.java index 47fa8ae..6797542 100644 --- a/src/main/java/xyz/vallat/louis/database/LanguageManager.java +++ b/src/main/java/xyz/vallat/louis/database/LanguageManager.java @@ -29,7 +29,7 @@ public final class LanguageManager { private LanguageManager() { } - public static void importLanguageIfNeeded(Connection connection) { + static void importLanguageIfNeeded(Connection connection) { String storedHash = null; try { logger.debug("Checking if we need to import languages again."); diff --git a/src/main/java/xyz/vallat/louis/database/SubtitleLine.java b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java similarity index 89% rename from src/main/java/xyz/vallat/louis/database/SubtitleLine.java rename to src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java index a3bc698..deeb54b 100644 --- a/src/main/java/xyz/vallat/louis/database/SubtitleLine.java +++ b/src/main/java/xyz/vallat/louis/database/SubtitleLineManager.java @@ -7,11 +7,11 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; -public final class SubtitleLine { +public final class SubtitleLineManager { - private static final Logger logger = LoggerFactory.getLogger(SubtitleLine.class.getCanonicalName()); + private static final Logger logger = LoggerFactory.getLogger(SubtitleLineManager.class.getCanonicalName()); - private SubtitleLine() { + private SubtitleLineManager() { } static void initializeSubtitleLine(Connection connection) throws SQLException { diff --git a/src/main/java/xyz/vallat/louis/database/exceptions/ImportationException.java b/src/main/java/xyz/vallat/louis/database/exceptions/ImportationException.java new file mode 100644 index 0000000..4023d2e --- /dev/null +++ b/src/main/java/xyz/vallat/louis/database/exceptions/ImportationException.java @@ -0,0 +1,4 @@ +package xyz.vallat.louis.database.exceptions; + +public class ImportationException extends Exception { +} diff --git a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java index dbef0d9..8a021e3 100644 --- a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java +++ b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java @@ -51,7 +51,7 @@ public final class OMDBClient { if (jsonMovie.get(IMDB_KEY) == null || jsonMovie.get(TITLE_KEY) == null) continue; movies.add(new Movie( jsonMovie.get(TITLE_KEY).getAsString(), - DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), + DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()).toInstant(), jsonMovie.get(IMDB_KEY).getAsString(), jsonMovie.get(TYPE_KEY).getAsString(), jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() @@ -79,7 +79,7 @@ public final class OMDBClient { if (jsonMovie.get(IMDB_KEY) != null && jsonMovie.get(TITLE_KEY) != null) { movie = new Movie( jsonMovie.get(TITLE_KEY).getAsString(), - DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()), + DATE_FORMAT.parse(jsonMovie.get(RELEASED_KEY).getAsString()).toInstant(), jsonMovie.get(IMDB_KEY).getAsString(), jsonMovie.get(TYPE_KEY).getAsString(), jsonMovie.get(POSTER_KEY).getAsString().equals("N/A") ? null : jsonMovie.get(POSTER_KEY).getAsString() diff --git a/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java b/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java index cffc39b..3f99d73 100644 --- a/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java +++ b/src/main/java/xyz/vallat/louis/omdb/objects/Movie.java @@ -1,16 +1,17 @@ package xyz.vallat.louis.omdb.objects; +import java.time.Instant; import java.util.Date; public class Movie { private final String title; - private final Date released; + private final Instant released; private final String imdbID; private final String type; private final String poster; - public Movie(String title, Date released, String imdbID, String type, String poster) { + public Movie(String title, Instant released, String imdbID, String type, String poster) { this.title = title; this.released = released; this.imdbID = imdbID; @@ -22,7 +23,7 @@ public class Movie { return title; } - public Date getReleased() { + public Instant getReleased() { return released; }