-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractRepositoryOrmLite.java
More file actions
83 lines (65 loc) · 3.2 KB
/
AbstractRepositoryOrmLite.java
File metadata and controls
83 lines (65 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.eternalcode.parcellockers.database.wrapper;
import com.eternalcode.commons.ThrowingFunction;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.parcellockers.database.DatabaseManager;
import com.eternalcode.parcellockers.shared.exception.DatabaseException;
import com.j256.ormlite.dao.Dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class AbstractRepositoryOrmLite {
private static final Logger LOGGER = Logger.getLogger(AbstractRepositoryOrmLite.class.getName());
protected final DatabaseManager databaseManager;
protected final Scheduler scheduler;
protected AbstractRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) {
this.databaseManager = databaseManager;
this.scheduler = scheduler;
}
protected <T> CompletableFuture<Dao.CreateOrUpdateStatus> save(Class<T> type, T entity) {
return this.action(type, dao -> dao.createOrUpdate(entity));
}
protected <T> CompletableFuture<T> saveIfNotExist(Class<T> type, T entity) {
return this.action(type, dao -> dao.createIfNotExists(entity));
}
protected <T, ID> CompletableFuture<T> select(Class<T> type, ID id) {
return this.action(type, dao -> dao.queryForId(id));
}
protected <T, ID> CompletableFuture<Optional<T>> selectSafe(Class<T> type, ID id) {
return this.action(type, dao -> Optional.ofNullable(dao.queryForId(id)));
}
protected <T> CompletableFuture<Integer> delete(Class<T> type, T entity) {
return this.action(type, dao -> dao.delete(entity));
}
protected <T> CompletableFuture<Integer> deleteAll(Class<T> type) {
return this.action(type, dao -> dao.deleteBuilder().delete());
}
protected <T, ID> CompletableFuture<Integer> deleteById(Class<T> type, ID id) {
return this.action(type, dao -> dao.deleteById(id));
}
protected <T> CompletableFuture<List<T>> selectAll(Class<T> type) {
return this.action(type, Dao::queryForAll);
}
protected <T, ID, R> CompletableFuture<R> action(Class<T> type, ThrowingFunction<Dao<T, ID>, R, SQLException> action) {
CompletableFuture<R> completableFuture = new CompletableFuture<>();
this.scheduler.runAsync(() -> {
Dao<T, ID> dao = this.databaseManager.getDao(type);
try {
completableFuture.complete(action.apply(dao));
} catch (SQLException sqlException) {
DatabaseException databaseException = new DatabaseException(
"Database operation failed for type: " + type.getSimpleName(),
sqlException
);
LOGGER.log(Level.SEVERE, "Database operation failed", databaseException);
completableFuture.completeExceptionally(databaseException);
} catch (Throwable throwable) {
LOGGER.log(Level.SEVERE, "Unexpected error during database operation", throwable);
completableFuture.completeExceptionally(throwable);
}
});
return completableFuture;
}
}