code cleanup

This commit is contained in:
Jannik 2018-03-03 16:51:14 +01:00
parent 8dbce96a25
commit f14159407f
1 changed files with 131 additions and 129 deletions

View File

@ -64,11 +64,12 @@ public class DBController {
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> filmsdbAll = new ArrayList<String>();
private List<String> filmsdbLocal = new ArrayList<String>(); private List<String> filmsdbDir = new ArrayList<String>();
private List<String> filmsdbStream = new ArrayList<String>(); private List<String> filmsdbStream = new ArrayList<String>();
private List<String> filmsdbStreamURL = new ArrayList<String>(); private List<String> filmsdbStreamURL = new ArrayList<String>();
private List<String> filmsAll = new ArrayList<String>(); private List<String> filmsAll = new ArrayList<String>();
private List<String> filmsDir = new ArrayList<String>(); private List<String> filmsDir = new ArrayList<String>();
private List<String> filmsDirURL = new ArrayList<String>();
private List<String> filmsStream = new ArrayList<String>(); private List<String> filmsStream = new ArrayList<String>();
private List<String> filmsStreamURL = new ArrayList<String>(); private List<String> filmsStreamURL = new ArrayList<String>();
private List<String> filmsStreamData = new ArrayList<String>(); private List<String> filmsStreamData = new ArrayList<String>();
@ -84,11 +85,7 @@ public class DBController {
} }
public void loadDatabase() { public void loadDatabase() {
if (System.getProperty("os.name").equals("Linux")) { DB_PATH = main.getDirectory() + "/Homeflix.db";
DB_PATH = System.getProperty("user.home") + "/HomeFlix/Homeflix.db";
}else{
DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db";
}
try { try {
// create a database connection // create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH); connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
@ -101,23 +98,30 @@ public class DBController {
} }
public void createDatabase() { public void createDatabase() {
PreparedStatement ps; /**
PreparedStatement psS; * if tables don't exist create them
* cache table: streamUrl is primary key
*/
try { try {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
stmt.executeUpdate("create table if not exists film_local (rating, titel, streamUrl, favIcon, cached)"); stmt.executeUpdate("create table if not exists film_local (rating, titel, streamUrl, favIcon, cached)");
stmt.executeUpdate("create table if not exists film_streaming (year, season, episode, rating, resolution, titel, streamUrl, favIcon, cached)"); stmt.executeUpdate("create table if not exists film_streaming (year, season, episode,"
+ " rating, resolution, titel, streamUrl, favIcon, cached)");
stmt.executeUpdate("create table if not exists cache ("
+ "streamUrl, Title, Year, Rated, Released, Runtime, Genre, Director, Writer,"
+ " Actors, Plot, Language, Country, Awards, Metascore, imdbRating, imdbVotes,"
+ " imdbID, Type, Poster, Response)");
stmt.close(); stmt.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error(e); LOGGER.error(e);
} }
// get all entries from the tables
try { try {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM film_local"); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local");
while (rs.next()) { while (rs.next()) {
filmsdbLocal.add(rs.getString(2)); filmsdbDir.add(rs.getString(2));
} }
stmt.close(); stmt.close();
rs.close(); rs.close();
@ -133,23 +137,22 @@ public class DBController {
LOGGER.error("Ups! an error occured!", e); LOGGER.error("Ups! an error occured!", e);
} }
// getting all files from the selected directory TODO rework // getting all files from the selected directory
String[] entries = new File(mainWindowController.getPath()).list(); if (new File(mainWindowController.getPath()).exists()) {
if (mainWindowController.getPath().equals("") || mainWindowController.getPath() == null) { for (String entry : new File(mainWindowController.getPath()).list()) {
LOGGER.warn("no path selected!"); filmsDir.add(cutOffEnd(entry));
} else if (new File(mainWindowController.getPath()).exists()) { filmsDirURL.add(entry);
LOGGER.info(entries.length); // System.out.println(cutOffEnd(entry));
for (int i = 0; i != entries.length; i++) { // System.out.println(entry);
filmsDir.add(cutOffEnd(entries[i]));
} }
} else { } else {
LOGGER.error(mainWindowController.getPath() + "dosen't exist!"); LOGGER.error(mainWindowController.getPath() + "dosen't exist!");
} }
// getting all entries from the streaming lists // getting all entries from the streaming lists
for (int v = 0; v < mainWindowController.getStreamingData().size(); v++) { for (int i = 0; i < mainWindowController.getStreamingData().size(); i++) {
String fileName = mainWindowController.getStreamingPath() + "/" String fileName = mainWindowController.getStreamingPath() + "/"
+ mainWindowController.getStreamingData().get(v).getStreamUrl(); + mainWindowController.getStreamingData().get(i).getStreamUrl();
try { try {
JsonObject object = Json.parse(new FileReader(fileName)).asObject(); JsonObject object = Json.parse(new FileReader(fileName)).asObject();
JsonArray items = object.get("entries").asArray(); JsonArray items = object.get("entries").asArray();
@ -157,6 +160,10 @@ public class DBController {
filmsStream.add(item.asObject().getString("titel", "")); filmsStream.add(item.asObject().getString("titel", ""));
filmsStreamURL.add(item.asObject().getString("streamUrl", "")); filmsStreamURL.add(item.asObject().getString("streamUrl", ""));
filmsStreamData.add(fileName); filmsStreamData.add(fileName);
// System.out.println(item.asObject().getString("titel", ""));
// System.out.println(item.asObject().getString("streamUrl", ""));
// System.out.println(fileName);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -166,73 +173,18 @@ public class DBController {
// add all entries to filmsAll and filmsdbAl, for later comparing // add all entries to filmsAll and filmsdbAl, for later comparing
filmsAll.addAll(filmsDir); filmsAll.addAll(filmsDir);
filmsAll.addAll(filmsStream); filmsAll.addAll(filmsStream);
filmsdbAll.addAll(filmsdbLocal); filmsdbAll.addAll(filmsdbDir);
filmsdbAll.addAll(filmsdbStream); filmsdbAll.addAll(filmsdbStream);
LOGGER.info("films in directory: " + filmsAll.size()); LOGGER.info("films in directory: " + filmsAll.size());
LOGGER.info("filme in db: " + filmsdbAll.size()); LOGGER.info("filme in db: " + filmsdbAll.size());
/** /**
* if filmsdbAll.size() == 0 database is empty, we need to fill it else check if * if filmsdbAll.size() == 0 the database is empty, we need to fill it with loadFilms()
* there is something to remove or to add TODO separate local and streaming for * else check if there is something to remove or to add
* better error handling
*/ */
if (filmsdbAll.size() == 0) { if (filmsdbAll.size() == 0) {
LOGGER.info("Database is empty, creating tables ..."); loadFilms();
try {
ps = connection.prepareStatement("insert into film_local values (?, ?, ?, ?, ?)");
psS = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (mainWindowController.getPath().equals("") || mainWindowController.getPath() == null) {
LOGGER.warn("no path selected!");
} else if (new File(mainWindowController.getPath()).exists()) {
for (int j = 0; j != entries.length; j++) // goes through all the files in the directory
{
ps.setInt(1, 0); // rating as integer 1. column
ps.setString(2, cutOffEnd(entries[j])); // name as String without ending 2. column
ps.setString(3, entries[j]); // path as String 3. column
ps.setString(4, "favorite_border_black");
ps.setBoolean(5, false);
ps.addBatch(); // add command to prepared statement
}
}
if (mainWindowController.getStreamingPath().equals("") || mainWindowController.getStreamingPath().equals(null)) {
LOGGER.warn("no path selected!");
} else {
for (int i = 0; i < mainWindowController.getStreamingData().size(); i++) {
String fileNamea = mainWindowController.getStreamingPath() + "/"
+ mainWindowController.getStreamingData().get(i).getStreamUrl();
try {
JsonObject object = Json.parse(new FileReader(fileNamea)).asObject();
JsonArray items = object.get("entries").asArray();
for (JsonValue item : items) {
psS.setInt(1, item.asObject().getInt("year", 0));
psS.setInt(2, item.asObject().getInt("season", 0));
psS.setInt(3, item.asObject().getInt("episode", 0));
psS.setInt(4, 0);
psS.setString(5, item.asObject().getString("resolution", ""));
psS.setString(6, item.asObject().getString("titel", ""));
psS.setString(7, item.asObject().getString("streamUrl", ""));
psS.setString(8, "favorite_border_black");
psS.setBoolean(9, false);
psS.addBatch(); // add command to prepared statement
}
} catch (IOException e) {
LOGGER.error(e);
}
}
}
ps.executeBatch(); // execute statement to write entries into table
psS.executeBatch();
connection.commit();
ps.close();
psS.close();
} catch (SQLException e) {
LOGGER.error(e);
}
} else { } else {
// check if film added or removed
try { try {
checkAddEntry(); checkAddEntry();
checkRemoveEntry(); checkRemoveEntry();
@ -240,35 +192,80 @@ public class DBController {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
// if cache table dosen't exist create it
private void loadFilms() {
PreparedStatement ps;
PreparedStatement psS;
LOGGER.info("Database is empty, filling tables ...");
try { try {
Statement stmt = connection.createStatement(); ps = connection.prepareStatement("insert into film_local values (?, ?, ?, ?, ?)");
// streamUrl is primary key psS = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
stmt.executeUpdate("create table if not exists cache ("
+ "streamUrl, Title, Year, Rated, Released, Runtime, Genre, Director, Writer," if (new File(mainWindowController.getPath()).exists()) {
+ " Actors, Plot, Language, Country, Awards, Metascore, imdbRating, imdbVotes," // go through all the files in the directory
+" imdbID, Type, Poster, Response)" for (int j = 0; j != filmsDir.size(); j++)
); {
stmt.close(); ps.setInt(1, 0); // rating as integer 1. column
ps.setString(2, filmsDir.get(j)); // name as String without ending 2. column
ps.setString(3, filmsDirURL.get(j)); // path as String 3. column
ps.setString(4, "favorite_border_black");
ps.setBoolean(5, false);
ps.addBatch(); // add command to prepared statement
}
}
if (mainWindowController.getStreamingPath().equals("") || mainWindowController.getStreamingPath().equals(null)) {
LOGGER.warn("no path selected!");
} else {
for (int i = 0; i < mainWindowController.getStreamingData().size(); i++) {
String fileNamea = mainWindowController.getStreamingPath() + "/"
+ mainWindowController.getStreamingData().get(i).getStreamUrl();
try {
JsonObject object = Json.parse(new FileReader(fileNamea)).asObject();
JsonArray items = object.get("entries").asArray();
for (JsonValue item : items) {
psS.setInt(1, item.asObject().getInt("year", 0));
psS.setInt(2, item.asObject().getInt("season", 0));
psS.setInt(3, item.asObject().getInt("episode", 0));
psS.setInt(4, 0);
psS.setString(5, item.asObject().getString("resolution", ""));
psS.setString(6, item.asObject().getString("titel", ""));
psS.setString(7, item.asObject().getString("streamUrl", ""));
psS.setString(8, "favorite_border_black");
psS.setBoolean(9, false);
psS.addBatch(); // add command to prepared statement
}
} catch (IOException e) {
LOGGER.error(e);
}
}
}
ps.executeBatch(); // execute statement to write entries into table
psS.executeBatch();
connection.commit();
ps.close();
psS.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error(e); LOGGER.error(e);
} }
} }
// loading data from database to mainWindowController // loading data from database to mainWindowController
public void loadData(){ public void loadData() {
LOGGER.info("loading data to mwc ..."); LOGGER.info("loading data to mwc ...");
try { try {
//load local Data //load local Data
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM film_local ORDER BY titel"); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local ORDER BY titel");
while (rs.next()) { while (rs.next()) {
if(rs.getString(4).equals("favorite_black")){ if (rs.getString(4).equals("favorite_black")) {
mainWindowController.getLocalFilms().add( new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_black),rs.getBoolean(5))); mainWindowController.getLocalFilms().add(new tableData(1, 1, 1, rs.getDouble(1), "1",
}else{ rs.getString(2), rs.getString(3), new ImageView(favorite_black), rs.getBoolean(5)));
mainWindowController.getLocalFilms().add( new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_border_black),rs.getBoolean(5))); } else {
mainWindowController.getLocalFilms().add(new tableData(1, 1, 1, rs.getDouble(1), "1",
rs.getString(2), rs.getString(3), new ImageView(favorite_border_black), rs.getBoolean(5)));
} }
} }
stmt.close(); stmt.close();
@ -277,10 +274,14 @@ public class DBController {
//load streaming Data FIXME check if there are streaming data before loading -> maybe there is an issue now //load streaming Data FIXME check if there are streaming data before loading -> maybe there is an issue now
rs = stmt.executeQuery("SELECT * FROM film_streaming ORDER BY titel;"); rs = stmt.executeQuery("SELECT * FROM film_streaming ORDER BY titel;");
while (rs.next()) { while (rs.next()) {
if(rs.getString(8).equals("favorite_black")){ if (rs.getString(8).equals("favorite_black")) {
mainWindowController.getStreamingFilms().add(new tableData(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6), rs.getString(7), new ImageView(favorite_black),rs.getBoolean(9))); mainWindowController.getStreamingFilms().add(new tableData(rs.getInt(1),
}else{ rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6),
mainWindowController.getStreamingFilms().add(new tableData(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6), rs.getString(7), new ImageView(favorite_border_black),rs.getBoolean(9))); rs.getString(7), new ImageView(favorite_black),rs.getBoolean(9)));
} else {
mainWindowController.getStreamingFilms().add(new tableData(rs.getInt(1),
rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6),
rs.getString(7), new ImageView(favorite_border_black), rs.getBoolean(9)));
} }
} }
stmt.close(); stmt.close();
@ -297,15 +298,17 @@ public class DBController {
//FIXME it seems that there is an issue at the moment with streaming refreshing wrong entry if there is more than one with the same name //FIXME it seems that there is an issue at the moment with streaming refreshing wrong entry if there is more than one with the same name
public void refresh(String name, int i) throws SQLException { public void refresh(String name, int i) throws SQLException {
LOGGER.info("refresh ..."); LOGGER.info("refresh ...");
Statement stmt; Statement stmt;
try { try {
stmt = connection.createStatement(); stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM film_local WHERE titel = \""+name+"\";" ); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local WHERE titel = \""+name+"\";" );
if(rs.getString(4).equals("favorite_black")){ if (rs.getString(4).equals("favorite_black")) {
mainWindowController.getLocalFilms().set(i, new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_black),rs.getBoolean(5))); mainWindowController.getLocalFilms().set(i, new tableData(1, 1, 1, rs.getDouble(1), "1",
}else{ rs.getString(2), rs.getString(3), new ImageView(favorite_black), rs.getBoolean(5)));
mainWindowController.getLocalFilms().set(i, new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_border_black),rs.getBoolean(5))); } else {
mainWindowController.getLocalFilms().set(i, new tableData(1, 1, 1, rs.getDouble(1), "1",
rs.getString(2), rs.getString(3), new ImageView(favorite_border_black), rs.getBoolean(5)));
} }
stmt.close(); stmt.close();
rs.close(); rs.close();
@ -314,10 +317,14 @@ public class DBController {
try { try {
stmt = connection.createStatement(); stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM film_streaming WHERE titel = \""+name+"\";" ); ResultSet rs = stmt.executeQuery("SELECT * FROM film_streaming WHERE titel = \""+name+"\";" );
if(rs.getString(8).equals("favorite_black")){ if (rs.getString(8).equals("favorite_black")) {
mainWindowController.getStreamingFilms().set(i,new tableData(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6), rs.getString(7), new ImageView(favorite_black),rs.getBoolean(9))); mainWindowController.getStreamingFilms().set(i, new tableData(rs.getInt(1), rs.getInt(2),
}else{ rs.getInt(3), rs.getDouble(4),rs.getString(5), rs.getString(6), rs.getString(7),
mainWindowController.getStreamingFilms().set(i,new tableData(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6), rs.getString(7), new ImageView(favorite_border_black),rs.getBoolean(9))); new ImageView(favorite_black), rs.getBoolean(9)));
} else {
mainWindowController.getStreamingFilms().set(i, new tableData(rs.getInt(1), rs.getInt(2),
rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6), rs.getString(7),
new ImageView(favorite_border_black), rs.getBoolean(9)));
} }
stmt.close(); stmt.close();
rs.close(); rs.close();
@ -329,33 +336,32 @@ public class DBController {
/** /**
* check if there are any entries that have been removed from the film-directory * check if there are any entries that have been removed from the film-directory
*
* @throws SQLException * @throws SQLException
*/ */
private void checkRemoveEntry() throws SQLException { private void checkRemoveEntry() throws SQLException {
LOGGER.info("checking for entrys to remove to DB ..."); LOGGER.info("checking for entrys to remove to DB ...");
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
for (int a = 0; a < filmsdbLocal.size(); a++) { for (int i = 0; i < filmsdbDir.size(); i++) {
if (!filmsDir.contains(filmsdbLocal.get(a))) { if (!filmsDir.contains(filmsdbDir.get(i))) {
try { try {
stmt.executeUpdate("delete from film_local where titel = \"" + filmsdbLocal.get(a) + "\""); stmt.executeUpdate("delete from film_local where titel = \"" + filmsdbDir.get(i) + "\"");
connection.commit(); connection.commit();
stmt.close(); stmt.close();
LOGGER.info("removed \"" + filmsdbLocal.get(a) + "\" from database"); LOGGER.info("removed \"" + filmsdbDir.get(i) + "\" from database");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
for (int b = 0; b < filmsdbStreamURL.size(); b++) { for (int j = 0; j < filmsdbStreamURL.size(); j++) {
if (!filmsStreamURL.contains(filmsdbStreamURL.get(b))) { if (!filmsStreamURL.contains(filmsdbStreamURL.get(j))) {
try { try {
stmt.executeUpdate("delete from film_streaming where titel = \"" + filmsdbStream.get(b) + "\""); stmt.executeUpdate("delete from film_streaming where titel = \"" + filmsdbStream.get(j) + "\"");
connection.commit(); connection.commit();
stmt.close(); stmt.close();
LOGGER.info("removed \"" + filmsdbStream.get(b) + "\" from database"); LOGGER.info("removed \"" + filmsdbStream.get(j) + "\" from database");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -365,24 +371,21 @@ public class DBController {
/** /**
* check if there are new films in the film-directory * check if there are new films in the film-directory
*
* @throws SQLException * @throws SQLException
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws IOException * @throws IOException
* if lastName != filmsStreamData.get(b) then set i = 0, file * if lastName != filmsStreamData.get(b) then set i = 0, file changed
* changed
*/ */
private void checkAddEntry() throws SQLException, FileNotFoundException, IOException { private void checkAddEntry() throws SQLException, FileNotFoundException, IOException {
String lastName = ""; String lastName = "";
LOGGER.info("checking for entrys to add to DB ..."); LOGGER.info("checking for entrys to add to DB ...");
String[] entries = new File(mainWindowController.getPath()).list(); String[] entries = new File(mainWindowController.getPath()).list();
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
PreparedStatement ps = connection PreparedStatement ps = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
int i = 0; int i = 0;
for (int a = 0; a < filmsDir.size(); a++) { for (int a = 0; a < filmsDir.size(); a++) {
if (!filmsdbLocal.contains(filmsDir.get(a))) { if (!filmsdbDir.contains(filmsDir.get(a))) {
stmt.executeUpdate("insert into film_local values (0, \"" + cutOffEnd(entries[a]) + "\", \"" stmt.executeUpdate("insert into film_local values (0, \"" + cutOffEnd(entries[a]) + "\", \""
+ entries[a] + "\",\"favorite_border_black\",0)"); + entries[a] + "\",\"favorite_border_black\",0)");
connection.commit(); connection.commit();
@ -479,8 +482,7 @@ public class DBController {
LOGGER.info("streaming:" + rsS.getString("rating") + ", " + rsS.getString("titel") + ", " + rsS.getString("favIcon")); LOGGER.info("streaming:" + rsS.getString("rating") + ", " + rsS.getString("titel") + ", " + rsS.getString("favIcon"));
stmtS.close(); stmtS.close();
rsS.close(); rsS.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Ups! an error occured!", e); LOGGER.error("Ups! an error occured!", e);
} }