few fix and begin of cache module

This commit is contained in:
Mateusz Kowalczyk 2020-01-14 23:05:39 +01:00
parent fd8556f26c
commit 6eb2a56fbf
9 changed files with 91 additions and 17 deletions

View File

@ -1,17 +1,14 @@
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.data.database.photo.Photo;
import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

View File

@ -0,0 +1,14 @@
package dev.mateuszkowalczyk.ffm.app.cache;
public class CacheService {
private static final CacheService instance = new CacheService();
private CacheService () {
CacheStructureChecker cacheStructureChecker = new CacheStructureChecker();
cacheStructureChecker.check();
}
public static CacheService getInstance() {
return instance;
}
}

View File

@ -0,0 +1,44 @@
package dev.mateuszkowalczyk.ffm.app.cache;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class CacheStructureChecker {
private String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY) + "/.cache";
private List<String> listToCheck = new ArrayList<String>();
public void check() {
this.setupPathsToCheck();
this.listToCheck.forEach(s -> this.checkDirectory(s));
}
private void setupPathsToCheck() {
this.listToCheck.add(this.path);
this.listToCheck.add(this.path + "/thumbnails");
}
private void checkDirectory(String path) {
if (!this.directoryExists(path)) {
this.createDirectory(path);
}
}
private void createDirectory(String path) {
File file = new File(path);
file.mkdir();
}
private boolean directoryExists(String path) {
File file = new File(path);
return file.exists();
}
}

View File

@ -1,5 +1,6 @@
package dev.mateuszkowalczyk.ffm.data;
import dev.mateuszkowalczyk.ffm.data.database.utils.DatabaseCreator;
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
import java.sql.Connection;

View File

@ -1,13 +1,13 @@
package dev.mateuszkowalczyk.ffm.data.database.image;
package dev.mateuszkowalczyk.ffm.data.database.photo;
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")
@Table(name = "photos")
public class Photo {
@Column
@Column(type = Column.Type.INT)
@PrimaryKey
private long id;

View File

@ -1,4 +1,4 @@
package dev.mateuszkowalczyk.ffm.data.database.image;
package dev.mateuszkowalczyk.ffm.data.database.photo;
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
import dev.mateuszkowalczyk.ffm.data.database.Dao;

View File

@ -1,7 +1,6 @@
package dev.mateuszkowalczyk.ffm.data;
package dev.mateuszkowalczyk.ffm.data.database.utils;
import dev.mateuszkowalczyk.ffm.data.database.image.Photo;
import dev.mateuszkowalczyk.ffm.data.database.utils.TableCreator;
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
import java.sql.Connection;
@ -32,6 +31,6 @@ public class DatabaseCreator {
private void createTables(Connection connection) {
TableCreator creator = new TableCreator(connection);
creator.create("photos", new Photo());
creator.create(Photo.class);
}
}

View File

@ -3,6 +3,7 @@ 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 dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
@ -13,17 +14,17 @@ import java.util.ArrayList;
import java.util.List;
public class TableCreator {
private TableMapper tableMapper = new TableMapper();
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;
public void create(Class object) {
String sql = "CREATE TABLE IF NOT EXISTS " + this.tableMapper.getName(object);
Field[] fields = object.getClass().getDeclaredFields();
Field[] fields = object.getDeclaredFields();
sql += this.addFields(fields);

View File

@ -0,0 +1,18 @@
package dev.mateuszkowalczyk.ffm.data.database.utils;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
public class TableMapper {
public String getName(Class object) {
if (object.isAnnotationPresent(Table.class)) {
Table annotation = (Table) object.getAnnotation(Table.class);
String name = annotation.name();
if (name.length() > 0) {
return name;
}
}
return object.getSimpleName().toLowerCase();
}
}