Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 1 | package eu.mulk.mulkcms2.benki.posts; |
| 2 | |
Matthias Andreas Benkard | 0dfcd14 | 2022-03-19 13:19:20 +0100 | [diff] [blame] | 3 | import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType; |
Matthias Andreas Benkard | 0351a8f | 2022-05-26 08:05:00 +0200 | [diff] [blame^] | 4 | import eu.mulk.mulkcms2.benki.posts.Post.Scope; |
| 5 | import eu.mulk.mulkcms2.common.markdown.MarkdownConverter; |
| 6 | import eu.mulk.mulkcms2.common.markdown.MarkdownConverter.Mode; |
Matthias Andreas Benkard | 299f5a5 | 2020-09-11 20:25:19 +0200 | [diff] [blame] | 7 | import io.quarkus.hibernate.orm.panache.PanacheEntityBase; |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 8 | import javax.annotation.CheckForNull; |
| 9 | import javax.json.bind.annotation.JsonbTransient; |
| 10 | import javax.persistence.Column; |
| 11 | import javax.persistence.Entity; |
| 12 | import javax.persistence.FetchType; |
| 13 | import javax.persistence.Id; |
| 14 | import javax.persistence.IdClass; |
| 15 | import javax.persistence.Inheritance; |
| 16 | import javax.persistence.InheritanceType; |
| 17 | import javax.persistence.JoinColumn; |
| 18 | import javax.persistence.ManyToOne; |
| 19 | import javax.persistence.Table; |
Matthias Andreas Benkard | 8563a3c | 2020-09-16 17:57:24 +0200 | [diff] [blame] | 20 | import org.hibernate.annotations.Generated; |
| 21 | import org.hibernate.annotations.GenerationTime; |
Matthias Andreas Benkard | 0dfcd14 | 2022-03-19 13:19:20 +0100 | [diff] [blame] | 22 | import org.hibernate.annotations.Type; |
| 23 | import org.hibernate.annotations.TypeDef; |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 24 | |
| 25 | @Entity |
| 26 | @Table(name = "post_texts", schema = "benki") |
| 27 | @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 28 | @IdClass(PostTextPK.class) |
Matthias Andreas Benkard | 0dfcd14 | 2022-03-19 13:19:20 +0100 | [diff] [blame] | 29 | @TypeDef(name = "tsvector", typeClass = PostgreSQLTSVectorType.class) |
Matthias Andreas Benkard | 299f5a5 | 2020-09-11 20:25:19 +0200 | [diff] [blame] | 30 | public abstract class PostText<OwningPost extends Post<?>> extends PanacheEntityBase { |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 31 | |
| 32 | private static final int DESCRIPTION_CACHE_VERSION = 1; |
| 33 | |
| 34 | @Id |
| 35 | @Column(name = "post", nullable = false, insertable = false, updatable = false) |
| 36 | public int postId; |
| 37 | |
| 38 | @Id |
| 39 | @Column(name = "language", nullable = false, length = -1) |
| 40 | public String language; |
| 41 | |
| 42 | @Column(name = "cached_description_version", nullable = true) |
| 43 | @CheckForNull |
| 44 | public Integer cachedDescriptionVersion; |
| 45 | |
| 46 | @Column(name = "cached_description_html", nullable = true) |
| 47 | @CheckForNull |
| 48 | public String cachedDescriptionHtml; |
| 49 | |
Matthias Andreas Benkard | 8563a3c | 2020-09-16 17:57:24 +0200 | [diff] [blame] | 50 | @Column(name = "search_terms") |
| 51 | @Generated(GenerationTime.ALWAYS) |
Matthias Andreas Benkard | 0dfcd14 | 2022-03-19 13:19:20 +0100 | [diff] [blame] | 52 | @Type(type = "tsvector") |
Matthias Andreas Benkard | 8563a3c | 2020-09-16 17:57:24 +0200 | [diff] [blame] | 53 | public String searchTerms; |
| 54 | |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 55 | @ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class) |
| 56 | @JoinColumn(name = "post", referencedColumnName = "id", nullable = false) |
| 57 | @JsonbTransient |
| 58 | public OwningPost post; |
| 59 | |
| 60 | @CheckForNull |
| 61 | public final String getDescriptionHtml() { |
| 62 | if (cachedDescriptionHtml != null |
| 63 | && cachedDescriptionVersion != null |
| 64 | && cachedDescriptionVersion >= DESCRIPTION_CACHE_VERSION) { |
| 65 | return cachedDescriptionHtml; |
| 66 | } else { |
| 67 | @CheckForNull var descriptionHtml = computeDescriptionHtml(); |
| 68 | cachedDescriptionHtml = descriptionHtml; |
| 69 | cachedDescriptionVersion = DESCRIPTION_CACHE_VERSION; |
| 70 | return descriptionHtml; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @CheckForNull |
Matthias Andreas Benkard | 0351a8f | 2022-05-26 08:05:00 +0200 | [diff] [blame^] | 75 | protected abstract String getDescriptionMarkup(); |
| 76 | |
| 77 | @CheckForNull |
| 78 | private String computeDescriptionHtml() { |
| 79 | var markup = getDescriptionMarkup(); |
| 80 | if (markup == null) { |
| 81 | return null; |
| 82 | } |
| 83 | return new MarkdownConverter(post.scope == Scope.top_level ? Mode.POST : Mode.COMMENT) |
| 84 | .htmlify(markup); |
| 85 | } |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 86 | } |