Package openbook.domain
Class PurchaseOrder
- java.lang.Object
-
- openbook.domain.PurchaseOrder
-
- All Implemented Interfaces:
java.io.Serializable
@Entity public class PurchaseOrder extends java.lang.Object implements java.io.Serializable
A persistent entity to demonstrate Master in a Master-Details or Composite pattern for persistent domain model.
The Purchase Order - Line Items relationship typically demonstrates a Master-Details pattern. In JPA 2.0, following new features are added to support this common pattern used in domain modeling, and this example demonstrates them.
- Compound, Derived identity: This feature allows the Details type to compose its identity from the the Master's identity.
- Orphan Delete or Dependent relation: This feature allows to impose composite relation semantics to normally associative relation semantics implied in Java. Composite relation in persistence terms also translates to deletion of Details record from database when the details lose their relation to master.
Besides the above two key features, this persistent type also shows usage of- Auto-generated identity.
- Enum type persistent attribute.
- Date type persistent attribute.
- One-to-One uni-directional, immutable mapping to Customer.
- One-to-Many bi-directional, immutable mapping to LineItem.
- Author:
- Pinaki Poddar
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PurchaseOrder.Status
Enumerates the status of a Purchase Order.
-
Constructor Summary
Constructors Modifier Constructor Description protected
PurchaseOrder()
A protected constructor satisfies two purposes:
i) Status and creation time is set consistently.PurchaseOrder(ShoppingCart cart)
Construct a new order by transferring the content of the given ShoppingCart.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Customer
getCustomer()
Gets the customer who placed this Purchase Order.java.util.Date
getDeliveredOn()
Gets the time when this order was delivered.long
getId()
Gets the immutable, auto-generated persistent identity of this Purchase Order.java.util.List<LineItem>
getItems()
Gets the items for this Purchase Order.java.sql.Timestamp
getPlacedOn()
Gets the time when this order was placed.PurchaseOrder.Status
getStatus()
Gets the status of this Purchase Order.double
getTotal()
Gets the total cost of all the items in this order.boolean
isDelivered()
void
setDelivered()
-
-
-
Constructor Detail
-
PurchaseOrder
protected PurchaseOrder()
A protected constructor satisfies two purposes:
- i) Status and creation time is set consistently.
- ii) OpenJPA Bytecode Enhancer requires an empty constructor for the domain classes. The public constructor of Purchase Order takes a Shopping Cart as argument.
-
PurchaseOrder
public PurchaseOrder(ShoppingCart cart)
Construct a new order by transferring the content of the given ShoppingCart.- Parameters:
cart
- a non-null, non-empty Shopping cart- Throws:
java.lang.NullPointerException
- if the cart is nulljava.lang.IllegalStateException
- if the cart is empty
-
-
Method Detail
-
getId
public long getId()
Gets the immutable, auto-generated persistent identity of this Purchase Order.
-
getCustomer
public Customer getCustomer()
Gets the customer who placed this Purchase Order.- Returns:
- immutable Customer.
-
getStatus
public PurchaseOrder.Status getStatus()
Gets the status of this Purchase Order.
-
isDelivered
public boolean isDelivered()
-
setDelivered
public void setDelivered()
Sets the status of this Purchase Order as delivered. Setting an order status as delivered nullifies the association to Line Items. Nullifying this association has the important side-effect of Line Item records be deleted from the database because the relation is annotated as orphan delete.- Throws:
java.lang.IllegalStateException
- if this order has already been delivered.
-
getItems
public java.util.List<LineItem> getItems()
Gets the items for this Purchase Order.- Returns:
- the line items of this order. The line items for a delivered order is always null.
- See Also:
setDelivered()
-
getTotal
public double getTotal()
Gets the total cost of all the items in this order.
-
getPlacedOn
public java.sql.Timestamp getPlacedOn()
Gets the time when this order was placed.
-
getDeliveredOn
public java.util.Date getDeliveredOn()
Gets the time when this order was delivered.- Returns:
- null if the order has not been delivered yet.
-
-