content: package org.agoncal.book.javaee7.chapter06.ex38;
  
  import javax.persistence.*;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.GregorianCalendar;
  
  /**
   * @author Antonio Goncalves
   *         APress Book - Beginning Java EE 7 with Glassfish 4
   *         http://www.apress.com/
   *         http://www.antoniogoncalves.org
   *         --
   */
  @Entity
  @Table(name = "ex38_customer")
  public class Customer38 {
  
    // ======================================
    // =             Attributes             =
    // ======================================
    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;
    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;
    @Transient
    private Integer age;
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
  
  // ======================================
  // =            Constructors            =
  // ======================================
  
    public Customer38() {
    }
  
    public Customer38(String firstName, String lastName, String email, String phoneNumber, Date dateOfBirth, Date creationDate) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.email = email;
      this.phoneNumber = phoneNumber;
      this.dateOfBirth = dateOfBirth;
      this.creationDate = creationDate;
    }
  
    public Customer38(String firstName, String lastName, String email, String phoneNumber) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.email = email;
      this.phoneNumber = phoneNumber;
    }
  
    // ======================================
    // =          Lifecycle Methods         =
    // ======================================
  
    @PrePersist
    @PreUpdate
    private void validate() {
      System.out.println("validateData()");
      if (firstName == null || "".equals(firstName))
        throw new IllegalArgumentException("Invalid first name");
      if (lastName == null || "".equals(lastName))
        throw new IllegalArgumentException("Invalid last name");
    }
  
    @PostLoad
    @PostPersist
    @PostUpdate
    public void calculateAge() {
      System.out.println("calculateAge()");
      if (dateOfBirth == null) {
        age = null;
        return;
      }
  
      Calendar birth = new GregorianCalendar();
      birth.setTime(dateOfBirth);
      Calendar now = new GregorianCalendar();
      now.setTime(new Date());
      int adjust = 0;
      if (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) {
        adjust = -1;
      }
      age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
    }
  
  // ======================================
  // =          Getters & Setters         =
  // ======================================
  
    public Long getId() {
      return id;
    }
  
    public String getFirstName() {
      return firstName;
    }
  
    public void setFirstName(String firstName) {
      this.firstName = firstName;
    }
  
    public String getLastName() {
      return lastName;
    }
  
    public void setLastName(String lastName) {
      this.lastName = lastName;
    }
  
    public String getEmail() {
      return email;
    }
  
    public void setEmail(String email) {
      this.email = email;
    }
  
    public String getPhoneNumber() {
      return phoneNumber;
    }
  
    public void setPhoneNumber(String phoneNumber) {
      this.phoneNumber = phoneNumber;
    }
  
    public Date getDateOfBirth() {
      return dateOfBirth;
    }
  
    public void setDateOfBirth(Date dateOfBirth) {
      this.dateOfBirth = dateOfBirth;
    }
  
    public Integer getAge() {
      return age;
    }
  
    public void setAge(Integer age) {
      this.age = age;
    }
  
    public Date getCreationDate() {
      return creationDate;
    }
  
    public void setCreationDate(Date creationDate) {
      this.creationDate = creationDate;
    }
  
  }