CostLineCosting.java

package com.tradecloud.domain.costing.clean;

import com.tradecloud.domain.costing.CostGroup;
import com.tradecloud.domain.document.invoice.*;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Generic CostLineCosting entity which houses the necessary items to break a costing's cost lines down to line item level.
 */
@Embeddable
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "CostLineCosting")
public class CostLineCosting implements Serializable, Costing {

    public CostLineCosting() {
        super();
    }

    private static final long serialVersionUID = 1L;

    @Transient
    @XmlElementWrapper(name = "CalculatedCostLineCostingCells")
    @XmlElement(name = "CalculatedCostLineCostingCell")
    private List<CalculatedCostingCell> calculatedCostingCells = new ArrayList<CalculatedCostingCell>();

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @XmlElementWrapper(name = "CostLineCostingCells")
    @XmlElement(name = "CostLineCostingCell")
    @ForeignKey(name = "fk_costlinecostingparent", inverseName = "fk_costlinecostingcell")
    private List<CostLineCostingCell> costLineCostingCells = new ArrayList<CostLineCostingCell>();

    @Transient
    @XmlTransient
    private final Map<String, CostingCell> costLineCodeToCostLineAmountCache = new WeakHashMap<String, CostingCell>();

    @Override
    public List<CostLineCostingCell> getCostLineCostingCells() {
        return costLineCostingCells;
    }

    @Override
    public void setCostLineCostingCells(List<CostLineCostingCell> costLineCostingCells) {
        this.costLineCostingCells = costLineCostingCells;
    }

    @Override
    public List<CostLineCostingCell> getCostLineCostingCellsList() {
        return costLineCostingCells;
    }

    @Override
    public List<CalculatedCostingCell> getCalculatedCostingCells() {
        return calculatedCostingCells;
    }

    @Override
    public void setCalculatedCostingCells(List<CalculatedCostingCell> calculatedCostingCells) {
        this.calculatedCostingCells = calculatedCostingCells;
    }

    @Override
    public CostingCell getCachedCostLineCostingCell(String costLineCode) {
        return BaseActual.getCachedAmount(this, costLineCodeToCostLineAmountCache, costLineCode);
    }

    @Override
    public void clearCachedCostLineCostingCells() {
        costLineCodeToCostLineAmountCache.clear();
    }

    @Override
    public CostingCell getCachedCalculatedCostingCell(String columnCode) {
        return BaseActual.getCachedAmount(this, costLineCodeToCostLineAmountCache, columnCode);
    }

    @Override
    public CostingCell getCalculatedCostingCell(String columnCode) {
        for (CalculatedCostingCell calculatedCostingCell : calculatedCostingCells) {
            if (calculatedCostingCell.getCode().equals(columnCode)) {
                return calculatedCostingCell;
            }
        }
        return null;
    }

    @Override
    public CostingCell getCostingCell(String columnCode) {
        for (CostLineCostingCell costLineCostingCell : costLineCostingCells) {
            if (costLineCostingCell.getCode().equals(columnCode)) {
                return costLineCostingCell;
            }
        }
        return null;
    }

    @Override
    public List<CostLineCostingCell> getCostLineCostGroupCostingCells(CostGroup costGroup) {
        List<CostLineCostingCell> costGroupCostLineCostingCells = new ArrayList<>();
        for (CostLineCostingCell costLineCostingCell : costLineCostingCells) {
            if (costLineCostingCell.getCostLine().getCostLineTemplate().getCostGroup().equals(costGroup)) {
                costGroupCostLineCostingCells.add(costLineCostingCell);
            }
        }
        return costGroupCostLineCostingCells;
    }

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

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

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

    public void add(CostLineCostingCell cell) {
        this.costLineCostingCells.add(cell);
    }
}