001package model.entity;
002
003import lombok.Getter;
004import lombok.Setter;
005
006import javax.persistence.*;
007import java.io.Serializable;
008import java.util.Objects;
009
010/**
011 * Entità rappresentate il voto ad un commento
012 */
013@Entity
014public class PostVote implements Serializable {
015
016    @SuppressWarnings("JpaDataSourceORMInspection") //bug IDEA-223439
017    @Embeddable
018    public static class Id implements Serializable{
019
020        @Getter
021        @Column(name = "user_id", nullable = false)
022        protected int userId;
023
024        @Getter
025        @Column(name = "post_id", nullable = false)
026        protected int postId;
027
028        protected Id(){}
029
030        public Id(int userId, int postId) {
031            this.userId = userId;
032            this.postId = postId;
033        }
034
035        @Override
036        public boolean equals(Object o) {
037            if (this == o) return true;
038            if (!(o instanceof PostVote.Id)) return false;
039            PostVote.Id id = (PostVote.Id) o;
040            return userId == id.userId && postId == id.postId;
041        }
042
043        @Override
044        public int hashCode() {
045            return Objects.hash(userId, postId);
046        }
047    }
048
049    @Getter
050    @EmbeddedId
051    protected PostVote.Id id = new PostVote.Id();
052
053    @Getter
054    @ManyToOne(optional = false) @MapsId("postId")
055    protected Post post;
056    public void setPost(Post post){
057        this.post = post;
058        this.id.postId = post.getId();
059    }
060
061    @Getter
062    @ManyToOne(optional = false) @MapsId("userId")
063    protected User user;
064    public void setUser(User user){
065        this.user = user;
066        this.id.userId = user.getId();
067    }
068
069    @Setter
070    @Getter
071    @Column(nullable = false)
072    protected Short vote;
073
074    protected PostVote(){}
075
076    public PostVote(User user, Post post, Short vote) {
077        this.user = user;
078        this.post = post;
079        this.vote = vote;
080        this.id = new PostVote.Id(user.getId(), post.getId());
081    }
082
083    @Override
084    public boolean equals(Object o) {
085        if (this == o) return true;
086        if (!(o instanceof PostVote)) return false;
087        PostVote that = (PostVote) o;
088        return id.equals(that.id);
089    }
090
091    @Override
092    public int hashCode() {
093        return Objects.hash(id);
094    }
095}