This repository was archived by the owner on Mar 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 691
/
Copy pathPost.java
91 lines (69 loc) · 2.61 KB
/
Post.java
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
87
88
89
90
91
package com.raysmond.blog.models;
import com.raysmond.blog.models.support.PostFormat;
import com.raysmond.blog.models.support.PostStatus;
import com.raysmond.blog.models.support.PostType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.springframework.util.StringUtils;
import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;
/**
* @author Raysmond
*/
@Entity
@Table(name = "posts",
indexes = {@Index(name = "posts_post_type", columnList = "post_type"),
@Index(name = "posts_permalink", columnList = "permalink")})
@Getter
@Setter
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "postCache")
public class Post extends BaseModel {
private static final SimpleDateFormat SLUG_DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
@ManyToOne
private User user;
@Column(nullable = false)
private String title;
@Type(type = "text")
private String content;
@Type(type = "text")
private String renderedContent;
@Type(type = "text")
private String summary;
@Type(type = "text")
private String renderedSummary;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private PostStatus postStatus = PostStatus.PUBLISHED;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private PostFormat postFormat = PostFormat.MARKDOWN;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private PostType postType = PostType.POST;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "posts_tags",
joinColumns = {@JoinColumn(name = "post_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "tag_id", nullable = false, updatable = false)}
)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "tagCache")
private Set<Tag> tags = new HashSet<>();
private String permalink;
private Integer views = 0;
public Integer getViews() {
return views == null ? 0 : views;
}
public String getRenderedContent() {
if (this.postFormat == PostFormat.MARKDOWN) {
return renderedContent;
}
return getContent();
}
public void setPermalink(String permalink) {
String token = permalink.toLowerCase().replace("\n", " ").replaceAll("[^a-z\\d\\s]", " ");
this.permalink = StringUtils.arrayToDelimitedString(StringUtils.tokenizeToStringArray(token, " "), "-");
}
}