move database to .cache folder and change workspace init
This commit is contained in:
parent
06848bb2d2
commit
79f5246cab
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,12 +13,13 @@ import javafx.application.Platform;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
public class WorkspaceService {
|
public class WorkspaceService {
|
||||||
private static final WorkspaceService instance = new WorkspaceService();
|
private static final WorkspaceService instance = new WorkspaceService();
|
||||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
private DatabaseService databaseService;
|
||||||
private DatabaseService databaseService = DatabaseService.getInstance();
|
private CacheService cacheService;
|
||||||
private CacheService cacheService = CacheService.getInstance();
|
|
||||||
private MainPageController mainPageController;
|
private MainPageController mainPageController;
|
||||||
|
|
||||||
private WorkspaceService() {
|
private WorkspaceService() {
|
||||||
|
this.cacheService = CacheService.getInstance();
|
||||||
|
this.databaseService = DatabaseService.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WorkspaceService getInstance() {
|
public static WorkspaceService getInstance() {
|
||||||
@ -33,19 +34,6 @@ public class WorkspaceService {
|
|||||||
this.mainPageController = mainPageController;
|
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() {
|
public void refreshWorkspace() {
|
||||||
Platform.runLater(() -> {this.getMainPageController().clearImages();});
|
Platform.runLater(() -> {this.getMainPageController().clearImages();});
|
||||||
var t = new Thread(new DirectoryScanner());
|
var t = new Thread(new DirectoryScanner());
|
||||||
|
@ -19,6 +19,10 @@ public class CacheService {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getPath(String name) {
|
||||||
|
return this.getPath() + "/" + name;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
package dev.mateuszkowalczyk.ffm.data;
|
package dev.mateuszkowalczyk.ffm.data;
|
||||||
|
|
||||||
|
import dev.mateuszkowalczyk.ffm.app.cache.CacheService;
|
||||||
import dev.mateuszkowalczyk.ffm.data.database.utils.DatabaseCreator;
|
import dev.mateuszkowalczyk.ffm.data.database.utils.DatabaseCreator;
|
||||||
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
|
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DatabaseService {
|
public class DatabaseService {
|
||||||
private static final DatabaseService instance = new DatabaseService();
|
|
||||||
private ResourceLoader resourceLoader = ResourceLoader.getInstance();
|
private ResourceLoader resourceLoader = ResourceLoader.getInstance();
|
||||||
|
private CacheService cacheService = CacheService.getInstance();
|
||||||
|
private static final DatabaseService instance = new DatabaseService();
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
private DatabaseService() {
|
private DatabaseService() {
|
||||||
@ -17,10 +20,12 @@ public class DatabaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkIfDatabaseExists() {
|
private void checkIfDatabaseExists() {
|
||||||
var db = this.resourceLoader.getResource("app.db");
|
String path = this.cacheService.getPath("app.db");
|
||||||
if (db == null) {
|
var file = new File(path);
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
DatabaseCreator creator = new DatabaseCreator();
|
DatabaseCreator creator = new DatabaseCreator();
|
||||||
this.connection = creator.create();
|
this.connection = creator.create(path);
|
||||||
} else {
|
} else {
|
||||||
this.connect();
|
this.connect();
|
||||||
}
|
}
|
||||||
|
@ -9,16 +9,16 @@ import java.sql.DriverManager;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DatabaseCreator {
|
public class DatabaseCreator {
|
||||||
public Connection create() {
|
public Connection create(String path) {
|
||||||
var connection = this.createDatabase();
|
var connection = this.createDatabase(path);
|
||||||
this.createTables(connection);
|
this.createTables(connection);
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection createDatabase() {
|
private Connection createDatabase(String path) {
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
String path = "jdbc:sqlite:" + ResourceLoader.getInstance().getPath() + "app.db";
|
path = "jdbc:sqlite:" + path;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection(path);
|
connection = DriverManager.getConnection(path);
|
||||||
|
@ -10,12 +10,13 @@ import java.net.URL;
|
|||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
public class MainPageController implements Initializable {
|
public class MainPageController implements Initializable {
|
||||||
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
private WorkspaceService workspaceService;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private FlowPane imagesContainer;
|
private FlowPane imagesContainer;
|
||||||
|
|
||||||
public MainPageController() {
|
public MainPageController() {
|
||||||
|
this.workspaceService = WorkspaceService.getInstance();
|
||||||
this.workspaceService.setMainPageController(this);
|
this.workspaceService.setMainPageController(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.mateuszkowalczyk.ffm.view.workspace;
|
package dev.mateuszkowalczyk.ffm.view.workspace;
|
||||||
|
|
||||||
|
import dev.mateuszkowalczyk.ffm.app.WorkspaceInitializer;
|
||||||
import dev.mateuszkowalczyk.ffm.app.WorkspaceService;
|
import dev.mateuszkowalczyk.ffm.app.WorkspaceService;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
|
||||||
@ -8,6 +9,7 @@ public class WelcomePageController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void startNewApp(){
|
public void startNewApp(){
|
||||||
WorkspaceService.getInstance().setupWorkspace();
|
WorkspaceInitializer workspaceInitializer = new WorkspaceInitializer();
|
||||||
|
workspaceInitializer.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user