ExternalReference.java

package com.tradecloud.common.externalreference;

import com.tradecloud.common.base.PersistenceBase;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * External Reference mapping.
 * <p>
 * Can map external references for our entities over multiple external/third party systems.
 * <p>
 * E.g. EXPEDITORS -> APPLE_REF_123
 *
 * @author ronan
 */
@Entity
@Table(name = "externalreference")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ExternalReference")
public class ExternalReference extends PersistenceBase {

    @ManyToOne
    @XmlElement(name = "IntegratedSystem")
    @NotNull
    private IntegratedSystem integratedSystem;

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

    /**
     * Constructor.
     */
    public ExternalReference(IntegratedSystem integratedSystem, String referenceValue) {
        this.integratedSystem = integratedSystem;
        this.referenceValue = referenceValue;
    }

    public ExternalReference() {
    }

    public IntegratedSystem getIntegratedSystem() {
        return integratedSystem;
    }

    public void setIntegratedSystem(IntegratedSystem integratedSystem) {
        this.integratedSystem = integratedSystem;
    }

    public String getReferenceValue() {
        return referenceValue;
    }

    public void setReferenceValue(String referenceValue) {
        this.referenceValue = referenceValue;
    }

    @Override
    public String toString() {
        return "ExternalReference[" + integratedSystem + ":" + referenceValue + "]";
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(integratedSystem != null ? integratedSystem.getCode() : "").append(referenceValue);
        return builder.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ExternalReference other = (ExternalReference) obj;

        EqualsBuilder builder = new EqualsBuilder();
        builder.append(integratedSystem != null ? integratedSystem.getCode() : "", other.integratedSystem.getCode())
                .append(referenceValue, other.referenceValue);
        return builder.isEquals();
    }

}