⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 order.java

📁 Java EE 5 许多新功能都包含经过修补的 EJB 架构
💻 JAVA
字号:
/*
 * Copyright 2006 Borys Burnayev
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rdacorp.petstore.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * @author Borys Burnayev
 */
@Entity
@Table(name = "order_summary")
@SequenceGenerator(name = "OrderSeq", sequenceName = "sq_order_summary")
public class Order {

   private Integer orderId;

   private BigDecimal amount;

   private Date orderTs;

   private Person billingPerson;

   private Person shippingPerson;

   private Payment payment;

   private Address shippingAddress;

   private Address billingAddress;

   private List<OrderItem> orderItems;

   private int version;

   @Column(name = "row_version")
   @Version
   public int getVersion() {
      return version;
   }

   public void setVersion(int version) {
      this.version = version;
   }

   public Order(BigDecimal amount, Person billingPerson, Address billingAddress, Person shippingPerson,
            Address shippingAddress, Payment payment) {
      this();
      this.amount = amount;
      this.billingPerson = billingPerson;
      this.shippingPerson = shippingPerson;
      this.payment = payment;
      this.shippingAddress = shippingAddress;
      this.billingAddress = billingAddress;
   }

   public Order(BigDecimal amount, Date orderTs, Person billingPerson, Address billingAddress, Person shippingPerson,
            Address shippingAddress, Payment payment) {
      this();
      this.amount = amount;
      this.orderTs = orderTs;
      this.billingPerson = billingPerson;
      this.shippingPerson = shippingPerson;
      this.payment = payment;
      this.shippingAddress = shippingAddress;
      this.billingAddress = billingAddress;
   }

   public Order() {
      orderItems = new ArrayList<OrderItem>();
   }

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "OrderSeq")
   @Column(name = "order_summary_sysid")
   public Integer getOrderId() {
      return this.orderId;
   }

   public void setOrderId(Integer orderId) {
      this.orderId = orderId;
   }

   @Column(name = "amount")
   public BigDecimal getAmount() {
      return this.amount;
   }

   public void setAmount(BigDecimal amount) {
      this.amount = amount;
   }

   @Column(name = "order_ts")
   public Date getOrderTs() {
      return this.orderTs;
   }

   public void setOrderTs(Date orderTs) {
      this.orderTs = orderTs;
   }

   @ManyToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "bill_to_person_sysid")
   public Person getBillingPerson() {
      return this.billingPerson;
   }

   public void setBillingPerson(Person billingPerson) {
      this.billingPerson = billingPerson;
   }

   @ManyToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "ship_to_person_sysid")
   public Person getShippingPerson() {
      return this.shippingPerson;
   }

   public void setShippingPerson(Person shippingPerson) {
      this.shippingPerson = shippingPerson;
   }

   @OneToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "payment_sysid")
   public Payment getPayment() {
      return this.payment;
   }

   public void setPayment(Payment payment) {
      this.payment = payment;
   }

   @ManyToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "ship_to_address_sysid")
   public Address getShippingAddress() {
      return this.shippingAddress;
   }

   public void setShippingAddress(Address shippingAddress) {
      this.shippingAddress = shippingAddress;
   }

   @ManyToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "bill_to_address_sysid")
   public Address getBillingAddress() {
      return this.billingAddress;
   }

   public void setBillingAddress(Address billingAddress) {
      this.billingAddress = billingAddress;
   }

   @OneToMany(mappedBy = "order", cascade = CascadeType.PERSIST)
   public List<OrderItem> getOrderItems() {
      return this.orderItems;
   }

   public void setOrderItems(List<OrderItem> orderItems) {
      this.orderItems = orderItems;
   }

   public String toString() {
      String id = orderId != null ? orderId.toString() : "<no ID>";
      return "Order " + id + ":\nAmount: " + amount + "\nOrder Timestamp: " + orderTs + "\n" + "Billed to:\n"
               + billingPerson + billingAddress + "Shippied to:\n" + shippingPerson + shippingAddress;
   }

   public void addItem(OrderItem orderItem) {
      if (orderItems.contains(orderItem)) {
         OrderItem item = (OrderItem) orderItems.get(orderItems.indexOf(orderItem));
         item.increaseQuantity(orderItem.getQuantity());
      }
      else
         orderItems.add(orderItem);
      updateAmount();
   }

   public void removeItem(OrderItem orderItem) {
      if (orderItems.contains(orderItem)) {
         OrderItem item = (OrderItem) orderItems.get(orderItems.indexOf(orderItem));
         item.decreaseQuantity(orderItem.getQuantity());
         if (item.getQuantity() == 0)
            orderItems.remove(orderItem);
      }
      updateAmount();
   }

   private void updateAmount() {
      BigDecimal updatedAmount = new BigDecimal(0);
      for (int i = 0; i < orderItems.size(); i++) {
         OrderItem item = orderItems.get(i);
         updatedAmount = updatedAmount.add(item.getExtendedPrice());
      }
      amount = updatedAmount;
   }

   public boolean equals(Object object) {
      if (!(object instanceof Order))
         return false;
      Order order = (Order) object;
      return amount.equals(order.getAmount()) && payment.equals(order.getPayment())
               && billingPerson.equals(order.billingPerson) && billingAddress.equals(order.billingAddress)
               && shippingPerson.equals(order.shippingPerson) && shippingAddress.equals(order.shippingAddress);
   }

   public int hashCode() {
      return amount.hashCode();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -