BaseClearingInstruction.java

package com.tradecloud.domain.shipment.clearing;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.configuration.clearing.za.*;
import com.tradecloud.domain.model.organisationalunit.OrganisationalUnit;
import com.tradecloud.domain.model.shipment.ShippingMode;
import com.tradecloud.domain.party.Employee;
import com.tradecloud.domain.party.ServiceProvider;
import com.tradecloud.domain.place.Country;
import com.tradecloud.domain.sars.Status;
import com.tradecloud.domain.shipment.AirShipment;
import com.tradecloud.domain.shipment.SeaShipment;
import com.tradecloud.domain.shipment.Shipment;
import com.tradecloud.domain.state.Stateful;

import javax.persistence.*;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlElement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class
BaseClearingInstruction extends PersistenceBase implements Stateful<Status, ClearingEvent>, MultiModalType {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shipment_id")
    protected Shipment shipment;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "importer_id")
    protected OrganisationalUnit importer;
    @ManyToOne
    private Country destinationCountry;
    @Enumerated(EnumType.STRING)
    protected Status status = Status.NEW;
    @Enumerated(value = EnumType.STRING)
    protected CustomsValuation customsValuation;
    @Enumerated(value = EnumType.STRING)
    protected ShippingMode inlandTransport;
    @Enumerated(value = EnumType.STRING)
    protected Preference preference;
    @Enumerated(value = EnumType.STRING)
    @Column(name = "paymentMethod")
    protected PaymentMethod paymentMethod;
    @Enumerated(value = EnumType.STRING)
    protected TypeOfGoods typeOfGoods;
    @Enumerated(value = EnumType.STRING)
    protected ValuationMethod valuationMethod;
    //default to: As per attached
    protected String TDNNo = "As per attached";
    //default to:As per attached
    @Size(max = 255)
    protected String tariffHeadings = "As per attached";
    @Size(max = 255)
    protected String specialInstruction;
    @Size(max = 255)
    protected String deliveryAddress;
    protected boolean marineInsurance = false;
    @ManyToOne(fetch = FetchType.EAGER)
    protected Employee declarer;
    protected boolean restrictedGoods;
    @XmlElement(name = "Transporter")
    @ManyToOne(fetch = FetchType.LAZY)
    @org.hibernate.annotations.ForeignKey(name = "fk_transporter")
    protected ServiceProvider transporter;
    @ManyToOne(fetch = FetchType.LAZY)
    private ServiceProvider clearingAgent;
    @Transient
    protected SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    private String prcc;

    public OrganisationalUnit getImporter() {
        return importer;
    }

    @Override
    public Status getState() {
        return status;
    }

    @Override
    public void setState(Status state) {
        this.status = state;
    }

    public void setImporter(OrganisationalUnit importer) {
        this.importer = importer;
    }

    public CustomsValuation getCustomsValuation() {
        return customsValuation;
    }

    public void setCustomsValuation(CustomsValuation customsValuation) {
        this.customsValuation = customsValuation;
    }

    public ShippingMode getInlandTransport() {
        return inlandTransport;
    }

    public void setInlandTransport(ShippingMode inlandTransport) {
        this.inlandTransport = inlandTransport;
    }

    public TypeOfGoods getTypeOfGoods() {
        return typeOfGoods;
    }

    public void setTypeOfGoods(TypeOfGoods typeOfGoods) {
        this.typeOfGoods = typeOfGoods;
    }

    public ValuationMethod getValuationMethod() {
        return valuationMethod;
    }

    public void setValuationMethod(ValuationMethod valuationMethod) {
        this.valuationMethod = valuationMethod;
    }

    public String getTDNNo() {
        return TDNNo;
    }

    public void setTDNNo(String TDNNo) {
        this.TDNNo = TDNNo;
    }

    public String getTariffHeadings() {
        return tariffHeadings;
    }

    public void setTariffHeadings(String tariffHeadings) {
        this.tariffHeadings = tariffHeadings;
    }

    public String getSpecialInstruction() {
        return specialInstruction;
    }

    public void setSpecialInstruction(String specialInstruction) {
        this.specialInstruction = specialInstruction;
    }

    public Employee getDeclarer() {
        return declarer;
    }

    public void setDeclarer(Employee declarer) {
        //do not allow null once declarer has been set
        if (declarer != null)
            this.declarer = declarer;
    }

    public boolean isMarineInsurance() {
        return marineInsurance;
    }

    public void setMarineInsurance(boolean marineInsurance) {
        this.marineInsurance = marineInsurance;
    }

    public String getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setDeliveryAddress(String deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }

    public ServiceProvider getTransporter() {
        return transporter;
    }

    public void setTransporter(ServiceProvider transporter) {
        this.transporter = transporter;
    }

    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public boolean isRestrictedGoods() {
        return restrictedGoods;
    }

    public void setRestrictedGoods(boolean restrictedGoods) {
        this.restrictedGoods = restrictedGoods;
    }

    public Shipment getShipment() {
        return shipment;
    }

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

    public void setStatus(Status status) {
        this.status = status;
    }

    public Status getStatus() {
        if (status == null) {
            status = Status.NEW;
        }
        return status;
    }

    @Override
    public Boolean getActive() {
        return status != Status.DELETED;
    }

    @Override
    public boolean inNonEditableState() {
        return shipment.inNonEditableState();
    }

    public Preference getPreference() {
        return preference;
    }

    public void setPreference(Preference preference) {
        this.preference = preference;
    }

    public Country getDestinationCountry() {
        return destinationCountry;
    }

    public void setDestinationCountry(Country destinationCountry) {
        this.destinationCountry = destinationCountry;
    }

    public ServiceProvider getClearingAgent() {
        if (clearingAgent == null) {
            if (shipment != null && shipment.getShippingInfo() != null && shipment.getShippingInfo().getClearingAgent() != null) {
                clearingAgent = shipment.getShippingInfo().getClearingAgent();
            }
        }
        return clearingAgent;
    }

    public void setClearingAgent(ServiceProvider clearingAgent) {
        this.clearingAgent = clearingAgent;
    }

    public boolean isClearingInstruction() {
        return false;
    }

    public boolean isCustomsDeclaration() {
        return false;
    }

    public String getPrcc() {
        return prcc;
    }

    public void setPrcc(String prcc) {
        this.prcc = prcc;
    }

    public abstract String getDocumentGroupName();

    public abstract Set<AdditionalClearingInfo> getAdditionalClearingInfo();

    public abstract void setAdditionalClearingInfo(Set<AdditionalClearingInfo> additionalClearingInfo);

    public abstract Set<AdvancedPayment> getAdvancedPayments();

    public abstract void setAdvancedPayments(Set<AdvancedPayment> advancedPayments);

    public boolean isImports() {
        return getShipment().isImports();
    }

    public void addAdvancedPayments(AdvancedPayment advancedPayment) {
        if (getAdvancedPayments() == null)
            setAdvancedPayments(new HashSet<>());

        if (getAdvancedPayments().stream().noneMatch(p -> p.equals(advancedPayment)))
            getAdvancedPayments().add(advancedPayment);
    }

    public void addAdditionalClearingInfo(AdditionalClearingInfo info) {
        if (getAdditionalClearingInfo() == null)
            setAdditionalClearingInfo(new HashSet<>());

        if (getAdditionalClearingInfo().stream().noneMatch(p -> p.equals(info)))
            getAdditionalClearingInfo().add(info);
    }

    public Date getShippedOnBoardDate() {
        if (getShipment() != null) {
            if (shipment.getShippingMode() == ShippingMode.SEA) {
                return (((SeaShipment) shipment).getShippedOnBoardDate());
            } else if (shipment.getShippingMode() == ShippingMode.AIR) {
                return (((AirShipment) shipment).getShippedOnBoardDate());
            }
        }
        return null;
    }
}