PackingListContainer.java

package com.tradecloud.domain.container;

import com.tradecloud.common.base.PersistenceBase;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Entity
@Table(name = "packinglistcontainer")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "PackingListContainer")
@NamedQueries({@NamedQuery(name = "packingListContainer.findByPackingListAndReference",
        query = "from PackingListContainer plc where plc.packingList = :packingList and plc.reference = :reference order by created desc")})
public class PackingListContainer extends PersistenceBase implements TypeOfContainer {

    private static final long serialVersionUID = 1L;

    private BigDecimal noOfCartons;

    /**
     * The bidirectional link back to the parent {@link PackingList}.
     */
    @ManyToOne(optional = false)
    @ForeignKey(name = "fk_packinglist")
    private PackingList packingList;

    /**
     * This is the reference from {@code ShipmentContainer}.  We add it at this level for convenience so a full load of the shipment container is
     * not required.
     */
    @XmlAttribute(required = true)
    @XmlID
    private String reference;

    @XmlElement(name = "ContainerType")
    @ManyToOne
    @NotNull
    private ContainerType containerType;

    @XmlElementWrapper(name = "PackingListOrders")
    @XmlElement(name = "PackingListOrder")
    @ForeignKey(name = "fk_packinglistcontainer")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "packingListContainer", orphanRemoval = true, fetch = FetchType.LAZY)
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("id, created")
    private List<PackingListOrder> orders = new ArrayList<PackingListOrder>();

    @XmlTransient
    @Transient
    private Map<Long, PackingListTotals> packingListTotals;
    @XmlTransient
    @Transient
    private Long shipmentId;

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public PackingList getPackingList() {
        return packingList;
    }

    public void setPackingList(PackingList packingList) {
        this.packingList = packingList;
    }

    public List<PackingListOrder> getOrders() {
        return orders;
    }

    public void setOrders(List<PackingListOrder> orders) {
        this.orders = orders;
    }

    public void addOrder(PackingListOrder order) {
        order.setPackingListContainer(this);
        orders.add(order);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof PackingListContainer)) {
            return false;
        }
        PackingListContainer other = (PackingListContainer) obj;
        return new EqualsBuilder().append(packingList, other.packingList).append(reference, other.reference).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(packingList).append(reference).toHashCode();
    }

    public ContainerType getContainerType() {
        return containerType;
    }

    public void setContainerType(ContainerType containerType) {
        this.containerType = containerType;
    }

    public void setPackingListTotals(Map<Long, PackingListTotals> packingListTotals) {
        this.packingListTotals = packingListTotals;
    }

    public Map<Long, PackingListTotals> getPackingListTotals() {
        return packingListTotals;
    }

    public Long getKey() {
        return getId() + PackingListContainer.class.hashCode();
    }

    public boolean isPacked() {
        if (orders != null) {
            return !orders.isEmpty();
        }

        return false;
    }

    public Long getShipmentId() {
        return shipmentId;
    }

    public void setShipmentId(Long shipmentId) {
        this.shipmentId = shipmentId;
    }

    public BigDecimal getNoOfCartons() {
        return noOfCartons;
    }

    public void setNoOfCartons(BigDecimal noOfCartons) {
        this.noOfCartons = noOfCartons;
    }
}