~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/Tag.scala

  • 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
import play.db.jpa.QueryFunctions._
 
9
 
 
10
@Entity
 
11
class Tag extends Model with Comparable[Tag] {
 
12
 
 
13
    @Required
 
14
    var name:String = _
 
15
    
 
16
    private def this(name: String) {
 
17
        this()
 
18
        this.name = name
 
19
    }
 
20
    
 
21
    override def toString() = {
 
22
        name
 
23
    }
 
24
    
 
25
    override def compareTo(otherTag: Tag) = {
 
26
        name.compareTo(otherTag.name)
 
27
    }
 
28
 
 
29
}
 
30
 
 
31
object Tag {
 
32
    
 
33
    def allTags = findAll[Tag]
 
34
    
 
35
    def findOrCreateByName(name: String) = {
 
36
        var tag = find[Tag]("byName", name).first
 
37
        if(tag == null) {
 
38
            tag = new Tag(name)
 
39
        }
 
40
        tag
 
41
    }
 
42
    
 
43
    def cloud = {
 
44
        find[Tag](
 
45
            "select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name"
 
46
        ).fetch
 
47
    }
 
48
    
 
49
}
 
 
b'\\ No newline at end of file'