Add database, some utils to database and create photo entity

This commit is contained in:
Mateusz Kowalczyk 2020-01-14 21:56:24 +01:00
parent 1d4dc9602f
commit fd8556f26c
14 changed files with 309 additions and 61 deletions

View File

@ -1,6 +1,5 @@
package dev.mateuszkowalczyk.ffm;
import dev.mateuszkowalczyk.ffm.view.SceneEnum;
import dev.mateuszkowalczyk.ffm.view.StageController;
import javafx.application.Application;
import javafx.stage.Stage;
@ -9,7 +8,6 @@ public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
StageController.getInstance().initApp(stage);
}

View File

@ -1,5 +1,7 @@
package dev.mateuszkowalczyk.ffm.app;
import dev.mateuszkowalczyk.ffm.data.database.image.Photo;
import dev.mateuszkowalczyk.ffm.data.database.image.PhotoDAO;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import javafx.application.Platform;
@ -17,6 +19,7 @@ import java.util.ArrayList;
public class DirectoryScanner implements Runnable {
private WorkspaceService workspaceService = WorkspaceService.getInstance();
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
private PhotoDAO photoDAO = new PhotoDAO();
@Override
public void run() {
@ -34,7 +37,10 @@ public class DirectoryScanner implements Runnable {
if(this.isImagePath(filename)) {
ImageView imageView = new ImageView();
InputStream inputStream = null;
Photo photo = new Photo();
photo.setPath(path.toString());
this.photoDAO.save(photo);
Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);

View File

@ -1,25 +1,19 @@
package dev.mateuszkowalczyk.ffm.app;
import dev.mateuszkowalczyk.ffm.data.Chooser;
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
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 dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.application.Platform;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class WorkspaceService {
private static final WorkspaceService instance = new WorkspaceService();
private PropertiesLoader propertiesLoader = new PropertiesLoader();
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
private DatabaseService databaseService = DatabaseService.getInstance();
private MainPageController mainPageController;
private WorkspaceService() {
@ -40,6 +34,7 @@ public class WorkspaceService {
public void setupWorkspace() {
Chooser chooser = new Chooser();
String path = chooser.chooseDirectory();
this.propertiesLoader.set(Property.PATH_TO_DIRECTORY, path);
try {
StageController.getInstance().setScene(SceneEnum.MainPage);
@ -49,54 +44,8 @@ public class WorkspaceService {
}
public void refreshWorkspace() {
this.getMainPageController().clearImages();
Platform.runLater(() -> {this.getMainPageController().clearImages();});
var t = new Thread(new DirectoryScanner());
t.start();
}
private void readImages() {
try {
Files.walk(Paths.get(this.propertiesLoader.get(Property.PATH_TO_DIRECTORY)))
.filter(Files::isRegularFile)
.forEach(path -> {
String filename = path.getFileName().toString();
if(this.isImagePath(filename)) {
System.out.println("read image");
ImageView imageView = new ImageView();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(path.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(inputStream);
Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
imageView.setImage(image1);
imageView.setFitHeight(200);
imageView.setFitWidth(200);
imageView.setSmooth(true);
imageView.setCache(true);
this.mainPageController.addImage(imageView);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean isImagePath(String filename) {
switch (filename.substring(filename.length() - 3).toLowerCase()) {
case "jpg":
return true;
default:
return false;
}
}
}

View File

@ -0,0 +1,37 @@
package dev.mateuszkowalczyk.ffm.data;
import dev.mateuszkowalczyk.ffm.data.database.image.Photo;
import dev.mateuszkowalczyk.ffm.data.database.utils.TableCreator;
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseCreator {
public Connection create() {
var connection = this.createDatabase();
this.createTables(connection);
return connection;
}
private Connection createDatabase() {
Connection connection = null;
String path = "jdbc:sqlite:" + ResourceLoader.getInstance().getPath() + "app.db";
try {
connection = DriverManager.getConnection(path);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return connection;
}
private void createTables(Connection connection) {
TableCreator creator = new TableCreator(connection);
creator.create("photos", new Photo());
}
}

View File

@ -0,0 +1,51 @@
package dev.mateuszkowalczyk.ffm.data;
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
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 Connection connection;
private DatabaseService() {
this.checkIfDatabaseExists();
}
private void checkIfDatabaseExists() {
var db = this.resourceLoader.getResource("app.db");
if (db == null) {
DatabaseCreator creator = new DatabaseCreator();
this.connection = creator.create();
} else {
this.connect();
}
}
public static DatabaseService getInstance() {
return instance;
}
public void connect() {
if (this.connection == null) {
String path = ResourceLoader.getInstance().getPath() + "app.db";
try {
this.connection = DriverManager.getConnection(path);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
}

View File

@ -0,0 +1,12 @@
package dev.mateuszkowalczyk.ffm.data.database;
import java.util.Optional;
public interface Dao<T> {
Optional<T> get(long id);
void save(T t);
void update(T t);
void delete (T t);
}

View File

@ -0,0 +1,18 @@
package dev.mateuszkowalczyk.ffm.data.database.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
public enum Type {
STRING, INT, DATE
}
public String name() default "";
public Type type() default Type.STRING;
}

View File

@ -0,0 +1,11 @@
package dev.mateuszkowalczyk.ffm.data.database.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface PrimaryKey {
}

View File

@ -0,0 +1,12 @@
package dev.mateuszkowalczyk.ffm.data.database.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Table {
public String name() default "";
}

View File

@ -0,0 +1,39 @@
package dev.mateuszkowalczyk.ffm.data.database.image;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Column;
import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
@Table(name = "photo")
public class Photo {
@Column
@PrimaryKey
private long id;
@Column
private String path;
@Column
private String cachedPath;
public long getId() {
return id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getCachedPath() {
return cachedPath;
}
public void setCachedPath(String cachedPath) {
this.cachedPath = cachedPath;
}
}

View File

@ -0,0 +1,42 @@
package dev.mateuszkowalczyk.ffm.data.database.image;
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
import dev.mateuszkowalczyk.ffm.data.database.Dao;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Optional;
public class PhotoDAO implements Dao<Photo> {
private DatabaseService databaseService = DatabaseService.getInstance();
@Override
public Optional<Photo> get(long id) {
return Optional.empty();
}
@Override
public void save(Photo photo) {
String sql = "INSERT INTO photos(path) values (?)";
try {
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
preparedStatement.setString(1, photo.getPath());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Photo photo) {
}
@Override
public void delete(Photo photo) {
}
}

View File

@ -0,0 +1,69 @@
package dev.mateuszkowalczyk.ffm.data.database.utils;
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Column;
import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class TableCreator {
private final Connection connection;
public TableCreator(Connection connection) {
this.connection = connection;
}
public void create(String name, Object object) {
String sql = "CREATE TABLE IF NOT EXISTS " + name;
Field[] fields = object.getClass().getDeclaredFields();
sql += this.addFields(fields);
try {
Statement stmt = this.connection.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private String addFields(Field[] fields) {
String sql = " ( \n ";
List<String> listOfFields = new ArrayList<>();
for (Field field :fields) {
if (field.isAnnotationPresent(Column.class)) {
listOfFields.add(
field.getName() +
" " +
this.getType(field.getAnnotation(Column.class)) +
(field.isAnnotationPresent(PrimaryKey.class) ? " PRIMARY KEY " : "")
);
}
}
sql += String.join(", \n ", listOfFields);
return sql + ") ";
}
private String getType(Column annotation) {
switch (annotation.type()) {
case INT:
return "integer";
case STRING:
default:
return "text";
}
}
}

View File

@ -13,7 +13,7 @@ public class PropertiesLoader {
private ResourceLoader resourceLoader = ResourceLoader.getInstance();
private Properties properties = null;
public PropertiesLoader () {}
private PropertiesLoader () {}
public static PropertiesLoader getInstance() {
return instance;

View File

@ -12,6 +12,10 @@ public class ResourceLoader {
return getClass().getClassLoader().getResource(name);
}
public String getPath() {
return this.getResource("").toString();
}
public InputStream getResourceAsStream(String name) {
return getClass().getClassLoader().getResourceAsStream( name);
}