removed all mwc usage in DBController

This commit is contained in:
Jannik 2019-01-14 18:44:36 +01:00
parent 291f183f5e
commit cc05600e0a
4 changed files with 167 additions and 124 deletions

View File

@ -85,8 +85,6 @@ public class Main extends Application {
primaryStage.setScene(scene); // append scene to stage
primaryStage.show(); // show stage
mainWindowController = loader.getController(); //Link of FXMLController and controller class
mainWindowController.init();

View File

@ -32,6 +32,7 @@ import java.io.InputStreamReader;
import java.io.Writer;
import java.math.BigInteger;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
@ -53,6 +54,7 @@ import com.jfoenix.controls.JFXToggleButton;
import com.jfoenix.transitions.hamburger.HamburgerBackArrowBasicTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
@ -60,6 +62,7 @@ import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
@ -79,6 +82,8 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
@ -195,7 +200,7 @@ public class MainWindowController {
@FXML
public void initialize() {
xmlController = new XMLController();
dbController = new DBController(this);
dbController = DBController.getInstance();
}
public void init() {
@ -210,6 +215,7 @@ public class MainWindowController {
initUI();
initActions();
dbController.init();
refreshAllFilms();
// load sources list in gui
addSourceToTable();
@ -318,7 +324,7 @@ public class MainWindowController {
public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
XMLController.setFontSize(fontsizeSlider.getValue());
if (!getCurrentTitle().isEmpty()) {
dbController.readCache(getCurrentStreamUrl());
setSelectedFilmInfo(dbController.readCache(getCurrentStreamUrl()));
}
xmlController.saveSettings();
}
@ -355,7 +361,7 @@ public class MainWindowController {
@Override
public void handle(ActionEvent event) {
dbController.like(getCurrentStreamUrl());
dbController.refresh(getCurrentStreamUrl(), indexList);
filmsList.set(indexList, dbController.getDatabaseFilm(getCurrentStreamUrl()));
refreshTableElement();
}
});
@ -364,7 +370,7 @@ public class MainWindowController {
@Override
public void handle(ActionEvent event) {
dbController.dislike(getCurrentStreamUrl());
dbController.refresh(getCurrentStreamUrl(), indexList);
filmsList.set(indexList, dbController.getDatabaseFilm(getCurrentStreamUrl()));
refreshTableElement();
}
});
@ -423,11 +429,28 @@ public class MainWindowController {
if (currentTableFilm.getCached() || dbController.searchCacheByURL(getCurrentStreamUrl())) {
LOGGER.info("loading from cache: " + getCurrentTitle());
dbController.readCache(getCurrentStreamUrl());
setSelectedFilmInfo(dbController.readCache(getCurrentStreamUrl()));
} else {
Thread omdbAPIThread = new Thread(new OMDbAPIController(dbController, currentTableFilm, XMLController.getOmdbAPIKey(), true));
omdbAPIThread.setName("OMDbAPI");
omdbAPIThread.start();
// this is not perfect!
new Thread(new Runnable() {
public void run() {
Thread omdbAPIThread = new Thread(new OMDbAPIController(dbController, currentTableFilm, XMLController.getOmdbAPIKey()));
omdbAPIThread.setName("OMDbAPI");
omdbAPIThread.start();
synchronized (omdbAPIThread) {
try {
omdbAPIThread.wait();
} catch (InterruptedException e) {
LOGGER.error(e);
}
// update the GUI for the selected film
Platform.runLater(() -> {
setSelectedFilmInfo(dbController.readCache(getCurrentStreamUrl()));
});
}
}
}).start();
}
}
});
@ -585,6 +608,18 @@ public class MainWindowController {
filmRoot.getChildren().get(indexTable).setValue(filmsList.get(indexList));
}
/**
* refresh all films in filmsList and in filmsTable
* clear the FilmsList and FilmRoot children, then update the database
*/
private void refreshAllFilms() {
getFilmsList().clear();
getFilmRoot().getChildren().clear();
dbController.refreshDataBase(); // refreshes the database after a source path was added
filmsList = dbController.getDatabaseFilmsList(); // returns a list of all films stored in the database
addFilmsToTable(filmsList);
}
/**
* add data from films-list to films-table
*/
@ -657,8 +692,9 @@ public class MainWindowController {
getSourcesList().clear();
getSourceRoot().getChildren().clear();
// update the database and all films from the database
dbController.refreshDataBase();
// clear the FilmsList and FilmRoot children, then update the database
refreshAllFilms();
checkAllPosters(); // check if there is anything to cache
}
@ -732,7 +768,7 @@ public class MainWindowController {
/**
* set the local based on the languageChoisBox selection
*/
void setLocalUI() {
private void setLocalUI() {
switch (XMLController.getUsrLocal()) {
case "en_US":
XMLController.setLocalBundle(ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.US)); // us_English
@ -768,6 +804,62 @@ public class MainWindowController {
columnEpisode.setText(XMLController.getLocalBundle().getString("columnEpisode"));
}
private void setSelectedFilmInfo(String[] cacheData) {
Font font = Font.font("System", FontWeight.BOLD, (int) Math.round(XMLController.getFontSize()));
ObservableList<Node> textFlow = getTextFlow().getChildren();
// TODO this should move! ***
ArrayList<Text> nameText = new ArrayList<Text>();
nameText.add(new Text(XMLController.getLocalBundle().getString("title") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("year") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("rated") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("released") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("season") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("episode") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("runtime") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("genre") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("director") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("writer") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("actors") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("plot") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("language") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("country") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("awards") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("metascore") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("imdbRating") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("type") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("boxOffice") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("website") + ": "));
// ***
// set the correct font for the nameText
for (Text text : nameText) {
text.setFont(font);
}
// clear the textFlow and add the new text
textFlow.clear();
// TODO rework
for(int i = 0; i < 20; i++) {
if (cacheData[i] != null && (i == 5 || i == 6) && cacheData[5].length() == 0) {
// do nothing
} else if(cacheData[i] != null){
textFlow.addAll(nameText.get(i), new Text(cacheData[i] + "\n"));
}
}
getTextFlow().setStyle("-fx-font-size : " + ((int) Math.round(XMLController.getFontSize()) + 1) + "px;");
// add the image
try {
getPosterImageView().setImage(new Image(new File(cacheData[20]).toURI().toString()));
} catch (Exception e) {
getPosterImageView().setImage(new Image("icons/close_black_2048x2048.png"));
LOGGER.error("No Poster found, useing default.");
}
}
// if AutoUpdate, then check for updates
private void checkAutoUpdate() {
@ -810,12 +902,15 @@ public class MainWindowController {
for (FilmTabelDataType entry : dbController.getAllNotCachedEntries()) {
System.out.println(entry.getStreamUrl() + " is NOT cached!");
Runnable OMDbAPIWorker = new OMDbAPIController(dbController, entry, XMLController.getOmdbAPIKey(), false);
Runnable OMDbAPIWorker = new OMDbAPIController(dbController, entry, XMLController.getOmdbAPIKey());
executor.execute(OMDbAPIWorker);
}
executor.shutdown();
// TODO show loading screen
// update all elements from the database
refreshAllFilms();
}
// getter and setter

View File

@ -21,7 +21,6 @@
package kellerkinder.HomeFlix.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLConnection;
@ -37,21 +36,17 @@ import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
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.MainWindowController;
import kellerkinder.HomeFlix.datatypes.DatabaseDataType;
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType;
import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType;
public class DBController {
private MainWindowController mainWindowController;
private static DBController instance = null;
private String DB_PATH;
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");
@ -65,8 +60,16 @@ public class DBController {
* @param main the Main object
* @param mainWindowController the MainWindowController object
*/
public DBController(MainWindowController mainWindowController) {
this.mainWindowController = mainWindowController;
public DBController() {
// Auto-generated constructor stub
}
public static DBController getInstance() {
if (instance == null) {
instance = new DBController();
}
return instance;
}
/**
@ -148,11 +151,12 @@ public class DBController {
}
/**
* load the data to the mainWindowController
* load the data from the database to a ObservableList
* order entries by title
* @return a ObservableList that contains all films from the database
*/
private void loadDataToFilmsList() {
ImageView imageView;
public ObservableList<FilmTabelDataType> getDatabaseFilmsList() {
ObservableList<FilmTabelDataType> filmsList = FXCollections.observableArrayList();
LOGGER.info("loading data to mwc ...");
try {
//load local Data
@ -160,8 +164,8 @@ public class DBController {
ResultSet rs = stmt.executeQuery("SELECT * FROM films ORDER BY title");
while (rs.next()) {
imageView = rs.getBoolean("favorite") ? new ImageView(favorite_black) : new ImageView(favorite_border_black);
mainWindowController.getFilmsList().add(new FilmTabelDataType(rs.getString("streamUrl"),
ImageView imageView = rs.getBoolean("favorite") ? new ImageView(favorite_black) : new ImageView(favorite_border_black);
filmsList.add(new FilmTabelDataType(rs.getString("streamUrl"),
rs.getString("title"), rs.getString("season"), rs.getString("episode") ,rs.getBoolean("favorite"),
rs.getBoolean("cached"), imageView));
}
@ -172,15 +176,17 @@ public class DBController {
}
LOGGER.info("loading data to the GUI ...");
mainWindowController.addFilmsToTable(mainWindowController.getFilmsList());
return filmsList;
}
/**
* refresh data in mainWindowController for one element
* get one film from the database with streamUrl = ?
* @param streamUrl of the film
* @param index of the film in LocalFilms list
* @return a FilmTabelDataType Object of the film with the given streamUrl
*/
public void refresh(String streamUrl, int indexList) {
public FilmTabelDataType getDatabaseFilm(String streamUrl) {
FilmTabelDataType film = null;
LOGGER.info("refresh data for " + streamUrl);
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM films WHERE streamUrl = ?");
@ -189,21 +195,21 @@ public class DBController {
while (rs.next()) {
ImageView imageView = rs.getBoolean("favorite") ? new ImageView(favorite_black) : new ImageView(favorite_border_black);
mainWindowController.getFilmsList().set(indexList, new FilmTabelDataType(rs.getString("streamUrl"),
film = new FilmTabelDataType(rs.getString("streamUrl"),
rs.getString("title"), rs.getString("season"), rs.getString("episode"), rs.getBoolean("favorite"),
rs.getBoolean("cached"), imageView));
rs.getBoolean("cached"), imageView);
}
rs.close();
ps.close();
} catch (Exception e) {
LOGGER.error("Ups! error while refreshing mwc!", e);
}
}
return film;
}
/**
* 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 ...");
@ -223,12 +229,6 @@ public class DBController {
} catch (Exception e) {
LOGGER.error("Error while refreshing the database", e);
}
// clear the FilmsList and FilmRoot children
mainWindowController.getFilmsList().clear();
mainWindowController.getFilmRoot().getChildren().clear();
loadDataToFilmsList(); // load the new data to the FilmsList
}
/**
@ -373,8 +373,6 @@ public class DBController {
} catch (SQLException e) {
LOGGER.error("Ups! an error occured!", e);
}
refresh(streamUrl, mainWindowController.getIndexList());
}
/**
@ -445,79 +443,40 @@ public class DBController {
}
/**
* sets the cached data to mwc's TextFlow
* read the cached data from the Database
* @param streamUrl URL of the film
* @return a String array (length = 21) with all cached data
*/
public void readCache(String streamUrl) {
public String[] readCache(String streamUrl) {
String[] cacheData = new String[21];
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM cache WHERE streamUrl = ?");
ps.setString(1, streamUrl);
ResultSet rs = ps.executeQuery();
Font font = Font.font("System", FontWeight.BOLD, (int) Math.round(XMLController.getFontSize()));
ObservableList<Node> textFlow = mainWindowController.getTextFlow().getChildren();
ArrayList<Text> nameText = new ArrayList<Text>();
nameText.add(new Text(XMLController.getLocalBundle().getString("title") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("year") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("rated") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("released") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("season") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("episode") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("runtime") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("genre") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("director") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("writer") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("actors") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("plot") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("language") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("country") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("awards") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("metascore") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("imdbRating") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("type") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("boxOffice") + ": "));
nameText.add(new Text(XMLController.getLocalBundle().getString("website") + ": "));
// set the correct font for the nameText
for (Text text : nameText) {
text.setFont(font);
}
// 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"));
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"));
}
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(XMLController.getFontSize()) + 1) + "px;");
// add the image
try {
mainWindowController.getPosterImageView().setImage(new Image(new File(rs.getString("Poster")).toURI().toString()));
} catch (Exception e) {
mainWindowController.getPosterImageView().setImage(new Image("icons/close_black_2048x2048.png"));
LOGGER.error("No Poster found, useing default.");
while (rs.next()) {
cacheData[0] = rs.getString("Title");
cacheData[1] = rs.getString("Year");
cacheData[2] = rs.getString("Rated");
cacheData[3] = rs.getString("Released");
cacheData[4] = rs.getString("Season");
cacheData[5] = rs.getString("Episode");
cacheData[6] = rs.getString("Runtime");
cacheData[7] = rs.getString("Genre");
cacheData[8] = rs.getString("Director");
cacheData[9] = rs.getString("Writer");
cacheData[10] = rs.getString("Actors");
cacheData[11] = rs.getString("Plot");
cacheData[12] = rs.getString("Language");
cacheData[13] = rs.getString("Country");
cacheData[14] = rs.getString("Awards");
cacheData[15] = rs.getString("metascore");
cacheData[16] = rs.getString("imdbRating");
cacheData[17] = rs.getString("Type");
cacheData[18] = rs.getString("BoxOffice");
cacheData[19] = rs.getString("Website");
cacheData[20] = rs.getString("Poster");
}
rs.close();
@ -525,6 +484,8 @@ public class DBController {
} catch (SQLException e) {
LOGGER.error("Ups! an error occured!", e);
}
return cacheData;
}
/**

View File

@ -37,8 +37,6 @@ import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import javafx.application.Platform;
import kellerkinder.HomeFlix.application.MainWindowController;
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType;
import kellerkinder.HomeFlix.datatypes.OMDbAPIResponseDataType;
@ -49,8 +47,7 @@ public class OMDbAPIController implements Runnable {
private String omdbAPIKey;
private String URL = "https://www.omdbapi.com/?apikey=";
private boolean useEpisode = true;
private boolean refresh;
private static final Logger LOGGER = LogManager.getLogger(MainWindowController.class.getName());
private static final Logger LOGGER = LogManager.getLogger(OMDbAPIController.class.getName());
/**
* constructor for the OMDbAPIController
@ -59,11 +56,10 @@ public class OMDbAPIController implements Runnable {
* @param currentTableFilm the current film object
* @param omdbAPIKey the omdbAPI key
*/
public OMDbAPIController(DBController dbController, FilmTabelDataType currentTableFilm, String omdbAPIKey, boolean refresh) {
public OMDbAPIController(DBController dbController, FilmTabelDataType currentTableFilm, String omdbAPIKey) {
this.dbController = dbController;
this.currentTableFilm = currentTableFilm;
this.omdbAPIKey = omdbAPIKey;
this.refresh = refresh;
}
@Override
@ -136,13 +132,6 @@ public class OMDbAPIController implements Runnable {
// adding to cache
dbController.addCache(currentTableFilm.getStreamUrl(), omdbResponse);
dbController.setCached(currentTableFilm.getStreamUrl());
// load data to the MainWindowController
if (refresh) {
Platform.runLater(() -> {
dbController.readCache(currentTableFilm.getStreamUrl());
});
}
}
return;