package at.ac.tuwien.inso.entity;

import javax.persistence.*;
import java.math.*;
import java.util.ArrayList;
import java.util.*;

import static java.util.Arrays.*;
import static java.util.Collections.*;

@Entity
public class Subject {

	@Id
	@GeneratedValue
	private Long id;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private BigDecimal ects;

	@ManyToMany
	private List<Lecturer> lecturers = new ArrayList<>();

	@ManyToMany
	private List<Subject> requiredSubjects = new ArrayList<>();

	public Subject() {
	}

	public Subject(String name, BigDecimal ects) {
		this.name = name;
		this.ects = ects;
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public BigDecimal getEcts() {
		return ects;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEcts(BigDecimal ects) {
		this.ects = ects;
	}

	public List<Lecturer> getLecturers() {
		return unmodifiableList(lecturers);
	}

	public List<Subject> getRequiredSubjects() {
		return requiredSubjects;
	}

	public void addLecturers(Lecturer... lecturers) {
		this.lecturers.addAll(asList(lecturers));
	}

	public void removeLecturers(Lecturer... lecturers) {
		this.lecturers.removeAll(asList(lecturers));
	}

	public void addRequiredSubjects(Subject... subjects) {
		this.requiredSubjects.addAll(asList(subjects));
	}

	public void removeRequiredSubjects(Subject... subjects) {
		this.requiredSubjects.removeAll(asList(subjects));
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		Subject subject = (Subject) o;

		if (!id.equals(subject.id)) return false;
		if (!name.equals(subject.name)) return false;
		if (!ects.equals(subject.ects)) return false;
		if (!lecturers.equals(subject.lecturers)) return false;
		return requiredSubjects.equals(subject.requiredSubjects);

	}

	@Override
	public int hashCode() {
		if(id!=null){
		int result = id.hashCode();
			result = 31 * result + name.hashCode();
			result = 31 * result + ects.hashCode();
			result = 31 * result + lecturers.hashCode();
			result = 31 * result + requiredSubjects.hashCode();
	
			return result;
		}else{
			return -1;
		}
	}

	@Override
	public String toString() {
		return "Subject{" +
				"id=" + id +
				", name='" + name + '\'' +
				", ects=" + ects +
				", lecturers=" + lecturers +
				", requiredSubjects=" + requiredSubjects +
				'}';
	}
}

@@ -26,24 +26,79 @@
 .entity;%0D%0A%0D%0A
+import com.fasterxml.jackson.annotation.JsonIgnore;%0D%0A%0D%0A
 import javax
@@ -1000,32 +1000,46 @@
  = ects;%0D%0A%09%7D%0D%0A%0D%0A
+%09@JsonIgnore%0D%0A
 %09public List%3CLec
@@ -1100,32 +1100,46 @@
 turers);%0D%0A%09%7D%0D%0A%0D%0A
+%09@JsonIgnore%0D%0A
 %09public List%3CSub
@@ -1834,16 +1834,29 @@
 %0D%0A%09%09if (
+id != null ? 
 !id.equa
@@ -1873,106 +1873,29 @@
 .id)
-) return false;%0D%0A%09%09if (!name.equals(subject.name)) return false;%0D%0A%09%09if (!ects.equals(subject.ects)
+ : subject.id != null
 ) re
@@ -1914,25 +1914,20 @@
 %0A%09%09if (!
-lecturers
+name
 .equals(
@@ -1934,25 +1934,20 @@
 subject.
-lecturers
+name
 )) retur
@@ -1961,36 +1961,24 @@
 ;%0D%0A%09%09return 
-requiredSubj
 ects.equals(
@@ -1985,28 +1985,16 @@
 subject.
-requiredSubj
 ects);%0D%0A
@@ -2046,142 +2046,53 @@
 %0A%09%09i
-f(id!=null)%7B%0D%0A%09%09int result = id.hashCode();%0D%0A%09%09%09result = 31 * result + name.hashCode();%0D%0A%09%09%09result = 31 * result + ects.hashCode()
+nt result = id != null ? id.hashCode() : 0
 ;%0D%0A
-%09
 %09%09re
@@ -2112,25 +2112,20 @@
 esult + 
-lecturers
+name
 .hashCod
@@ -2132,17 +2132,16 @@
 e();%0D%0A%09%09
-%09
 result =
@@ -2155,28 +2155,16 @@
 esult + 
-requiredSubj
 ects.has
@@ -2174,20 +2174,16 @@
 de();%0D%0A%09
-%0D%0A%09%09
 %09return 
@@ -2195,38 +2195,8 @@
 t;%0D%0A
-%09%09%7Delse%7B%0D%0A%09%09%09return -1;%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A

annotate with @JsonIgnore to prevent cyclic recursions; override equals() and hashCode() again.