134 lines
3.4 KiB
Java
134 lines
3.4 KiB
Java
package com.webproglabs.lab1.lab34.model;
|
|
|
|
import javax.persistence.*;
|
|
import javax.validation.constraints.NotBlank;
|
|
import javax.validation.constraints.Size;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
@Entity
|
|
@Table(name="tab_user")
|
|
public class Profile {
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
private Long id;
|
|
|
|
@Column(nullable = false, unique = true, length = 64)
|
|
@NotBlank
|
|
@Size(min = 3, max = 64)
|
|
private String login;
|
|
|
|
@Column(nullable = false, length = 64)
|
|
@NotBlank
|
|
@Size(min = 6, max = 64)
|
|
private String password;
|
|
|
|
private UserRole role;
|
|
|
|
public UserRole getRole() {
|
|
return role;
|
|
}
|
|
|
|
@OneToMany(mappedBy = "owner", orphanRemoval = true, fetch = FetchType.EAGER)
|
|
private List<Comment> comments = new ArrayList<Comment>();
|
|
|
|
@OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER)
|
|
private List<Post> posts = new ArrayList<Post>();
|
|
|
|
public Profile(){}
|
|
public Profile(String login, String password, List<Comment> comments, List<Post> posts) {
|
|
this.login = login;
|
|
this.password=password;
|
|
this.role = UserRole.USER;
|
|
for (int i = 0; i < comments.size(); i++) {
|
|
addComment(comments.get(i));
|
|
}
|
|
for (int i = 0; i < posts.size(); i++) {
|
|
addPost(posts.get(i));
|
|
}
|
|
};
|
|
public Profile(String login, String password, List<Comment> comments, List<Post> posts, UserRole role) {
|
|
this.login = login;
|
|
this.password=password;
|
|
this.role = role;
|
|
for (int i = 0; i < comments.size(); i++) {
|
|
addComment(comments.get(i));
|
|
}
|
|
for (int i = 0; i < posts.size(); i++) {
|
|
addPost(posts.get(i));
|
|
}
|
|
};
|
|
|
|
public Long getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getLogin() {
|
|
return login;
|
|
}
|
|
|
|
public void setLogin(String login) {
|
|
this.login = login;
|
|
}
|
|
|
|
public String getPassword() {
|
|
return password;
|
|
}
|
|
|
|
public void setPassword(String password) {
|
|
this.password = password;
|
|
}
|
|
|
|
public List<Comment> getComments() { return comments; }
|
|
|
|
public List<Post> getPosts() {return posts; }
|
|
|
|
public int getCommentsSize() {
|
|
if (comments == null) return 0;
|
|
else return comments.size();
|
|
}
|
|
|
|
public int getPostsSize() {
|
|
if (posts == null) return 0;
|
|
else return posts.size();
|
|
}
|
|
|
|
public void addComment(Comment comment) {
|
|
this.comments.add(comment);
|
|
if (comment.getOwner() != this) {
|
|
comment.setOwner(this);
|
|
}
|
|
}
|
|
|
|
public void addPost(Post post) {
|
|
this.posts.add(post);
|
|
if (post.getAuthor() != this) {
|
|
post.setAuthor(this);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
Profile user = (Profile) o;
|
|
return Objects.equals(id, user.id);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(id);
|
|
}
|
|
|
|
public String ToString() {
|
|
return "User{" +
|
|
"id=" + id +
|
|
", login='" + login + '\'' +
|
|
", password='" + password + '\'' +
|
|
", comments='" + getCommentsSize() + '\'' +
|
|
", comments='" + getPostsSize() + '\'' +
|
|
'}';
|
|
}
|
|
}
|