Agent.java

package com.tradecloud.domain.agent;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tradecloud.domain.model.payment.*;
import com.tradecloud.domain.party.ServiceProvider;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@Entity
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "Agent")
@Table(name = "agent")
public class Agent extends AbstractAgent implements Comparable<Agent> {

    @ManyToOne
    @JsonIgnore
    private ServiceProvider serviceProvider;

    public Agent() {
    }

    public Agent(String name) {
        super(name);
    }

    public Agent(String name, String externalReference) {
        super(name, externalReference);
    }

    public Agent(List<AgentTypes> agentTypes, PaymentTerm paymentTerm, PaymentMethod paymentMethod, EstimatedPaymentBasis estimatedPaymentBasis,
                 ActualPaymentBasis actualPaymentBasis, AgentCommissionBasedOn agentCommissionBasedOn) {
        super();
        setAgentTypes(agentTypes);
        setPaymentTerm(paymentTerm);
        setPaymentMethod(paymentMethod);
        setEstimatedPaymentBasis(estimatedPaymentBasis);
        setActualPaymentBasis(actualPaymentBasis);
        setCommissionBasedOn(agentCommissionBasedOn);
    }

    private static final long serialVersionUID = 1L;

    @Override
    public int compareTo(Agent agent) {
        if (getName() != null) {
            if (getName().compareTo(agent.getName()) != 0) {
                return getName().compareTo(agent.getName());
            }
        }
        if (getExternalReference() != null) {
            if (getExternalReference().compareTo(agent.getExternalReference()) != 0) {
                return getExternalReference().compareTo(agent.getExternalReference());
            }
        }

        if (getSalesTaxRegistrationNumber() != null) {
            if (getSalesTaxRegistrationNumber().compareTo(agent.getSalesTaxRegistrationNumber()) != 0) {
                return getSalesTaxRegistrationNumber().compareTo(agent.getSalesTaxRegistrationNumber());
            }
        }

        if (getVatRegistrationNumber() != null) {
            if (getVatRegistrationNumber().compareTo(agent.getVatRegistrationNumber()) != 0) {
                return getVatRegistrationNumber().compareTo(agent.getVatRegistrationNumber());
            }
        }

        if (getCompanyRegistrationNumber() != null) {
            if (getCompanyRegistrationNumber().compareTo(agent.getCompanyRegistrationNumber()) != 0) {
                return getCompanyRegistrationNumber().compareTo(agent.getCompanyRegistrationNumber());
            }
        }

        if (getWarehouseCode() != null) {
            if (getWarehouseCode().compareTo(agent.getWarehouseCode()) != 0) {
                return getWarehouseCode().compareTo(agent.getWarehouseCode());
            }
        }

        if (getCurrency() != null) {
            if (getCurrency().compareTo(agent.getCurrency()) != 0) {
                return getCurrency().compareTo(agent.getCurrency());
            }
        }

        if (getPaymentMethod() != null) {
            if (getPaymentMethod().compareTo(agent.getPaymentMethod()) != 0) {
                return getPaymentMethod().compareTo(agent.getPaymentMethod());
            }
        }

        return 0;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(super.toString()).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Agent)) {
            return false;
        }
        Agent other = (Agent) obj;
        return new EqualsBuilder().appendSuper(super.equals(obj)).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().appendSuper(super.hashCode()).toHashCode();
    }

    @XmlID
    public String getIdStr() {
        return String.valueOf(super.getId());
    }

    public String getAgentTypeStr() {
        List<AgentTypes> agentTypeList = getAgentTypes();
        StringBuilder agentTypBuilder = new StringBuilder();
        for (AgentTypes agentTypes : agentTypeList) {
            agentTypBuilder.append(agentTypes.getType().toString()).append(',');
        }
        String result = agentTypBuilder.toString();
        if (result.length() >= 1) {
            if (result.lastIndexOf(',') == result.length() - 1) {
                result = result.substring(0, result.length() - 1);
            }
        }

        return result;
    }

    public ServiceProvider getServiceProvider() {
        return serviceProvider;
    }

    public void setServiceProvider(ServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    //below get and set is workaround for jsf as it getting confused between isServiceProvider and getServiceProvider
    public void setServiceProviderObj(ServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    public ServiceProvider getServiceProviderObj() {
        return serviceProvider;
    }
}