Company.java

package com.tradecloud.domain.party.base;

import com.tradecloud.common.base.HibernateUtils;
import com.tradecloud.domain.BankAccount;
import com.tradecloud.domain.common.IntegratedPersistenceBase;
import com.tradecloud.domain.supplier.SwiftCompliantAddress;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.ForeignKey;

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

/**
 * Represents a company in our system.
 */
@MappedSuperclass
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Company extends IntegratedPersistenceBase {

    /**
     * UID.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Is the entity 'active'. Should it be displayed in drop downs on the
     * system. Used for soft deletes of entities that can't be hard deleted due
     * to referential constraints.
     */
    @XmlAttribute
    protected Boolean active = Boolean.TRUE;
    // not deprecated - we're using it to uniquely identify the company, on for
    // example rates uploads
    @XmlAttribute
    @Size(max = 40, message = "External Reference cannot be longer than 40 characters")
    protected String externalReference;
    /**
     * Company Name.
     */
    @NotNull(message = "Name is required")
    @XmlAttribute
    private String name;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @XmlElement(name = "PhysicalAddress")
    private Address physicalAddress;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @XmlElement(name = "PostalAddress")
    private Address postalAddress;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @XmlElement(name = "SwiftCompliantAddress")
    @ForeignKey(name = "fk_swiftaddress")
    private SwiftCompliantAddress swiftCompliantAddress;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @XmlElementWrapper(name = "Contacts", nillable = true)
    @XmlElement(name = "Contact")
    private List<Contact> contacts = new ArrayList<>();

    @XmlAttribute
    private String salesTaxRegistrationNumber;

    @XmlAttribute
    private String vatRegistrationNumber;

    @XmlAttribute
    private String companyRegistrationNumber;

    @XmlAttribute
    private String warehouseCode;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<BankAccount> bankAccounts = new ArrayList<>();

    public Company() {
    }

    public Company(String name) {
        super();
        this.name = name;
    }

    public Company(Address physicalAddress, Address postalAddress, List<Contact> contacts, String name, String externalReference,
                   String salesTaxRegistrationNumber, String vatRegistrationNumber, String companyRegistrationNumber, String warehouseCode) {
        super();
        this.name = name;
        this.externalReference = externalReference;

        this.physicalAddress = physicalAddress;
        this.postalAddress = postalAddress;
        this.contacts = contacts;

        this.salesTaxRegistrationNumber = salesTaxRegistrationNumber;
        this.vatRegistrationNumber = vatRegistrationNumber;
        this.companyRegistrationNumber = companyRegistrationNumber;
        this.warehouseCode = warehouseCode;
    }

    public Company(Address physicalAddress, Address postalAddress, List<Contact> contacts, String name, String externalReference,
                   String salesTaxRegistrationNumber, String vatRegistrationNumber, String companyRegistrationNumber, String warehouseCode,
                   SwiftCompliantAddress swiftCompliantAddress) {
        super();
        this.name = name;
        this.externalReference = externalReference;

        this.physicalAddress = physicalAddress;
        this.postalAddress = postalAddress;
        this.swiftCompliantAddress = swiftCompliantAddress;
        this.contacts = contacts;

        this.salesTaxRegistrationNumber = salesTaxRegistrationNumber;
        this.vatRegistrationNumber = vatRegistrationNumber;
        this.companyRegistrationNumber = companyRegistrationNumber;
        this.warehouseCode = warehouseCode;
    }

    public List<BankAccount> getBankAccounts() {
        return bankAccounts;
    }

    public void setBankAccounts(List<BankAccount> bankAccounts) {
        this.bankAccounts = bankAccounts;
    }

    public Address getPhysicalAddress() {
        return physicalAddress;
    }

    public void setPhysicalAddress(Address physicalAddress) {
        this.physicalAddress = physicalAddress;
    }

    public Address getPostalAddress() {
        return postalAddress;
    }

    public void setPostalAddress(Address postalAddress) {
        this.postalAddress = postalAddress;
    }

    public List<Contact> getContacts() {
        return contacts;
    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;
    }

    /**
     * @deprecated use getExternalReferences
     */
    @Deprecated
    public String getExternalReference() {
        return externalReference;
    }

    /**
     * @deprecated use setExternalReference
     */
    @Deprecated
    public void setExternalReference(String externalReference) {
        this.externalReference = externalReference;
    }

    public String getCode() {
        return externalReference;
    }

    public String getSalesTaxRegistrationNumber() {
        return salesTaxRegistrationNumber;
    }

    public void setSalesTaxRegistrationNumber(String salesTaxRegistrationNumber) {
        this.salesTaxRegistrationNumber = salesTaxRegistrationNumber;
    }

    public String getVatRegistrationNumber() {
        return vatRegistrationNumber;
    }

    public void setVatRegistrationNumber(String vatRegistrationNumber) {
        this.vatRegistrationNumber = vatRegistrationNumber;
    }

    public String getCompanyRegistrationNumber() {
        return companyRegistrationNumber;
    }

    public void setCompanyRegistrationNumber(String companyRegistrationNumber) {
        this.companyRegistrationNumber = companyRegistrationNumber;
    }

    public String getWarehouseCode() {
        return warehouseCode;
    }

    public void setWarehouseCode(String warehouseCode) {
        this.warehouseCode = warehouseCode;
    }

    @Override
    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!HibernateUtils.proxyClassEquals(this, obj)) {
            return false;
        }
        Company other = (Company) obj;
        return new EqualsBuilder().append(name, other.getName()).append(externalReference, other.getExternalReference()).isEquals();
    }

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

    @Override
    public String toString() {
        // Address and contacts are lazy init. So do not put them here.
        return new ToStringBuilder(this).append(name).append(externalReference).append(salesTaxRegistrationNumber).append(vatRegistrationNumber)
                .append(companyRegistrationNumber).append(warehouseCode).toString();
    }

    public SwiftCompliantAddress getSwiftCompliantAddress() {
        return swiftCompliantAddress;
    }

    public void setSwiftCompliantAddress(SwiftCompliantAddress swiftCompliantAddress) {
        this.swiftCompliantAddress = swiftCompliantAddress;
    }

    public String externalRefAndName() {
        return externalReference + " - " + name;
    }

    public boolean isCanContact() {
        return false;
    }

    public boolean isServiceProvider() {
        return false;
    }

}