FECDealLink.java

package com.tradecloud.domain.model;

import com.tradecloud.domain.common.Percentage;
import com.tradecloud.domain.helper.XMLHelper;
import com.tradecloud.domain.model.deal.Deal;
import com.tradecloud.domain.model.fec.FEC;
import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;

@SuppressWarnings("serial")
@Entity
@Table(name = "fec_deal_link")
public class FECDealLink extends DealLink implements Serializable {

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private FEC fec;

    @Embedded
    @AttributeOverrides({@AttributeOverride(name = "currency", column = @Column(name = "rate_currency")),
            @AttributeOverride(name = "value", column = @Column(name = "rate_value", precision = 19, scale = 6))})
    private Money rate;

    @Column(name = "creation_date")
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private LocalDate creationDate;

    @Transient
    private Boolean isPartialDealLink = false;

    /**
     * This constructor is for Hibernate use only.
     */
    public FECDealLink() {
    }

    public FECDealLink(Deal deal, FEC fec, Money amount, Percentage percentage, Money rate) {
        super(deal, amount, percentage);
        this.fec = fec;
        this.rate = rate;
        creationDate = new LocalDate();
    }

    public Money getRate() {
        return rate;
    }

    public FEC getFec() {
        return fec;
    }

    public void updateRate(Money rate) {
        this.rate = rate;
    }

    public LocalDate getCreationDate() {
        return creationDate;
    }

    public Boolean getIsPartialDealLink() {
        return isPartialDealLink;
    }

    public void setIsPartialDealLink(Boolean isPartialDealLink) {
        this.isPartialDealLink = isPartialDealLink;
    }

    public String getStateAsXML() {
        StringBuilder result = new StringBuilder();

        result.append("<fecDealLink>");
        result.append("<dealId>").append(getDeal().getId()).append("</dealId>");
        result.append("<fecId>").append(fec.getId()).append("</fecId>");
        result.append("<amount>").append(XMLHelper.getMoneyXML(getAmount())).append("</amount>");
        result.append("<localCurrencyAmount>").append(XMLHelper.getLocalCurrencyXML(rate.getValue(), getAmount().getValue()))
                .append("</localCurrencyAmount>");
        result.append("<percentage>").append(getPercentage().getValue()).append("</percentage>");
        result.append("<rate>").append(XMLHelper.getMoneyXML(rate)).append("</rate>");
        result.append("<creationDate>").append(XMLHelper.getDateXML(creationDate)).append("</creationDate>");
        result.append("</fecDealLink>");

        return result.toString();
    }

    public boolean doesLinkHaveZeroAmount() {
        return getAmount().getValue().compareTo(BigDecimal.ZERO) == 0;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getAmount() == null) ? 0 : getAmount().hashCode());
        result = prime * result + ((creationDate == null) ? 0 : creationDate.hashCode());
        result = prime * result + ((getDeal() == null) ? 0 : getDeal().hashCode());
        result = prime * result + ((fec == null) ? 0 : fec.hashCode());
        result = prime * result + ((getPercentage() == null) ? 0 : getPercentage().hashCode());
        result = prime * result + ((rate == null) ? 0 : rate.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        FECDealLink other = (FECDealLink) obj;
        if (getAmount() == null) {
            if (other.getAmount() != null)
                return false;
        } else if (!getAmount().equals(other.getAmount()))
            return false;
        if (creationDate == null) {
            if (other.creationDate != null)
                return false;
        } else if (!creationDate.equals(other.creationDate))
            return false;
        if (getDeal() == null) {
            if (other.getDeal() != null)
                return false;
        } else if (!getDeal().equals(other.getDeal()))
            return false;
        if (fec == null) {
            if (other.fec != null)
                return false;
        } else if (!fec.equals(other.fec))
            return false;
        if (getPercentage() == null) {
            if (other.getPercentage() != null)
                return false;
        } else if (!getPercentage().equals(other.getPercentage()))
            return false;
        if (rate == null) {
            if (other.rate != null)
                return false;
        } else if (!rate.equals(other.rate))
            return false;
        return true;
    }
}