CostLineToleranceRepositoryImpl.java

package com.tradecloud.repository.impl;

import com.tradecloud.domain.costing.CostGroup;
import com.tradecloud.domain.costing.CostLineTolerance;
import com.tradecloud.domain.model.shipment.ShippingMode;
import com.tradecloud.dto.tolerance.ToleranceSearchDTO;
import com.tradecloud.repository.CostLineToleranceRepository;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * Default implementation of the {@code CostLineToleranceRepository} interface.
 */
@Repository(value = "costLineToleranceRepository")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class CostLineToleranceRepositoryImpl extends RepositoryBaseImpl<CostLineTolerance, ToleranceSearchDTO> implements
        CostLineToleranceRepository {

    private static final long serialVersionUID = 1L;

    @Override
    public List<CostLineTolerance> findByCostGroupOrderByCostLineName(CostGroup costGroup, ShippingMode shippingMode) {
        if (costGroup == null) {
            throw new IllegalArgumentException("Must supply a valid costGroup");
        }
        if (costGroup == CostGroup.BASE_SUPPLY) {
            //no tolerance required
            return Collections.EMPTY_LIST;
        }
//        String[] names={"costGroup","shippingMode"};
//        Object[] values={costGroup,shippingMode};
//        List<CostLineTolerance> list = (List<CostLineTolerance>) findByNamedQueryAndNamedParam("costLineTolerance.byCostGroupOrderedByCostLineName",
//                        names, values);
//
//        return list;
        return search(new ToleranceSearchDTO(shippingMode, null, costGroup));
    }

    @Override
    public List<CostLineTolerance> search(ToleranceSearchDTO search) {
        StringBuilder s = new StringBuilder("select ct from Tolerance t join t.costLineTolerances ct where t.id is not null");
        String and = null;

        if (search.getShippingMode() != null) {
            and = " and ";
            s.append(and + "t.shippingMode = :shippingMode");
        }
        if (search.getOrganisationalUnit() != null) {
            and = and == null ? " and " : and;
            s.append(and + "t.organisationalUnit = :organisationalUnit");
        } else {
            and = and == null ? " and " : and;
            s.append(and + "t.organisationalUnit is NULL");
        }
        if (search.getCostGroup() != null) {
            and = and == null ? " and " : and;
            s.append(and + "ct.costLineTemplate.costGroup = :costGroup");
        }

        Query query = getCurrentSession().createQuery(s.toString());

        if (search.getShippingMode() != null) {
            query.setParameter("shippingMode", search.getShippingMode());
        }
        if (search.getOrganisationalUnit() != null) {
            query.setParameter("organisationalUnit", search.getOrganisationalUnit());
        }
        if (search.getCostGroup() != null) {
            query.setParameter("costGroup", search.getCostGroup());
        }

        return query.list();
    }
}