001package model.entity;
002
003import lombok.Getter;
004import lombok.Setter;
005import org.hibernate.annotations.NaturalId;
006
007import javax.persistence.*;
008import java.io.Serializable;
009import java.time.Instant;
010import java.util.ArrayList;
011import java.util.Collections;
012import java.util.List;
013
014/**
015 * Entità rappresentate un utente della community
016 */
017@Entity
018public class User implements Serializable {
019
020    @Getter @Setter
021    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
022    protected Integer id;
023
024    @Getter @Setter
025    @NaturalId(mutable = false) @Column(length = 30, unique = true, nullable = false)
026    protected String username;
027
028    @Getter @Setter
029    @Column(length = 16, nullable = false)
030    protected byte[] password;
031
032    @Getter @Setter
033    @Column(length = 16, nullable = false)
034    protected byte[] salt;
035
036    @Getter @Setter
037    @Column(length = 255, unique = true, nullable = false)
038    protected String email;
039
040    @Getter @Setter
041    @Column(length = 255)
042    protected String description;
043
044    @Getter @Setter
045    @Column(length = 4096)
046    protected String picture;
047
048    @Getter
049    @Column(insertable = false, updatable = false, nullable = false)
050    protected Instant creationDate;
051
052    @Getter @Setter
053    @Column(nullable = false)
054    protected Boolean admin;
055
056    @OneToMany(mappedBy = "user")
057    @OrderBy("endTime desc")
058    protected List<Ban> bans = new ArrayList<>();
059    public List<Ban> getBans(){
060        return Collections.unmodifiableList(bans);
061    }
062
063    public User(){}
064
065    @Override
066    public boolean equals(Object o) {
067        if (this == o) return true;
068        if (!(o instanceof User)) return false;
069        User user = (User) o;
070        return id.equals(user.id);
071    }
072
073    @Override
074    public int hashCode() {
075        return getClass().hashCode();
076    }
077}