From 3d399f3e0b150064b778a64ebfd495da2640bb4f Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 22 Mar 2020 07:23:07 +0100 Subject: Make paged post queries generic over Bookmark and LazychatMessage. Change-Id: Ie0ac96bfc80c3c31bb948681ef90a9a50a512cf4 --- .../eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java | 19 ++++-- .../mulkcms2/benki/bookmarks/BookmarkResource.java | 78 ++------------------- .../java/eu/mulk/mulkcms2/benki/generic/Post.java | 79 +++++++++++++++++++++- .../mulkcms2/benki/lazychat/LazychatMessage.java | 19 ++++-- 4 files changed, 109 insertions(+), 86 deletions(-) (limited to 'src/main/java/eu') diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java index f7dc9c0..8beda97 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java @@ -4,6 +4,7 @@ import eu.mulk.mulkcms2.benki.generic.Post; import eu.mulk.mulkcms2.benki.users.User; import eu.mulk.mulkcms2.common.markdown.MarkdownConverter; import io.quarkus.security.identity.SecurityIdentity; +import java.util.List; import java.util.Set; import javax.annotation.CheckForNull; import javax.persistence.CollectionTable; @@ -14,8 +15,7 @@ import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.persistence.Transient; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; +import org.hibernate.Session; @Entity @Table(name = "bookmarks", schema = "benki") @@ -43,12 +43,17 @@ public class Bookmark extends Post { return new MarkdownConverter().htmlify(description); } - public static CriteriaQuery findViewable( - SecurityIdentity readerIdentity, + public static List findViewable( + Session session, SecurityIdentity viewer, @CheckForNull User owner) { + return findViewable(Bookmark.class, session, viewer, owner, null, null).posts; + } + + public static PostPage findViewable( + Session session, + SecurityIdentity viewer, @CheckForNull User owner, @CheckForNull Integer cursor, - CriteriaBuilder cb, - boolean forward) { - return Post.findViewable(Bookmark.class, readerIdentity, owner, cursor, cb, forward); + @CheckForNull Integer count) { + return findViewable(Bookmark.class, session, viewer, owner, cursor, count); } } 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 7dd35d4..2272db2 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java @@ -31,7 +31,6 @@ import java.time.temporal.TemporalAccessor; import java.util.Comparator; import java.util.Date; import java.util.List; -import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.CheckForNull; @@ -102,10 +101,11 @@ public class BookmarkResource { maxResults = maxResults == null ? defaultMaxResults : maxResults; - var q = selectBookmarks(null, cursor, maxResults); + var session = entityManager.unwrap(Session.class); + var q = Bookmark.findViewable(session, identity, null, cursor, maxResults); return bookmarkList - .data("bookmarks", q.bookmarks) + .data("bookmarks", q.posts) .data("feedUri", "/bookmarks/feed") .data("authenticated", !identity.isAnonymous()) .data("hasPreviousPage", q.prevCursor != null) @@ -126,10 +126,11 @@ public class BookmarkResource { maxResults = maxResults == null ? defaultMaxResults : maxResults; var owner = User.findByNickname(ownerName); - var q = selectBookmarks(owner, cursor, maxResults); + var session = entityManager.unwrap(Session.class); + var q = Bookmark.findViewable(session, identity, owner, cursor, maxResults); return bookmarkList - .data("bookmarks", q.bookmarks) + .data("bookmarks", q.posts) .data("feedUri", String.format("/bookmarks/~%s/feed", ownerName)) .data("authenticated", !identity.isAnonymous()) .data("hasPreviousPage", q.prevCursor != null) @@ -155,7 +156,7 @@ public class BookmarkResource { } private String makeFeed(@Nullable User owner, @Nullable String ownerName) throws FeedException { - var bookmarks = selectBookmarks(owner); + var bookmarks = Bookmark.findViewable(entityManager.unwrap(Session.class), identity, owner); var feed = new Feed("atom_1.0"); var feedSubId = owner == null ? "" : String.format("/%d", owner.id); @@ -289,69 +290,4 @@ public class BookmarkResource { static String htmlDateTime(TemporalAccessor x) { return htmlDateFormatter.format(x); } - - private static class BookmarkPage { - @CheckForNull Integer prevCursor; - @CheckForNull Integer cursor; - @CheckForNull Integer nextCursor; - List bookmarks; - - public BookmarkPage( - @CheckForNull Integer c0, - @CheckForNull Integer c1, - @CheckForNull Integer c2, - List resultList) { - this.prevCursor = c0; - this.cursor = c1; - this.nextCursor = c2; - this.bookmarks = resultList; - } - } - - private List selectBookmarks(@CheckForNull User owner) { - return selectBookmarks(owner, null, null).bookmarks; - } - - private BookmarkPage selectBookmarks( - @CheckForNull User owner, @CheckForNull Integer cursor, @CheckForNull Integer count) { - - if (cursor != null) { - Objects.requireNonNull(count); - } - - var cb = entityManager.unwrap(Session.class).getCriteriaBuilder(); - - var forwardCriteria = Bookmark.findViewable(identity, owner, cursor, cb, true); - var forwardQuery = entityManager.createQuery(forwardCriteria); - - if (count != null) { - forwardQuery.setMaxResults(count + 1); - } - - log.debug(forwardQuery.unwrap(org.hibernate.query.Query.class).getQueryString()); - - @CheckForNull Integer prevCursor = null; - @CheckForNull Integer nextCursor = null; - - if (cursor != null) { - // Look backwards as well so we can find the prevCursor. - var backwardCriteria = Bookmark.findViewable(identity, owner, cursor, cb, false); - var backwardQuery = entityManager.createQuery(backwardCriteria); - backwardQuery.setMaxResults(count); - var backwardResults = backwardQuery.getResultList(); - if (!backwardResults.isEmpty()) { - prevCursor = backwardResults.get(backwardResults.size() - 1).id; - } - } - - var forwardResults = forwardQuery.getResultList(); - if (count != null) { - if (forwardResults.size() == count + 1) { - nextCursor = forwardResults.get(count).id; - forwardResults.remove((int) count); - } - } - - return new BookmarkPage(prevCursor, cursor, nextCursor, forwardResults); - } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/generic/Post.java b/src/main/java/eu/mulk/mulkcms2/benki/generic/Post.java index 502cb0d..898eeb4 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/generic/Post.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/generic/Post.java @@ -9,6 +9,8 @@ import io.quarkus.hibernate.orm.panache.PanacheEntityBase; import io.quarkus.security.identity.SecurityIdentity; import java.time.OffsetDateTime; import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import java.util.Set; import javax.annotation.CheckForNull; import javax.persistence.Column; @@ -30,12 +32,16 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.From; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Predicate; +import org.hibernate.Session; +import org.jboss.logging.Logger; @Entity @Table(name = "posts", schema = "benki") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Post extends PanacheEntityBase { + private static Logger log = Logger.getLogger(Post.class); + @Id @SequenceGenerator( allocationSize = 1, @@ -69,7 +75,7 @@ public abstract class Post extends PanacheEntityBase { inverseJoinColumns = @JoinColumn(name = "target")) public Set targets; - public static CriteriaQuery findViewable( + protected static CriteriaQuery queryViewable( Class entityClass, SecurityIdentity readerIdentity, @CheckForNull User owner, @@ -124,4 +130,75 @@ public abstract class Post extends PanacheEntityBase { return query; } + + public static class PostPage { + public @CheckForNull Integer prevCursor; + public @CheckForNull Integer cursor; + public @CheckForNull Integer nextCursor; + public List posts; + + private PostPage( + @CheckForNull Integer c0, + @CheckForNull Integer c1, + @CheckForNull Integer c2, + List resultList) { + this.prevCursor = c0; + this.cursor = c1; + this.nextCursor = c2; + this.posts = resultList; + } + } + + protected static List findViewable( + Class entityClass, Session session, SecurityIdentity viewer, @CheckForNull User owner) { + return findViewable(entityClass, session, viewer, owner, null, null).posts; + } + + protected static PostPage findViewable( + Class entityClass, + Session session, + SecurityIdentity viewer, + @CheckForNull User owner, + @CheckForNull Integer cursor, + @CheckForNull Integer count) { + + if (cursor != null) { + Objects.requireNonNull(count); + } + + var cb = session.getCriteriaBuilder(); + + var forwardCriteria = Bookmark.queryViewable(entityClass, viewer, owner, cursor, cb, true); + var forwardQuery = session.createQuery(forwardCriteria); + + if (count != null) { + forwardQuery.setMaxResults(count + 1); + } + + log.debug(forwardQuery.unwrap(org.hibernate.query.Query.class).getQueryString()); + + @CheckForNull Integer prevCursor = null; + @CheckForNull Integer nextCursor = null; + + if (cursor != null) { + // Look backwards as well so we can find the prevCursor. + var backwardCriteria = Bookmark.queryViewable(entityClass, viewer, owner, cursor, cb, false); + var backwardQuery = session.createQuery(backwardCriteria); + backwardQuery.setMaxResults(count); + var backwardResults = backwardQuery.getResultList(); + if (!backwardResults.isEmpty()) { + prevCursor = backwardResults.get(backwardResults.size() - 1).id; + } + } + + var forwardResults = forwardQuery.getResultList(); + if (count != null) { + if (forwardResults.size() == count + 1) { + nextCursor = forwardResults.get(count).id; + forwardResults.remove((int) count); + } + } + + return new PostPage(prevCursor, cursor, nextCursor, forwardResults); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java index 504dd10..4e58259 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java @@ -5,6 +5,7 @@ import eu.mulk.mulkcms2.benki.users.User; import eu.mulk.mulkcms2.common.markdown.MarkdownConverter; import io.quarkus.security.identity.SecurityIdentity; import java.util.Collection; +import java.util.List; import javax.annotation.CheckForNull; import javax.persistence.Column; import javax.persistence.Entity; @@ -12,8 +13,7 @@ import javax.persistence.FetchType; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; +import org.hibernate.Session; @Entity @Table(name = "lazychat_messages", schema = "benki") @@ -33,12 +33,17 @@ public class LazychatMessage extends Post { return new MarkdownConverter().htmlify(content); } - public static CriteriaQuery findViewable( - SecurityIdentity readerIdentity, + public static List findViewable( + Session session, SecurityIdentity viewer, @CheckForNull User owner) { + return findViewable(LazychatMessage.class, session, viewer, owner, null, null).posts; + } + + public static PostPage findViewable( + Session session, + SecurityIdentity viewer, @CheckForNull User owner, @CheckForNull Integer cursor, - CriteriaBuilder cb, - boolean forward) { - return Post.findViewable(LazychatMessage.class, readerIdentity, owner, cursor, cb, forward); + @CheckForNull Integer count) { + return findViewable(LazychatMessage.class, session, viewer, owner, cursor, count); } } -- cgit v1.2.3