summaryrefslogtreecommitdiff
path: root/src/main/java/eu/mulk/mulkcms2/benki/posts/PostText.java
blob: 80971b1e15e9eab034865a4e4520aabecf7f90f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package eu.mulk.mulkcms2.benki.posts;

import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType;
import eu.mulk.mulkcms2.benki.posts.Post.Scope;
import eu.mulk.mulkcms2.common.markdown.MarkdownConverter;
import eu.mulk.mulkcms2.common.markdown.MarkdownConverter.Mode;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import javax.annotation.CheckForNull;
import javax.json.bind.annotation.JsonbTransient;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

@Entity
@Table(name = "post_texts", schema = "benki")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@IdClass(PostTextPK.class)
@TypeDef(name = "tsvector", typeClass = PostgreSQLTSVectorType.class)
public abstract class PostText<OwningPost extends Post<?>> extends PanacheEntityBase {

  private static final int DESCRIPTION_CACHE_VERSION = 1;

  @Id
  @Column(name = "post", nullable = false, insertable = false, updatable = false)
  public int postId;

  @Id
  @Column(name = "language", nullable = false, length = -1)
  public String language;

  @Column(name = "cached_description_version", nullable = true)
  @CheckForNull
  public Integer cachedDescriptionVersion;

  @Column(name = "cached_description_html", nullable = true)
  @CheckForNull
  public String cachedDescriptionHtml;

  @Column(name = "search_terms")
  @Generated(GenerationTime.ALWAYS)
  @Type(type = "tsvector")
  public String searchTerms;

  @ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class)
  @JoinColumn(name = "post", referencedColumnName = "id", nullable = false)
  @JsonbTransient
  public OwningPost post;

  @CheckForNull
  public final String getDescriptionHtml() {
    if (cachedDescriptionHtml != null
        && cachedDescriptionVersion != null
        && cachedDescriptionVersion >= DESCRIPTION_CACHE_VERSION) {
      return cachedDescriptionHtml;
    } else {
      @CheckForNull var descriptionHtml = computeDescriptionHtml();
      cachedDescriptionHtml = descriptionHtml;
      cachedDescriptionVersion = DESCRIPTION_CACHE_VERSION;
      return descriptionHtml;
    }
  }

  @CheckForNull
  protected abstract String getDescriptionMarkup();

  @CheckForNull
  private String computeDescriptionHtml() {
    var markup = getDescriptionMarkup();
    if (markup == null) {
      return null;
    }
    return new MarkdownConverter(post.scope == Scope.top_level ? Mode.POST : Mode.COMMENT)
        .htmlify(markup);
  }
}