/* * 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.comparators.ActivityComparator; import ee.tlu.htk.dippler.comparators.AssignmentComparator; import ee.tlu.htk.dippler.comparators.FacilitatorComparator; import ee.tlu.htk.dippler.comparators.GroupComparator; import ee.tlu.htk.dippler.comparators.LearnerComparator; import ee.tlu.htk.dippler.comparators.LearningOutcomeComparator; import ee.tlu.htk.dippler.comparators.PostComparator; import ee.tlu.htk.dippler.comparators.ResourceComparator; import ee.tlu.htk.dippler.utils.Utility; import java.io.Serializable; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; 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.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.XmlRootElement; /** * * @author metz */ @XmlRootElement @Entity @Table(name = "course") @NamedQueries({ @NamedQuery(name = "Course.findAll", query = "SELECT c FROM Course c"), @NamedQuery(name = "Course.findApproved", query = "SELECT c FROM Course c WHERE c.approved = true"), @NamedQuery(name = "Course.findById", query = "SELECT c FROM Course c WHERE c.id = :id"), @NamedQuery(name = "Course.findByTitle", query = "SELECT c FROM Course c WHERE c.title = :title"), @NamedQuery(name = "Course.findByIdentifier", query = "SELECT c FROM Course c WHERE c.identifier = :identifier"), @NamedQuery(name = "Course.findByCredits", query = "SELECT c FROM Course c WHERE c.credits = :credits"), @NamedQuery(name = "Course.findByProvider", query = "SELECT c FROM Course c WHERE c.provider = :provider"), @NamedQuery(name = "Course.findByStart", query = "SELECT c FROM Course c WHERE c.start = :start"), @NamedQuery(name = "Course.findByEnd", query = "SELECT c FROM Course c WHERE c.end = :end"), @NamedQuery(name = "Course.findByCloses", query = "SELECT c FROM Course c WHERE c.closes = :closes"), @NamedQuery(name = "Course.findByPermission", query = "SELECT c FROM Course c WHERE c.permission = :permission"), @NamedQuery(name = "Course.findByEnrollment", query = "SELECT c FROM Course c WHERE c.enrollment = :enrollment"), @NamedQuery(name = "Course.findByCreated", query = "SELECT c FROM Course c WHERE c.created = :created"), @NamedQuery(name = "Course.findByModified", query = "SELECT c FROM Course c WHERE c.modified = :modified"), @NamedQuery(name = "Course.findByApproved", query = "SELECT c FROM Course c WHERE c.approved = :approved")}) public class Course implements Serializable { private static final long serialVersionUID = 1L; /* * Some variables, status */ public static final int PREPARATION = 0; public static final int ACTIVE = 1; public static final int FINISHED = 2; @Id @GeneratedValue(generator = "CourseID") @TableGenerator(name="CourseID", table="id_gen", pkColumnName="ID_NAME", valueColumnName = "ID_VAL", pkColumnValue="LAST_COURSE", 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 = "identifier") private String identifier; @Basic(optional = false) @Column(name = "credits") private int credits; @Column(name = "provider") private int provider; @Basic(optional = false) @Lob @Column(name = "description") private String description; @Basic(optional = false) @Column(name = "start") private int start; @Basic(optional = false) @Column(name = "end") private int end; @Basic(optional = false) @Column(name = "closes") private int closes; @Column(name = "permission") private String permission; @Column(name = "enrollment") private String enrollment; @Column(name = "educationlevel") private int educationlevel; @Column(name = "languageofinstruction") private int languageofinstruction; @Column(name = "attendancemode") private String attendancemode; @Column(name = "objective") private String objective; @Column(name = "assessment") private String assessment; @Column(name = "prerequisite") private String prerequisite; @Column(name = "tag") private String tag; @JoinColumn(name = "image", referencedColumnName = "id") @OneToOne ( cascade = CascadeType.PERSIST ) private Image image; @Transient @XmlElement(name="course-image") public Image course_image; @Column(name = "status") private String status; @Basic(optional = false) @Column(name = "created") @Temporal(TemporalType.TIMESTAMP) private Date created; @Basic(optional = false) @Column(name = "modified") @Temporal(TemporalType.TIMESTAMP) private Date modified; @Basic(optional = false) @Column(name = "approved") private boolean approved; @Basic(optional = false) @Column(name = "trashed") private boolean trashed; @JoinColumn(name = "organization", referencedColumnName = "id") @ManyToOne private Organization organization; @JoinColumn(name = "owner", referencedColumnName = "id") @ManyToOne private User owner; @JoinColumn(name = "creator", referencedColumnName = "id") @ManyToOne private User creator; @OneToMany (mappedBy = "course", orphanRemoval=true, cascade= CascadeType.PERSIST) private List facilitators = new ArrayList(); @OneToMany ( mappedBy = "course", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }) private List learners; @OneToMany ( mappedBy = "course") private List posts; @OneToMany ( mappedBy = "course" ) @JoinTable(name = "assignment", joinColumns = @JoinColumn(name = "course"), inverseJoinColumns = @JoinColumn(name = "id") ) private List assignments; @OneToMany ( mappedBy = "course" ) @JoinTable(name = "coursegroup", joinColumns = @JoinColumn(name = "course"), inverseJoinColumns = @JoinColumn(name = "id") ) private List groups; @OneToMany ( mappedBy = "course" ) @JoinTable(name = "learning_outcome", joinColumns = @JoinColumn(name = "course"), inverseJoinColumns = @JoinColumn(name = "id") ) private List learning_outcomes; @OneToMany(mappedBy = "course", fetch = FetchType.LAZY) private Collection categories; // Relation between resource and its course @OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REFRESH }) private List learningResources; @OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REFRESH }) private List resourceFolders; @OneToMany (mappedBy = "course", cascade= CascadeType.PERSIST) @JoinTable(name = "activity", joinColumns = @JoinColumn(name = "course"), inverseJoinColumns = @JoinColumn(name = "id") ) private List activities; public Course() { activities = new ArrayList(); resourceFolders = new ArrayList(); } public Course(Long id) { this.id = id; } public Course(Long id, String title, String identifier, int credits, int provider, int educationlevel, int languageofinstruction, String objective, String description, String assessment, String prerequisite, String tag, String attendancemode, String status, Image image, int start, int end, int closes, Date created, Date modified, boolean approved, boolean trashed) { this.id = id; this.title = title; this.identifier = identifier; this.credits = credits; this.provider = provider; this.educationlevel = educationlevel; this.languageofinstruction = languageofinstruction; this.objective = objective; this.description = description; this.assessment = assessment; this.prerequisite = prerequisite; this.tag = tag; this.attendancemode = attendancemode; this.status = status; this.image = image; this.start = start; this.end = end; this.closes = closes; this.created = created; this.modified = modified; this.approved = approved; this.trashed = trashed; } 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 getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } public int getCredits() { return credits; } public void setCredits(int credits) { this.credits = credits; } public int getProvider() { return provider; } public void setProvider(int provider) { this.provider = provider; } 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 int getEnd() { return end; } public void setEnd(int end) { this.end = end; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } public String getAssessment() { return assessment; } public void setAssessment(String assessment) { this.assessment = assessment; } public String getPrerequisite() { return prerequisite; } public void setPrerequisite(String prerequisite) { this.prerequisite = prerequisite; } public String getAttendancemode() { return attendancemode; } public void setAttendancemode(String attendancemode) { this.attendancemode = attendancemode; } public String getObjective() { return objective; } public void setObjective(String objective) { this.objective = objective; } public int getEducationlevel() { return educationlevel; } public void setEducationlevel(int educationlevel) { this.educationlevel = educationlevel; } public int getLanguageofinstruction() { return languageofinstruction; } public void setLanguageofinstruction(int languageofinstruction) { this.languageofinstruction = languageofinstruction; } public boolean canApply() { if (this.getCloses() > 0) { Calendar current_date = Utility.getCalendarInstance(); Calendar apply_until = Utility.getCalendarInstance( this.getCloses() ); //If calendar instance compare is < 0 = before || == 0 - the same day if ( current_date.compareTo(apply_until) <= 0 ) { return true; } return false; } return true; } public int getCloses() { return closes; } public void setCloses(int closes) { this.closes = closes; } public String getPermission() { return permission; } public void setPermission(String permission) { this.permission = permission; } public String getEnrollment() { return enrollment; } public void setEnrollment(String enrollment) { this.enrollment = enrollment; } 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 boolean getApproved() { return approved; } public void setApproved(boolean approved) { this.approved = approved; } public boolean getTrashed() { return trashed; } public void setTrashed(boolean trashed) { this.trashed = trashed; } public Collection getLearningOutcomes() { Collections.sort(this.learning_outcomes, new LearningOutcomeComparator()); return learning_outcomes; } public void addLearningOutcome(LearningOutcome outcome) { this.learning_outcomes.add(outcome); if ( outcome.getCourse() != this) { outcome.setCourse(this); } } public Collection getGroups() { Collections.sort(this.groups, new GroupComparator()); return groups; } public Collection getGroups(int offset, int limit) { int groups_length = getGroups().size(); //offset, limit logic if ( offset > 0) { if ( offset > groups_length ) { offset = groups_length; } limit = offset + limit; if ( limit > groups_length ) { limit = groups_length; } } else { if ( limit > groups_length ) { limit = groups_length; } } return ((List) getGroups()).subList(offset, limit); } public void addGroup(Coursegroup group) { this.groups.add(group); if ( group.getCourse() != this) { group.setCourse(this); } } public Collection getFacilitators() { Collections.sort(this.facilitators, new FacilitatorComparator()); return this.facilitators; } public void setFacilitators(ArrayList facilitators) { this.facilitators = facilitators; } public void addFacilitator(Facilitator facilitator) { this.facilitators.add(facilitator); if ( facilitator.getCourse() != this ) { facilitator.setCourse(this); } } public void addFacilitator(User user) { Facilitator facilitator = new Facilitator(); facilitator.setUser(user); facilitator.setStatus(5); facilitator.setCourse(this); //Finally call addFacilitator with facilitator object this.addFacilitator(facilitator); } public void remove(Object o) { if ( o instanceof Facilitator ) { this.facilitators.remove((Facilitator) o); } if ( o instanceof Learner) { this.learners.remove((Learner) o); } if ( o instanceof LearningOutcome ) { this.learning_outcomes.remove((LearningOutcome) o); } if ( o instanceof Coursegroup) { this.groups.remove((Coursegroup) o); } if ( o instanceof CourseblogPost) { this.posts.remove((CourseblogPost) o); } } public Learner getIsLearner(Long user_id) { for( Learner learner : getLearners() ) { if ( learner.getUser().getId() == user_id ) { return learner; } } return null; } public Collection getLearners() { Collections.sort(this.learners, new LearnerComparator()); return learners; } public void setLearners(List learners) { this.learners = learners; } public void addLearner(Learner learner) { this.learners.add(learner); if ( learner.getCourse() != this) { learner.setCourse(this); } } public Learner addLearner(User user, int status) { Learner new_learner = new Learner(); new_learner.setStatus(status); new_learner.setUser(user); new_learner.setCourse(this); new_learner.setUser_id(user.getId()); new_learner.setCourseId(this.getId()); learners.add(new_learner); return new_learner; } public List getPosts(int start, int end) { if ( end > getPosts().size() ) { end = getPosts().size(); } //If start and end both are 0 if ( start == 0 && end == 0) { return (List) getPosts(); } return ((List) getPosts()).subList(start, end); } public Collection getPosts() { Collections.sort(this.posts, new PostComparator()); return posts; } public void setPosts(List posts) { this.posts = posts; } public void addPost(CourseblogPost post) { this.posts.add(post); if (post.getCourse() != this) { post.setCourse(this); } } public Organization getOrganization() { return organization; } public void setOrganization(Organization organization) { this.organization = organization; } public Collection getCategories() { return categories; } public void setCategories(Collection categories) { this.categories = categories; } public void addCategory(Category category) { this.categories.add(category); if ( category.getCourse() != this ) { category.setCourse(this); } } public User getOwner() { return owner; } public void setOwner(User user) { this.owner = user; } public User getCreator() { return creator; } public void setCreator(User user) { this.creator = user; } public List getAssignments(int start, int end) { if ( end > getAssignments().size() ) { end = getAssignments().size(); } return ((List) getAssignments()).subList(start, end); } public Collection getAssignments() { Collections.sort(this.assignments, new AssignmentComparator()); return assignments; } public void setAssignments(List assignments) { this.assignments = assignments; } public void addAssignment(Assignment assignment) { this.assignments.add(assignment); if ( assignment.getCourse() != this ) { assignment.setCourse(this); } } public Collection getLearningResources() { Collections.sort(this.learningResources, new ResourceComparator()); return learningResources; } public void setLearningResources(List learningResources) { this.learningResources = learningResources; } public void addLearningResource(LearningResource learning_resource) { this.learningResources.add(learning_resource); if ( learning_resource.getCourse() != this ) { learning_resource.setCourse(this); } } public Collection getResourceFolders() { Collections.sort(this.resourceFolders, new ResourceComparator()); return resourceFolders; } public void setResourceFolders(List resourceFolders) { this.resourceFolders = resourceFolders; } public void addResourceFolder(ResourceFolder folder) { this.resourceFolders.add(folder); if ( folder.getCourse() != this ) { folder.setCourse(this); } } public Collection getActivities() { Collections.sort(this.activities, new ActivityComparator()); return activities; } public Collection getActivities(int offset, int limit) { int activities_length = getActivities().size(); //offset, limit logic if ( offset > 0) { if ( offset > activities_length ) { offset = activities_length; } limit = offset + limit; if ( limit > activities_length ) { limit = activities_length; } } else { if ( limit > activities_length ) { limit = activities_length; } } return ((List) getActivities()).subList(offset, limit); } public void setActivities(List activities) { this.activities = activities; } public void addActivity(Activity activity) { this.activities.add(activity); if ( activity.getCourse() != this) { activity.setCourse(this); } } public boolean isPraparation() { return ( Integer.parseInt(this.getStatus()) == Course.PREPARATION ); } public boolean isActive() { return ( Integer.parseInt(this.getStatus()) == Course.ACTIVE ); } public boolean isFinished() { return ( Integer.parseInt(this.getStatus()) == Course.FINISHED ); } @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 Course)) { return false; } Course other = (Course) 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.Course[id=" + id + "]"; } }