CostedRepositoryImpl.java

package com.tradecloud.repository.impl;

import com.tradecloud.domain.costing.clean.Costed;
import com.tradecloud.domain.costing.clean.CostedConsignment;
import com.tradecloud.domain.costing.clean.CostedLineItem;
import com.tradecloud.domain.costing.clean.CostedOrder;
import com.tradecloud.domain.document.invoice.CostLineCostingCell;
import com.tradecloud.domain.item.LineItem;
import com.tradecloud.repository.CostedRepository;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "costedRepository")
public class CostedRepositoryImpl extends RepositoryBaseImpl<Costed, Object> implements CostedRepository {

    @Override
    public CostedConsignment findCostedConsignmentUsingCostLineCostingCell(CostLineCostingCell costLineCostingCell) {
        List<Costed> costedConsignments = (List<Costed>) findByNamedQueryAndNamedParam("findCostedConsignmentByCostLineCostingCell" + "", "id",
                costLineCostingCell.getId());
        if (!costedConsignments.isEmpty()) {
            return (CostedConsignment) costedConsignments.get(0);
        }
        return null;
    }

    @Override
    public CostedOrder findCostedOrderUsingCostLineCostingCell(CostLineCostingCell costLineCostingCell) {
        List<Costed> costedOrders = (List<Costed>) findByNamedQueryAndNamedParam("findCostedOrderByCostLineCostingCell" + "", "id",
                costLineCostingCell.getId());
        if (!costedOrders.isEmpty()) {
            return (CostedOrder) costedOrders.get(0);
        }
        return null;
    }

    @Override
    public CostedLineItem findCostedLineItemUsingCostLineCostingCell(CostLineCostingCell costLineCostingCell) {
        List<Costed> costedLineItems = (List<Costed>) findByNamedQueryAndNamedParam("findCostedLineItemByCostLineCostingCell" + "", "id",
                costLineCostingCell.getId());
        if (!costedLineItems.isEmpty()) {
            return (CostedLineItem) costedLineItems.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CostedLineItem> findCostedLineItemsByLineItem(LineItem lineItem) {
        return getCurrentSession()
                .createQuery("from CostedLineItem c where c.lineItem = :lineItem")
                .setParameter("lineItem", lineItem)
                .list();
    }

}