/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.entities; import ee.tlu.htk.dippler.utils.Utility; import java.io.Serializable; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Set; 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.Lob; import javax.persistence.ManyToMany; 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 metz */ @XmlRootElement @Entity @Table(name = "assignment") @NamedQueries({ @NamedQuery(name = "Assignment.findAll", query = "SELECT a FROM Assignment a ORDER BY a.end DESC"), @NamedQuery(name = "Assignment.findById", query = "SELECT a FROM Assignment a WHERE a.id = :id"), @NamedQuery(name = "Assignment.findByTitle", query = "SELECT a FROM Assignment a WHERE a.title = :title ORDER BY a.end DESC"), @NamedQuery(name = "Assignment.findByStart", query = "SELECT a FROM Assignment a WHERE a.start = :start ORDER BY a.start DESC"), @NamedQuery(name = "Assignment.findByEnd", query = "SELECT a FROM Assignment a WHERE a.end = :end ORDER BY a.end DESC"), @NamedQuery(name = "Assignment.findByCourse", query = "SELECT a FROM Assignment a WHERE a.course = :course ORDER BY a.end DESC"), @NamedQuery(name = "Assignment.findByCourse_WP", query = "SELECT a FROM Assignment a WHERE a.start <= :start_date AND a.course = :course ORDER BY a.end DESC"), @NamedQuery(name = "Assignment.findByCreated", query = "SELECT a FROM Assignment a WHERE a.created = :created ORDER BY a.created DESC"), @NamedQuery(name = "Assignment.findByModified", query = "SELECT a FROM Assignment a WHERE a.modified = :modified ORDER BY a.modified DESC")}) public class Assignment implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "AssignmentID") @TableGenerator(name="AssignmentID", table="id_gen", pkColumnName="ID_NAME", valueColumnName = "ID_VAL", pkColumnValue="LAST_ASSIGNMENT", allocationSize=1) @Basic(optional = false) @Column(name = "id") private Long id; @Basic(optional = false) @Column(name = "subtype") private String subtype; @Column(name = "type") private String type; @Basic(optional = false) @Column(name = "title") private String title; @Basic(optional = false) @Lob @Column(name = "description") private String description; @Basic(optional = true) @Lob @Column(name = "structure") private String structure; @Basic(optional = false) @Column(name = "start") private int start; @Basic(optional = false) @Column(name = "end") private int end; @Basic(optional = false) @Column(name = "maxpoints") private int maxpoints; @Basic(optional = false) @Column(name = "created") @Temporal(TemporalType.TIMESTAMP) private Date created = new Date(); @JoinColumn(name = "creator", referencedColumnName = "id") @ManyToOne(optional = false, fetch = FetchType.LAZY) private User creator; @Basic(optional = false) @Column(name = "modified") @Temporal(TemporalType.TIMESTAMP) private Date modified = new Date(); @Basic(optional = false) @Column(name = "late_submission") private boolean late_submission; @Basic(optional = false) @Column(name = "submission_visibility") private boolean submission_visibility; @JoinColumn(name="course", referencedColumnName = "id") @ManyToOne(optional=false, fetch = FetchType.LAZY) private Course course; @JoinTable(name = "assignment_target_group", joinColumns = { @JoinColumn(name = "assignment_id", referencedColumnName = "id")}, inverseJoinColumns = { @JoinColumn(name = "coursegroup_id", referencedColumnName = "id")}) @ManyToMany private Collection target_group; @JoinTable(name = "assignment_learning_outcomes", joinColumns = { @JoinColumn(name = "assignment", referencedColumnName = "id")}, inverseJoinColumns = { @JoinColumn(name = "learningoutcome", referencedColumnName = "id")}) @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) private Set learning_outcomes; @Transient @XmlElement(name="course-id") private Long courseId; @Transient @XmlElementWrapper(name="target-group") @XmlElement(name="group-id") public Collection groupIds; @OneToMany (mappedBy="assignment") @JoinTable(name = "answer", joinColumns = @JoinColumn(name = "assignment"), inverseJoinColumns = @JoinColumn(name = "id") ) private Set answers; @Transient @XmlElementWrapper(name="tags") @XmlElement(name="tag") public Collection tags; @Transient @XmlElementWrapper(name="outcomes") @XmlElement(name="outcome") public Set outcomes; public Assignment() { answers = new HashSet(); } public Assignment(Long id) { this.id = id; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } 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 int getStart() { return start; } public void setStart(int start) { this.start = start; } public boolean isSubmissionValid() { Calendar current_date = Utility.getCalendarInstance(); //Init start date calendar Calendar start_date = Utility.getCalendarInstance( this.getStart() ); //Init end date calendar Calendar end_date = Utility.getCalendarInstance( this.getEnd() ); //First check, if assignemnt is available (start date) //let's assume somebody is trying to hack via assignment ID in WP if ( current_date.before(start_date) ) { return false; } //Second check end date value before or equal and late_submission flag if ( this.isLate_submission() || current_date.compareTo(end_date) <= 0 ) { return true; } return false; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public User getCreator() { return creator; } public void setCreator(User user) { this.creator = user; } public Date getModified() { return modified; } public void setModified(Date modified) { this.modified = modified; } public Collection getTargetGroup() { return target_group; } public void setTargetGroup(Collection target_group) { this.target_group = target_group; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; if ( !this.course.getAssignments().contains(this) ) { this.course.addAssignment(this); } } public Long getCourseId() { return courseId; } public Collection getAnswers() { return answers; } public Answer getLearnerAnswer(Long user_id) { for( Answer a : getAnswers() ) { if ( a.getCreator().getId() == user_id ) { return a; } } return null; } public void setAnswers(Set answers) { this.answers = answers; } public void addAnswer(Answer answer) { this.answers.add(answer); //Bi-directional assignment set if ( answer.getAssignment() != this ) { answer.setAssignment(this); } } public Set getLearning_outcomes() { return learning_outcomes; } public void setLearning_outcomes(Set learning_outcomes) { this.learning_outcomes = learning_outcomes; } public void setTags(Collection cts) { this.tags = cts; } public String getSubtype() { return subtype; } public void setSubtype(String subtype) { this.subtype = subtype; } public String getStructure() { return structure; } public void setStructure(String structure) { this.structure = structure; } public int getMaxpoints() { return maxpoints; } public void setMaxpoints(int maxpoints) { this.maxpoints = maxpoints; } public boolean isLate_submission() { return late_submission; } public void setLate_submission(boolean late_submission) { this.late_submission = late_submission; } public boolean isSubmission_visibility() { return submission_visibility; } public void setSubmission_visibility(boolean submission_visibility) { this.submission_visibility = submission_visibility; } @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 Assignment)) { return false; } Assignment other = (Assignment) 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.Assignment[id=" + id + "]"; } }