SellingPriceListRepositoryImpl.java

package com.tradecloud.repository.impl;

import com.tradecloud.domain.configuration.PriceCode;
import com.tradecloud.domain.export.ExportParty;
import com.tradecloud.domain.item.SellingPriceList;
import com.tradecloud.dto.product.SellingPriceListSearch;
import com.tradecloud.repository.SellingPriceListRepository;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
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.List;

@Repository(value = "sellingPriceListRepository")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class SellingPriceListRepositoryImpl extends RepositoryBaseImpl<SellingPriceList, SellingPriceListSearch> implements
        SellingPriceListRepository {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean isSellingPriceExists(SellingPriceList sellingPriceList) {
        String queryString =
                "select count(i.id) from SellingPriceList i where " + "i.exportParty =:exportParty and i.effectiveDate =:effectiveDate and "
                        + "i.currency =:currency ";
        if (null != sellingPriceList.getIncoterm()) {
            queryString = queryString + "and i.incoterm =:incoterm ";
        }
        Query query = getSessionCustom().createQuery(queryString);
        if (null != sellingPriceList.getIncoterm()) {
            query.setParameter("incoterm", sellingPriceList.getIncoterm());
        }
        query.setParameter("exportParty", sellingPriceList.getExportParty());
        query.setParameter("effectiveDate", sellingPriceList.getEffectiveDate());
        query.setParameter("currency", sellingPriceList.getCurrency());
        Long result = (Long) query.uniqueResult();
        return result == 0;
    }

    @Override
    public List<SellingPriceList> search(SellingPriceListSearch search) {
        DetachedCriteria criteria = createSellingPriceListCriteria(search);
        return getExecutableCriteriaList(criteria, search.getSearchMetaParams());
    }

    @Override
    public long count(SellingPriceListSearch search) {
        DetachedCriteria criteria = createSellingPriceListCriteria(search);
        return getExecutableCriteriaCount(criteria);
    }

    private DetachedCriteria createSellingPriceListCriteria(SellingPriceListSearch search) {
        DetachedCriteria criteria = DetachedCriteria.forClass(SellingPriceList.class);
        if (null != search.getExportParty()) {
            criteria.add(Restrictions.eq("exportParty", search.getExportParty()));
        }
        if (null != search.getIncoterm()) {
            criteria.add(Restrictions.eq("incoterm", search.getIncoterm()));
        }/* else {
            criteria.add(Restrictions.isNull("incoterm"));
        }*/
        if (null != search.getEffectiveDateFrom()) {
            criteria.add(Restrictions.ge("effectiveDate", search.getEffectiveDateFrom()));
        }
        if (null != search.getEffectiveDateTo()) {
            criteria.add(Restrictions.le("effectiveDate", search.getEffectiveDateTo()));
        }
        return criteria;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SellingPriceList> findByPriceCodeAndCustomer(PriceCode priceCode, ExportParty exportParty) {
        DetachedCriteria criteria = DetachedCriteria.forClass(SellingPriceList.class);
        if (null != exportParty) {
            criteria.add(Restrictions.eq("exportParty", exportParty));
        }
        if (null != priceCode) {
            criteria.add(Restrictions.eq("priceCode", priceCode));
        }
        Criteria crit = criteria.getExecutableCriteria(getSessionCustom());
        return crit.list();
    }

    @Override
    public List<SellingPriceList> findByPriceCode(PriceCode priceCode) {
        DetachedCriteria criteria = DetachedCriteria.forClass(SellingPriceList.class);
        if (null != priceCode) {
            criteria.add(Restrictions.eq("priceCode", priceCode));
        }
        Criteria crit = criteria.getExecutableCriteria(getSessionCustom());
        return crit.list();
    }
}