DBController cleanup

This commit is contained in:
Jannik 2018-05-16 23:02:07 +02:00
parent 26f26ec9f8
commit 036ed8ad11
3 changed files with 23 additions and 38 deletions

View File

@ -17,7 +17,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-10"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-9">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>

View File

@ -1,13 +1,13 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=10 org.eclipse.jdt.core.compiler.codegen.targetPlatform=9
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=10 org.eclipse.jdt.core.compiler.compliance=9
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=10 org.eclipse.jdt.core.compiler.source=9

View File

@ -51,21 +51,19 @@ import javafx.scene.text.FontWeight;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import kellerkinder.HomeFlix.application.Main; import kellerkinder.HomeFlix.application.Main;
import kellerkinder.HomeFlix.application.MainWindowController; import kellerkinder.HomeFlix.application.MainWindowController;
import kellerkinder.HomeFlix.datatypes.SourceDataType;
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType; import kellerkinder.HomeFlix.datatypes.FilmTabelDataType;
import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType; import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType;
import kellerkinder.HomeFlix.datatypes.SourceDataType;
public class DBController { public class DBController {
private MainWindowController mainWindowController; private MainWindowController mainWindowController;
private Main main; private Main main;
private String DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db"; //path to database file private String DB_PATH;
private Image favorite_black = new Image("icons/ic_favorite_black_18dp_1x.png"); private Image favorite_black = new Image("icons/ic_favorite_black_18dp_1x.png");
private Image favorite_border_black = new Image("icons/ic_favorite_border_black_18dp_1x.png"); private Image favorite_border_black = new Image("icons/ic_favorite_border_black_18dp_1x.png");
private List<String> filmsdbAll = new ArrayList<String>(); private List<String> filmsdbStreamURL = new ArrayList<String>(); // contains all films stored in the database
private List<String> filmsdbDir = new ArrayList<String>(); private List<String> filmsStreamURL = new ArrayList<String>(); // contains all films from the sources
private List<String> filmsdbStreamURL = new ArrayList<String>(); // needed
private List<String> filmsStreamURL = new ArrayList<String>(); // needed
private Connection connection = null; private Connection connection = null;
private static final Logger LOGGER = LogManager.getLogger(DBController.class.getName()); private static final Logger LOGGER = LogManager.getLogger(DBController.class.getName());
@ -74,7 +72,7 @@ public class DBController {
* @param main the Main object * @param main the Main object
* @param mainWindowController the MainWindowController object * @param mainWindowController the MainWindowController object
*/ */
public DBController(Main main, MainWindowController mainWindowController) { public DBController(Main main, MainWindowController mainWindowController) {
this.main = main; this.main = main;
this.mainWindowController = mainWindowController; this.mainWindowController = mainWindowController;
} }
@ -138,7 +136,6 @@ public class DBController {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM films"); ResultSet rs = stmt.executeQuery("SELECT * FROM films");
while (rs.next()) { while (rs.next()) {
filmsdbDir.add(rs.getString("title"));
filmsdbStreamURL.add(rs.getString("streamUrl")); filmsdbStreamURL.add(rs.getString("streamUrl"));
} }
stmt.close(); stmt.close();
@ -146,11 +143,6 @@ public class DBController {
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Ups! an error occured!", e); LOGGER.error("Ups! an error occured!", e);
} }
// add all entries to filmsAll and filmsdbAl, for later comparing
filmsdbAll.addAll(filmsdbDir);
LOGGER.info("films in directory: " + filmsStreamURL.size());
LOGGER.info("filme in db: " + filmsdbStreamURL.size());
} }
/** /**
@ -210,7 +202,7 @@ public class DBController {
* load the data to the mainWindowController * load the data to the mainWindowController
* order entries by title * order entries by title
*/ */
private void loadDataToMWC() { private void loadDataToFilmsList() {
LOGGER.info("loading data to mwc ..."); LOGGER.info("loading data to mwc ...");
try { try {
//load local Data //load local Data
@ -276,14 +268,15 @@ public class DBController {
LOGGER.info("refreshing the Database ..."); LOGGER.info("refreshing the Database ...");
// clean all ArraLists // clean all ArraLists
filmsdbAll.clear();
filmsdbDir.clear();
filmsdbStreamURL.clear(); filmsdbStreamURL.clear();
filmsStreamURL.clear(); filmsStreamURL.clear();
loadSources(); // reload all sources loadSources(); // reload all sources
loadDatabase(); // reload all films saved in the DB loadDatabase(); // reload all films saved in the DB
LOGGER.info("films in directory: " + filmsStreamURL.size());
LOGGER.info("filme in db: " + filmsdbStreamURL.size());
try { try {
checkAddEntry(); checkAddEntry();
checkRemoveEntry(); checkRemoveEntry();
@ -291,11 +284,11 @@ public class DBController {
LOGGER.error("Error while refreshing the database", e); LOGGER.error("Error while refreshing the database", e);
} }
// remove all films from the mwc lists // clear the FilmsList and FilmRoot chlidren
mainWindowController.getFilmsList().removeAll(mainWindowController.getFilmsList()); mainWindowController.getFilmsList().clear();
mainWindowController.getFilmRoot().getChildren().removeAll(mainWindowController.getFilmRoot().getChildren()); mainWindowController.getFilmRoot().getChildren().clear();
loadDataToMWC(); // load the new data to the mwc loadDataToFilmsList(); // load the new data to the FilmsList
} }
/** /**
@ -469,7 +462,7 @@ public class DBController {
* update the database entry for the given film, cached = 1 * update the database entry for the given film, cached = 1
* @param streamUrl URL of the film * @param streamUrl URL of the film
*/ */
void setCached(String streamUrl) { public void setCached(String streamUrl) {
try { try {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
stmt.executeUpdate("UPDATE films SET cached=1 WHERE streamUrl=\"" + streamUrl + "\";"); stmt.executeUpdate("UPDATE films SET cached=1 WHERE streamUrl=\"" + streamUrl + "\";");
@ -487,7 +480,7 @@ public class DBController {
* @param streamUrl URL of the film * @param streamUrl URL of the film
* @param omdbResponse the response data from omdbAPI * @param omdbResponse the response data from omdbAPI
*/ */
void addCache(String streamUrl, OMDbAPIResponseDataType omdbResponse) { public void addCache(String streamUrl, OMDbAPIResponseDataType omdbResponse) {
try { try {
PreparedStatement ps = connection.prepareStatement("insert into cache values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); PreparedStatement ps = connection.prepareStatement("insert into cache values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
@ -688,17 +681,14 @@ public class DBController {
if (rsEpisode > episode && rsEpisode < nextEpisode) { if (rsEpisode > episode && rsEpisode < nextEpisode) {
// fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode // fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode
nextEpisode = rsEpisode; nextEpisode = rsEpisode;
System.out.println("next episode is: " + nextEpisode);
// favorite image is black
nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"),
rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"),
rs.getBoolean("cached"), new ImageView(favorite_black)); rs.getBoolean("cached"), new ImageView());
} }
} }
if (nextFilm == null) { if (nextFilm == null) {
int nextSeason = 3000; int nextSeason = 3000;
System.out.println("searching next season");
rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\";"); rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\";");
while(rs.next()) { while(rs.next()) {
int rsSeason = Integer.parseInt(rs.getString("season")); int rsSeason = Integer.parseInt(rs.getString("season"));
@ -708,18 +698,15 @@ public class DBController {
} }
if (nextSeason != 3000) { if (nextSeason != 3000) {
System.out.println("next season is: " + nextSeason);
rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\" AND season = \"" + season + "\";"); rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\" AND season = \"" + season + "\";");
while(rs.next()) { while(rs.next()) {
int rsEpisode = Integer.parseInt(rs.getString("episode")); int rsEpisode = Integer.parseInt(rs.getString("episode"));
if (rsEpisode > episode && rsEpisode < nextEpisode) { if (rsEpisode > episode && rsEpisode < nextEpisode) {
// fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode // fitting episode found in current season, if rsEpisode < nextEpisode -> nextEpisode = rsEpisode
nextEpisode = rsEpisode; nextEpisode = rsEpisode;
System.out.println("next episode is: " + nextEpisode);
// favorite image is black
nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"),
rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"),
rs.getBoolean("cached"), new ImageView(favorite_black)); rs.getBoolean("cached"), new ImageView());
} }
} }
} }
@ -746,16 +733,14 @@ public class DBController {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\";"); ResultSet rs = stmt.executeQuery("SELECT * FROM films WHERE title = \"" + title + "\";");
while (rs.next()) { while (rs.next()) {
// favorite image is black
nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"),
rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"),
rs.getBoolean("cached"), new ImageView(favorite_black)); rs.getBoolean("cached"), new ImageView());
if (rs.getDouble("currentTime") > lastCurrentTime) { if (rs.getDouble("currentTime") > lastCurrentTime) {
lastCurrentTime = rs.getDouble("currentTime"); lastCurrentTime = rs.getDouble("currentTime");
// favorite image is black
nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"), nextFilm = new FilmTabelDataType(rs.getString("streamUrl"), rs.getString("title"),
rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"),
rs.getBoolean("cached"), new ImageView(favorite_black)); rs.getBoolean("cached"), new ImageView());
break; break;
} }
} }