|
@@ -18,6 +18,14 @@ public abstract class DatabaseTable<T extends Persistable> {
|
|
|
|
|
|
private Map<Long, T> cacheMap = new HashMap<>();
|
|
private Map<Long, T> cacheMap = new HashMap<>();
|
|
|
|
|
|
|
|
+ public T get(T obj) {
|
|
|
|
+ return getById(obj.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Optional<T> find(T obj) {
|
|
|
|
+ return findById(obj.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
public T getById(long id) {
|
|
public T getById(long id) {
|
|
if (cacheMap.containsKey(id)) {
|
|
if (cacheMap.containsKey(id)) {
|
|
return cacheMap.get(id);
|
|
return cacheMap.get(id);
|
|
@@ -63,25 +71,23 @@ public abstract class DatabaseTable<T extends Persistable> {
|
|
if(!optObj.isPresent()) {
|
|
if(!optObj.isPresent()) {
|
|
throw new IllegalStateException("Unable to find entity in the database");
|
|
throw new IllegalStateException("Unable to find entity in the database");
|
|
}
|
|
}
|
|
- T obj = optObj.get();
|
|
|
|
- cache(obj);
|
|
|
|
- return obj;
|
|
|
|
|
|
+ return cache(optObj.get());
|
|
}
|
|
}
|
|
|
|
|
|
public T refresh(T obj) {
|
|
public T refresh(T obj) {
|
|
return refresh(obj.getId());
|
|
return refresh(obj.getId());
|
|
}
|
|
}
|
|
|
|
|
|
- public void save(T obj) {
|
|
|
|
|
|
+ public T save(T obj) {
|
|
if (cacheMap.containsKey(obj.getId())) {
|
|
if (cacheMap.containsKey(obj.getId())) {
|
|
update(obj);
|
|
update(obj);
|
|
} else {
|
|
} else {
|
|
insert(obj);
|
|
insert(obj);
|
|
}
|
|
}
|
|
- cache(obj);
|
|
|
|
|
|
+ return cache(obj);
|
|
}
|
|
}
|
|
|
|
|
|
- public final void delete(long id) {
|
|
|
|
|
|
+ public void delete(long id) {
|
|
DbTable dbTable = getDbTableAnnotation();
|
|
DbTable dbTable = getDbTableAnnotation();
|
|
// Cherche l'id
|
|
// Cherche l'id
|
|
String idField = null;
|
|
String idField = null;
|
|
@@ -116,10 +122,13 @@ public abstract class DatabaseTable<T extends Persistable> {
|
|
}
|
|
}
|
|
|
|
|
|
private T cache(T obj) {
|
|
private T cache(T obj) {
|
|
- if (cacheMap.containsKey(obj.getId())) {
|
|
|
|
- cacheMap.replace(obj.getId(), obj);
|
|
|
|
- } else {
|
|
|
|
- cacheMap.put(obj.getId(), obj);
|
|
|
|
|
|
+ // Si le cache est actif
|
|
|
|
+ if (DatabaseProperties.getBool("cache")) {
|
|
|
|
+ if (cacheMap.containsKey(obj.getId())) {
|
|
|
|
+ cacheMap.replace(obj.getId(), obj);
|
|
|
|
+ } else {
|
|
|
|
+ cacheMap.put(obj.getId(), obj);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
//System.out.println("Cache size: " + cacheMap.size());
|
|
//System.out.println("Cache size: " + cacheMap.size());
|
|
//cacheMap.forEach((key, val) -> System.out.println("Cache: " + key));
|
|
//cacheMap.forEach((key, val) -> System.out.println("Cache: " + key));
|
|
@@ -241,9 +250,10 @@ public abstract class DatabaseTable<T extends Persistable> {
|
|
if(field.isAnnotationPresent(DbLink.class)) {
|
|
if(field.isAnnotationPresent(DbLink.class)) {
|
|
DatabaseUtils.checkIfPersistable(field);
|
|
DatabaseUtils.checkIfPersistable(field);
|
|
if(field.get(obj) == null) {
|
|
if(field.get(obj) == null) {
|
|
- params.add(0l);
|
|
|
|
|
|
+ params.add(null);
|
|
} else {
|
|
} else {
|
|
Persistable persistable = (Persistable) field.get(obj);
|
|
Persistable persistable = (Persistable) field.get(obj);
|
|
|
|
+ // TODO Sauvegarder l'autre objet avant
|
|
params.add(persistable.getId());
|
|
params.add(persistable.getId());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -310,9 +320,10 @@ public abstract class DatabaseTable<T extends Persistable> {
|
|
if(field.isAnnotationPresent(DbLink.class)) {
|
|
if(field.isAnnotationPresent(DbLink.class)) {
|
|
DatabaseUtils.checkIfPersistable(field);
|
|
DatabaseUtils.checkIfPersistable(field);
|
|
if(field.get(obj) == null) {
|
|
if(field.get(obj) == null) {
|
|
- params.add(0l);
|
|
|
|
|
|
+ params.add(null);
|
|
} else {
|
|
} else {
|
|
Persistable persistable = (Persistable) field.get(obj);
|
|
Persistable persistable = (Persistable) field.get(obj);
|
|
|
|
+ // TODO Sauvegarder l'autre objet avant
|
|
params.add(persistable.getId());
|
|
params.add(persistable.getId());
|
|
}
|
|
}
|
|
}
|
|
}
|