content: package org.agoncal.application.petstore.domain;
  
  import javax.persistence.*;
  import javax.xml.bind.annotation.XmlRootElement;
  import java.util.Date;
  import java.util.List;
  
  /**
   * @author Antonio Goncalves
   *         http://www.antoniogoncalves.org
   *         --
   */
  
  @Entity
  @Table(name = "t_order")
  @XmlRootElement
  @NamedQueries({
          @NamedQuery(name = Order.FIND_ALL, query = "SELECT o FROM Order o")
  })
  public class Order {
  
      // ======================================
      // =             Attributes             =
      // ======================================
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      @Column(name = "order_date", updatable = false)
      @Temporal(TemporalType.DATE)
      private Date orderDate;
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "customer_fk", nullable = false)
      private Customer customer;
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinTable(name = "t_order_order_line",
              joinColumns = {@JoinColumn(name = "order_fk")},
              inverseJoinColumns = {@JoinColumn(name = "order_line_fk")})
      private List<OrderLine> orderLines;
      @Embedded
      private Address deliveryAddress;
      @Embedded
      private CreditCard creditCard = new CreditCard();
  
      // ======================================
      // =             Constants              =
      // ======================================
  
      public static final String FIND_ALL = "Order.findAll";
  
      // ======================================
      // =            Constructors            =
      // ======================================
  
      public Order() {
      }
  
      public Order(Customer customer, CreditCard creditCard, Address deliveryAddress) {
          this.customer = customer;
          this.creditCard = creditCard;
          this.deliveryAddress = deliveryAddress;
      }
  
      // ======================================
      // =          Lifecycle Methods         =
      // ======================================
  
      @PrePersist
      private void setDefaultData() {
          orderDate = new Date();
      }
  
      // ======================================
      // =              Public Methods        =
      // ======================================
  
      public Float getTotal() {
          if (orderLines == null || orderLines.isEmpty())
              return 0f;
  
          Float total = 0f;
  
          // Sum up the quantities
          for (OrderLine orderLine : orderLines) {
              total += (orderLine.getSubTotal());
          }
  
          return total;
      }
  
      // ======================================
      // =         Getters & setters          =
      // ======================================
  
      public Long getId() {
          return id;
      }
  
      public Date getOrderDate() {
          return orderDate;
      }
  
      public Customer getCustomer() {
          return customer;
      }
  
      public void setCustomer(Customer customer) {
          this.customer = customer;
      }
  
      public List<OrderLine> getOrderLines() {
          return orderLines;
      }
  
      public void setOrderLines(List<OrderLine> orderLines) {
          this.orderLines = orderLines;
      }
  
      public Address getDeliveryAddress() {
          return deliveryAddress;
      }
  
      public void setDeliveryAddress(Address deliveryAddress) {
          this.deliveryAddress = deliveryAddress;
      }
  
      public CreditCard getCreditCard() {
          return creditCard;
      }
  
      public void setCreditCard(CreditCard creditCard) {
          this.creditCard = creditCard;
      }
  
      public String getCreditCardNumber() {
          return creditCard.getCreditCardNumber();
      }
  
      public void setCreditCardNumber(String creditCardNumber) {
          creditCard.setCreditCardNumber(creditCardNumber);
      }
  
      public CreditCardType getCreditCardType() {
          return creditCard.getCreditCardType();
      }
  
      public void setCreditCardType(CreditCardType creditCardType) {
          creditCard.setCreditCardType(creditCardType);
      }
  
      public String getCreditCardExpiryDate() {
          return creditCard.getCreditCardExpDate();
      }
  
      public void setCreditCardExpiryDate(String creditCardExpiryDate) {
          creditCard.setCreditCardExpDate(creditCardExpiryDate);
      }
  
      // ======================================
      // =   Methods hash, equals, toString   =
      // ======================================
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (!(o instanceof Order)) return false;
  
          Order order = (Order) o;
  
          if (!customer.equals(order.customer)) return false;
          if (orderDate != null && !orderDate.equals(order.orderDate)) return false;
  
          return true;
      }
  
      @Override
      public int hashCode() {
          int result = orderDate != null ? orderDate.hashCode() : 0;
          result = 31 * result + customer.hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          final StringBuilder sb = new StringBuilder();
          sb.append("Order");
          sb.append("{id=").append(id);
          sb.append(", orderDate=").append(orderDate);
          sb.append(", customer=").append(customer);
          sb.append(", orderLines=").append(orderLines);
          sb.append(", deliveryAddress=").append(deliveryAddress);
          sb.append(", creditCard=").append(creditCard);
          sb.append('}');
          return sb.toString();
      }
  }
  