View Javadoc
1   package model.entity;
2   
3   import lombok.Getter;
4   import lombok.Setter;
5   
6   import javax.persistence.*;
7   import java.io.Serializable;
8   import java.time.Instant;
9   import java.util.Map;
10  
11  /**
12   * Entità rappresentante il commento ad un post
13   */
14  @Entity
15  public class Comment implements Serializable {
16  
17      @Setter @Getter
18      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
19      protected Integer id;
20  
21      @Setter @Getter
22      @ManyToOne(fetch = FetchType.LAZY, optional = false)
23      protected Post post;
24  
25      @Setter @Getter
26      @ManyToOne(fetch = FetchType.LAZY, optional = false)
27      protected User author;
28  
29      /**
30       * <p>Riferimento al commento padre (o <pre>null</pre> se il commento in questione è un commento radice</p>
31       */
32      @Setter @Getter
33      @ManyToOne(fetch = FetchType.LAZY, optional = true)
34      protected Comment parentComment;
35  
36      @Setter @Getter
37      @Column(columnDefinition = "TEXT", nullable = false)
38      protected String content;
39  
40      @Getter
41      @Column(nullable = false, updatable = false, insertable = false)
42      protected Instant creationDate;
43  
44      @Getter
45      @Column(name="votes", insertable = false, updatable = false)
46      protected Integer votesCount;
47  
48      /**
49       * <p>Il percorso materializzato del commento, contenente tutti gli ID (in base 36) dei commenti che, da
50       * sinistra verso destra, costituiscono il percorso per arrivare al commento in questione a partire dalla radice.</p>
51       */
52      @Getter
53      @Column(insertable = false, updatable = false)
54      protected String path;
55  
56      @OneToMany(mappedBy="comment")
57      @MapKeyJoinColumn(name="user_id", updatable = false, insertable = false)
58      protected Map<User, CommentVote> votes;
59  
60      /**
61       * Ottieni il voto di un utente al commento in questione (o <pre>null</pre> se il voto non è presente)
62       * @param user Riferimento all'utente
63       * @return Un oggetto {@link CommentVote}
64       */
65      public CommentVote getVote(User user){
66          return votes.get(user);
67      }
68  
69      public Comment(){}
70  
71      @Override
72      public boolean equals(Object o) {
73          if (this == o) return true;
74          if (!(o instanceof Comment)) return false;
75          Comment comment = (Comment) o;
76          return id.equals(comment.id);
77      }
78  
79      @Override
80      public int hashCode() {
81          return getClass().hashCode();
82      }
83  }