~peter-kovac11/play/play-scala-console-improvements

« back to all changes in this revision

Viewing changes to samples-and-tests/yabe-with-scala/app/models/Post.java

  • Committer: guillaume
  • Date: 2009-11-11 17:19:54 UTC
  • Revision ID: guillaume@macbook-pro-de-guillaume.local-20091111171954-1z761dqr0aw93747
yabe-with-scala

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package models;
2
 
 
3
 
import java.util.*;
4
 
import javax.persistence.*;
5
 
 
6
 
import play.db.jpa.*;
7
 
import play.data.validation.*;
8
 
 
9
 
@Entity
10
 
public class Post extends Model {
11
 
 
12
 
    @Required
13
 
    public String title;
14
 
    
15
 
    @Required
16
 
    public Date postedAt;
17
 
    
18
 
    @Lob
19
 
    @Required
20
 
    @MaxSize(10000)
21
 
    public String content;
22
 
    
23
 
    @Required
24
 
    @ManyToOne
25
 
    public User author;
26
 
    
27
 
    @OneToMany(mappedBy="post", cascade=CascadeType.ALL)
28
 
    public List<Comment> comments;
29
 
    
30
 
    @ManyToMany(cascade=CascadeType.PERSIST)
31
 
    public Set<Tag> tags;
32
 
    
33
 
    public Post(User author, String title, String content) { 
34
 
        this.comments = new ArrayList<Comment>();
35
 
        this.tags = new TreeSet();  
36
 
        this.author = author;
37
 
        this.title = title;
38
 
        this.content = content;
39
 
        this.postedAt = new Date();
40
 
    }
41
 
    
42
 
    public Post addComment(String author, String content) {
43
 
        Comment newComment = new Comment(this, author, content).save();
44
 
        this.comments.add(newComment);
45
 
        return this;
46
 
    }
47
 
    
48
 
    public Post previous() {
49
 
        return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
50
 
    }
51
 
 
52
 
    public Post next() {
53
 
        return Post.find("postedAt > ? order by postedAt asc", postedAt).first();
54
 
    }
55
 
    
56
 
    public Post tagItWith(String name) {
57
 
        tags.add(Tag.findOrCreateByName(name));
58
 
        return this;
59
 
    }
60
 
    
61
 
    public static List<Post> findTaggedWith(String tag) {
62
 
        return Post.find(
63
 
            "select distinct p from Post p join p.tags as t where t.name = ?",
64
 
            tag
65
 
        ).fetch();
66
 
    }
67
 
    
68
 
    public static List<Post> findTaggedWith(String... tags) {
69
 
        return Post.find(
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();
72
 
    }
73
 
    
74
 
    public String toString() {
75
 
        return title;
76
 
    }
77
 
 
78
 
}
 
 
b'\\ No newline at end of file'