RateSearch.java

package com.tradecloud.dto.rate;

import com.tradecloud.domain.common.Currency;
import com.tradecloud.domain.costing.CostingType;
import com.tradecloud.domain.dto.base.SearchBase;
import com.tradecloud.domain.item.ItemCategory;
import com.tradecloud.domain.model.organisationalunit.OrganisationalUnit;
import com.tradecloud.domain.rate.Rate;
import com.tradecloud.domain.rate.RateLookupPriorityConfig;
import com.tradecloud.domain.rate.RateUnit;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Rate search base class.
 */
@SuperBuilder
@NoArgsConstructor
public abstract class RateSearch extends SearchBase implements Serializable {

    private static final long serialVersionUID = 1L;

    protected String costLine;

    protected Date effectiveDateLower;

    protected Date effectiveDateUpper;

    private Currency currency;

    protected CostingType costingType;

    // Some fields in the table can be null, which will represent 'all'.
    // So when looking for a rate, you want to return these rows, even if the
    // query has a value
    private boolean allowNull = false;

    // if this is true, null values in the search must match null in the DB,
    //  if false, null represents 'all'
    private boolean specificSearch = false;

    private List<RateLookupPriorityConfig> priorityConfig;
    //when duplicating newDuplicatedRates, we need to return only new duplicates.
    private List<Rate> newDuplicatedRates;

    protected OrganisationalUnit organisationalUnit;

    protected RateUnit rateUnit;

    protected boolean costCompareOnly;

    private ItemCategory itemCategory;

    public RateSearch(RateSearch rateSearch) {
        super();
        this.costLine = rateSearch.costLine;
        this.effectiveDateLower = rateSearch.effectiveDateLower;
        this.effectiveDateUpper = rateSearch.effectiveDateUpper;
        this.currency = rateSearch.currency;
        this.costingType = rateSearch.costingType;
        this.allowNull = rateSearch.allowNull;
        this.specificSearch = rateSearch.specificSearch;
        this.rateUnit = rateSearch.rateUnit;
        this.costCompareOnly = rateSearch.costCompareOnly;
        this.itemCategory = rateSearch.itemCategory;
        if (rateSearch.priorityConfig != null) {
            this.priorityConfig = new ArrayList<RateLookupPriorityConfig>(rateSearch.priorityConfig.size());
            for (RateLookupPriorityConfig rateLookupPriorityConfig : rateSearch.priorityConfig) {
                this.priorityConfig.add(rateLookupPriorityConfig);
            }
        }
    }

    public RateSearch(boolean allowNull) {
        this.allowNull = allowNull;
    }

    public String getCostLine() {
        return costLine;
    }

    public void setCostLine(String costLine) {
        this.costLine = costLine;
    }

    public Date getEffectiveDateLower() {
        return effectiveDateLower;
    }

    public void setEffectiveDateLower(Date effectiveDateLower) {
        this.effectiveDateLower = effectiveDateLower;
    }

    public Date getEffectiveDateUpper() {
        return effectiveDateUpper;
    }

    public void setEffectiveDateUpper(Date effectiveDateUpper) {
        this.effectiveDateUpper = effectiveDateUpper;
    }

    public boolean isAllowNull() {
        return allowNull;
    }

    public void setAllowNull(boolean allowNull) {
        this.allowNull = allowNull;
    }

    public boolean isSpecificSearch() {
        return specificSearch;
    }

    public void setSpecificSearch(boolean specificSearch) {
        this.specificSearch = specificSearch;
    }

    public OrganisationalUnit getOrganisationalUnit() {
        return this.organisationalUnit;
    }

    public void setOrganisationalUnit(OrganisationalUnit organisationalUnit) {
        this.organisationalUnit = organisationalUnit;
    }

    public void reset() {
        costLine = null;
        effectiveDateLower = null;
        effectiveDateUpper = null;
        costingType = null;
        newDuplicatedRates = null;
        this.costCompareOnly = false;
        itemCategory = null;
    }

    public List<RateLookupPriorityConfig> getPriorityConfig() {
        return priorityConfig;
    }

    public void setPriorityConfig(List<RateLookupPriorityConfig> priorityConfig) {
        this.priorityConfig = priorityConfig;
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
        if (currency != null) {
            this.rateUnit = RateUnit.CURRENCY;
        }
    }

    public CostingType getCostingType() {
        return costingType;
    }

    public void setCostingType(CostingType costingType) {
        this.costingType = costingType;
    }

    public RateUnit getRateUnit() {
        return rateUnit;
    }

    public void setRateUnit(RateUnit rateUnit) {
        this.rateUnit = rateUnit;
    }

    @Override
    public String toString() {
        return "RateSearch{" +
                "costLine='" + costLine + '\'' +
                ", effectiveDateLower=" + effectiveDateLower +
                ", effectiveDateUpper=" + effectiveDateUpper +
                ", currency=" + currency +
                ", costingType=" + costingType +
                ", allowNull=" + allowNull +
                ", specificSearch=" + specificSearch +
                ", priorityConfig=" + priorityConfig +
                ", costCompareOnly=" + costCompareOnly +
                ", itemCategory=" + itemCategory +
                '}';
    }

    @Override
    public String getTableName() {
        return "";
    }

    public void setNewDuplicatedRates(List<Rate> newDuplicatedRates) {
        this.newDuplicatedRates = newDuplicatedRates;
    }

    public List<Rate> getNewDuplicatedRates() {
        return newDuplicatedRates;
    }

    public boolean isCostCompareOnly() {
        return costCompareOnly;
    }

    public void setCostCompareOnly(boolean costCompareOnly) {
        this.costCompareOnly = costCompareOnly;
    }

    public ItemCategory getItemCategory() {
        return itemCategory;
    }

    public void setItemCategory(ItemCategory itemCategory) {
        this.itemCategory = itemCategory;
    }

    public int generate(Object... objects) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        for (Object object : objects) {
            if (object != null) {
                if (object.getClass().isEnum()) {
                    hashCodeBuilder.append(object.toString());  // enum.hashCode can differ
                } else if (object instanceof Date) {
                    String format = simpleDateFormat.format((Date) object);
                    hashCodeBuilder.append(format);
                } else {
                    hashCodeBuilder.append(object.hashCode());
                }
            }
        }
        return hashCodeBuilder.toHashCode();
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        return new HashCodeBuilder().append(allowNull).append(specificSearch).append(costLine).
                append(effectiveDateLower).append(effectiveDateUpper).
                append(currency).append(costingType).append(priorityConfig).
                append(newDuplicatedRates).append(rateUnit).append(costCompareOnly).append(itemCategory).hashCode() + result;
    }
}