Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 1 | package eu.mulk.mulkcms2.benki.posts; |
| 2 | |
Matthias Andreas Benkard | 299f5a5 | 2020-09-11 20:25:19 +0200 | [diff] [blame^] | 3 | import io.quarkus.hibernate.orm.panache.PanacheEntityBase; |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 4 | import javax.annotation.CheckForNull; |
| 5 | import javax.json.bind.annotation.JsonbTransient; |
| 6 | import javax.persistence.Column; |
| 7 | import javax.persistence.Entity; |
| 8 | import javax.persistence.FetchType; |
| 9 | import javax.persistence.Id; |
| 10 | import javax.persistence.IdClass; |
| 11 | import javax.persistence.Inheritance; |
| 12 | import javax.persistence.InheritanceType; |
| 13 | import javax.persistence.JoinColumn; |
| 14 | import javax.persistence.ManyToOne; |
| 15 | import javax.persistence.Table; |
| 16 | |
| 17 | @Entity |
| 18 | @Table(name = "post_texts", schema = "benki") |
| 19 | @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 20 | @IdClass(PostTextPK.class) |
Matthias Andreas Benkard | 299f5a5 | 2020-09-11 20:25:19 +0200 | [diff] [blame^] | 21 | public abstract class PostText<OwningPost extends Post<?>> extends PanacheEntityBase { |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 22 | |
| 23 | private static final int DESCRIPTION_CACHE_VERSION = 1; |
| 24 | |
| 25 | @Id |
| 26 | @Column(name = "post", nullable = false, insertable = false, updatable = false) |
| 27 | public int postId; |
| 28 | |
| 29 | @Id |
| 30 | @Column(name = "language", nullable = false, length = -1) |
| 31 | public String language; |
| 32 | |
| 33 | @Column(name = "cached_description_version", nullable = true) |
| 34 | @CheckForNull |
| 35 | public Integer cachedDescriptionVersion; |
| 36 | |
| 37 | @Column(name = "cached_description_html", nullable = true) |
| 38 | @CheckForNull |
| 39 | public String cachedDescriptionHtml; |
| 40 | |
| 41 | @ManyToOne(fetch = FetchType.LAZY, targetEntity = Post.class) |
| 42 | @JoinColumn(name = "post", referencedColumnName = "id", nullable = false) |
| 43 | @JsonbTransient |
| 44 | public OwningPost post; |
| 45 | |
| 46 | @CheckForNull |
| 47 | public final String getDescriptionHtml() { |
| 48 | if (cachedDescriptionHtml != null |
| 49 | && cachedDescriptionVersion != null |
| 50 | && cachedDescriptionVersion >= DESCRIPTION_CACHE_VERSION) { |
| 51 | return cachedDescriptionHtml; |
| 52 | } else { |
| 53 | @CheckForNull var descriptionHtml = computeDescriptionHtml(); |
| 54 | cachedDescriptionHtml = descriptionHtml; |
| 55 | cachedDescriptionVersion = DESCRIPTION_CACHE_VERSION; |
| 56 | return descriptionHtml; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @CheckForNull |
| 61 | protected abstract String computeDescriptionHtml(); |
| 62 | } |