From 79f5246cab897f72e7e80c84b2a2818d8fa8cf9a Mon Sep 17 00:00:00 2001 From: Mateusz Kowalczyk Date: Fri, 24 Jan 2020 14:45:52 +0100 Subject: [PATCH] move database to .cache folder and change workspace init --- .../ffm/app/WorkspaceInitializer.java | 23 +++++++++++++++++++ .../ffm/app/WorkspaceService.java | 20 ++++------------ .../ffm/app/cache/CacheService.java | 4 ++++ .../ffm/data/DatabaseService.java | 13 +++++++---- .../data/database/utils/DatabaseCreator.java | 8 +++---- .../view/workspace/MainPageController.java | 3 ++- .../view/workspace/WelcomePageController.java | 4 +++- 7 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceInitializer.java diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceInitializer.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceInitializer.java new file mode 100644 index 0000000..97070b8 --- /dev/null +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceInitializer.java @@ -0,0 +1,23 @@ +package dev.mateuszkowalczyk.ffm.app; + +import dev.mateuszkowalczyk.ffm.data.Chooser; +import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; +import dev.mateuszkowalczyk.ffm.utils.Property; +import dev.mateuszkowalczyk.ffm.view.SceneEnum; +import dev.mateuszkowalczyk.ffm.view.StageController; + +import java.io.IOException; + +public class WorkspaceInitializer { + public void init() { + Chooser chooser = new Chooser(); + String path = chooser.chooseDirectory(); + + PropertiesLoader.getInstance().set(Property.PATH_TO_DIRECTORY, path); + try { + StageController.getInstance().setScene(SceneEnum.MainPage); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceService.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceService.java index b949eac..cd27dfe 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceService.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/WorkspaceService.java @@ -13,12 +13,13 @@ import javafx.application.Platform; import java.io.*; public class WorkspaceService { private static final WorkspaceService instance = new WorkspaceService(); - private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance(); - private DatabaseService databaseService = DatabaseService.getInstance(); - private CacheService cacheService = CacheService.getInstance(); + private DatabaseService databaseService; + private CacheService cacheService; private MainPageController mainPageController; private WorkspaceService() { + this.cacheService = CacheService.getInstance(); + this.databaseService = DatabaseService.getInstance(); } public static WorkspaceService getInstance() { @@ -33,19 +34,6 @@ public class WorkspaceService { this.mainPageController = mainPageController; } - public void setupWorkspace() { - Chooser chooser = new Chooser(); - String path = chooser.chooseDirectory(); - - this.propertiesLoader.set(Property.PATH_TO_DIRECTORY, path); - this.cacheService.check(); - try { - StageController.getInstance().setScene(SceneEnum.MainPage); - } catch (IOException e) { - e.printStackTrace(); - } - } - public void refreshWorkspace() { Platform.runLater(() -> {this.getMainPageController().clearImages();}); var t = new Thread(new DirectoryScanner()); diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java index 042f806..d96fd44 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/CacheService.java @@ -19,6 +19,10 @@ public class CacheService { return instance; } + public String getPath(String name) { + return this.getPath() + "/" + name; + } + public String getPath() { return path; } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java b/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java index f2dcaa9..b3f19b2 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/data/DatabaseService.java @@ -1,15 +1,18 @@ package dev.mateuszkowalczyk.ffm.data; +import dev.mateuszkowalczyk.ffm.app.cache.CacheService; import dev.mateuszkowalczyk.ffm.data.database.utils.DatabaseCreator; import dev.mateuszkowalczyk.ffm.utils.ResourceLoader; +import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseService { - private static final DatabaseService instance = new DatabaseService(); private ResourceLoader resourceLoader = ResourceLoader.getInstance(); + private CacheService cacheService = CacheService.getInstance(); + private static final DatabaseService instance = new DatabaseService(); private Connection connection; private DatabaseService() { @@ -17,10 +20,12 @@ public class DatabaseService { } private void checkIfDatabaseExists() { - var db = this.resourceLoader.getResource("app.db"); - if (db == null) { + String path = this.cacheService.getPath("app.db"); + var file = new File(path); + + if (!file.exists()) { DatabaseCreator creator = new DatabaseCreator(); - this.connection = creator.create(); + this.connection = creator.create(path); } else { this.connect(); } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/data/database/utils/DatabaseCreator.java b/src/main/java/dev/mateuszkowalczyk/ffm/data/database/utils/DatabaseCreator.java index 398aad7..bda8acf 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/data/database/utils/DatabaseCreator.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/data/database/utils/DatabaseCreator.java @@ -9,16 +9,16 @@ import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseCreator { - public Connection create() { - var connection = this.createDatabase(); + public Connection create(String path) { + var connection = this.createDatabase(path); this.createTables(connection); return connection; } - private Connection createDatabase() { + private Connection createDatabase(String path) { Connection connection = null; - String path = "jdbc:sqlite:" + ResourceLoader.getInstance().getPath() + "app.db"; + path = "jdbc:sqlite:" + path; try { connection = DriverManager.getConnection(path); diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/MainPageController.java b/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/MainPageController.java index a2cfe6f..9f62787 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/MainPageController.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/MainPageController.java @@ -10,12 +10,13 @@ import java.net.URL; import java.util.ResourceBundle; public class MainPageController implements Initializable { - private WorkspaceService workspaceService = WorkspaceService.getInstance(); + private WorkspaceService workspaceService; @FXML private FlowPane imagesContainer; public MainPageController() { + this.workspaceService = WorkspaceService.getInstance(); this.workspaceService.setMainPageController(this); } diff --git a/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/WelcomePageController.java b/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/WelcomePageController.java index 5f49a26..5237c42 100644 --- a/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/WelcomePageController.java +++ b/src/main/java/dev/mateuszkowalczyk/ffm/view/workspace/WelcomePageController.java @@ -1,5 +1,6 @@ package dev.mateuszkowalczyk.ffm.view.workspace; +import dev.mateuszkowalczyk.ffm.app.WorkspaceInitializer; import dev.mateuszkowalczyk.ffm.app.WorkspaceService; import javafx.fxml.FXML; @@ -8,6 +9,7 @@ public class WelcomePageController { @FXML public void startNewApp(){ - WorkspaceService.getInstance().setupWorkspace(); + WorkspaceInitializer workspaceInitializer = new WorkspaceInitializer(); + workspaceInitializer.init(); } }