move database to .cache folder and change workspace init

This commit is contained in:
Mateusz Kowalczyk 2020-01-24 14:45:52 +01:00
parent 06848bb2d2
commit 79f5246cab
7 changed files with 49 additions and 26 deletions

View File

@ -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();
}
}
}

View File

@ -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());

View File

@ -19,6 +19,10 @@ public class CacheService {
return instance;
}
public String getPath(String name) {
return this.getPath() + "/" + name;
}
public String getPath() {
return path;
}

View File

@ -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();
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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();
}
}