Skip to content

Commit 5326f5e

Browse files
committed
Revert "De-generics-ize Post, PostText."
This reverts commit 475bf00. Change-Id: I78bc6e061c3cd8ecc7c8794168596e26de1e6af8
1 parent 2b3aff0 commit 5326f5e

File tree

11 files changed

+39
-58
lines changed

11 files changed

+39
-58
lines changed

src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@Entity
1515
@Table(name = "bookmarks", schema = "benki")
16-
public class Bookmark extends Post {
16+
public class Bookmark extends Post<BookmarkText> {
1717

1818
@Column(name = "uri", nullable = false, length = -1)
1919
public String uri;
@@ -79,9 +79,4 @@ public void setDescription(String x) {
7979
text.description = x;
8080
text.cachedDescriptionHtml = null;
8181
}
82-
83-
@Override
84-
public BookmarkText getText() {
85-
return (BookmarkText) super.getText();
86-
}
8782
}

src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkText.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@Entity
1111
@Table(name = "bookmark_texts", schema = "benki")
12-
public class BookmarkText extends PostText {
12+
public class BookmarkText extends PostText<Bookmark> {
1313

1414
@Column(name = "title", nullable = true, length = -1)
1515
@CheckForNull
@@ -24,9 +24,4 @@ public class BookmarkText extends PostText {
2424
protected String getDescriptionMarkup() {
2525
return description;
2626
}
27-
28-
@Override
29-
public Bookmark getPost() {
30-
return (Bookmark) super.getPost();
31-
}
3227
}

src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Entity
1414
@Table(name = "lazychat_messages", schema = "benki")
15-
public class LazychatMessage extends Post {
15+
public class LazychatMessage extends Post<LazychatMessageText> {
1616

1717
@ManyToMany
1818
@JoinTable(
@@ -21,7 +21,7 @@ public class LazychatMessage extends Post {
2121
joinColumns = {@JoinColumn(name = "referrer")},
2222
inverseJoinColumns = {@JoinColumn(name = "referee")})
2323
@JsonbTransient
24-
public Collection<Post> referees;
24+
public Collection<Post<?>> referees;
2525

2626
@CheckForNull
2727
@Override
@@ -57,9 +57,4 @@ public void setContent(String x) {
5757
text.cachedDescriptionHtml = null;
5858
text.content = x;
5959
}
60-
61-
@Override
62-
public LazychatMessageText getText() {
63-
return (LazychatMessageText) super.getText();
64-
}
6560
}

src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessageText.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@Entity
1111
@Table(name = "lazychat_message_texts", schema = "benki")
12-
public class LazychatMessageText extends PostText {
12+
public class LazychatMessageText extends PostText<LazychatMessage> {
1313

1414
@Column(name = "content", nullable = true, length = -1)
1515
@CheckForNull
@@ -21,9 +21,4 @@ public class LazychatMessageText extends PostText {
2121
protected String getDescriptionMarkup() {
2222
return content;
2323
}
24-
25-
@Override
26-
public LazychatMessage getPost() {
27-
return (LazychatMessage) super.getPost();
28-
}
2924
}

src/main/java/eu/mulk/mulkcms2/benki/newsletter/Newsletter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public class Newsletter extends PanacheEntityBase {
2525

2626
@OneToMany(mappedBy = "newsletter", fetch = FetchType.LAZY)
2727
@OrderBy("date")
28-
public Collection<Post> posts;
28+
public Collection<Post<?>> posts;
2929
}

src/main/java/eu/mulk/mulkcms2/benki/newsletter/NewsletterSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static native MailTemplateInstance newsletter(
5353
void run() throws InterruptedException, TimeoutException, ExecutionException {
5454
var session = em.unwrap(Session.class);
5555

56-
List<Post> posts =
56+
List<Post<?>> posts =
5757
Post.find(
5858
"""
5959
SELECT DISTINCT p FROM Post p

src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Map;
4545
import java.util.Objects;
4646
import java.util.Set;
47+
import java.util.TimeZone;
4748
import java.util.stream.Collectors;
4849
import javax.annotation.CheckForNull;
4950
import org.hibernate.annotations.Type;
@@ -52,7 +53,7 @@
5253
@Entity
5354
@Table(name = "posts", schema = "benki")
5455
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55-
public abstract class Post extends PanacheEntityBase {
56+
public abstract class Post<Text extends PostText<?>> extends PanacheEntityBase {
5657

5758
public enum Scope {
5859
top_level,
@@ -118,11 +119,15 @@ public enum Scope {
118119
@JsonbTransient
119120
public Collection<LazychatMessage> comments;
120121

121-
@OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
122+
@OneToMany(
123+
mappedBy = "post",
124+
fetch = FetchType.LAZY,
125+
cascade = CascadeType.ALL,
126+
targetEntity = PostText.class)
122127
@MapKey(name = "language")
123-
public Map<String, PostText> texts = new HashMap<>();
128+
public Map<String, Text> texts = new HashMap<>();
124129

125-
public Map<String, PostText> getTexts() {
130+
public Map<String, Text> getTexts() {
126131
return texts;
127132
}
128133

@@ -148,7 +153,7 @@ public Visibility getVisibility() {
148153
}
149154
}
150155

151-
protected static <T extends Post> CriteriaBuilder<T> queryViewable(
156+
protected static <T extends Post<?>> CriteriaBuilder<T> queryViewable(
152157
Class<T> entityClass,
153158
@CheckForNull User reader,
154159
@CheckForNull User owner,
@@ -241,7 +246,7 @@ public final boolean isTopLevel() {
241246
return scope == Scope.top_level;
242247
}
243248

244-
public static class Day<T extends Post> {
249+
public static class Day<T extends Post<? extends PostText<?>>> {
245250
public final @CheckForNull LocalDate date;
246251
public final List<T> posts;
247252

@@ -257,12 +262,14 @@ public void cacheDescriptions() {
257262
}
258263
}
259264

260-
public static class PostPage<T extends Post> {
265+
public static class PostPage<T extends Post<? extends PostText<?>>> {
261266
public @CheckForNull final Integer prevCursor;
262267
public @CheckForNull final Integer cursor;
263268
public @CheckForNull final Integer nextCursor;
264269
public final List<T> posts;
265270

271+
private static final TimeZone timeZone = TimeZone.getDefault();
272+
266273
public PostPage(
267274
@CheckForNull Integer c0,
268275
@CheckForNull Integer c1,
@@ -289,7 +296,7 @@ public List<Day<T>> days() {
289296
}
290297
}
291298

292-
public static PostPage<Post> findViewable(
299+
public static PostPage<Post<? extends PostText<?>>> findViewable(
293300
PostFilter postFilter,
294301
EntityManager em,
295302
CriteriaBuilderFactory cbf,
@@ -298,7 +305,7 @@ public static PostPage<Post> findViewable(
298305
return findViewable(postFilter, em, cbf, viewer, owner, null, null, null);
299306
}
300307

301-
public static PostPage<Post> findViewable(
308+
public static PostPage<Post<? extends PostText<?>>> findViewable(
302309
PostFilter postFilter,
303310
EntityManager em,
304311
CriteriaBuilderFactory cbf,
@@ -321,7 +328,7 @@ public static PostPage<Post> findViewable(
321328
return findViewable(entityClass, em, cbf, viewer, owner, cursor, count, searchQuery);
322329
}
323330

324-
protected static <T extends Post> PostPage<T> findViewable(
331+
protected static <T extends Post<? extends PostText<?>>> PostPage<T> findViewable(
325332
Class<? extends T> entityClass,
326333
EntityManager em,
327334
CriteriaBuilderFactory cbf,
@@ -372,7 +379,7 @@ protected static <T extends Post> PostPage<T> findViewable(
372379
return new PostPage<>(prevCursor, cursor, nextCursor, forwardResults);
373380
}
374381

375-
public static <T extends Post> void fetchTexts(Collection<T> posts) {
382+
public static <T extends Post<?>> void fetchTexts(Collection<T> posts) {
376383
var postIds = posts.stream().map(x -> x.id).collect(toList());
377384

378385
if (!postIds.isEmpty()) {
@@ -382,7 +389,7 @@ public static <T extends Post> void fetchTexts(Collection<T> posts) {
382389
}
383390

384391
@CheckForNull
385-
public PostText getText() {
392+
public Text getText() {
386393
var texts = getTexts();
387394
if (texts.isEmpty()) {
388395
return null;
@@ -410,7 +417,7 @@ public boolean equals(Object o) {
410417
if (!(o instanceof Post)) {
411418
return false;
412419
}
413-
Post post = (Post) o;
420+
Post<?> post = (Post<?>) o;
414421
return Objects.equals(id, post.id);
415422
}
416423

src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public abstract class PostResource {
9191
@CheckedTemplate
9292
static class Templates {
9393

94-
public static native <P extends Post> TemplateInstance postList(
94+
public static native <P extends Post<?>> TemplateInstance postList(
9595
List<Post.Day<P>> postDays,
9696
@CheckForNull String feedUri,
9797
String pageTitle,
@@ -242,7 +242,7 @@ protected final PageKey ensurePageKey(User reader, String pagePath) {
242242
@GET
243243
@Produces(APPLICATION_JSON)
244244
@Path("{id}")
245-
public Post getPostJson(@PathParam("id") int id) {
245+
public Post<?> getPostJson(@PathParam("id") int id) {
246246
return getPostIfVisible(id);
247247
}
248248

@@ -253,7 +253,7 @@ public TemplateInstance getPostHtml(@PathParam("id") int id) {
253253
var post = getPostIfVisible(id);
254254

255255
return Templates.postList(
256-
new PostPage<>(null, null, null, List.of(post)).days(),
256+
new PostPage<Post<? extends PostText<?>>>(null, null, null, List.of(post)).days(),
257257
null,
258258
String.format("Post #%d", id),
259259
false,
@@ -319,7 +319,7 @@ public Uni<Response> postComment(
319319
Response.status(Status.BAD_REQUEST).entity("invalid hashcash").build());
320320
}
321321

322-
Post post = Post.findById(postId);
322+
Post<?> post = Post.findById(postId);
323323

324324
var comment = new LazychatMessage();
325325
comment.date = OffsetDateTime.now();
@@ -509,7 +509,7 @@ protected final Session getSession() {
509509
return entityManager.unwrap(Session.class);
510510
}
511511

512-
protected static void assignPostTargets(Post.Visibility visibility, User user, Post post) {
512+
protected static void assignPostTargets(Post.Visibility visibility, User user, Post<?> post) {
513513
switch (visibility) {
514514
case PUBLIC:
515515
post.targets = Set.of(Role.getWorld());
@@ -525,7 +525,7 @@ protected static void assignPostTargets(Post.Visibility visibility, User user, P
525525
}
526526
}
527527

528-
protected final Post getPostIfVisible(int id) {
528+
protected final Post<?> getPostIfVisible(int id) {
529529
@CheckForNull var user = getCurrentUser();
530530
var message = getSession().byId(Post.class).load(id);
531531

src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@Table(name = "post_texts", schema = "benki")
2828
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
2929
@IdClass(PostTextPK.class)
30-
public abstract class PostText extends PanacheEntityBase {
30+
public abstract class PostText<OwningPost extends Post<?>> extends PanacheEntityBase {
3131

3232
private static final int DESCRIPTION_CACHE_VERSION = 1;
3333

@@ -52,14 +52,10 @@ public abstract class PostText extends PanacheEntityBase {
5252
@Type(value = PostgreSQLTSVectorType.class)
5353
public String searchTerms;
5454

55-
@ManyToOne(fetch = FetchType.LAZY)
55+
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class)
5656
@JoinColumn(name = "post", referencedColumnName = "id", nullable = false)
5757
@JsonbTransient
58-
public Post post;
59-
60-
public Post getPost() {
61-
return post;
62-
}
58+
public OwningPost post;
6359

6460
@CheckForNull
6561
public final String getDescriptionHtml() {

src/main/java/eu/mulk/mulkcms2/benki/posts/PostTextPK.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package eu.mulk.mulkcms2.benki.posts;
22

33
import jakarta.persistence.Column;
4-
import jakarta.persistence.FetchType;
54
import jakarta.persistence.Id;
6-
import jakarta.persistence.JoinColumn;
7-
import jakarta.persistence.ManyToOne;
85
import java.io.Serializable;
96
import java.util.Objects;
107

@@ -43,7 +40,8 @@ public boolean equals(Object o) {
4340
return false;
4441
}
4542
PostTextPK that = (PostTextPK) o;
46-
return Objects.equals(getPostId(), that.getPostId()) && getLanguage().equals(that.getLanguage());
43+
return Objects.equals(getPostId(), that.getPostId())
44+
&& getLanguage().equals(that.getLanguage());
4745
}
4846

4947
@Override

0 commit comments

Comments
 (0)