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 .
* /
2018-12-04 22:31:11 +01:00
2018-03-01 15:42:47 +01:00
package kellerkinder.HomeFlix.controller ;
import java.io.File ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
2018-04-05 12:09:39 +02:00
import java.net.URLConnection ;
2018-03-01 15:42:47 +01:00
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 ;
2018-04-29 14:21:19 +02:00
import javafx.collections.ObservableList ;
import javafx.scene.Node ;
2018-03-01 15:42:47 +01:00
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-12-04 22:31:11 +01:00
import kellerkinder.HomeFlix.datatypes.DatabaseDataType ;
2018-03-05 22:37:32 +01:00
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType ;
2018-04-29 14:21:19 +02:00
import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType ;
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 ;
2018-05-16 23:02:07 +02:00
private String DB_PATH ;
2018-03-01 15:42:47 +01:00
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 " ) ;
2018-12-04 22:31:11 +01:00
private List < DatabaseDataType > databaseStream = new ArrayList < DatabaseDataType > ( ) ; // contains all films stored in the database
private List < DatabaseDataType > sourceStreams = new ArrayList < DatabaseDataType > ( ) ; // contains all films from the sources
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
2018-04-19 12:13:18 +02:00
* @param main the Main object
* @param mainWindowController the MainWindowController object
2018-04-02 18:29:59 +02:00
* /
2018-05-16 23:02:07 +02:00
public DBController ( Main main , MainWindowController mainWindowController ) {
2018-04-02 18:29:59 +02:00
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
* /
2018-04-29 14:21:19 +02:00
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 ( "
2018-04-29 14:21:19 +02:00
+ " streamUrl, Title, Year, Rated, Released, Season, Episode ,Runtime, Genre, Director, Writer, "
+ " Actors, Plot, Language, Country, Awards, Poster, Metascore, imdbRating, imdbVotes, "
+ " imdbID, Type, dvd, BoxOffice, Website, 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-12-04 22:31:11 +01:00
databaseStream . add ( new DatabaseDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) ,
rs . getInt ( " favorite " ) , rs . getBoolean ( " cached " ) , rs . getDouble ( " currentTime " ) ) ) ;
2018-03-01 15:42:47 +01:00
}
stmt . close ( ) ;
rs . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
2018-03-03 16:51:14 +01:00
}
2018-04-02 18:29:59 +02:00
/ * *
2018-12-04 22:31:11 +01:00
* load all sources
2018-04-02 18:29:59 +02:00
* /
2018-03-04 20:07:43 +01:00
private void loadSources ( ) {
2018-12-04 22:31:11 +01:00
SourcesController sourcesController = new SourcesController ( main , mainWindowController ) ;
sourceStreams = sourcesController . loadSources ( ) ;
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-05-16 23:02:07 +02:00
private void loadDataToFilmsList ( ) {
2018-12-04 22:31:11 +01:00
ImageView imageView ;
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-12-04 22:31:11 +01:00
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films ORDER BY title " ) ;
2018-03-01 15:42:47 +01:00
while ( rs . next ( ) ) {
2018-12-04 22:31:11 +01:00
imageView = rs . getBoolean ( " favorite " ) ? new ImageView ( favorite_black ) : new ImageView ( favorite_border_black ) ;
mainWindowController . getFilmsList ( ) . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
rs . getBoolean ( " cached " ) , imageView ) ) ;
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 ... " ) ;
2018-05-09 22:55:32 +02:00
mainWindowController . addFilmsToTable ( mainWindowController . getFilmsList ( ) ) ;
2018-03-01 15:42:47 +01:00
}
2018-03-05 22:37:32 +01:00
/ * *
2018-04-03 18:23:54 +02:00
* refresh data in mainWindowController for one element
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-12-04 22:31:11 +01:00
LOGGER . info ( " refresh data for " + streamUrl ) ;
2018-03-01 15:42:47 +01:00
try {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-03-05 22:37:32 +01:00
2018-04-14 16:14:10 +02:00
while ( rs . next ( ) ) {
2018-12-04 22:31:11 +01:00
ImageView imageView = rs . getBoolean ( " favorite " ) ? new ImageView ( favorite_black ) : new ImageView ( favorite_border_black ) ;
mainWindowController . getFilmsList ( ) . set ( indexList , new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
rs . getBoolean ( " cached " ) , imageView ) ) ;
2018-03-05 22:37:32 +01:00
}
2018-03-07 00:20:14 +01:00
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-03-05 22:37:32 +01:00
} 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
2018-12-04 22:31:11 +01:00
databaseStream . clear ( ) ;
sourceStreams . clear ( ) ;
2018-03-04 20:07:43 +01:00
loadSources ( ) ; // reload all sources
loadDatabase ( ) ; // reload all films saved in the DB
2018-12-04 22:31:11 +01:00
LOGGER . info ( " filme in db: " + databaseStream . size ( ) ) ;
2018-05-16 23:02:07 +02:00
2018-03-04 20:07:43 +01:00
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 ) ;
}
2018-12-04 22:31:11 +01:00
// clear the FilmsList and FilmRoot children
2018-05-16 23:02:07 +02:00
mainWindowController . getFilmsList ( ) . clear ( ) ;
mainWindowController . getFilmRoot ( ) . getChildren ( ) . clear ( ) ;
2018-03-04 20:07:43 +01:00
2018-05-16 23:02:07 +02:00
loadDataToFilmsList ( ) ; // load the new data to the FilmsList
2018-03-04 20:07:43 +01:00
}
2018-03-01 15:42:47 +01:00
/ * *
* check if there are any entries that have been removed from the film - directory
2018-12-04 22:31:11 +01:00
* @throws SQLException
2018-03-01 15:42:47 +01:00
* /
2018-12-04 22:31:11 +01:00
private void checkRemoveEntry ( ) throws SQLException {
PreparedStatement ps = connection . prepareStatement ( " DELETE FROM films WHERE streamUrl = ? " ) ;
2018-03-01 15:42:47 +01:00
LOGGER . info ( " checking for entrys to remove to DB ... " ) ;
2018-12-04 22:31:11 +01:00
for ( DatabaseDataType dbStreamEntry : databaseStream ) {
// if the directory doen't contain the entry form the database, remove it
2018-03-04 20:07:43 +01:00
2018-12-04 22:31:11 +01:00
// if sourceStreams has a item where StreamUrl equals dbStreamEntry.getStreamUrl() return it, else null
DatabaseDataType result = sourceStreams . stream ( )
. filter ( x - > dbStreamEntry . getStreamUrl ( ) . equals ( x . getStreamUrl ( ) ) )
. findAny ( )
. orElse ( null ) ;
// if the result is null, the file is missing, remove it from the database
if ( result = = null ) {
ps . setString ( 1 , dbStreamEntry . getStreamUrl ( ) ) ;
ps . addBatch ( ) ;
LOGGER . info ( " removed \" " + dbStreamEntry . getTitle ( ) + " \" from database " ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-12-04 22:31:11 +01:00
ps . executeBatch ( ) ;
connection . commit ( ) ;
ps . close ( ) ;
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 {
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 ... " ) ;
2018-12-04 22:31:11 +01:00
// new
for ( DatabaseDataType sourceStreamEntry : sourceStreams ) {
// if databaseStream has a item where StreamUrl equals sourceStreamEntry.getStreamUrl() return it, else null
DatabaseDataType result = databaseStream . stream ( )
. filter ( x - > sourceStreamEntry . getStreamUrl ( ) . equals ( x . getStreamUrl ( ) ) )
. findAny ( )
. orElse ( null ) ;
// if the result is null, the entry is missing, add it to the database
if ( result = = null ) {
ps . setString ( 1 , sourceStreamEntry . getStreamUrl ( ) ) ;
ps . setString ( 2 , sourceStreamEntry . getTitle ( ) ) ;
ps . setString ( 3 , sourceStreamEntry . getSeason ( ) ) ;
ps . setString ( 4 , sourceStreamEntry . getEpisode ( ) ) ;
ps . setInt ( 5 , sourceStreamEntry . getFavorite ( ) ) ;
ps . setBoolean ( 6 , sourceStreamEntry . getCached ( ) ) ;
ps . setDouble ( 7 , sourceStreamEntry . getCurrentTime ( ) ) ;
ps . addBatch ( ) ; // adds the entry
LOGGER . info ( " Added \" " + sourceStreamEntry . getTitle ( ) + " \" to database " ) ;
databaseStream . add ( sourceStreamEntry ) ;
2018-03-01 15:42:47 +01:00
}
}
2018-12-04 22:31:11 +01:00
ps . executeBatch ( ) ;
connection . commit ( ) ;
ps . close ( ) ;
2018-11-19 22:49:09 +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
}
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
stmt . close ( ) ;
2018-03-01 15:42:47 +01:00
} 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-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET favorite = 0 WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-03-05 22:37:32 +01:00
connection . commit ( ) ;
2018-12-04 22:31:11 +01:00
ps . 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-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET favorite = 1 WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
2018-12-04 22:31:11 +01:00
ps . 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
}
/ * *
* update the database entry for the given film , cached = 1
* @param streamUrl URL of the film
* /
2018-05-16 23:02:07 +02:00
public void setCached ( String streamUrl ) {
2018-03-01 15:42:47 +01:00
try {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET cached = 1 WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-03-01 15:42:47 +01:00
connection . commit ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-03-01 15:42:47 +01:00
} 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
2018-04-29 14:21:19 +02:00
* @param streamUrl URL of the film
* @param omdbResponse the response data from omdbAPI
2018-03-05 22:37:32 +01:00
* /
2018-05-16 23:02:07 +02:00
public void addCache ( String streamUrl , OMDbAPIResponseDataType omdbResponse ) {
2018-03-08 17:59:28 +01:00
try {
2018-04-29 14:21:19 +02:00
PreparedStatement ps = connection . prepareStatement ( " insert into cache values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " ) ;
2018-03-08 17:59:28 +01:00
2018-12-20 13:47:32 +01:00
LOGGER . info ( " adding cache for: " + streamUrl ) ;
2018-03-08 17:59:28 +01:00
ps . setString ( 1 , streamUrl ) ;
2018-04-29 14:21:19 +02:00
ps . setString ( 2 , omdbResponse . getTitle ( ) ) ;
ps . setString ( 3 , omdbResponse . getYear ( ) ) ;
ps . setString ( 4 , omdbResponse . getRated ( ) ) ;
ps . setString ( 5 , omdbResponse . getReleased ( ) ) ;
ps . setString ( 6 , omdbResponse . getSeason ( ) ) ;
ps . setString ( 7 , omdbResponse . getEpisode ( ) ) ;
ps . setString ( 8 , omdbResponse . getRuntime ( ) ) ;
ps . setString ( 9 , omdbResponse . getGenre ( ) ) ;
ps . setString ( 10 , omdbResponse . getDirector ( ) ) ;
ps . setString ( 11 , omdbResponse . getWriter ( ) ) ;
ps . setString ( 12 , omdbResponse . getActors ( ) ) ;
ps . setString ( 13 , omdbResponse . getPlot ( ) ) ;
ps . setString ( 14 , omdbResponse . getLanguage ( ) ) ;
ps . setString ( 15 , omdbResponse . getCountry ( ) ) ;
ps . setString ( 16 , omdbResponse . getAwards ( ) ) ;
ps . setString ( 17 , omdbResponse . getPoster ( ) ) ;
ps . setString ( 18 , omdbResponse . getMetascore ( ) ) ;
ps . setString ( 19 , omdbResponse . getImdbRating ( ) ) ;
ps . setString ( 20 , omdbResponse . getImdbVotes ( ) ) ;
ps . setString ( 21 , omdbResponse . getImdbID ( ) ) ;
ps . setString ( 22 , omdbResponse . getType ( ) ) ;
ps . setString ( 23 , omdbResponse . getDvd ( ) ) ;
ps . setString ( 24 , omdbResponse . getBoxOffice ( ) ) ;
ps . setString ( 25 , omdbResponse . getWebsite ( ) ) ;
ps . setString ( 26 , omdbResponse . getResponse ( ) ) ;
2018-03-08 17:59:28 +01:00
ps . addBatch ( ) ;
ps . executeBatch ( ) ;
connection . commit ( ) ;
ps . close ( ) ;
} catch ( Exception e ) {
LOGGER . error ( e ) ;
}
2018-03-01 15:42:47 +01:00
}
2018-04-15 21:30:26 +02:00
/ * *
* checks if there is already a entry with the given streamUrl in the cache
* @param streamUrl URL of the element
* @return true if the element is already cached , else false
* /
2018-04-29 14:21:19 +02:00
public boolean searchCacheByURL ( String streamUrl ) {
2018-04-14 16:14:10 +02:00
boolean retValue = false ;
try {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM cache WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-14 16:14:10 +02:00
retValue = rs . next ( ) ;
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-04-14 16:14:10 +02:00
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while getting the current time! " , e ) ;
}
return retValue ;
}
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 {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM cache WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-29 14:21:19 +02:00
Font font = Font . font ( " System " , FontWeight . BOLD , ( int ) Math . round ( mainWindowController . getFontSize ( ) ) ) ;
ObservableList < Node > textFlow = mainWindowController . getTextFlow ( ) . getChildren ( ) ;
2018-03-01 15:42:47 +01:00
ArrayList < Text > nameText = new ArrayList < Text > ( ) ;
2018-03-05 22:37:32 +01:00
2018-04-29 14:21:19 +02:00
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " title " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " year " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " rated " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " released " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " season " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " episode " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " runtime " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " genre " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " director " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " writer " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " actors " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " plot " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " language " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " country " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " awards " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " metascore " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " imdbRating " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " type " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " boxOffice " ) + " : " ) ) ;
nameText . add ( new Text ( mainWindowController . getBundle ( ) . getString ( " website " ) + " : " ) ) ;
2018-04-13 11:11:25 +02:00
2018-04-29 14:21:19 +02:00
// set the correct font for the nameText
for ( Text text : nameText ) {
text . setFont ( font ) ;
2018-03-01 15:42:47 +01:00
}
2018-04-29 14:21:19 +02:00
// clear the textFlow and all the new text
textFlow . clear ( ) ;
textFlow . addAll ( nameText . get ( 0 ) , new Text ( rs . getString ( " Title " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 1 ) , new Text ( rs . getString ( " Year " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 2 ) , new Text ( rs . getString ( " Rated " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 3 ) , new Text ( rs . getString ( " Released " ) + " \ n " ) ) ;
2018-05-09 22:40:14 +02:00
if ( rs . getString ( " Episode " ) . length ( ) > 0 ) {
textFlow . addAll ( nameText . get ( 4 ) , new Text ( rs . getString ( " Season " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 5 ) , new Text ( rs . getString ( " Episode " ) + " \ n " ) ) ;
}
2018-04-29 14:21:19 +02:00
textFlow . addAll ( nameText . get ( 6 ) , new Text ( rs . getString ( " Runtime " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 7 ) , new Text ( rs . getString ( " Genre " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 8 ) , new Text ( rs . getString ( " Director " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 9 ) , new Text ( rs . getString ( " Writer " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 10 ) , new Text ( rs . getString ( " Actors " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 11 ) , new Text ( rs . getString ( " Plot " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 12 ) , new Text ( rs . getString ( " Language " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 13 ) , new Text ( rs . getString ( " Country " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 14 ) , new Text ( rs . getString ( " Awards " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 15 ) , new Text ( rs . getString ( " metascore " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 16 ) , new Text ( rs . getString ( " imdbRating " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 17 ) , new Text ( rs . getString ( " Type " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 18 ) , new Text ( rs . getString ( " BoxOffice " ) + " \ n " ) ) ;
textFlow . addAll ( nameText . get ( 19 ) , new Text ( rs . getString ( " Website " ) + " \ n " ) ) ;
mainWindowController . getTextFlow ( ) . setStyle ( " -fx-font-size : " + ( ( int ) Math . round ( mainWindowController . getFontSize ( ) ) + 1 ) + " px; " ) ;
// add the image
2018-03-01 15:42:47 +01:00
try {
2018-04-29 14:21:19 +02:00
mainWindowController . getPosterImageView ( ) . setImage ( new Image ( new File ( rs . getString ( " Poster " ) ) . toURI ( ) . toString ( ) ) ) ;
2018-03-01 15:42:47 +01:00
} catch ( Exception e ) {
2018-05-04 18:53:51 +02:00
mainWindowController . getPosterImageView ( ) . setImage ( new Image ( " icons/close_black_2048x2048.png " ) ) ;
LOGGER . error ( " No Poster found, useing default. " ) ;
2018-03-01 15:42:47 +01:00
}
2018-04-29 14:21:19 +02:00
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-03-01 15:42:47 +01:00
} catch ( SQLException e ) {
LOGGER . error ( " Ups! an error occured! " , e ) ;
}
}
2018-08-13 23:56:16 +02:00
/ * *
* get all NOT cached entries
* @return a { @link ArrayList } of all NOT cached entries
* /
public ArrayList < FilmTabelDataType > getAllNotCachedEntries ( ) {
ArrayList < FilmTabelDataType > notCachedEntries = new ArrayList < > ( ) ;
try {
Statement stmt = connection . createStatement ( ) ;
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE cached = 0 " ) ;
while ( rs . next ( ) ) {
notCachedEntries . add ( new FilmTabelDataType ( rs . getString ( " streamUrl " ) ,
rs . getString ( " title " ) , rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
rs . getBoolean ( " cached " ) , new ImageView ( favorite_border_black ) ) ) ;
}
stmt . close ( ) ;
rs . close ( ) ;
} catch ( SQLException e ) {
LOGGER . error ( " An error occured, while getting all NOT cached entries " , e ) ;
}
2018-12-20 13:47:32 +01:00
LOGGER . info ( " There are {} entries not Chached! " , notCachedEntries . size ( ) ) ;
2018-08-13 23:56:16 +02:00
return notCachedEntries ;
}
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 ) {
2018-12-04 22:31:11 +01:00
LOGGER . info ( " get currentTime: " + streamUrl ) ;
2018-04-02 02:18:43 +02:00
double currentTime = 0 ;
try {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE streamUrl = ? " ) ;
ps . setString ( 1 , streamUrl ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-02 02:18:43 +02:00
currentTime = rs . getDouble ( " currentTime " ) ;
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-04-02 02:18:43 +02:00
} catch ( Exception e ) {
2018-04-13 11:11:25 +02:00
LOGGER . error ( " Ups! error while getting the current time! " , e ) ;
2018-04-02 02:18:43 +02:00
}
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 ) {
2018-12-04 22:31:11 +01:00
LOGGER . info ( " set currentTime: " + streamUrl ) ;
2018-04-02 02:18:43 +02:00
try {
2018-12-04 22:31:11 +01:00
PreparedStatement ps = connection . prepareStatement ( " UPDATE films SET currentTime = ? WHERE streamUrl = ? " ) ;
ps . setDouble ( 1 , currentTime ) ;
ps . setString ( 2 , streamUrl ) ;
ps . executeUpdate ( ) ;
2018-04-02 02:18:43 +02:00
connection . commit ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-04-02 02:18:43 +02:00
} catch ( SQLException e ) {
2018-12-04 22:31:11 +01:00
LOGGER . error ( " Ups! error while updateing the current time! " , e ) ;
2018-04-02 02:18:43 +02:00
}
}
2018-04-26 15:13:15 +02:00
/ * *
2018-12-04 22:31:11 +01:00
* get the next episode of a series
* @param title title of the film
* @param episode episode currently played
2018-04-03 18:03:43 +02:00
* @return { @link FilmTabelDataType } the next episode as object
2018-04-02 18:29:59 +02:00
* /
2018-04-26 15:13:15 +02:00
public FilmTabelDataType getNextEpisode ( String title , int episode , int season ) {
2018-04-03 18:03:43 +02:00
FilmTabelDataType nextFilm = null ;
2018-04-26 15:13:15 +02:00
2018-04-02 02:18:43 +02:00
try {
2018-12-04 22:31:11 +01:00
// try to get a new episode of the current season
PreparedStatement ps = connection . prepareStatement ( " SELECT * FROM films WHERE title = ? AND season = ? AND episode = ? " ) ;
ps . setString ( 1 , title ) ;
ps . setString ( 2 , Integer . toString ( season ) ) ;
ps . setString ( 3 , Integer . toString ( episode + 1 ) ) ;
ResultSet rs = ps . executeQuery ( ) ;
2018-04-26 15:13:15 +02:00
2018-12-04 22:31:11 +01:00
/ * if that fails get the next season and try to get a episode there ,
* we need to test only for the next season , first episode ,
* any other entry would not exist in the database
* /
if ( ! rs . next ( ) ) {
ps . setString ( 1 , title ) ;
ps . setString ( 2 , Integer . toString ( season + 1 ) ) ;
ps . setString ( 3 , Integer . toString ( 1 ) ) ;
rs = ps . executeQuery ( ) ;
if ( ! rs . next ( ) ) return nextFilm ; // if we haven't found anything return an empty object
2018-04-26 15:13:15 +02:00
}
2018-12-04 22:31:11 +01:00
// at this point we have found the correct episode
nextFilm = new FilmTabelDataType ( rs . getString ( " streamUrl " ) , rs . getString ( " title " ) ,
rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
rs . getBoolean ( " cached " ) , new ImageView ( ) ) ;
2018-04-02 02:18:43 +02:00
rs . close ( ) ;
2018-12-04 22:31:11 +01:00
ps . close ( ) ;
2018-04-02 02:18:43 +02:00
} catch ( Exception e ) {
2018-04-03 18:03:43 +02:00
LOGGER . error ( " Ups! error while getting next episode! " , e ) ;
2018-04-15 21:30:26 +02:00
}
2018-04-03 18:03:43 +02:00
return nextFilm ;
2018-04-02 02:18:43 +02:00
}
2018-04-28 12:53:44 +02:00
/ * *
2018-04-14 16:14:10 +02:00
* get the last watched episode
* @param title the title of the series
* @return the last watched episode as { @link FilmTabelDataType } object
* /
public FilmTabelDataType getLastWatchedEpisode ( String title ) {
2018-04-13 11:11:25 +02:00
LOGGER . info ( " last watched episode of: " + title ) ;
2018-04-14 16:14:10 +02:00
FilmTabelDataType nextFilm = null ;
double lastCurrentTime = 0 ;
2018-04-13 11:11:25 +02:00
try {
Statement stmt = connection . createStatement ( ) ;
ResultSet rs = stmt . executeQuery ( " SELECT * FROM films WHERE title = \" " + title + " \" ; " ) ;
while ( rs . next ( ) ) {
2018-04-28 12:53:44 +02:00
nextFilm = new FilmTabelDataType ( rs . getString ( " streamUrl " ) , rs . getString ( " title " ) ,
rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-05-16 23:02:07 +02:00
rs . getBoolean ( " cached " ) , new ImageView ( ) ) ;
2018-04-13 11:11:25 +02:00
if ( rs . getDouble ( " currentTime " ) > lastCurrentTime ) {
lastCurrentTime = rs . getDouble ( " currentTime " ) ;
2018-04-28 12:53:44 +02:00
nextFilm = new FilmTabelDataType ( rs . getString ( " streamUrl " ) , rs . getString ( " title " ) ,
rs . getString ( " season " ) , rs . getString ( " episode " ) , rs . getBoolean ( " favorite " ) ,
2018-05-16 23:02:07 +02:00
rs . getBoolean ( " cached " ) , new ImageView ( ) ) ;
2018-04-14 16:14:10 +02:00
break ;
2018-04-13 11:11:25 +02:00
}
}
2018-04-14 16:14:10 +02:00
rs . close ( ) ;
stmt . close ( ) ;
2018-04-13 11:11:25 +02:00
} catch ( Exception e ) {
LOGGER . error ( " Ups! error while getting the last watched episode! " , e ) ;
}
2018-04-14 16:14:10 +02:00
return nextFilm ;
2018-04-13 11:11:25 +02:00
}
2018-04-05 12:09:39 +02:00
/ * *
* check if a file is a video
* @param path the path to the file
* @return true if the file is a video , else false
* /
public static boolean isVideoFile ( String path ) {
String mimeType = URLConnection . guessContentTypeFromName ( path ) ;
return mimeType ! = null & & mimeType . startsWith ( " video " ) ;
}
2018-03-01 15:42:47 +01:00
}