AbstractSABSTariff.java

package com.tradecloud.domain.sabs;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.common.Currency;
import com.tradecloud.domain.event.Event;
import com.tradecloud.domain.event.SABSTariffEvent;
import com.tradecloud.domain.item.UnitType;
import com.tradecloud.domain.state.Stateful;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
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;
import javax.xml.bind.annotation.XmlElementWrapper;
import java.math.BigDecimal;
import java.util.*;

@MappedSuperclass
public abstract class AbstractSABSTariff extends PersistenceBase implements Stateful<SABSTariffState, SABSTariffEvent> {

    private final static Collection<SABSTariffState> NON_EDITABLE_STATES = Arrays.asList(SABSTariffState.LIQUIDATED, SABSTariffState.SUSPENDED);

    private static final long serialVersionUID = 1L;

    @ManyToOne
    @XmlElement(name = "Currency", required = true)
    @ForeignKey(name = "fk_currency")
    private Currency currency;

    @XmlElement(name = "EffectiveDate")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = true)
    private Date effectiveDate = new Date();

    @NotNull
    @Column(nullable = false)
    @XmlElement(name = "Code", required = true)
    private String code;

    @NotNull
    @Column(nullable = false)
    @XmlElement(name = "Description", required = true)
    private String description;

    @NotNull
    @Column(nullable = false)
    @XmlElement(name = "Category", required = true)
    private String category;

    @Column
    @XmlElement(name = "TariffValue")
    private BigDecimal tariffValue;

    @ManyToOne
    @XmlElement(name = "SABSSlidingScaleUnitValue")
    @JoinColumn(name = "sabsslidingscaleunitvalue_id", referencedColumnName = "id")
    @ForeignKey(name = "fk_sabsslidingscaleunitvalue")
    private SABSSlidingScaleUnitValue sabsSlidingScaleUnitValue = null;

    @Column
    @XmlElement(name = "UnitValue")
    private Integer unitValue;

    @ManyToOne
    @XmlElement(name = "UnitType")
    @JoinColumn(name = "unittype_code", referencedColumnName = "code")
    @ForeignKey(name = "fk_unittype")
    private UnitType unitType;

    @ManyToOne
    @XmlElement(name = "SABSSlidingScale")
    @JoinColumn(name = "sabsslidingscale_id", referencedColumnName = "id")
    @ForeignKey(name = "fk_sabsslidingscale")
    private SABSSlidingScale sabsSlidingScale = null;

    @Column
    @XmlElement(name = "TariffValue")
    private Boolean useSlidingScale = Boolean.TRUE;

    /**
     * SABSSlidingScaleState INITIALISED after object initialisation. (This is an internal
     * state only)
     */
    @NotNull
    @XmlAttribute
    @Enumerated(EnumType.STRING)
    private SABSTariffState state = SABSTariffState.COMPLETE;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @Fetch(value = FetchMode.SUBSELECT)
    @XmlElementWrapper(name = "SABSTariffEvents")
    @XmlElement(name = "SABSTariffEvent")
    @OrderBy("createDateTime")
    private List<SABSTariffEvent> events = new LinkedList<SABSTariffEvent>();

    public AbstractSABSTariff() {
        super();
    }

    public AbstractSABSTariff(String description, Date effectiveDate) {
        super();
        setDescription(description);
        setEffectiveDate(effectiveDate);
    }

    public AbstractSABSTariff(String description) {
        super();
        setDescription(description);
    }

    public AbstractSABSTariff(String description, Date effectiveDate, Currency currency, String code, String category) {
        super();
        setDescription(description);
        setEffectiveDate(effectiveDate);
        setCurrency(currency);
        setCode(code);
        setCategory(category);
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public Date getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(Date effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public BigDecimal getTariffValue() {
        return tariffValue;
    }

    public void setTariffValue(BigDecimal tariffValue) {
        this.tariffValue = tariffValue;
    }

    public SABSSlidingScaleUnitValue getSabsSlidingScaleUnitValue() {
        return sabsSlidingScaleUnitValue;
    }

    public void setSabsSlidingScaleUnitValue(SABSSlidingScaleUnitValue sabsSlidingScaleUnitValue) {
        this.sabsSlidingScaleUnitValue = sabsSlidingScaleUnitValue;
    }

    public SABSSlidingScale getSabsSlidingScale() {
        return sabsSlidingScale;
    }

    public void setSabsSlidingScale(SABSSlidingScale sabsSlidingScale) {
        this.sabsSlidingScale = sabsSlidingScale;
    }

    public Boolean getUseSlidingScale() {
        return useSlidingScale;
    }

    public void setUseSlidingScale(Boolean useSlidingScale) {
        this.useSlidingScale = useSlidingScale;
    }

    public SABSTariffState getState() {
        return state;
    }

    public void setState(SABSTariffState state) {
        this.state = state;
    }

    public List<SABSTariffEvent> getEvents() {
        return events;
    }

    public void setEvents(List<SABSTariffEvent> events) {
        this.events = events;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(effectiveDate).append(description).append(code).append(category).append(currency).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof AbstractSABSTariff)) {
            return false;
        }
        AbstractSABSTariff other = (AbstractSABSTariff) obj;
        return new EqualsBuilder().append(description, other.getDescription()).append(currency, other.getCurrency()).append(code, other.getCode())
                .append(category, other.getCategory()).append(effectiveDate, other.getEffectiveDate()).isEquals();
    }

    @Override
    public SABSTariffEvent getLastEvent() {
        return Event.getLastEvent(events);
    }

    @Override
    public boolean inNonEditableState() {
        return NON_EDITABLE_STATES.contains(state);
    }

    @Override
    public String toString() {

        return new StringBuilder("description=").append(description).
                append(",code=").append(code).
                append(",category=").append(category).
                append(",effectiveDate=").append(effectiveDate).toString();
    }

    public Integer getUnitValue() {
        return unitValue;
    }

    public void setUnitValue(Integer unitValue) {
        this.unitValue = unitValue;
    }

    public UnitType getUnitType() {
        return unitType;
    }

    public void setUnitType(UnitType unitType) {
        this.unitType = unitType;
    }
}