SubShipment.java

package com.tradecloud.domain.shipment;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.comment.AddedComment;
import com.tradecloud.domain.comment.CommentType;
import com.tradecloud.domain.comment.Commentable;
import com.tradecloud.domain.event.Event;
import com.tradecloud.domain.event.SubShipmentEvent;
import com.tradecloud.domain.model.ordermanagement.Order;
import com.tradecloud.domain.state.Stateful;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
import java.util.*;

@Entity
@Table(name = "SubShipment", uniqueConstraints = {@UniqueConstraint(columnNames = {"reference"})})
@Inheritance(strategy = InheritanceType.JOINED)
@Access(AccessType.FIELD)
public class SubShipment extends PersistenceBase implements Commentable<AddedComment>, Stateful<SubShipmentState, SubShipmentEvent> {

    @NotNull(message = "reference is required")
    private String reference;

    @XmlElementWrapper(name = "Orders")
    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @Fetch(value = FetchMode.SUBSELECT)
    @XmlElement(name = "Order")
    private Set<Order> orders = new LinkedHashSet<Order>();

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @XmlElementWrapper(name = "SubShipmentEvents")
    @XmlElement(name = "SubShipmentEvent")
    @OrderBy("createDateTime")
    private List<SubShipmentEvent> events = new LinkedList<SubShipmentEvent>();

    @Enumerated(EnumType.STRING)
    private SubShipmentState state;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shipment_id")
    @XmlTransient
    private Shipment shipment;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "subshipment_freetextcomments", joinColumns = {@JoinColumn(name = "subshipment_id", unique = false)})
    @Column(name = "reason", unique = true)
    @org.hibernate.annotations.ForeignKey(name = "fk_subshipment")
    @Fetch(value = FetchMode.SUBSELECT)
    @XmlElementWrapper(name = "FreeTextComments")
    @XmlElement(name = "FreeTextComment")
    private List<AddedComment> comments = new ArrayList<AddedComment>();

    private String houseBillOfLadingReference;
    private Date houseBillOfLadingDate;

    public String getReference() {
        return reference;
    }

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

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    @Override
    public SubShipmentEvent getLastEvent() {
        return Event.getLastEvent(events);
    }

    public List<SubShipmentEvent> getEvents() {
        return events;
    }

    @Override
    public boolean inNonEditableState() {
        return false;
    }

    public void setEvents(List<SubShipmentEvent> events) {
        this.events = events;
    }

    public SubShipmentState getState() {
        return state;
    }

    public void setState(SubShipmentState state) {
        this.state = state;
    }

    @Override
    public List<AddedComment> getComments() {
        return comments;
    }

    @Override
    public void setComments(List<AddedComment> comments) {
        this.comments = comments;
    }

    @Override
    public CommentType getCommentType() {
        return CommentType.SUB_SHIPMENT;
    }

    public Shipment getShipment() {
        return shipment;
    }

    public void setShipment(Shipment shipment) {
        this.shipment = shipment;
    }

    public void addOrder(Order order) {
        order.setSubShipment(this);
        orders.add(order);
    }

    public void removeOrder(Order order) {
        order.setSubShipment(null);
        orders.remove(order);
    }

    public String getHouseBillOfLadingReference() {
        return houseBillOfLadingReference;
    }

    public void setHouseBillOfLadingReference(String houseBillOfLadingReference) {
        this.houseBillOfLadingReference = houseBillOfLadingReference;
    }

    public Date getHouseBillOfLadingDate() {
        return houseBillOfLadingDate;
    }

    public void setHouseBillOfLadingDate(Date houseBillOfLadingDate) {
        this.houseBillOfLadingDate = houseBillOfLadingDate;
    }
}