package de.learny.domain;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import de.learny.JsonView.View;
@Entity
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonView(View.Summary.class)
private long id;
@Column(unique=true, nullable=false)
@JsonView(View.Summary.class)
private String accountName;
@JsonView(View.Summary.class)
private String firstname, lastname;
@Column(unique=true, nullable=false)
private String email;
private String password, avatarUri, myNote;
@ManyToMany
private Set<Role> roles = new HashSet<Role>();
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<TestScore> testScores = new HashSet<TestScore>();
@ManyToMany
private Set<Subject> administratedSubjects = new HashSet<Subject>();
@OneToMany
private Set<Achievement> achievements = new HashSet<Achievement>();
@ManyToMany
private Set <Subject> joinedSubjects = new HashSet<Subject>();
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
private PasswordResetToken passwordResetToken;
public Account(String accountName, String password) {
this.accountName = accountName;
this.password = password;
}
public Account() {
}
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatarUri() {
return avatarUri;
}
public void setAvatarUri(String avatarUri) {
this.avatarUri = avatarUri;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
roles.add(role);
}
public long getId() {
return id;
}
@JsonIgnore
public PasswordResetToken getPasswordResetToken() {
return passwordResetToken;
}
public void setPasswordResetToken(PasswordResetToken passwordResetToken) {
this.passwordResetToken = passwordResetToken;
}
public String getAccountName() {
return accountName;
}
@JsonIgnore
public Set<TestScore> getTestScores() {
return testScores;
}
@JsonIgnore
public Set<Subject> getAdministratedSubjects() {
return Collections.unmodifiableSet(administratedSubjects);
}
public boolean addAdministratedSubject(Subject subject) {
this.administratedSubjects.add(subject);
if (!subject.getAccountsInCharge().contains(this)) {
subject.addAccountInCharge(this);
}
return true;
}
public boolean removeAdministratedSubject(Subject subject) {
this.administratedSubjects.remove(subject);
if(subject.getAccountsInCharge().contains(this)) {
subject.removeAccountInCharge(this);
}
return true;
}
public Set<Achievement> getAchievements() {
return Collections.unmodifiableSet(achievements);
}
public boolean addAchievement(Achievement achievement) {
return this.achievements.add(achievement);
}
public boolean removeAchievement(Achievement achievement) {
return this.achievements.remove(achievement);
}
@JsonIgnore
public Set<Subject> getJoinedSubjects() {
return Collections.unmodifiableSet(joinedSubjects);
}
public boolean addJoinedSubject(Subject subject) {
this.joinedSubjects.add(subject);
if(!subject.getParticipants().contains(this)) {
subject.addParticipant(this);
}
return true;
}
public boolean removeJoinedSubject(Subject subject) {
this.joinedSubjects.remove(subject);
if(subject.getParticipants().contains(this)) {
subject.removeParticipant(this);
}
return true;
}
@Override
public String toString() {
String result = "";
result += "\nAccountName: " + accountName;
result += "\nVorname: " + firstname;
result += "\nNachname: " + lastname;
result += "\nE-Mail: " + email;
result += "\nPasswort: " + password;
return result;
}
public boolean hasRole(String role){
for(Role x : this.getRoles()){
if(x.getName().equals(role)){
return true;
}
}
return false;
}
public String getMyNote() {
return myNote;
}
public void setMyNote(String myNote) {
this.myNote = myNote;
}
}