| 1 | 6 | 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 Tag extends Model implements Comparable<Tag> { |
| 11 | |
|
| 12 | |
@Required |
| 13 | |
public String name; |
| 14 | |
|
| 15 | 9 | private Tag(String name) { |
| 16 | 9 | this.name = name; |
| 17 | 9 | } |
| 18 | |
|
| 19 | |
public static Tag findOrCreateByName(String name) { |
| 20 | 12 | Tag tag = Tag.find("byName", name).first(); |
| 21 | 12 | if(tag == null) { |
| 22 | 9 | tag = new Tag(name); |
| 23 | |
} |
| 24 | 12 | return tag; |
| 25 | |
} |
| 26 | |
|
| 27 | |
public static List<Map> getCloud() { |
| 28 | 6 | List<Map> result = Tag.find( |
| 29 | 3 | "select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name" |
| 30 | 3 | ).fetch(); |
| 31 | 3 | return result; |
| 32 | |
} |
| 33 | |
|
| 34 | |
public String toString() { |
| 35 | 10 | return name; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public int compareTo(Tag otherTag) { |
| 39 | 6 | return name.compareTo(otherTag.name); |
| 40 | |
} |
| 41 | |
|
| 42 | |
} |