package com.adms.elearning.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Formula;

import com.adms.common.domain.BaseAuditDomain;

@Entity
@Table(name="COURSE_ENROLMENT")
public class CourseEnrolment extends BaseAuditDomain {

	private static final long serialVersionUID = 5044971945518545158L;

	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	@Column(name="EXAM_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	private Date examDate;
	
	@ManyToOne
	@JoinColumn(name="STUDENT")
	private Student student;
	
	@ManyToOne
	@JoinColumn(name="EXAM_LEVEL_CODE", referencedColumnName="EXAM_LEVEL_CODE")
	private ExamLevel examLevel;
	
	@ManyToOne
	@JoinColumn(name="EXAM_TYPE_CODE", referencedColumnName="EXAM_TYPE_CODE")
	private ExamType examType;
	
	@ManyToOne
	@JoinColumn(name="COURSE")
	private Course course;
	
	@OneToMany(fetch=FetchType.LAZY, mappedBy="courseEnrolment", cascade=CascadeType.ALL)
	private List<CourseResult> courseResults;
	
	@Formula("(select count(o.id) "
			+ " from COURSE_RESULT o "
			+ "	inner join ANSWER a on o.answer = a.id"
			+ " where o.COURSE_ENROLMENT = id"
			+ " and a.ANSWER_TYPE = 'CORRECT_ANS')")
	private int marks;
	
	@Formula("(select count(o.id) "
			+ " from COURSE_RESULT o "
			+ " where o.COURSE_ENROLMENT = id)")
	private int fullMarks;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public ExamLevel getExamLevel() {
		return examLevel;
	}

	public void setExamLevel(ExamLevel examLevel) {
		this.examLevel = examLevel;
	}

	public ExamType getExamType() {
		return examType;
	}

	public void setExamType(ExamType examType) {
		this.examType = examType;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public Date getExamDate() {
		return examDate;
	}

	public void setExamDate(Date examDate) {
		this.examDate = examDate;
	}

	public List<CourseResult> getCourseResults() {
		return courseResults;
	}

	public void setCourseResults(List<CourseResult> courseResults) {
		this.courseResults = courseResults;
	}

	public int getMarks() {
		return marks;
	}

	public void setMarks(int marks) {
		this.marks = marks;
	}

	public int getFullMarks() {
		return fullMarks;
	}

	public void setFullMarks(int fullMarks) {
		this.fullMarks = fullMarks;
	}

	@Override
	public String toString() {
		return "CourseEnrolment [id=" + id + ", examDate=" + examDate
				+ ", student=" + student + ", examLevel=" + examLevel
				+ ", examType=" + examType + ", course=" + course + "]";
	}
	
}

@@ -926,19 +926,18 @@
 ng id;%0D%0A
-%09
 %0D%0A
+
 %09@Column
@@ -1013,27 +1013,26 @@
  examDate;%0D%0A
-%09
 %0D%0A
+
 %09@ManyToOne%0D
@@ -1089,19 +1089,18 @@
 udent;%0D%0A
-%09
 %0D%0A
+
 %09@ManyTo
@@ -1213,19 +1213,18 @@
 Level;%0D%0A
-%09
 %0D%0A
+
 %09@ManyTo
@@ -1333,19 +1333,18 @@
 mType;%0D%0A
-%09
 %0D%0A
+
 %09@ManyTo
@@ -1402,17 +1402,88 @@
 ourse;%0D%0A
-%09
+%0D%0A%09@ManyToOne%0D%0A%09@JoinColumn(name=%22SECTION%22)%0D%0A%09private Section section;%0D%0A
 %0D%0A%09@OneT
@@ -1604,27 +1604,26 @@
 seResults;%0D%0A
-%09
 %0D%0A
+
 %09@Formula(%22(
@@ -1828,19 +1828,18 @@
 marks;%0D%0A
-%09
 %0D%0A
+
 %09@Formul
@@ -1961,17 +1961,16 @@
 Marks;%0D%0A
-%09
 %0D%0A%09publi
@@ -3196,239 +3196,139 @@
 %0A%0D%0A%09
-@Override%0D%0A%09public String toString() %7B%0D%0A%09%09return %22CourseEnrolment %5Bid=%22 + id + %22, examDate=%22 + examDate%0D%0A%09%09%09%09+ %22, student=%22 + student + %22, examLevel=%22 + examLevel%0D%0A%09%09%09%09+ %22, examType=%22 + examType + %22, course=%22 + course + %22%5D%22
+public Section getSection() %7B%0D%0A%09%09return section;%0D%0A%09%7D%0D%0A%0D%0A%09public void setSection(Section section) %7B%0D%0A%09%09this.section = section
 ;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%7D%0D

Add new column 'SECTION'