blob: 11ac98aa1dcbf5421b765e890837231f31929796 [file] [log] [blame]
Matthias Andreas Benkardd5498fc2020-08-23 21:51:00 +02001package eu.mulk.mulkcms2.benki.posts;
2
Matthias Andreas Benkard0dfcd142022-03-19 13:19:20 +01003import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType;
Matthias Andreas Benkard299f5a52020-09-11 20:25:19 +02004import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
Matthias Andreas Benkardd5498fc2020-08-23 21:51:00 +02005import javax.annotation.CheckForNull;
6import javax.json.bind.annotation.JsonbTransient;
7import javax.persistence.Column;
8import javax.persistence.Entity;
9import javax.persistence.FetchType;
10import javax.persistence.Id;
11import javax.persistence.IdClass;
12import javax.persistence.Inheritance;
13import javax.persistence.InheritanceType;
14import javax.persistence.JoinColumn;
15import javax.persistence.ManyToOne;
16import javax.persistence.Table;
Matthias Andreas Benkard8563a3c2020-09-16 17:57:24 +020017import org.hibernate.annotations.Generated;
18import org.hibernate.annotations.GenerationTime;
Matthias Andreas Benkard0dfcd142022-03-19 13:19:20 +010019import org.hibernate.annotations.Type;
20import org.hibernate.annotations.TypeDef;
Matthias Andreas Benkardd5498fc2020-08-23 21:51:00 +020021
22@Entity
23@Table(name = "post_texts", schema = "benki")
24@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
25@IdClass(PostTextPK.class)
Matthias Andreas Benkard0dfcd142022-03-19 13:19:20 +010026@TypeDef(name = "tsvector", typeClass = PostgreSQLTSVectorType.class)
Matthias Andreas Benkard299f5a52020-09-11 20:25:19 +020027public abstract class PostText<OwningPost extends Post<?>> extends PanacheEntityBase {
Matthias Andreas Benkardd5498fc2020-08-23 21:51:00 +020028
29 private static final int DESCRIPTION_CACHE_VERSION = 1;
30
31 @Id
32 @Column(name = "post", nullable = false, insertable = false, updatable = false)
33 public int postId;
34
35 @Id
36 @Column(name = "language", nullable = false, length = -1)
37 public String language;
38
39 @Column(name = "cached_description_version", nullable = true)
40 @CheckForNull
41 public Integer cachedDescriptionVersion;
42
43 @Column(name = "cached_description_html", nullable = true)
44 @CheckForNull
45 public String cachedDescriptionHtml;
46
Matthias Andreas Benkard8563a3c2020-09-16 17:57:24 +020047 @Column(name = "search_terms")
48 @Generated(GenerationTime.ALWAYS)
Matthias Andreas Benkard0dfcd142022-03-19 13:19:20 +010049 @Type(type = "tsvector")
Matthias Andreas Benkard8563a3c2020-09-16 17:57:24 +020050 public String searchTerms;
51
Matthias Andreas Benkardd5498fc2020-08-23 21:51:00 +020052 @ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class)
53 @JoinColumn(name = "post", referencedColumnName = "id", nullable = false)
54 @JsonbTransient
55 public OwningPost post;
56
57 @CheckForNull
58 public final String getDescriptionHtml() {
59 if (cachedDescriptionHtml != null
60 && cachedDescriptionVersion != null
61 && cachedDescriptionVersion >= DESCRIPTION_CACHE_VERSION) {
62 return cachedDescriptionHtml;
63 } else {
64 @CheckForNull var descriptionHtml = computeDescriptionHtml();
65 cachedDescriptionHtml = descriptionHtml;
66 cachedDescriptionVersion = DESCRIPTION_CACHE_VERSION;
67 return descriptionHtml;
68 }
69 }
70
71 @CheckForNull
72 protected abstract String computeDescriptionHtml();
73}