IntegratedSupplier.java

package com.tradecloud.domain.supplier;

import com.tradecloud.common.externalreference.ExternalReference;
import com.tradecloud.domain.BankingDetails;
import com.tradecloud.domain.model.payment.ActualPaymentBasis;
import com.tradecloud.domain.model.payment.PaymentMethod;
import com.tradecloud.domain.model.payment.PaymentTerm;
import com.tradecloud.domain.party.base.Address;
import com.tradecloud.domain.party.base.Contact;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.util.*;

@Entity
@Table(name = "integratedsupplier")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "IntegratedSupplier")
@NamedQueries({@NamedQuery(name = "findIntegratedSupplierByIdWithEventsLoaded",
        query = "from IntegratedSupplier supplier left join fetch supplier.events where supplier.id=:id")})
public class IntegratedSupplier extends AbstractSupplier implements Comparable<IntegratedSupplier> {

    /**
     * If this supplier is linked to an org unit we'll update the OrganisationalUnitSupplier details.
     * <p>
     * If this value is null then it's a change to the default supplier details.
     */
    @ElementCollection
    @XmlElementWrapper(name = "AccessibleClients")
    @XmlElement(name = "ClientCode")
    private Set<String> organisationalUnitCodes = new HashSet<String>();

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public IntegratedSupplier() {
        super();
        setIntegrated(true);
        setActive(Boolean.FALSE);
    }

    public IntegratedSupplier(PaymentTerm paymentTerm, PaymentMethod paymentMethod, ActualPaymentBasis actualPaymentBasis) {
        super();
        setPaymentTerm(paymentTerm);
        setPaymentMethod(paymentMethod);
        setActualPaymentBasis(actualPaymentBasis);
        setIntegrated(true);
        setActive(Boolean.FALSE);
    }

    public IntegratedSupplier(Address physicalAddress, Address postalAddress, List<Contact> contacts, String name, String externalReference,
                              String salesTaxRegistrationNumber, String vatRegistrationNumber, String companyRegistrationNumber, String warehouseCode,
                              List<BankingDetails> bankingDetails, PaymentTerm paymentTerm, PaymentMethod paymentMethod,
                              ActualPaymentBasis actualPaymentBasis) {
        super(physicalAddress, postalAddress, contacts, name, externalReference, salesTaxRegistrationNumber, vatRegistrationNumber,
                companyRegistrationNumber, warehouseCode);
        setPaymentTerm(paymentTerm);
        setPaymentMethod(paymentMethod);
        setActualPaymentBasis(actualPaymentBasis);
        setIntegrated(true);
        setActive(Boolean.FALSE);
    }

    /**
     * Creates a supplier from an Integrated Supplier.
     *
     * @param supplier
     */
    public IntegratedSupplier(OrganisationalUnitSupplier supplier) {
        setCompanyRegistrationNumber(supplier.getCompanyRegistrationNumber());
        setExternalReference(supplier.getExternalReference());
        setSalesTaxRegistrationNumber(supplier.getSalesTaxRegistrationNumber());
        setVatRegistrationNumber(supplier.getVatRegistrationNumber());
        setWarehouseCode(supplier.getWarehouseCode());
        setPhysicalAddress(new Address(supplier.getPhysicalAddress()));
        setPaymentMethod(supplier.getPaymentMethod());
        setPaymentTerm(supplier.getPaymentTerm());
        setEstimatedPaymentBasis(supplier.getEstimatedPaymentBasis());
        setActualPaymentBasis(supplier.getActualPaymentBasis());
        setCurrency(supplier.getCurrency());
        setIncoterm(supplier.getIncoterm());
        setUnitPricePerItem(supplier.getUnitPricePerItem());
        setFreightForwarder(supplier.getFreightForwarder());
        setClearingAgent(supplier.getClearingAgent());
        setTransporter(supplier.getTransporter());
        setNominatedBank(supplier.getNominatedBank());
        setAllowPartShipment(supplier.getAllowPartShipment());
        setAllowTransShipment(supplier.getAllowTransShipment());
        setLcToleranceAbove(supplier.getLcToleranceAbove());
        setLcToleranceBelow(supplier.getLcToleranceBelow());
        setName(supplier.getName());
        setPlaceOfExpiry(supplier.getPlaceOfExpiry());
        setPostalAddress(new Address(supplier.getPostalAddress()));
        setPresentationDays(supplier.getPresentationDays());
        setAdvisingBank(supplier.getAdvisingBank());
        setIntegrated(true);
        setActive(Boolean.FALSE);
        setType(supplier.getSupplier().getType());

        for (ExternalReference er : supplier.getExternalReferences()) {
            addExternalReference(new ExternalReference(er.getIntegratedSystem(), er.getReferenceValue()));
        }
    }

