summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2020-03-22 06:47:48 +0100
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2020-03-22 06:47:48 +0100
commit43831d18f2d504c93a07ad3c32f52e2a887ed97f (patch)
treee539a590432c87a3befbfa8dafa96ee3c7080293 /src
parent82c342dd6d9c45f8ca17e2c08517170ee36998c3 (diff)
Make post list querying generic over all subtypes of Post.
Change-Id: I41d1c2e75675ba0ae2176a9d02ea76fb0bb0d76c
Diffstat (limited to 'src')
-rw-r--r--src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java
index dc45f34..429514b 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java
@@ -12,6 +12,9 @@ import com.rometools.rome.feed.synd.SyndPersonImpl;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.WireFeedOutput;
import eu.mulk.mulkcms2.benki.accesscontrol.Role;
+import eu.mulk.mulkcms2.benki.generic.Post;
+import eu.mulk.mulkcms2.benki.generic.Post_;
+import eu.mulk.mulkcms2.benki.lazychat.LazychatMessage;
import eu.mulk.mulkcms2.benki.users.User;
import eu.mulk.mulkcms2.benki.users.User_;
import io.quarkus.qute.Template;
@@ -328,7 +331,7 @@ public class BookmarkResource {
var cb = entityManager.unwrap(Session.class).getCriteriaBuilder();
- var forwardCriteria = generateBookmarkCriteriaQuery(owner, cursor, cb, true);
+ var forwardCriteria = queryPostList(Bookmark.class, owner, cursor, cb, true);
var forwardQuery = entityManager.createQuery(forwardCriteria);
if (count != null) {
@@ -342,7 +345,7 @@ public class BookmarkResource {
if (cursor != null) {
// Look backwards as well so we can find the prevCursor.
- var backwardCriteria = generateBookmarkCriteriaQuery(owner, cursor, cb, false);
+ var backwardCriteria = queryPostList(Bookmark.class, owner, cursor, cb, false);
var backwardQuery = entityManager.createQuery(backwardCriteria);
backwardQuery.setMaxResults(count);
var backwardResults = backwardQuery.getResultList();
@@ -362,16 +365,20 @@ public class BookmarkResource {
return new BookmarkPage(prevCursor, cursor, nextCursor, forwardResults);
}
- private CriteriaQuery<Bookmark> generateBookmarkCriteriaQuery(
- @CheckForNull User owner, @CheckForNull Integer cursor, CriteriaBuilder cb, boolean forward) {
- CriteriaQuery<Bookmark> query = cb.createQuery(Bookmark.class);
+ private <T extends Post> CriteriaQuery<T> queryPostList(
+ Class<T> entityClass,
+ @CheckForNull User owner,
+ @CheckForNull Integer cursor,
+ CriteriaBuilder cb,
+ boolean forward) {
+ CriteriaQuery<T> query = cb.createQuery(entityClass);
var conditions = new ArrayList<Predicate>();
- From<?, Bookmark> bm;
+ From<?, T> post;
if (identity.isAnonymous()) {
- bm = query.from(Bookmark.class);
- var target = bm.join(Bookmark_.targets);
+ post = query.from(entityClass);
+ var target = post.join(Post_.targets);
conditions.add(cb.equal(target, Role.getWorld()));
} else {
var userName = identity.getPrincipal().getName();
@@ -379,27 +386,32 @@ public class BookmarkResource {
var root = query.from(User.class);
conditions.add(cb.equal(root, user));
- bm = root.join(User_.visibleBookmarks);
+ if (entityClass.isAssignableFrom(Bookmark.class)) {
+ post = (From<?, T>) root.join(User_.visibleBookmarks);
+ } else {
+ assert entityClass.isAssignableFrom(LazychatMessage.class) : entityClass;
+ post = (From<?, T>) root.join(User_.visibleLazychatMessages);
+ }
}
- query.select(bm);
- bm.fetch(Bookmark_.owner, JoinType.LEFT);
+ query.select(post);
+ post.fetch(Post_.owner, JoinType.LEFT);
if (owner != null) {
- conditions.add(cb.equal(bm.get(Bookmark_.owner), owner));
+ conditions.add(cb.equal(post.get(Post_.owner), owner));
}
if (forward) {
- query.orderBy(cb.desc(bm.get(Bookmark_.id)));
+ query.orderBy(cb.desc(post.get(Post_.id)));
} else {
- query.orderBy(cb.asc(bm.get(Bookmark_.id)));
+ query.orderBy(cb.asc(post.get(Post_.id)));
}
if (cursor != null) {
if (forward) {
- conditions.add(cb.le(bm.get(Bookmark_.id), cursor));
+ conditions.add(cb.le(post.get(Post_.id), cursor));
} else {
- conditions.add(cb.gt(bm.get(Bookmark_.id), cursor));
+ conditions.add(cb.gt(post.get(Post_.id), cursor));
}
}