package models;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

@Entity
public class Request extends Model {
  private static final long serialVersionUID = 7490409966063340450L;
  @Id
  private Long primaryKey;

  @Column(nullable=false)
  @Required
  private String requestId;

  @Required
  @ManyToOne(cascade = CascadeType.PERSIST)
  private Student student;

  @Required
  @ManyToOne(cascade = CascadeType.PERSIST)
  private Book book;

  private Condition condition;

  @Required
  private int targetPrice;

  public Request(String requestId, Student student, Book book, int targetPrice) {
    this.requestId = requestId;
    this.student = student;
    this.book = book;
    this.targetPrice = targetPrice;

    this.student.getRequests().add(this);
    this.book.getRequests().add(this);
  }

  @Override
  public void delete() {
    // remove this entry from the book's and student's lists
    this.book.getRequests().remove(this);
    this.student.getRequests().remove(this);

    // kill link to books and students
    this.book = null;
    this.student = null;

    super.delete();
  }

  public static Finder<Long, Request> find() {
    return new Finder<Long, Request>(Long.class, Request.class);
  }

  @Override
  public String toString() {
    return String.format("[Request %s %s %s %d]", this.requestId, this.student.getName(),
        this.book.getTitle(), this.targetPrice);
  }

  public Long getPrimaryKey() {
    return primaryKey;
  }

  public void setPrimaryKey(Long primaryKey) {
    this.primaryKey = primaryKey;
  }

  public String getRequestId() {
    return requestId;
  }

  public void setRequestId(String requestId) {
    this.requestId = requestId;
  }

  public Student getStudent() {
    return student;
  }

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

  public Book getBook() {
    return book;
  }

  public void setBook(Book book) {
    this.book = book;
  }

  public Condition getCondition() {
    return condition;
  }

  public void setCondition(Condition condition) {
    this.condition = condition;
  }

  public Integer getTargetPrice() {
    return targetPrice;
  }

  public void setTargetPrice(Integer targetPrice) {
    this.targetPrice = targetPrice;
  }
}

@@ -179,16 +179,52 @@
 yToOne;%0A
+import javax.persistence.Transient;%0A
 import p
@@ -509,16 +509,60 @@
 estId;%0A%0A
+  @Transient%0A  public String studentName;%0A//
   @Requi
@@ -637,16 +637,59 @@
 udent;%0A%0A
+  @Transient%0A  private String bookTitle;%0A//
   @Requi
@@ -830,16 +830,143 @@
 Price;%0A%0A
+  public Request(String requestId, int targetPrice) %7B%0A    this.requestId = requestId;%0A    this.targetPrice = targetPrice;%0A  %7D%0A%0A
   public
@@ -2149,542 +2149,1370 @@
 c St
-udent getStudent() %7B%0A    return student;%0A  %7D%0A%0A  public void setStudent(Student student) %7B%0A    this.student = student;%0A  %7D%0A%0A  public Book getBook() %7B%0A    return book;%0A  %7D%0A%0A  public void setBook(Book book) %7B%0A    this.book = book;%0A  %7D%0A%0A  public Condition getCondition() %7B%0A    return condition;%0A  %7D%0A%0A  public void setCondition(Condition condition) %7B%0A    this.condition = condition;%0A  %7D%0A%0A  public Integer getTargetPrice() %7B%0A    return targetPrice;%0A  %7D%0A%0A  public void setTargetPrice(Integer targetPrice) %7B%0A    this.targetPrice = targetPrice
+ring getStudentName() %7B%0A    return this.studentName;%0A  %7D%0A%0A  public void setStudentName(String studentName) %7B%0A    this.studentName = studentName;%0A  %7D%0A%0A  public Student getStudent() %7B%0A    return student;%0A  %7D%0A%0A  public void setStudent(Student student) %7B%0A    this.student = student;%0A  %7D%0A%0A  public String getBookTitle() %7B%0A    return this.bookTitle;%0A  %7D%0A%0A  public void setBookTitle(String bookTitle) %7B%0A    this.bookTitle = bookTitle;%0A  %7D%0A%0A  public Book getBook() %7B%0A    return book;%0A  %7D%0A%0A  public void setBook(Book book) %7B%0A    this.book = book;%0A  %7D%0A%0A  public Condition getCondition() %7B%0A    return condition;%0A  %7D%0A%0A  public void setCondition(Condition condition) %7B%0A    this.condition = condition;%0A  %7D%0A%0A  public Integer getTargetPrice() %7B%0A    return targetPrice;%0A  %7D%0A%0A  public void setTargetPrice(Integer targetPrice) %7B%0A    this.targetPrice = targetPrice;%0A  %7D%0A%0A  public String validate() %7B%0A    if (this.studentName != null) %7B%0A      this.student = Student.find().where().eq(%22name%22, this.studentName).findUnique();%0A      if (this.student == null) %7B%0A        return %22Could not find the student named: %22 + this.studentName;%0A      %7D%0A    %7D%0A%0A    if (this.bookTitle != null) %7B%0A      this.book = Book.find().where().eq(%22title%22, this.bookTitle).findUnique();%0A      if (this.book == null) %7B%0A        return %22Could not find book named: %22 + this.bookTitle;%0A      %7D%0A    %7D%0A%0A    return null
 ;%0A  

update Request model