/* * 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.Collection; import java.util.Date; import javax.persistence.Basic; 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.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.TableGenerator; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; /** * * @author madis */ @XmlRootElement @Entity @Table(name = "resource_folder") @NamedQueries({ @NamedQuery(name = "ResourceFolder.findAll", query = "SELECT r FROM ResourceFolder r"), @NamedQuery(name = "ResourceFolder.findById", query = "SELECT r FROM ResourceFolder r WHERE r.id = :id"), @NamedQuery(name = "ResourceFolder.findByTitle", query = "SELECT r FROM ResourceFolder r WHERE r.title = :title"), @NamedQuery(name = "ResourceFolder.findByPosition", query = "SELECT r FROM ResourceFolder r WHERE r.position = :position"), @NamedQuery(name = "ResourceFolder.rootSubfolders", query = "SELECT r FROM ResourceFolder r WHERE r.course = :course AND r.parentFolder IS NULL ORDER BY r.position ASC"), @NamedQuery(name = "ResourceFolder.subfolders", query = "SELECT r FROM ResourceFolder r WHERE r.parentFolder = :parent ORDER BY r.position ASC") // @NamedQuery(name = "ResourceFolder.findByContent", query = "SELECT r FROM ResourceFolder r WHERE r.content = :content"), }) public class ResourceFolder implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "LearningResourceFolderID") @TableGenerator(name="LearningResourceFolderID", table="id_gen", pkColumnName="ID_NAME", valueColumnName = "ID_VAL", pkColumnValue="LAST_RESOURCE_FOLDER", allocationSize=1) @Basic(optional = false) @Column(name = "id") private Long id; @Basic(optional = false) @Column(name = "title") private String title; @Basic(optional = false) @Column(name = "position") private int position; @JoinColumn(name = "creator", referencedColumnName = "id") @ManyToOne(optional = false, fetch = FetchType.LAZY) private User creator; @Basic(optional = false) @Column(name = "created") @Temporal(TemporalType.TIMESTAMP) private Date created; @Basic(optional = false) @Column(name = "modified") @Temporal(TemporalType.TIMESTAMP) private Date modified; @JoinColumn(name = "course", referencedColumnName = "id") @ManyToOne(optional = false, fetch = FetchType.LAZY) private Course course; @Transient @XmlElement(name="course-id") public Long courseId; // Relation between resource and its folder @OneToMany(mappedBy = "resourceFolder") private Collection learningResources; @OneToMany(mappedBy = "parentFolder") private Collection subfolders; @JoinColumn(name = "parent", referencedColumnName = "id") @ManyToOne(optional = false, fetch = FetchType.LAZY) private ResourceFolder parentFolder; @Transient @XmlElement(name="parentfolder-id") public Long parentFolderId; // TODO Transient for folder/resource position changes @Transient @XmlElementWrapper(name="resource-ids") @XmlElement(name="resource-id") public Collection resourceIds; @Transient @XmlElementWrapper(name="subfolder-ids") @XmlElement(name="subfolder-id") public Collection subfolderIds; public ResourceFolder() { } public ResourceFolder(Long id) { this.id = id; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public User getCreator() { return creator; } public void setCreator(User user) { this.creator = user; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getModified() { return modified; } public void setModified(Date modified) { this.modified = modified; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; if ( !this.course.getResourceFolders().contains(this) ) { this.course.addResourceFolder(this); } } public Long getCourseId() { return courseId; } public Long getParentFolderId() { return parentFolderId; } public Collection getLearningResources() { return learningResources; } public void setLearningResources(Collection learningResources) { this.learningResources = learningResources; } public Collection getSubfolders() { return subfolders; } public void setSubfolders(Collection subfolders) { this.subfolders = subfolders; } public ResourceFolder getParentFolder() { return parentFolder; } public void setParentFolder(ResourceFolder resourceFolder) { this.parentFolder = resourceFolder; } public Collection getResourceIds() { return resourceIds; } public Collection getSubfolderIds() { return subfolderIds; } @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 ResourceFolder)) { return false; } ResourceFolder other = (ResourceFolder) 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.ResourceFolder[ id=" + id + " ]"; } }