diff --git a/src/main/java/kellerkinder/HomeFlix/controller/DBController.java b/src/main/java/kellerkinder/HomeFlix/controller/DBController.java index c209fdb..87ea8af 100644 --- a/src/main/java/kellerkinder/HomeFlix/controller/DBController.java +++ b/src/main/java/kellerkinder/HomeFlix/controller/DBController.java @@ -161,6 +161,7 @@ public class DBController { String path = source.asObject().getString("path", ""); String mode = source.asObject().getString("mode", ""); mainWindowController.addSourceToTable(path, mode); // add source to source-table + if (mode.equals("local")) { for (File file : new File(path).listFiles()) { if (file.isFile() && isVideoFile(file.getPath())) { @@ -350,11 +351,11 @@ public class DBController { int sn = 1; for (File season : file.listFiles()) { if (season.isDirectory()) { - int ep = 1; + int ep = getLastEpisode(cutOffEnd(file.getName()), Integer.toString(sn)) + 1; for (File episode : season.listFiles()) { if (!filmsdbStreamURL.contains(episode.getPath())) { ps.setString(1, episode.getPath().replace("'", "''")); - ps.setString(2, cutOffEnd(file.getName())); + ps.setString(2, cutOffEnd(file.getName())); // the title is the series root folder's name ps.setString(3, Integer.toString(sn)); ps.setString(4, Integer.toString(ep)); ps.setInt(5, 0); @@ -406,6 +407,34 @@ public class DBController { } } + /** + * gets the last episode of a season of a given series + * @param seriesTitle the actual series + * @param season the actual season + * @return the last episode number + */ + private int getLastEpisode(String seriesTitle, String season) { + int lastEpisode = 0; + try { + Statement stmt = connection.createStatement(); + PreparedStatement ps = connection.prepareStatement("SELECT * FROM films WHERE title = ? AND season = ?"); + ps.setString(1, seriesTitle); + ps.setString(2, season); + ResultSet rs = ps.executeQuery(); + + while (rs.next()) { + if (Integer.parseInt(rs.getString("episode")) > lastEpisode) + lastEpisode = Integer.parseInt(rs.getString("episode")); + } + stmt.close(); + rs.close(); + } catch (SQLException e) { + LOGGER.error("An error occured, while printing all entries", e); + } + + return lastEpisode; + } + /** * DEBUG * prints all entries from the database to the console