HfcOrder.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradecloud.domain.model.hfcmanagement;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.common.Currency;
import com.tradecloud.domain.common.Incoterm;
import com.tradecloud.domain.model.elcordermanagement.ElcOrderStatus;
import com.tradecloud.domain.model.ordermanagement.OrderDates;
import com.tradecloud.domain.shipment.ShippingInformation;
import com.tradecloud.domain.supplier.Supplier;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.NaturalId;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Entity
@DiscriminatorValue("HFC_ORDER")
@Access(AccessType.FIELD)
@Table(name = "hfcOrder")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "HfcOrder")
public class HfcOrder extends PersistenceBase {

    private static final long serialVersionUID = -6810282606308665575L;
    @NaturalId(mutable = true)
    @NotNull
    @XmlID
    // XML id used for setting parent order in child lineitem
    private String hfcReference;
    @ManyToOne
    @NotNull
    private Supplier supplier;
    @ManyToOne
    private EventLabel eventLabel;
    private boolean includeCustomsDuty;
    @ManyToOne
    @NotNull
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    //TODO: create hfcShippingInformation instead of sharing shippingInformation.
    private ShippingInformation shippingInformation = new ShippingInformation();
    private String description;
    @ManyToOne
    @NotNull
    private Incoterm incoterm;
    @NotNull
    private String buyer;
    @Embedded
    private OrderDates orderDates = new OrderDates();
    @Enumerated(EnumType.STRING)
    private ElcOrderStatus status;
    //TODO: remove this field completely. Product group linked through product group reference.
    @Transient
    private ProductGroup productGroup;
    private String productGroupReference;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Currency currency;
    private double factor;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @XmlElementWrapper(name = "simpleProducts")
    @XmlElement(name = "simpleProduct")
    private List<SimpleProduct> simpleProducts = new ArrayList<SimpleProduct>();
    private int orderCount;

    public String getHfcReference() {
        return hfcReference;
    }

    public void setHfcReference(String hfcReference) {
        this.hfcReference = hfcReference;
    }

    public EventLabel getEventLabel() {
        return eventLabel;
    }

    public void setEventLabel(EventLabel eventLabel) {
        this.eventLabel = eventLabel;
    }

    public boolean isIncludeCustomsDuty() {
        return includeCustomsDuty;
    }

    public void setIncludeCustomsDuty(boolean includeCustomsDuty) {
        this.includeCustomsDuty = includeCustomsDuty;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public ShippingInformation getShippingInformation() {
        return shippingInformation;
    }

    public void setShippingInformation(ShippingInformation shippingInformation) {
        this.shippingInformation = shippingInformation;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Incoterm getIncoterm() {
        return incoterm;
    }

    public void setIncoterm(Incoterm incoterm) {
        this.incoterm = incoterm;
    }

    public String getBuyer() {
        return buyer;
    }

    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }

    public OrderDates getOrderDates() {
        return orderDates;
    }

    public void setOrderDates(OrderDates orderDates) {
        this.orderDates = orderDates;
    }

    public ElcOrderStatus getStatus() {
        return status;
    }

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

    public ProductGroup getProductGroup() {
        return productGroup;
    }

    public void setProductGroup(ProductGroup productGroup) {
        this.productGroup = productGroup;
    }

    public double getFactor() {
        return factor;
    }

    public void setFactor(double factor) {
        this.factor = factor;
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public void overwriteFieldsFromSupplier() {
        if (supplier != null) {
            this.setCurrency(supplier.getCurrency());
            if (this.incoterm == null) {
                this.incoterm = supplier.getIncoterm();
            }
        }
    }

    public List<SimpleProduct> getSimpleProducts() {
        return simpleProducts;
    }

    public void setSimpleProducts(List<SimpleProduct> simpleProducts) {
        this.simpleProducts = simpleProducts;
    }

    public String getFormattedCreated() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date created = new Date();
        return sdf.format(created);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 13 * hash + (this.hfcReference != null ? this.hfcReference.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final HfcOrder other = (HfcOrder) obj;
        if ((this.hfcReference == null) ? (other.hfcReference != null) : !this.hfcReference.equals(other.hfcReference)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "HfcOrder [" + (hfcReference != null ? "hfcReference=" + hfcReference + ", " : "") + (supplier != null ? "supplier=" + supplier + "," +
                "" + "" + " " : "") + (eventLabel != null ? "eventLabel=" + eventLabel + ", " : "") + "includeCustomsDuty=" + includeCustomsDuty +
                ", " + "" + "" + (shippingInformation != null ? "shippingInformation=" + shippingInformation + ", " +
                "" + "" + "" : "") + (description != null ? "description=" + description + ", " : "") + (incoterm != null ? "incoterm=" + incoterm
                + ", " + "" + "" : "") + (buyer != null ? "buyer=" + buyer + ", " : "") + (orderDates != null ? "orderDates=" + orderDates + ", " +
                "" + "" + "" : "") + (status != null ? "status=" + status + ", " : "") + (productGroup != null ? "productGroup=" + productGroup +
                ", " + "" + "" : "") + (currency != null ? "currency=" + currency + ", " : "") + "factor=" + factor + ", " +
                "" + "" + "" + (simpleProducts != null ? "simpleProducts=" + simpleProducts : "") + "]";
    }

    public String toStringHeadingsForCsv() {

        return "HFC description,HFC created date,Shipment Date,Port of Loading,Country of Origin,Sub Class,Supplier,Currency,HFC";

    }

    public String getFormattedLatestShipmentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date latestShipmentDate = getOrderDates().getLatestShipmentDate();
        return sdf.format(latestShipmentDate);
    }

    public String toStringForCsv() {
        String csvString = "";

        csvString += description + "##" + getFormattedCreated() + "##";

        csvString += (getFormattedLatestShipmentDate() != null ? getFormattedLatestShipmentDate() : "") + "##";

        csvString += (getShippingInformation().getPlaceOfLoading() != null ? getShippingInformation().getPlaceOfLoading().getName() : "") + "##";
        csvString += (getShippingInformation().getCountryOfOrigin() != null ? getShippingInformation().getCountryOfOrigin().getName() : "") + "##";
        csvString += getProductGroupReference() + "##";
        csvString += (getSupplier() != null ? getSupplier().getName() : "") + "##";
        csvString += (getCurrency() != null ? getCurrency().getCode() : "") + "##";
        csvString += (getFactor() + "##");

        return csvString;

    }

    public int getOrderCount() {
        return orderCount;
    }

    public void setOrderCount(int orderCount) {
        this.orderCount = orderCount;
    }

    public void setProductGroupReference(String productGroupReference) {
        this.productGroupReference = productGroupReference;
    }

    public String getProductGroupReference() {
        return productGroupReference;
    }
}