From 475bf006583288e6544374ba7c5d085f0b4235f2 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 6 Aug 2023 20:56:30 +0200 Subject: De-generics-ize Post, PostText. Hibernate 6 did not like the old scheme. Change-Id: I060e077648ab54589d5b7574137c10517b4c4114 --- .../eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java | 7 ++++- .../mulkcms2/benki/bookmarks/BookmarkText.java | 7 ++++- .../mulkcms2/benki/lazychat/LazychatMessage.java | 9 ++++-- .../benki/lazychat/LazychatMessageText.java | 7 ++++- .../mulk/mulkcms2/benki/newsletter/Newsletter.java | 2 +- .../benki/newsletter/NewsletterSender.java | 2 +- .../java/eu/mulk/mulkcms2/benki/posts/Post.java | 33 +++++++++------------- .../eu/mulk/mulkcms2/benki/posts/PostResource.java | 12 ++++---- .../eu/mulk/mulkcms2/benki/posts/PostText.java | 10 +++++-- .../eu/mulk/mulkcms2/benki/posts/PostTextPK.java | 4 +-- .../resources/templates/PostResource/postList.html | 2 +- 11 files changed, 56 insertions(+), 39 deletions(-) (limited to 'src') 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 01e8a99..ecfe69c 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java @@ -13,7 +13,7 @@ import javax.annotation.CheckForNull; @Entity @Table(name = "bookmarks", schema = "benki") -public class Bookmark extends Post { +public class Bookmark extends Post { @Column(name = "uri", nullable = false, length = -1) public String uri; @@ -79,4 +79,9 @@ public class Bookmark extends Post { text.description = x; text.cachedDescriptionHtml = null; } + + @Override + public BookmarkText getText() { + return (BookmarkText) super.getText(); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkText.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkText.java index 019dce5..300484d 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkText.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkText.java @@ -9,7 +9,7 @@ import javax.annotation.CheckForNull; @Entity @Table(name = "bookmark_texts", schema = "benki") -public class BookmarkText extends PostText { +public class BookmarkText extends PostText { @Column(name = "title", nullable = true, length = -1) @CheckForNull @@ -24,4 +24,9 @@ public class BookmarkText extends PostText { protected String getDescriptionMarkup() { return description; } + + @Override + public Bookmark getPost() { + return (Bookmark) super.getPost(); + } } 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 9181adf..a1d81d5 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java @@ -12,7 +12,7 @@ import javax.annotation.CheckForNull; @Entity @Table(name = "lazychat_messages", schema = "benki") -public class LazychatMessage extends Post { +public class LazychatMessage extends Post { @ManyToMany @JoinTable( @@ -21,7 +21,7 @@ public class LazychatMessage extends Post { joinColumns = {@JoinColumn(name = "referrer")}, inverseJoinColumns = {@JoinColumn(name = "referee")}) @JsonbTransient - public Collection> referees; + public Collection referees; @CheckForNull @Override @@ -57,4 +57,9 @@ public class LazychatMessage extends Post { text.cachedDescriptionHtml = null; text.content = x; } + + @Override + public LazychatMessageText getText() { + return (LazychatMessageText) super.getText(); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessageText.java b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessageText.java index 1a84909..ace7529 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessageText.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessageText.java @@ -9,7 +9,7 @@ import javax.annotation.CheckForNull; @Entity @Table(name = "lazychat_message_texts", schema = "benki") -public class LazychatMessageText extends PostText { +public class LazychatMessageText extends PostText { @Column(name = "content", nullable = true, length = -1) @CheckForNull @@ -21,4 +21,9 @@ public class LazychatMessageText extends PostText { protected String getDescriptionMarkup() { return content; } + + @Override + public LazychatMessage getPost() { + return (LazychatMessage) super.getPost(); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/newsletter/Newsletter.java b/src/main/java/eu/mulk/mulkcms2/benki/newsletter/Newsletter.java index 64fd6f7..7c9e50f 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/newsletter/Newsletter.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/newsletter/Newsletter.java @@ -25,5 +25,5 @@ public class Newsletter extends PanacheEntityBase { @OneToMany(mappedBy = "owner", fetch = FetchType.LAZY) @OrderBy("date") - public Collection> posts; + public Collection posts; } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/newsletter/NewsletterSender.java b/src/main/java/eu/mulk/mulkcms2/benki/newsletter/NewsletterSender.java index ae1ef9e..5f7937f 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/newsletter/NewsletterSender.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/newsletter/NewsletterSender.java @@ -53,7 +53,7 @@ public class NewsletterSender { void run() throws InterruptedException, TimeoutException, ExecutionException { var session = em.unwrap(Session.class); - List> posts = + List posts = Post.find( """ SELECT DISTINCT p FROM Post p diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java index a0245c8..0350a20 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java @@ -44,7 +44,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.TimeZone; import java.util.stream.Collectors; import javax.annotation.CheckForNull; import org.hibernate.annotations.Type; @@ -53,7 +52,7 @@ import org.hibernate.annotations.Where; @Entity @Table(name = "posts", schema = "benki") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) -public abstract class Post> extends PanacheEntityBase { +public abstract class Post extends PanacheEntityBase { public enum Scope { top_level, @@ -119,15 +118,11 @@ public abstract class Post> extends PanacheEntityBase { @JsonbTransient public Collection comments; - @OneToMany( - mappedBy = "post", - fetch = FetchType.LAZY, - cascade = CascadeType.ALL, - targetEntity = PostText.class) + @OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @MapKey(name = "language") - public Map texts = new HashMap<>(); + public Map texts = new HashMap<>(); - public Map getTexts() { + public Map getTexts() { return texts; } @@ -153,7 +148,7 @@ public abstract class Post> extends PanacheEntityBase { } } - protected static > CriteriaBuilder queryViewable( + protected static CriteriaBuilder queryViewable( Class entityClass, @CheckForNull User reader, @CheckForNull User owner, @@ -246,7 +241,7 @@ public abstract class Post> extends PanacheEntityBase { return scope == Scope.top_level; } - public static class Day>> { + public static class Day { public final @CheckForNull LocalDate date; public final List posts; @@ -262,14 +257,12 @@ public abstract class Post> extends PanacheEntityBase { } } - public static class PostPage>> { + public static class PostPage { public @CheckForNull final Integer prevCursor; public @CheckForNull final Integer cursor; public @CheckForNull final Integer nextCursor; public final List posts; - private static final TimeZone timeZone = TimeZone.getDefault(); - public PostPage( @CheckForNull Integer c0, @CheckForNull Integer c1, @@ -296,7 +289,7 @@ public abstract class Post> extends PanacheEntityBase { } } - public static PostPage>> findViewable( + public static PostPage findViewable( PostFilter postFilter, EntityManager em, CriteriaBuilderFactory cbf, @@ -305,7 +298,7 @@ public abstract class Post> extends PanacheEntityBase { return findViewable(postFilter, em, cbf, viewer, owner, null, null, null); } - public static PostPage>> findViewable( + public static PostPage findViewable( PostFilter postFilter, EntityManager em, CriteriaBuilderFactory cbf, @@ -328,7 +321,7 @@ public abstract class Post> extends PanacheEntityBase { return findViewable(entityClass, em, cbf, viewer, owner, cursor, count, searchQuery); } - protected static >> PostPage findViewable( + protected static PostPage findViewable( Class entityClass, EntityManager em, CriteriaBuilderFactory cbf, @@ -379,7 +372,7 @@ public abstract class Post> extends PanacheEntityBase { return new PostPage<>(prevCursor, cursor, nextCursor, forwardResults); } - public static > void fetchTexts(Collection posts) { + public static void fetchTexts(Collection posts) { var postIds = posts.stream().map(x -> x.id).collect(toList()); if (!postIds.isEmpty()) { @@ -389,7 +382,7 @@ public abstract class Post> extends PanacheEntityBase { } @CheckForNull - public Text getText() { + public PostText getText() { var texts = getTexts(); if (texts.isEmpty()) { return null; @@ -417,7 +410,7 @@ public abstract class Post> extends PanacheEntityBase { if (!(o instanceof Post)) { return false; } - Post post = (Post) o; + Post post = (Post) o; return Objects.equals(id, post.id); } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java index db613ae..4f7a43e 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java @@ -91,7 +91,7 @@ public abstract class PostResource { @CheckedTemplate static class Templates { - public static native

> TemplateInstance postList( + public static native

TemplateInstance postList( List> postDays, @CheckForNull String feedUri, String pageTitle, @@ -242,7 +242,7 @@ public abstract class PostResource { @GET @Produces(APPLICATION_JSON) @Path("{id}") - public Post getPostJson(@PathParam("id") int id) { + public Post getPostJson(@PathParam("id") int id) { return getPostIfVisible(id); } @@ -253,7 +253,7 @@ public abstract class PostResource { var post = getPostIfVisible(id); return Templates.postList( - new PostPage>>(null, null, null, List.of(post)).days(), + new PostPage<>(null, null, null, List.of(post)).days(), null, String.format("Post #%d", id), false, @@ -319,7 +319,7 @@ public abstract class PostResource { Response.status(Status.BAD_REQUEST).entity("invalid hashcash").build()); } - Post post = Post.findById(postId); + Post post = Post.findById(postId); var comment = new LazychatMessage(); comment.date = OffsetDateTime.now(); @@ -509,7 +509,7 @@ public abstract class PostResource { return entityManager.unwrap(Session.class); } - protected static void assignPostTargets(Post.Visibility visibility, User user, Post post) { + protected static void assignPostTargets(Post.Visibility visibility, User user, Post post) { switch (visibility) { case PUBLIC: post.targets = Set.of(Role.getWorld()); @@ -525,7 +525,7 @@ public abstract class PostResource { } } - protected final Post getPostIfVisible(int id) { + protected final Post getPostIfVisible(int id) { @CheckForNull var user = getCurrentUser(); var message = getSession().byId(Post.class).load(id); diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java index dc7a228..d25615e 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java @@ -27,7 +27,7 @@ import org.hibernate.annotations.Type; @Table(name = "post_texts", schema = "benki") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @IdClass(PostTextPK.class) -public abstract class PostText> extends PanacheEntityBase { +public abstract class PostText extends PanacheEntityBase { private static final int DESCRIPTION_CACHE_VERSION = 1; @@ -52,10 +52,14 @@ public abstract class PostText> extends PanacheEntity @Type(value = PostgreSQLTSVectorType.class) public String searchTerms; - @ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post", referencedColumnName = "id", nullable = false) @JsonbTransient - public OwningPost post; + public Post post; + + public Post getPost() { + return post; + } @CheckForNull public final String getDescriptionHtml() { diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostTextPK.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostTextPK.java index d737bf4..42f0d88 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostTextPK.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostTextPK.java @@ -14,13 +14,13 @@ public class PostTextPK implements Serializable { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post", referencedColumnName = "id", nullable = false) - public Post post; + public Post post; @Id @Column(name = "language", nullable = false, length = -1) private String language; - public Post getPost() { + public Post getPost() { return post; } diff --git a/src/main/resources/templates/PostResource/postList.html b/src/main/resources/templates/PostResource/postList.html index 89267a2..434c22a 100644 --- a/src/main/resources/templates/PostResource/postList.html +++ b/src/main/resources/templates/PostResource/postList.html @@ -1,4 +1,4 @@ -{@java.util.List>> postDays} +{@java.util.List> postDays} {@java.lang.String feedUri} {@java.lang.String pageTitle} {@boolean showBookmarkForm} -- cgit v1.2.3