daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Sep 20th, 2018 59 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package models;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import play.*;
  9.  
  10. import play.data.validation.Required;
  11. import siena.DateTime;
  12. import siena.Id;
  13. import siena.Index;
  14. import siena.Model;
  15. import siena.Query;
  16. import siena.Table;
  17. import siena.core.Aggregated;
  18. import siena.core.Many;
  19. import siena.core.Owned;
  20. import siena.Text;
  21.  
  22. public class Post extends Model {
  23.    
  24.     @Id
  25.     public Long id;
  26.    
  27.     @Required
  28.     public String title;
  29.    
  30.     @Required
  31.     @DateTime
  32.     public Date postedAt;
  33.    
  34.     @Text
  35.     @Required
  36.     public String content;
  37.    
  38.     @Required
  39.     @Index("author_index")
  40.     public User author;
  41.    
  42.     @Owned(mappedBy="post")
  43.     public Many<Comment> comments;                                
  44.  
  45.     @Aggregated
  46.     public Many<Tag> tags;
  47.    
  48.     public Post(String title, User author, String content) {
  49.         this.author = author;
  50.         this.title = title;
  51.         this.content = content;
  52.         this.postedAt = new Date();    
  53.     }
  54.    
  55.     public Post addComment(String author, String content) {
  56.         Comment newComment = new Comment(this, author, content);
  57.         this.comments.asList().add(newComment);    
  58.         this.save();
  59.         this.update();
  60.         return this;
  61.     }
  62.    
  63.     public static Query<Post> all() {
  64.         return Model.all(Post.class);
  65.     }
  66.     public Post previous () {
  67.         return all().order("-postedAt").filter("postedAt<", postedAt).get();
  68.     }
  69.    
  70.     public Post next() {
  71.         return all().order("postedAt").filter("postedAt>", postedAt).get();
  72.     }  
  73.    
  74.     public Post tagItWith(String name) {
  75.         tags.asList().add(Tag.findOrCreateByName(name));
  76.         return this;
  77.     }
  78.    
  79.     public static List<Post> findTaggedWith(String tag) {
  80.         List <Post> posts = all().fetch();
  81.         List <Post> result = new ArrayList();
  82.        
  83.         Iterator<Post> postIterator = posts.iterator();
  84.         while(postIterator.hasNext())
  85.         {
  86.             if(postIterator.next().tags.asQuery().filter("name", tag).count() >= 1)
  87.                 result.add(postIterator.next());           
  88.         }
  89.         return result;
  90.     }
  91.    
  92.     public String toString() {
  93.         return this.title;
  94.     }
  95.    
  96.     public static Post findById(Long id) {
  97.         return all().filter("id", id).get();
  98.     }
  99.     public static int count() {
  100.         return all().fetch().size();
  101.     }
  102.    
  103.     public List<Comment> findComments(){
  104.         return this.comments.asList();
  105.     }
  106.     public List<Tag> findTags() {      
  107.         return this.tags.asList();
  108.     }
  109.     public Long getId() {
  110.         return id;
  111.     }
  112.    
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top