/* * 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.JoinTable; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.OneToOne; 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 = "learning_resource") @NamedQueries({ @NamedQuery(name = "LearningResource.findAll", query = "SELECT r FROM LearningResource r"), @NamedQuery(name = "LearningResource.findById", query = "SELECT r FROM LearningResource r WHERE r.id = :id"), @NamedQuery(name = "LearningResource.findByTitle", query = "SELECT r FROM LearningResource r WHERE r.title = :title"), // TODO findbyurl? @NamedQuery(name = "LearningResource.findByCreated", query = "SELECT r FROM LearningResource r WHERE r.created = :created"), @NamedQuery(name = "LearningResource.findByModified", query = "SELECT r FROM LearningResource r WHERE r.modified = :modified"), @NamedQuery(name = "LearningResource.findByPosition", query = "SELECT r FROM LearningResource r WHERE r.position = :position"), @NamedQuery(name = "LearningResource.findByCourse", query = "SELECT r FROM LearningResource r WHERE r.course = :course AND r.resourceFolder IS NULL ORDER BY r.position ASC"), @NamedQuery(name = "LearningResource.findByFolder", query = "SELECT r FROM LearningResource r WHERE r.resourceFolder = :parent ORDER BY r.position ASC") }) public class LearningResource implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "LearningResourceID") @TableGenerator(name="LearningResourceID", table="id_gen", pkColumnName="ID_NAME", valueColumnName = "ID_VAL", pkColumnValue="LAST_LEARNING_RESOURCE", allocationSize=1) @Basic(optional = false) @Column(name = "id") private Long id; @Basic(optional = false) @Column(name = "title") private String title; @Column(name = "description") private String description; @Column(name = "type") private String type; @Column(name = "subtype") private String subtype; @Column(name = "url") private String url; @Column(name = "author") private String author; @JoinColumn(name = "creator", referencedColumnName = "id") @OneToOne(optional = false, fetch = FetchType.LAZY) private User creator; @Column(name = "licence") private String licence; @Column(name = "published") private int published; @Basic(optional = false) @Column(name = "created") @Temporal(TemporalType.TIMESTAMP) private Date created; @Basic(optional = false) @Column(name = "modified") @Temporal(TemporalType.TIMESTAMP) private Date modified; // TODO Relation between resource and its course @JoinColumn(name="course", referencedColumnName = "id") @ManyToOne(optional=false, fetch = FetchType.LAZY) private Course course; @Transient @XmlElement(name="course-id") public Long courseId; @Basic(optional = false) @Column(name = "position") private int position; @JoinColumn(name = "folder", referencedColumnName = "id") @ManyToOne private ResourceFolder resourceFolder; @Transient @XmlElement(name="folder-id") public Long folderId; @Transient @XmlElementWrapper(name="tags") @XmlElement(name="tag") public Collection tags; @OneToMany @JoinTable(name = "category_relationship", joinColumns = { @JoinColumn(name = "parent_id", referencedColumnName = "id"), @JoinColumn(name = "parent_type", referencedColumnName = "type") }, inverseJoinColumns = { @JoinColumn(name = "category_id", referencedColumnName = "id") } ) private Collection categories; @Transient @XmlElementWrapper(name="categoryIds") @XmlElement(name="category") public Collection categoryIds; public LearningResource() { } public LearningResource(Long id) { this.id = id; } /* public LearningResource(Long id, String title, String content, String url, User author, Date created, Date modified, Course course, int weight) { this.id = id; this.title = title; this.content = content; this.url = url; this.author = author; this.created = created; this.modified = modified; this.course = course; this.weight = weight; } */ 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 String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public User getCreator() { return creator; } public void setCreator(User creator) { this.creator = creator; } public String getLicence() { return licence; } public void setLicence(String licence) { this.licence = licence; } public int getPublished() { return published; } public void setPublished(int published) { this.published = published; } 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.getLearningResources().contains(this) ) { this.course.addLearningResource(this); } } public Long getCourseId() { return courseId; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public ResourceFolder getResourceFolder() { return resourceFolder; } public void setResourceFolder(ResourceFolder resourceFolder) { this.resourceFolder = resourceFolder; } public Long getFolderId() { return folderId; } public void setTags(Collection tags) { this.tags = tags; } public String getSubtype() { return subtype; } public void setSubtype(String subtype) { this.subtype = subtype; } public Collection getCategories() { return categories; } public void setCategories(Collection categories) { this.categories = categories; } public void setCategoryIds(Collection categoryIds) { this.categoryIds = categoryIds; } /*public final Collection getTagRelationships() { List tagrelationships = new LinkedList(); try { Query trListing = em.createNativeQuery("SELECT * FROM tag_relationship tr WHERE tr.parent_id=?1 && tr.parent_type='LearningResource'", TagRelationship.class); trListing.setParameter(1, this.getId().toString()); tagrelationships = (List) trListing.getResultList(); } catch (Exception e){ } return tagrelationships; }*/ @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 LearningResource)) { return false; } LearningResource other = (LearningResource) 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.LearningResource[ id=" + id + " ]"; } }