    /**
     * Creates a Integrated Supplier from an Supplier.
     *
     * @param supplier
     */
    public IntegratedSupplier(Supplier supplier) {
        setCompanyRegistrationNumber(supplier.getCompanyRegistrationNumber());
        setExternalReference(supplier.getExternalReference());
        setSalesTaxRegistrationNumber(supplier.getSalesTaxRegistrationNumber());
        setVatRegistrationNumber(supplier.getVatRegistrationNumber());
        setWarehouseCode(supplier.getWarehouseCode());
        if (supplier.getPhysicalAddress() != null)
            setPhysicalAddress(new Address(supplier.getPhysicalAddress()));
        setPaymentMethod(supplier.getPaymentMethod());
        setPaymentTerm(supplier.getPaymentTerm());
        setEstimatedPaymentBasis(supplier.getEstimatedPaymentBasis());
        setActualPaymentBasis(supplier.getActualPaymentBasis());
        setCurrency(supplier.getCurrency());
        setIncoterm(supplier.getIncoterm());
        setUnitPricePerItem(supplier.getUnitPricePerItem());
        setFreightForwarder(supplier.getFreightForwarder());
        setClearingAgent(supplier.getClearingAgent());
        setTransporter(supplier.getTransporter());
        setNominatedBank(supplier.getNominatedBank());
        setAllowPartShipment(supplier.getAllowPartShipment());
        setAllowTransShipment(supplier.getAllowTransShipment());
        setLcToleranceAbove(supplier.getLcToleranceAbove());
        setLcToleranceBelow(supplier.getLcToleranceBelow());
        setName(supplier.getName());
        setPlaceOfExpiry(supplier.getPlaceOfExpiry());
        if (supplier.getPostalAddress() != null)
            setPostalAddress(new Address(supplier.getPostalAddress()));
        setPresentationDays(supplier.getPresentationDays());
        setAdvisingBank(supplier.getAdvisingBank());
        if (supplier.getContacts().get(0) != null) {
            getContacts().add(new Contact(supplier.getContacts().get(0)));
        }
        setIntegrated(true);
        setActive(Boolean.FALSE);
        setType(supplier.getType());
    }

    @Override
    public int compareTo(IntegratedSupplier supplier) {
        if (getName() != null) {
            if (getName().compareTo(supplier.getName()) != 0) {
                return getName().compareTo(supplier.getName());
            }
        }
        return 0;
    }

    public Set<String> getOrganisationalUnitCodes() {
        return organisationalUnitCodes;
    }

    public List<String> getOrganisationalUnitCodesList() {
        return new ArrayList<String>(organisationalUnitCodes);
    }

    public void setOrganisationalUnitCodes(Set<String> organisationalUnitCodes) {
        this.organisationalUnitCodes = organisationalUnitCodes;
    }

    @Override
    public boolean inNonEditableState() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        hashCodeBuilder.appendSuper(super.hashCode());
        if (organisationalUnitCodes != null) {
            List<String> list = new ArrayList<String>(organisationalUnitCodes);
            Collections.sort(list);
            for (String organisationalUnitCode : list) {
                hashCodeBuilder.append(organisationalUnitCode);
            }
        }
        return hashCodeBuilder.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof IntegratedSupplier)) {
            return false;
        }
        IntegratedSupplier other = (IntegratedSupplier) obj;

        if (organisationalUnitCodes != null && other.organisationalUnitCodes != null) {
            if (organisationalUnitCodes.size() == other.organisationalUnitCodes.size()) {
                for (String organisationalUnitCode : organisationalUnitCodes) {
                    if (!other.organisationalUnitCodes.contains(organisationalUnitCode)) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
        return new EqualsBuilder().appendSuper(super.equals(obj)).isEquals();
    }
}