| /******************************************************************************* |
| * Copyright (c) 2011, 2012 Oracle and/or its affiliates. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Oracle - initial impl |
| ******************************************************************************/ |
| package model; |
| |
| import java.io.Externalizable; |
| import java.io.IOException; |
| import java.io.ObjectInput; |
| import java.io.ObjectOutput; |
| import java.io.Serializable; |
| import java.math.BigDecimal; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import javax.persistence.*; |
| import javax.xml.bind.annotation.XmlAttribute; |
| import javax.xml.bind.annotation.XmlID; |
| import javax.xml.bind.annotation.XmlRootElement; |
| |
| //import com.tangosol.io.pof.PofReader; |
| //import com.tangosol.io.pof.PofWriter; |
| //import com.tangosol.io.pof.PortableObject; |
| |
| /** |
| * Order |
| * @author James Sutherland |
| */ |
| @Entity |
| @Table(name="PERF_ORDER") |
| @XmlRootElement |
| public class Order implements Serializable { //Externalizable {//, PortableObject { |
| @Id |
| @GeneratedValue(generator="ORD_SEQ") |
| @TableGenerator(name="ORD_SEQ") |
| @Column(name="ORDER_ID") |
| @XmlID |
| @XmlAttribute |
| private long id; |
| @Basic |
| @XmlAttribute |
| private String description; |
| @Basic |
| @XmlAttribute |
| private BigDecimal totalCost = BigDecimal.valueOf(0); |
| @OneToMany(mappedBy="order", fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true) |
| @OrderBy("lineNumber") |
| private List<OrderLine> orderLines = new ArrayList<OrderLine>(); |
| @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST) |
| private Customer customer; |
| |
| public Order() { |
| } |
| |
| /*public void readExternal(PofReader in) throws IOException { |
| this.id = in.readLong(0); |
| this.description = in.readString(1); |
| this.totalCost = in.readBigDecimal(2); |
| this.customer = (Customer)in.readObject(3); |
| this.orderLines = (List)in.readCollection(4, new ArrayList()); |
| } |
| |
| public void writeExternal(PofWriter out) throws IOException { |
| out.writeLong(0, this.id); |
| out.writeString(1, this.description); |
| out.writeBigDecimal(2, this.totalCost); |
| out.writeObject(3, this.customer); |
| out.writeCollection(4, this.orderLines); |
| }*/ |
| |
| public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException { |
| this.id = stream.readLong(); |
| this.description = (String)stream.readObject(); |
| this.totalCost = (BigDecimal)stream.readObject(); |
| this.customer = (Customer)stream.readObject(); |
| this.orderLines = (List)stream.readObject(); |
| } |
| |
| public void writeExternal(ObjectOutput stream) throws IOException { |
| stream.writeLong(this.id); |
| stream.writeObject(this.description); |
| stream.writeObject(this.totalCost); |
| stream.writeObject(this.customer); |
| stream.writeObject(this.orderLines); |
| } |
| |
| public long getId() { |
| return id; |
| } |
| |
| public void setId(long id) { |
| this.id = id; |
| } |
| |
| public String getDescription() { |
| return description; |
| } |
| |
| public void setDescription(String description) { |
| this.description = description; |
| } |
| |
| public BigDecimal getTotalCost() { |
| return totalCost; |
| } |
| |
| public void setTotalCost(BigDecimal totalCost) { |
| this.totalCost = totalCost; |
| } |
| |
| public List<OrderLine> getOrderLines() { |
| return orderLines; |
| } |
| |
| public void setOrderLines(List<OrderLine> orderLines) { |
| this.orderLines = orderLines; |
| } |
| |
| public Customer getCustomer() { |
| return customer; |
| } |
| |
| public void setCustomer(Customer customer) { |
| this.customer = customer; |
| } |
| |
| /** |
| * Add the order line to the order, and set the back reference and update the order cost. |
| */ |
| public void addOrderLine(OrderLine orderLine) { |
| orderLine.setOrder(this); |
| getOrderLines().add(orderLine); |
| orderLine.setLineNumber(getOrderLines().size()); |
| setTotalCost(getTotalCost().add(orderLine.getCost())); |
| } |
| } |