Route.java

package com.tradecloud.domain.place;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.model.shipment.ShippingMode;
import com.tradecloud.domain.party.ServiceProvider;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

/**
 *
 */
@Entity
@Table(name = "route")
@NamedQueries({
        @NamedQuery(name = "findRouteById", query = "from Route route where route.id=:id")})
public class Route extends PersistenceBase {

    /**
     * The shipping mode that will be used to deliver the order. This should
     * default to the shipping mode configured in the client setup.
     */
    @XmlAttribute
    @NotNull
    @Basic(optional = false)
    @Enumerated(value = EnumType.STRING)
    private ShippingMode shippingMode;

    /**
     * The place where the order is loaded. This should default to the place of
     * loading configured in the client setup.
     */
    //@NotNull
    @ManyToOne
    @XmlElement(name = "PlaceOfLoading")
    @ForeignKey(name = "fk_placeofloading")
    private PlaceOfLoading placeOfLoading;

    /**
     * The place where the order is discharged. This should default to the place
     * of discharge configured in the client setup.
     */
    //@NotNull
    @ManyToOne
    @XmlElement(name = "PlaceOfDischarge")
    @ForeignKey(name = "fk_placeofdischarge")
    private PlaceOfDischarge placeOfDischarge;

    /**
     * The freight forwarder responsible for the order. This should default to
     * the the supplier's freight forwarder. If there is no freight forwarder
     * provided for the supplier then the freight forwarder configured in the
     * client setup will be used.
     */
    @NotNull
    @XmlElement(name = "Carrier")
    @ManyToOne
    @ForeignKey(name = "fk_carrier")
    private ServiceProvider carrier;

    public ShippingMode getShippingMode() {
        return shippingMode;
    }

    public void setShippingMode(ShippingMode shippingMode) {
        this.shippingMode = shippingMode;
    }

    public PlaceOfLoading getPlaceOfLoading() {
        return placeOfLoading;
    }

    public void setPlaceOfLoading(PlaceOfLoading placeOfLoading) {
        this.placeOfLoading = placeOfLoading;
    }

    public PlaceOfDischarge getPlaceOfDischarge() {
        return placeOfDischarge;
    }

    public void setPlaceOfDischarge(PlaceOfDischarge placeOfDischarge) {
        this.placeOfDischarge = placeOfDischarge;
    }

    public ServiceProvider getCarrier() {
        return carrier;
    }

    public void setCarrier(ServiceProvider carrier) {
        this.carrier = carrier;
    }

    @Override
    public String toString() {
        String toReturn = ""; // super.toString();

        String shippingModeStr = "Shipping Mode: '" + shippingMode + "', ";
        String placeOfLoadingStr = "Place of Loading: '" +
                (placeOfLoading != null ? placeOfLoading.getName() : "") + "', ";
        String placeOfDischargeStr = "Place of Discharge: '" +
                (placeOfDischarge != null ? placeOfDischarge.getName() : "") + "', ";
        String carrierStr = "Carrier: '" + (carrier != null ? carrier.getName() : "") + "', ";

        toReturn += shippingModeStr + placeOfLoadingStr +
                placeOfDischargeStr + carrierStr;

        return toReturn;
    }

    protected boolean objectsEqual(Object a, Object b) {
        if (a == null && b == null)
            return true;
        if (a == null && b != null)
            return false;
        if (!a.equals(b))
            return false;

        return true;
    }

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

        if (!super.equals(obj))
            return false;

        Route other = (Route) obj;

        if (other.getShippingMode() == shippingMode
                && objectsEqual(placeOfDischarge, other.getPlaceOfDischarge())
                && objectsEqual(placeOfLoading, other.getPlaceOfLoading())
                && objectsEqual(carrier, other.getCarrier())
        ) {
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().appendSuper(super.hashCode())
                .append(carrier)
                .append(placeOfDischarge)
                .append(placeOfLoading)
                .append(shippingMode)
                .toHashCode();
    }

}