/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.entities; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.TableGenerator; import javax.persistence.Transient; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * * @author metz */ @XmlRootElement @Entity @Table(name = "user") @NamedQueries({ @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"), @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"), @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"), @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"), @NamedQuery(name = "User.findBySalt", query = "SELECT u FROM User u WHERE u.salt = :salt"), @NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"), @NamedQuery(name = "User.findByFirstname", query = "SELECT u FROM User u WHERE u.firstname = :firstname"), @NamedQuery(name = "User.findByLastname", query = "SELECT u FROM User u WHERE u.lastname = :lastname"), @NamedQuery(name = "User.findByLevel", query = "SELECT u FROM User u WHERE u.level = :level"), @NamedQuery(name = "User.findByApproved", query = "SELECT u FROM User u WHERE u.approved = :approved"), @NamedQuery(name = "User.findAvailableFacilitators", query = "SELECT u FROM User u WHERE :organization MEMBER OF u.organizations AND (u.email LIKE :term OR u.firstname LIKE :term OR u.lastname LIKE :term OR CONCAT(u.firstname, ' ', u.lastname) LIKE :term) AND (u.approved = true AND u.level >= 50 AND u.id NOT IN (SELECT f.user_id FROM Facilitator f WHERE f.course = :course))"), @NamedQuery(name = "User.findAvailableLearners", query = "SELECT u FROM User u WHERE :organization MEMBER OF u.organizations AND (u.email LIKE :term OR u.firstname LIKE :term OR u.lastname LIKE :term OR CONCAT(u.firstname, ' ', u.lastname) LIKE :term) AND (u.approved = true AND u.level >= 10 AND u.id NOT IN (SELECT l.user.id FROM Learner l WHERE l.course = :course))"), @NamedQuery(name = "User.searchUsers", query = "SELECT u FROM User u WHERE (u.email LIKE :term OR u.firstname LIKE :term OR u.lastname LIKE :term OR CONCAT(u.firstname, ' ', u.lastname) LIKE :term) AND u.approved = true"), @NamedQuery(name = "User.findUsersByApproved", query = "SELECT u FROM User u WHERE :organization MEMBER OF u.organizations AND u.approved = :approved"), @NamedQuery(name = "User.countUsersByApproved", query = "SELECT COUNT(u) FROM User u WHERE :organization MEMBER OF u.organizations AND u.approved = :approved"), @NamedQuery(name = "User.findOrganizationAdmins", query = "SELECT u FROM User u WHERE :organization MEMBER OF u.organizations AND u.approved = TRUE AND u.level = 95") }) public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "UserID") @TableGenerator(name="UserID", table="id_gen", pkColumnName="ID_NAME", valueColumnName = "ID_VAL", pkColumnValue="LAST_USER_ID", allocationSize=1) @Basic(optional = false) @Column(name = "id") private Long id; @Basic(optional = false) @Column(name = "email") private String email; @Basic(optional = false) @Column(name = "password") private String password; @Basic(optional = false) @Column(name = "salt") private String salt; @Basic(optional = false) @Column(name = "username") private String username; @Column(name = "firstname") private String firstname; @Column(name = "lastname") private String lastname; @Basic(optional = false) @Column(name = "level") private int level; @Basic(optional = false) @Column(name = "approved") private boolean approved; @OneToMany(mappedBy = "user", cascade= CascadeType.PERSIST, orphanRemoval=true) private Collection facilitators = new ArrayList(); /*@ManyToMany @JoinTable(name = "learner", joinColumns = @JoinColumn(name = "learner", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "course", referencedColumnName = "id") )*/ /*@OneToMany (mappedBy = "user") private Collection learningCourses;*/ @ManyToMany @JoinTable(name = "user_organization", joinColumns = @JoinColumn(name = "user", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "organization", referencedColumnName = "id")) private Collection organizations; @Transient private Client client; // TODO Check again @OneToMany(mappedBy = "creator", fetch = FetchType.LAZY) private Collection resourceFolders; @OneToMany(mappedBy = "creator", fetch = FetchType.LAZY) private Collection learningResources; @Transient @XmlElement(name="new_password") private String new_password; public User() { } public User(Long id) { this.id = id; } public User(Long id, String email, String password, String salt, String username, int level, boolean approved) { this.id = id; this.email = email; this.password = password; this.salt = salt; this.username = username; this.level = level; this.approved = approved; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * Return boolean isUserSession value */ public boolean inUserSession() { if ( this.client == null ) { return true; } return false; } /** * Return is client session */ public boolean inClientSession() { if ( this.client != null ) { return true; } return false; } public Client getClient() { return client; } public void setClient(Client cl) { this.client = cl; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } 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 getFullname() { return firstname + " " + lastname; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public boolean getApproved() { return approved; } public void setApproved(boolean approved) { this.approved = approved; } //Never ever called since 30.10.2012 /* public Collection getLearningCourses() { return learningCourses; } public void setLearningCourses(Collection courses) { this.learningCourses = courses; }*/ public Collection getFacilitators() { return facilitators; } public void setFacilitators(ArrayList facilitators) { this.facilitators = facilitators; } public void addFacilitator(Facilitator facilitator) { this.facilitators.add(facilitator); if ( facilitator.getUser() != this ) { facilitator.setUser(this); } } public void remove(Object o) { if ( o instanceof Facilitator ) { this.facilitators.remove((Facilitator) o); } } public Collection getOrganizations() { return organizations; } public void setOrganizations(Collection orgs) { this.organizations = orgs; } public Collection getResourceFolders() { return resourceFolders; } public void setResourceFolders(Collection resourceFolders) { this.resourceFolders = resourceFolders; } public Collection getLearningResources() { return learningResources; } public void setLearningResources(Collection learningResources) { this.learningResources = learningResources; } public String getNew_password() { return new_password; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof User)) { return false; } User other = (User) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "ee.tlu.htk.dippler.entities.User[id=" + id + "]"; } }