4
import javax.persistence.*;
7
import play.data.validation.*;
10
public class Post extends Model {
21
public String content;
27
@OneToMany(mappedBy="post", cascade=CascadeType.ALL)
28
public List<Comment> comments;
30
@ManyToMany(cascade=CascadeType.PERSIST)
33
public Post(User author, String title, String content) {
34
this.comments = new ArrayList<Comment>();
35
this.tags = new TreeSet();
38
this.content = content;
39
this.postedAt = new Date();
42
public Post addComment(String author, String content) {
43
Comment newComment = new Comment(this, author, content).save();
44
this.comments.add(newComment);
48
public Post previous() {
49
return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
53
return Post.find("postedAt > ? order by postedAt asc", postedAt).first();
56
public Post tagItWith(String name) {
57
tags.add(Tag.findOrCreateByName(name));
61
public static List<Post> findTaggedWith(String tag) {
63
"select distinct p from Post p join p.tags as t where t.name = ?",
68
public static List<Post> findTaggedWith(String... tags) {
70
"select distinct p.id from Post p join p.tags as t where t.name in (:tags) group by p.id having count(t.id) = :size"
71
).bind("tags", tags).bind("size", tags.length).fetch();
74
public String toString() {
b'\\ No newline at end of file'