Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mbsystem.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.validation.constraints.NotNull; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; /** * * @author Di */ @Entity @NamedQueries({ @NamedQuery(name = "Loan.findAll", query = "SELECT l FROM Loan l"), @NamedQuery(name = "Loan.findById", query = "SELECT l FROM Loan l WHERE l.id = :id"), @NamedQuery(name = "Loan.findByPatronEmail", query = "SELECT l FROM Loan l WHERE l.patron.email = :email") }) public class Loan implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "LOAN_ID") private Integer id; @NotNull private String loanDate; @NotNull(message = "Due Date field required.") private String dueDate; @NotNull(message = "Returned field required.") private boolean returned; @ManyToOne @JoinColumn(name = "PATRON_ID") private Patron patron; @ManyToOne @JoinColumn(name = "BOOK_ID") private Book book; /** * Constructors */ public Loan() { returned = false; } // Getters and setters public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLoanDate() { return loanDate; } public void setLoanDate(String loanDate) { this.loanDate = loanDate; } public String getDueDate() { return dueDate; } public void setDueDate(String dueDate) { this.dueDate = dueDate; } public boolean isReturned() { return returned; } public void setReturned(boolean returned) { this.returned = returned; } public Patron getPatron() { return patron; } public void setPatron(Patron patron) { this.patron = patron; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } /** * Check if overdue based on if it's after the due date and the book is not returned * @return overdue */ public boolean isOverdue() { DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy hh:mm:ss"); try { DateTime due = DateTime.parse(dueDate, dtf); return (due.isBeforeNow() && returned == false); } catch (Exception e) { return false; } } @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 Loan)) { return false; } Loan other = (Loan) 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 "mbsystem.models.Loan[ id=" + id + " ]"; } }