2018-03-01 15:42:47 +01:00
/ * *
* Project - HomeFlix
*
* Copyright 2016 - 2018 < @Seil0 >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
* /
package kellerkinder.HomeFlix.controller ;
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 org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
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 ;
import kellerkinder.HomeFlix.application.Main ;
import kellerkinder.HomeFlix.application.MainWindowController ;
2018-03-05 22:37:32 +01:00
import kellerkinder.HomeFlix.datatypes.SourceDataType ;
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType ;
2018-03-01 15:42:47 +01:00
public class DBController {
2018-04-02 18:29:59 +02:00
2018-03-01 15:42:47 +01:00
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 < String > filmsdbAll = new ArrayList < String > ( ) ;
2018-03-07 00:20:14 +01:00
private List < String > filmsdbDir = new ArrayList < String > ( ) ;
2018-03-04 20:07:43 +01:00
private List < String > filmsdbStreamURL = new ArrayList < String > ( ) ; // needed
private List < String > filmsStreamURL = new ArrayList < String > ( ) ; // needed
2018-03-01 15:42:47 +01:00
private Connection connection = null ;
private static final Logger LOGGER = LogManager . getLogger ( DBController . class . getName ( ) ) ;
2018-04-02 18:29:59 +02:00
/ * *
* constructor for DBController
* @param main the main object
* @param mainWindowController the mainWindowController object
* /
public DBController ( Main main , MainWindowController mainWindowController ) {
this . main = main ;
this . mainWindowController = mainWindowController ;
}
/ * *
* initialize the { @link DBController }
* initialize the database connection
* check if there is a need to create a new database
* refresh the database
* /
2018-03-01 15:42:47 +01:00
public void init ( ) {
LOGGER . info ( " <========== starting loading sql ==========> " ) ;
2018-03-04 20:07:43 +01:00
initDatabaseConnection ( ) ;
2018-03-01 15:42:47 +01:00
createDatabase ( ) ;
2018-03-04 20:07:43 +01:00
refreshDataBase ( ) ;
2018-03-01 15:42:47 +01:00
LOGGER . info ( " <========== finished loading sql ==========> " ) ;
}
2018-04-02 18:29:59 +02:00
/ * *
* create a new connection to the HomeFlix . db database
* AutoCommit is set to false to prevent some issues , so manual commit is active !
* /
2018-03-05 22:37:32 +01:00
private void initDatabaseConnection ( ) {
2018-03-03 16:51:14 +01:00
DB_PATH = main . getDirectory ( ) + " /Homeflix.db " ;
2018-03-01 15:42:47 +01:00
try {
// create a database connection
connection = DriverManager . getConnection ( " jdbc:sqlite: " + DB_PATH ) ;
2018-04-02 18:29:59 +02:00
connection . setAutoCommit ( false ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
// if the error message is "out of memory", it probably means no database file is found
LOGGER . error ( " error while loading the ROM database " , e ) ;
}
LOGGER . info ( " ROM database loaded successfull " ) ;
}
2018-03-04 20:07:43 +01:00
/ * *
* if tables don ' t exist create them
2018-04-02 18:29:59 +02:00
* films table : streamUrl is primary key
2018-03-04 20:07:43 +01:00
* cache table : streamUrl is primary key
* /
private void createDatabase ( ) {
2018-03-01 15:42:47 +01:00
try {
Statement stmt = connection . createStatement ( ) ;
2018-04-02 02:18:43 +02:00
stmt . executeUpdate ( " create table if not exists films (streamUrl, title, season, episode, favorite, cached, currentTime) " ) ;
2018-03-03 16:51:14 +01:00
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) " ) ;
2018-03-01 15:42:47 +01:00
stmt . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( e ) ;
2018-03-04 20:07:43 +01:00
}
}
2018-04-02 18:29:59 +02:00
/ * *
* get all database entries
* /
2018-03-04 20:07:43 +01:00
private void loadDatabase ( ) {
2018-03-07 00:20:14 +01:00
// get all entries from the table
2018-03-01 15:42:47 +01:00
try {
Statement stmt = connection . createStatement ( ) ;
2018-03-07 00:20:14 +01:00
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films " ) ;
2018-03-01 15:42:47 +01:00
while ( rs . next ( ) ) {
2018-03-05 23:03:05 +01:00
filmsdbDir . add ( rs . getString ( " title " ) ) ;
2018-03-06 20:41:52 +01:00
filmsdbStreamURL . add ( rs . getString ( " streamUrl " ) ) ;
2018-03-01 15:42:47 +01:00
}
stmt . close ( ) ;
rs . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
// add all entries to filmsAll and filmsdbAl, for later comparing
2018-03-03 16:51:14 +01:00
filmsdbAll . addAll ( filmsdbDir ) ;
2018-03-07 00:20:14 +01:00
LOGGER . info ( " films in directory: " + filmsStreamURL . size ( ) ) ;
LOGGER . info ( " filme in db: " + filmsdbStreamURL . size ( ) ) ;
2018-03-03 16:51:14 +01:00
}
2018-04-02 18:29:59 +02:00
/ * *
* load sources from sources . json
* if mode = = local , get all files and series - folder from the directory
* else mode must be streaming , read all entries from the streaming file
* /
2018-03-04 20:07:43 +01:00
private void loadSources ( ) {
// remove sources from table
mainWindowController . getSourcesList ( ) . removeAll ( mainWindowController . getSourcesList ( ) ) ;
2018-03-07 23:51:02 +01:00
mainWindowController . getSourceRoot ( ) . getChildren ( ) . removeAll ( mainWindowController . getSourceRoot ( ) . getChildren ( ) ) ;
2018-03-03 16:51:14 +01:00
2018-03-01 15:42:47 +01:00
try {
2018-03-04 20:07:43 +01:00
JsonArray sources = Json . parse ( new FileReader ( main . getDirectory ( ) + " /sources.json " ) ) . asArray ( ) ;
for ( JsonValue source : sources ) {
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 " ) ) {
2018-03-07 00:20:14 +01:00
for ( File file : new File ( path ) . listFiles ( ) ) {
if ( file . isFile ( ) ) {
// get all files (films)
filmsStreamURL . add ( file . getPath ( ) ) ;
} else {
// get all folders (series)
for ( File season : file . listFiles ( ) ) {
if ( season . isDirectory ( ) ) {
for ( File episode : season . listFiles ( ) ) {
if ( ! filmsdbStreamURL . contains ( episode . getPath ( ) ) ) {
filmsStreamURL . add ( episode . getPath ( ) ) ;
}
}
}
}
2018-03-04 20:07:43 +01:00
}
}
2018-03-07 00:20:14 +01:00
LOGGER . info ( " added files from: " + path ) ;
2018-03-04 20:07:43 +01:00
} else {
// getting all entries from the streaming lists
2018-03-03 16:51:14 +01:00
try {
2018-03-04 20:07:43 +01:00
JsonObject object = Json . parse ( new FileReader ( path ) ) . asObject ( ) ;
2018-03-03 16:51:14 +01:00
JsonArray items = object . get ( " entries " ) . asArray ( ) ;
for ( JsonValue item : items ) {
2018-03-04 20:07:43 +01:00
filmsStreamURL . add ( item . asObject ( ) . getString ( " streamUrl " , " " ) ) ;
2018-03-03 16:51:14 +01:00
}
2018-03-04 20:07:43 +01:00
LOGGER . info ( " added films from: " + path ) ;
2018-03-03 16:51:14 +01:00
} catch ( IOException e ) {
LOGGER . error ( e ) ;
}
}
}
2018-03-04 20:07:43 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-04-02 18:29:59 +02:00
/ * *
* load the data to the mainWindowController
* order entries by title
* /
2018-03-05 22:37:32 +01:00
private void loadDataToMWC ( ) {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " loading data to mwc ... " ) ;
2018-03-03 16:51:14 +01:00
try {
2018-03-01 15:42:47 +01:00
//load local Data
Statement stmt = connection . createStatement ( ) ;
2018-03-07 00:20:14 +01:00
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films ORDER BY title " ) ;
2018-03-01 15:42:47 +01:00
while ( rs . next ( ) ) {
2018-03-09 20:12:03 +01:00
// System.out.println(rs.getString("title") + "Season:" + rs.getString("season") + ":");
if ( rs . getBoolean ( " favorite " ) = = true ) {
2018-03-07 23:51:02 +01:00
mainWindowController . getFilmsList ( ) . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
2018-03-09 20:12:03 +01:00
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-03-06 20:41:52 +01:00
rs . getBoolean ( " cached " ) , new ImageView ( favorite_black ) ) ) ;
2018-03-03 16:51:14 +01:00
} else {
2018-03-07 23:51:02 +01:00
mainWindowController . getFilmsList ( ) . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
2018-03-09 20:12:03 +01:00
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-03-06 20:41:52 +01:00
rs . getBoolean ( " cached " ) , new ImageView ( favorite_border_black ) ) ) ;
2018-03-01 15:42:47 +01:00
}
}
stmt . close ( ) ;
2018-03-07 00:20:14 +01:00
rs . close ( ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
LOGGER . info ( " loading data to the GUI ... " ) ;
mainWindowController . addDataUI ( ) ;
}
2018-03-05 22:37:32 +01:00
/ * *
* refresh data in mainWindowController localFilms and streamingFilms
2018-03-06 20:41:52 +01:00
* @param streamUrl of the film
* @param index of the film in LocalFilms list
2018-03-05 22:37:32 +01:00
* /
2018-03-08 23:01:07 +01:00
public void refresh ( String streamUrl , int indexList ) {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " refresh ... " ) ;
try {
2018-03-05 22:37:32 +01:00
Statement stmt = connection . createStatement ( ) ;
2018-03-07 00:20:14 +01:00
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE streamUrl = \" " + streamUrl + " \" ; " ) ;
2018-03-05 22:37:32 +01:00
2018-03-09 20:12:03 +01:00
if ( rs . getBoolean ( " favorite " ) = = true ) {
2018-03-08 23:01:07 +01:00
mainWindowController . getFilmsList ( ) . set ( indexList , new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
2018-03-09 20:12:03 +01:00
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-03-07 00:20:14 +01:00
rs . getBoolean ( " cached " ) , new ImageView ( favorite_black ) ) ) ;
2018-03-03 16:51:14 +01:00
} else {
2018-03-08 23:01:07 +01:00
mainWindowController . getFilmsList ( ) . set ( indexList , new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
2018-03-09 20:12:03 +01:00
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-03-07 00:20:14 +01:00
rs . getBoolean ( " cached " ) , new ImageView ( favorite_border_black ) ) ) ;
2018-03-05 22:37:32 +01:00
}
2018-03-08 23:01:07 +01:00
2018-03-07 00:20:14 +01:00
rs . close ( ) ;
2018-03-05 22:37:32 +01:00
stmt . close ( ) ;
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while refreshing mwc! " , e ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-03-04 20:07:43 +01:00
/ * *
* refresh database to contain all ( new added ) entries
* refresh the MainWindowController content ,
* to contain all ( new added ) entries from the database
* /
public void refreshDataBase ( ) {
LOGGER . info ( " refreshing the Database ... " ) ;
// clean all ArraLists
filmsdbAll . removeAll ( filmsdbAll ) ;
filmsdbDir . removeAll ( filmsdbDir ) ;
filmsdbStreamURL . removeAll ( filmsdbStreamURL ) ;
filmsStreamURL . removeAll ( filmsStreamURL ) ;
loadSources ( ) ; // reload all sources
loadDatabase ( ) ; // reload all films saved in the DB
try {
checkAddEntry ( ) ;
2018-03-07 00:20:14 +01:00
checkRemoveEntry ( ) ;
2018-03-04 20:07:43 +01:00
} catch ( Exception e ) {
LOGGER . error ( " Error while refreshing the database " , e ) ;
}
// remove all films from the mwc lists
2018-03-07 23:51:02 +01:00
mainWindowController . getFilmsList ( ) . removeAll ( mainWindowController . getFilmsList ( ) ) ;
2018-03-05 22:37:32 +01:00
mainWindowController . getFilmRoot ( ) . getChildren ( ) . removeAll ( mainWindowController . getFilmRoot ( ) . getChildren ( ) ) ;
2018-03-04 20:07:43 +01:00
loadDataToMWC ( ) ; // load the new data to the mwc
}
2018-03-01 15:42:47 +01:00
/ * *
* check if there are any entries that have been removed from the film - directory
* /
2018-03-04 20:07:43 +01:00
private void checkRemoveEntry ( ) {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " checking for entrys to remove to DB ... " ) ;
2018-03-04 20:07:43 +01:00
try {
Statement stmt = connection . createStatement ( ) ;
for ( String entry : filmsdbStreamURL ) {
if ( ! filmsStreamURL . contains ( entry ) ) {
2018-03-07 00:20:14 +01:00
System . out . println ( filmsdbStreamURL + " \ n " ) ;
System . out . println ( filmsStreamURL ) ;
stmt . executeUpdate ( " delete from films where streamUrl = \" " + entry + " \" " ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
2018-03-07 00:20:14 +01:00
LOGGER . info ( " removed \" " + entry + " \" from database " ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-03-07 00:20:14 +01:00
2018-03-05 22:37:32 +01:00
stmt . close ( ) ;
2018-03-04 20:07:43 +01:00
} catch ( Exception e ) {
LOGGER . error ( e ) ;
2018-03-01 15:42:47 +01:00
}
}
/ * *
* check if there are new films in the film - directory
* @throws SQLException
* @throws FileNotFoundException
* @throws IOException
* /
private void checkAddEntry ( ) throws SQLException , FileNotFoundException , IOException {
Statement stmt = connection . createStatement ( ) ;
2018-04-02 02:18:43 +02:00
PreparedStatement ps = connection . prepareStatement ( " insert into films values (?, ?, ?, ?, ?, ?, ?) " ) ;
2018-03-04 20:07:43 +01:00
LOGGER . info ( " checking for entrys to add to DB ... " ) ;
// source is a single source of the sources list
2018-03-05 22:37:32 +01:00
for ( SourceDataType source : mainWindowController . getSourcesList ( ) ) {
2018-03-04 20:07:43 +01:00
// if it's a local source check the folder for new film
2018-03-05 22:37:32 +01:00
if ( source . getMode ( ) . equals ( " local " ) ) {
2018-03-06 20:41:52 +01:00
for ( File file : new File ( source . getPath ( ) ) . listFiles ( ) ) {
if ( file . isFile ( ) ) {
// get all files (films)
if ( ! filmsdbStreamURL . contains ( file . getPath ( ) ) ) {
2018-03-07 00:20:14 +01:00
stmt . executeUpdate ( " insert into films values ( "
2018-03-06 20:41:52 +01:00
+ " ' " + file . getPath ( ) + " ', "
2018-04-02 02:18:43 +02:00
+ " ' " + cutOffEnd ( file . getName ( ) ) + " ', '', '', 0, 0, 0.0) " ) ;
2018-03-06 20:41:52 +01:00
connection . commit ( ) ;
stmt . close ( ) ;
LOGGER . info ( " Added \" " + file . getName ( ) + " \" to database " ) ;
2018-03-07 00:20:14 +01:00
filmsdbStreamURL . add ( file . getPath ( ) ) ;
2018-03-06 20:41:52 +01:00
}
} else {
// get all folders (series)
int sn = 1 ;
for ( File season : file . listFiles ( ) ) {
if ( season . isDirectory ( ) ) {
int ep = 1 ;
for ( File episode : season . listFiles ( ) ) {
if ( ! filmsdbStreamURL . contains ( episode . getPath ( ) ) ) {
2018-03-07 00:20:14 +01:00
LOGGER . info ( " Added \" " + file . getName ( ) + " \" , Episode: " + episode . getName ( ) + " to database " ) ;
stmt . executeUpdate ( " insert into films values ( "
2018-03-28 21:58:01 +02:00
+ " ' " + episode . getPath ( ) . replace ( " ' " , " '' " ) + " ', "
2018-04-02 02:18:43 +02:00
+ " ' " + cutOffEnd ( file . getName ( ) ) + " ',' " + sn + " ',' " + ep + " ', 0, 0, 0.0) " ) ;
2018-03-06 20:41:52 +01:00
connection . commit ( ) ;
stmt . close ( ) ;
2018-03-07 00:20:14 +01:00
filmsStreamURL . add ( episode . getPath ( ) ) ;
2018-03-06 20:41:52 +01:00
filmsdbStreamURL . add ( episode . getPath ( ) ) ;
ep + + ;
}
}
sn + + ;
}
}
2018-03-04 20:07:43 +01:00
}
2018-03-06 20:41:52 +01:00
2018-03-04 20:07:43 +01:00
}
} else {
2018-03-07 23:51:02 +01:00
// if it's a streaming source check the file for new films
2018-03-05 16:51:51 +01:00
for ( String entry : filmsStreamURL ) {
if ( ! filmsdbStreamURL . contains ( entry ) ) {
2018-03-05 22:37:32 +01:00
JsonArray items = Json . parse ( new FileReader ( source . getPath ( ) ) ) . asObject ( ) . get ( " entries " ) . asArray ( ) ;
2018-03-05 16:51:51 +01:00
// for each item, check if it's the needed
for ( JsonValue item : items ) {
String streamUrl = item . asObject ( ) . getString ( " streamUrl " , " " ) ;
2018-03-05 23:03:05 +01:00
String title = item . asObject ( ) . getString ( " title " , " " ) ;
2018-03-05 16:51:51 +01:00
// if it's the needed add it to the database
if ( streamUrl . equals ( entry ) ) {
2018-03-06 11:26:39 +01:00
ps . setString ( 1 , streamUrl ) ;
ps . setString ( 2 , title ) ;
2018-03-09 20:12:03 +01:00
ps . setString ( 3 , item . asObject ( ) . getString ( " season " , " " ) ) ;
ps . setString ( 4 , item . asObject ( ) . getString ( " episode " , " " ) ) ;
2018-03-06 11:26:39 +01:00
ps . setInt ( 5 , 0 ) ;
2018-03-05 23:03:05 +01:00
ps . setBoolean ( 6 , false ) ;
2018-04-02 02:18:43 +02:00
ps . setDouble ( 7 , 0 ) ;
2018-03-05 16:51:51 +01:00
ps . addBatch ( ) ; // adds the entry
2018-03-06 20:41:52 +01:00
LOGGER . info ( " Added \" " + title + " \" to database " ) ;
2018-03-07 00:20:14 +01:00
filmsdbStreamURL . add ( streamUrl ) ;
2018-03-05 16:51:51 +01:00
}
}
}
2018-03-01 15:42:47 +01:00
}
2018-03-05 16:51:51 +01:00
ps . executeBatch ( ) ;
connection . commit ( ) ;
ps . close ( ) ;
2018-03-01 15:42:47 +01:00
}
}
}
2018-04-02 18:29:59 +02:00
/ * *
* DEBUG
* prints all entries from the database to the console
* /
2018-03-05 16:51:51 +01:00
public void printAllDBEntriesDEBUG ( ) {
System . out . println ( " Outputting all entries ... \ n " ) ;
2018-03-01 15:42:47 +01:00
try {
Statement stmt = connection . createStatement ( ) ;
2018-03-07 00:20:14 +01:00
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films " ) ;
2018-03-01 15:42:47 +01:00
while ( rs . next ( ) ) {
2018-03-05 22:37:32 +01:00
System . out . println ( rs . getString ( " streamUrl " ) ) ;
2018-03-06 11:26:39 +01:00
System . out . println ( rs . getString ( " title " ) ) ;
System . out . println ( rs . getString ( " season " ) ) ;
System . out . println ( rs . getString ( " episode " ) ) ;
System . out . println ( rs . getString ( " rating " ) ) ;
2018-04-02 02:18:43 +02:00
System . out . println ( rs . getString ( " cached " ) ) ;
System . out . println ( rs . getString ( " currentTime " ) + " \ n " ) ;
2018-03-01 15:42:47 +01:00
}
stmt . close ( ) ;
rs . close ( ) ;
} catch ( SQLException e ) {
2018-03-05 16:51:51 +01:00
LOGGER . error ( " An error occured, while printing all entries " , e ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-03-05 22:37:32 +01:00
/ * *
2018-03-09 20:12:03 +01:00
* update the database entry for the given film , favorite = 0
2018-03-05 22:37:32 +01:00
* @param streamUrl URL of the film
* /
public void dislike ( String streamUrl ) {
LOGGER . info ( " dislike " + streamUrl ) ;
2018-03-01 15:42:47 +01:00
try {
2018-03-05 22:37:32 +01:00
Statement stmt = connection . createStatement ( ) ;
2018-03-09 20:12:03 +01:00
stmt . executeUpdate ( " UPDATE films SET favorite=0 WHERE streamUrl= \" " + streamUrl + " \" ; " ) ;
2018-03-05 22:37:32 +01:00
connection . commit ( ) ;
stmt . close ( ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-03-05 22:37:32 +01:00
/ * *
2018-03-09 20:12:03 +01:00
* update the database entry for the given film , favorite = 1
2018-03-05 22:37:32 +01:00
* @param streamUrl URL of the film
* /
public void like ( String streamUrl ) {
LOGGER . info ( " like " + streamUrl ) ;
2018-03-01 15:42:47 +01:00
try {
2018-03-05 22:37:32 +01:00
Statement stmt = connection . createStatement ( ) ;
2018-03-09 20:12:03 +01:00
stmt . executeUpdate ( " UPDATE films SET favorite=1 WHERE streamUrl= \" " + streamUrl + " \" ; " ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
stmt . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
2018-03-05 22:37:32 +01:00
}
/ * *
* update the database entry for the given film , cached = 1
* @param streamUrl URL of the film
* /
void setCached ( String streamUrl ) {
2018-03-01 15:42:47 +01:00
try {
Statement stmt = connection . createStatement ( ) ;
2018-03-07 00:20:14 +01:00
stmt . executeUpdate ( " UPDATE films SET cached=1 WHERE streamUrl= \" " + streamUrl + " \" ; " ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
stmt . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
2018-03-08 17:59:28 +01:00
2018-03-08 23:01:07 +01:00
refresh ( streamUrl , mainWindowController . getIndexList ( ) ) ;
2018-03-01 15:42:47 +01:00
}
2018-03-05 22:37:32 +01:00
/ * *
* add the received data to the cache table
* @param streamUrl URL of the film
* @param Title
* @param Year
* @param Rated
* @param Released
* @param Runtime
* @param Genre
* @param Director
* @param Writer
* @param Actors
* @param Plot
* @param Language
* @param Country
* @param Awards
* @param Metascore
* @param imdbRating
* @param Type
* @param imdbVotes
* @param imdbID
* @param Poster
* @param Response
* @throws SQLException
* /
2018-03-01 15:42:47 +01:00
void addCache ( String streamUrl , String Title , String Year , String Rated , String Released , String Runtime , String Genre , String Director ,
String Writer , String Actors , String Plot , String Language , String Country , String Awards , String Metascore , String imdbRating ,
2018-03-08 17:59:28 +01:00
String Type , String imdbVotes , String imdbID , String Poster , String Response ) {
try {
PreparedStatement ps = connection . prepareStatement ( " insert into cache values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " ) ;
LOGGER . info ( " adding to cache: " + Title ) ;
ps . setString ( 1 , streamUrl ) ;
ps . setString ( 2 , Title ) ;
ps . setString ( 3 , Year ) ;
ps . setString ( 4 , Rated ) ;
ps . setString ( 5 , Released ) ;
ps . setString ( 6 , Runtime ) ;
ps . setString ( 7 , Genre ) ;
ps . setString ( 8 , Director ) ;
ps . setString ( 9 , Writer ) ;
ps . setString ( 10 , Actors ) ;
ps . setString ( 11 , Plot ) ;
ps . setString ( 12 , Language ) ;
ps . setString ( 13 , Country ) ;
ps . setString ( 14 , Awards ) ;
ps . setString ( 15 , Metascore ) ;
ps . setString ( 16 , imdbRating ) ;
ps . setString ( 17 , imdbVotes ) ;
ps . setString ( 18 , imdbID ) ;
ps . setString ( 19 , Type ) ;
ps . setString ( 20 , Poster ) ;
ps . setString ( 21 , Response ) ;
ps . addBatch ( ) ;
ps . executeBatch ( ) ;
connection . commit ( ) ;
ps . close ( ) ;
LOGGER . info ( " done! " ) ;
} catch ( Exception e ) {
LOGGER . error ( e ) ;
}
2018-03-01 15:42:47 +01:00
}
2018-03-05 22:37:32 +01:00
/ * *
* sets the cached data to mwc ' s TextFlow
* @param streamUrl URL of the film
* /
2018-03-01 15:42:47 +01:00
public void readCache ( String streamUrl ) {
try {
Statement stmt = connection . createStatement ( ) ;
ResultSet rs = stmt . executeQuery ( " SELECT * FROM cache WHERE streamUrl= \" " + streamUrl + " \" ; " ) ;
ArrayList < Text > nameText = new ArrayList < Text > ( ) ;
ArrayList < Text > responseText = new ArrayList < Text > ( ) ;
String fontFamily = main . getFONT_FAMILY ( ) ;
Image im ;
int fontSize = ( int ) Math . round ( mainWindowController . size ) ;
int j = 2 ;
nameText . add ( 0 , new Text ( mainWindowController . getBundle ( ) . getString ( " title " ) + " : " ) ) ;
nameText . add ( 1 , new Text ( mainWindowController . getBundle ( ) . getString ( " year " ) + " : " ) ) ;
nameText . add ( 2 , new Text ( mainWindowController . getBundle ( ) . getString ( " rating " ) + " : " ) ) ;
nameText . add ( 3 , new Text ( mainWindowController . getBundle ( ) . getString ( " publishedOn " ) + " : " ) ) ;
nameText . add ( 4 , new Text ( mainWindowController . getBundle ( ) . getString ( " duration " ) + " : " ) ) ;
nameText . add ( 5 , new Text ( mainWindowController . getBundle ( ) . getString ( " genre " ) + " : " ) ) ;
nameText . add ( 6 , new Text ( mainWindowController . getBundle ( ) . getString ( " director " ) + " : " ) ) ;
nameText . add ( 7 , new Text ( mainWindowController . getBundle ( ) . getString ( " writer " ) + " : " ) ) ;
nameText . add ( 8 , new Text ( mainWindowController . getBundle ( ) . getString ( " actors " ) + " : " ) ) ;
nameText . add ( 9 , new Text ( mainWindowController . getBundle ( ) . getString ( " plot " ) + " : " ) ) ;
nameText . add ( 10 , new Text ( mainWindowController . getBundle ( ) . getString ( " language " ) + " : " ) ) ;
nameText . add ( 11 , new Text ( mainWindowController . getBundle ( ) . getString ( " country " ) + " : " ) ) ;
nameText . add ( 12 , new Text ( mainWindowController . getBundle ( ) . getString ( " awards " ) + " : " ) ) ;
nameText . add ( 13 , new Text ( mainWindowController . getBundle ( ) . getString ( " metascore " ) + " : " ) ) ;
nameText . add ( 14 , new Text ( mainWindowController . getBundle ( ) . getString ( " imdbRating " ) + " : " ) ) ;
nameText . add ( 15 , new Text ( mainWindowController . getBundle ( ) . getString ( " type " ) + " : " ) ) ;
2018-03-05 22:37:32 +01:00
2018-03-01 15:42:47 +01:00
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 < nameText . size ( ) ; i + + ) {
nameText . get ( i ) . setFont ( Font . font ( fontFamily , FontWeight . BOLD , fontSize ) ) ;
responseText . get ( i ) . setFont ( Font . font ( fontFamily , fontSize ) ) ;
}
mainWindowController . getTextFlow ( ) . getChildren ( ) . remove ( 0 ,
mainWindowController . getTextFlow ( ) . getChildren ( ) . size ( ) ) ;
for ( int i = 0 ; i < nameText . size ( ) ; i + + ) {
mainWindowController . getTextFlow ( ) . getChildren ( ) . addAll ( nameText . get ( i ) , responseText . get ( i ) ) ;
}
try {
2018-03-07 23:51:02 +01:00
mainWindowController . getPosterImageView ( ) . setImage ( im ) ;
2018-03-01 15:42:47 +01:00
} catch ( Exception e ) {
2018-03-07 23:51:02 +01:00
mainWindowController . getPosterImageView ( ) . setImage ( new Image ( " resources/icons/close_black_2048x2048.png " ) ) ;
2018-03-01 15:42:47 +01:00
LOGGER . error ( e ) ;
}
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-04-02 18:29:59 +02:00
/ * *
* return the currentTime in ms saved in the database
* @param streamUrl URL of the film
* @return { @link Double } currentTime in ms
* /
2018-04-02 02:18:43 +02:00
public double getCurrentTime ( String streamUrl ) {
LOGGER . info ( " currentTime: " + streamUrl ) ;
double currentTime = 0 ;
try {
Statement stmt = connection . createStatement ( ) ;
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE streamUrl = \" " + streamUrl + " \" ; " ) ;
currentTime = rs . getDouble ( " currentTime " ) ;
rs . close ( ) ;
stmt . close ( ) ;
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while refreshing mwc! " , e ) ;
}
return currentTime ;
}
2018-04-02 18:29:59 +02:00
/ * *
* save the currentTime to the database
* @param streamUrl URL of the film
* @param currentTime currentTime in ms of the film
* /
2018-04-02 02:18:43 +02:00
public void setCurrentTime ( String streamUrl , double currentTime ) {
LOGGER . info ( " currentTime: " + streamUrl ) ;
try {
Statement stmt = connection . createStatement ( ) ;
stmt . executeUpdate ( " UPDATE films SET currentTime= " + currentTime + " WHERE streamUrl= \" " + streamUrl + " \" ; " ) ;
connection . commit ( ) ;
stmt . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-04-02 18:29:59 +02:00
/ * *
* get the next episode of a
* @param title URL of the film
* @param nextEp number of the next episode
* @return { @link String } the streamUrl of the next episode
* /
public String getNextEpisode ( String title , int nextEp ) {
2018-04-02 02:18:43 +02:00
String nextStreamUrl = " " ;
try {
Statement stmt = connection . createStatement ( ) ;
ResultSet rs = stmt . executeQuery (
2018-04-02 18:29:59 +02:00
" SELECT * FROM films WHERE title = \" " + title + " \" AND episode = " + nextEp + " ; " ) ;
2018-04-02 02:18:43 +02:00
nextStreamUrl = rs . getString ( " streamUrl " ) ;
rs . close ( ) ;
stmt . close ( ) ;
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while refreshing mwc! " , e ) ;
}
return nextStreamUrl ;
}
2018-03-01 15:42:47 +01:00
// removes the ending
private String cutOffEnd ( String str ) {
if ( str = = null ) return null ;
int pos = str . lastIndexOf ( " . " ) ;
if ( pos = = - 1 ) return str ;
return str . substring ( 0 , pos ) ;
}
}