/** * @author Jannik * DBController for Project HomeFlix * connection is in manual commit! */ package org.kellerkinder.Project_HomeFlix; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.eclipsesource.json.Json; import com.eclipsesource.json.JsonArray; import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonValue; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class DBController { public DBController(MainWindowController m, Main main) { mainWindowController = m; this.main = main; } private MainWindowController mainWindowController; private Main main; private String DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db"; //path to database file 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 List filmsdbAll = new ArrayList(); private List filmsdbLocal = new ArrayList(); private List filmsdbStream = new ArrayList(); private List filmsdbStreamURL = new ArrayList(); private List filmsAll = new ArrayList(); private List filmsDir = new ArrayList(); private List filmsStream = new ArrayList(); private List filmsStreamURL = new ArrayList(); private List filmsStreamData = new ArrayList(); Connection connection = null; public void main() { if (System.getProperty("os.name").equals("Linux")) { DB_PATH = System.getProperty("user.home") + "/HomeFlix/Homeflix.db"; }else{ DB_PATH = System.getProperty("user.home") + "\\Documents\\HomeFlix" + "\\" + "Homeflix.db"; } try { // create a database connection connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH); connection.setAutoCommit(false); //AutoCommit to false -> manual commit is active } catch (SQLException e) { // if the error message is "out of memory", it probably means no database file is found System.err.println(e.getMessage()); } //close connection -> at the moment this kills the program // finally { // try { // if (connection != null) // connection.close(); // } catch (SQLException e) { // // connection close failed. // System.err.println(e); // } // } } void createDatabase() { System.out.println("<==========starting loading sql==========>"); PreparedStatement ps; PreparedStatement psS; try { 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_streaming (year, season, episode, rating, resolution, titel, streamUrl, favIcon, cached)"); stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local"); while (rs.next()) { filmsdbLocal.add(rs.getString(2)); } stmt.close(); rs.close(); rs = stmt.executeQuery("SELECT * FROM film_streaming;"); while (rs.next()) { filmsdbStream.add(rs.getString(6)); filmsdbStreamURL.add(rs.getString(7)); } stmt.close(); rs.close(); }catch (SQLException ea){ System.err.println("Ups! an error occured!"); ea.printStackTrace(); } //getting all files from the selected directory TODO rework String[] entries = new File(mainWindowController.getPath()).list(); if(mainWindowController.getPath().equals("") || mainWindowController.getPath() == null){ System.out.println("Kein Pfad angegeben"); //if path == null or "" }else if(new File(mainWindowController.getPath()).exists()) { System.out.println(entries.length); for(int i=0;i!=entries.length;i++){ filmsDir.add(cutOffEnd(entries[i])); } } else { System.out.println(mainWindowController.getPath() + "dosen't exist!"); } //getting all entries from the streaming lists for(int v=0; v< mainWindowController.streamingData.size(); v++){ String fileName = mainWindowController.getStreamingPath()+"/"+mainWindowController.streamingData.get(v).getStreamUrl(); try { JsonObject object = Json.parse(new FileReader(fileName)).asObject(); JsonArray items = object.get("entries").asArray(); for (JsonValue item : items) { filmsStream.add(item.asObject().getString("titel","")); filmsStreamURL.add(item.asObject().getString("streamUrl","")); filmsStreamData.add(fileName); } } catch (IOException e) { e.printStackTrace(); } } //add all entries to filmsAll and filmsdbAl, for later comparing filmsAll.addAll(filmsDir); filmsAll.addAll(filmsStream); filmsdbAll.addAll(filmsdbLocal); filmsdbAll.addAll(filmsdbStream); System.out.println("films in directory: "+filmsAll.size()); System.out.println("filme in db: "+filmsdbAll.size()); /** * if filmsdbAll.size() == 0 database is empty, we need to fill it * else check if there is something to remove or to add * TODO separate local and streaming for better error handling */ if(filmsdbAll.size() == 0){ System.out.println("creating entries ..."); try{ ps = connection.prepareStatement("insert into film_local values (?, ?, ?, ?, ?)"); psS = connection.prepareStatement("insert into film_streaming values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); if(mainWindowController.getPath().equals("") || mainWindowController.getPath() == null){ System.out.println("Kein Pfad angegeben"); //if path == null or "" }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)){ System.out.println("Kein Pfad angegeben"); //if path == null or "" }else{ for(int i=0; i< mainWindowController.streamingData.size(); i++){ String fileNamea = mainWindowController.getStreamingPath()+"/"+mainWindowController.streamingData.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) { e.printStackTrace(); } } } ps.executeBatch(); //execute statement to write entries into table psS.executeBatch(); connection.commit(); ps.close(); psS.close(); }catch (SQLException ea) { System.err.println("Ups! an error occured!"); ea.printStackTrace(); } }else { try { try { checkAddEntry(); //check if added a new file } catch (IOException e) { e.printStackTrace(); } checkRemoveEntry(); //check if removed a file } catch (SQLException e) { e.printStackTrace(); } } //start of cache-table try { Statement stmt = connection.createStatement(); stmt.executeUpdate( "create table if not exists cache (streamUrl, Title, Year, Rated, Released, Runtime, Genre, Director, Writer," //streamUrl is primary key +" Actors, Plot, Language, Country, Awards, Metascore, imdbRating, imdbVotes, imdbID, Type, Poster, Response)"); stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } //loading data from database to mainWindowController void loadData(){ System.out.println("loading data to mwc ..."); try { //load local Data Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local ORDER BY titel"); while (rs.next()) { if(rs.getString(4).equals("favorite_black")){ mainWindowController.localFilms.add( new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_black),rs.getBoolean(5))); }else{ mainWindowController.localFilms.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(); rs.close(); //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;"); while (rs.next()) { if(rs.getString(8).equals("favorite_black")){ mainWindowController.streamingFilms.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))); }else{ mainWindowController.streamingFilms.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(); rs.close(); } catch (SQLException e) { System.err.println("Ups! an error occured!"); e.printStackTrace(); } System.out.println("<==========finished loading sql==========>"); } //Refreshes the data in mainWindowController.newDaten and mainWindowController.streamData //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 void refresh(String name,int i) throws SQLException{ System.out.println("refresh ..."); Statement stmt; try { stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM film_local WHERE titel = \""+name+"\";" ); if(rs.getString(4).equals("favorite_black")){ mainWindowController.localFilms.set(i, new tableData(1, 1, 1, rs.getDouble(1), "1", rs.getString(2), rs.getString(3), new ImageView(favorite_black),rs.getBoolean(5))); }else{ mainWindowController.localFilms.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(); rs.close(); } catch (SQLException e) { try { stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM film_streaming WHERE titel = \""+name+"\";" ); if(rs.getString(8).equals("favorite_black")){ mainWindowController.streamingFilms.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))); }else{ mainWindowController.streamingFilms.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(); rs.close(); } catch (SQLException e1) { System.err.println("Ups! an error occured!"); e1.printStackTrace(); } } } /** * check if there are any entries that have been removed from the film-directory * @throws SQLException */ private void checkRemoveEntry() throws SQLException{ System.out.println("checking for entrys to remove to DB ..."); Statement stmt = connection.createStatement(); for(int a=0; a nameText = new ArrayList(); ArrayList responseText = new ArrayList(); String fontFamily = main.getFONT_FAMILY(); Image im; int fontSize = (int) Math.round(mainWindowController.size); int j=2; nameText.add(0, new Text(mainWindowController.title+": ")); nameText.add(1, new Text(mainWindowController.year+": ")); nameText.add(2, new Text(mainWindowController.rating+": ")); nameText.add(3, new Text(mainWindowController.publishedOn+": ")); nameText.add(4, new Text(mainWindowController.duration+": ")); nameText.add(5, new Text(mainWindowController.genre+": ")); nameText.add(6, new Text(mainWindowController.director+": ")); nameText.add(7, new Text(mainWindowController.writer+": ")); nameText.add(8, new Text(mainWindowController.actors+": ")); nameText.add(9, new Text(mainWindowController.plot+": ")); nameText.add(10, new Text(mainWindowController.language+": ")); nameText.add(11, new Text(mainWindowController.country+": ")); nameText.add(12, new Text(mainWindowController.awards+": ")); nameText.add(13, new Text(mainWindowController.metascore+": ")); nameText.add(14, new Text(mainWindowController.imdbRating+": ")); nameText.add(15, new Text(mainWindowController.type+": ")); for(int i=0; i<15; i++){ responseText.add(new Text(rs.getString(j)+"\n")); j++; } responseText.add(new Text(rs.getString(19)+"\n")); im = new Image(new File(rs.getString(20)).toURI().toString()); stmt.close(); rs.close(); for(int i=0; i