CostingSummaryRepositoryImpl.java
package com.tradecloud.repository.impl;
import com.tradecloud.domain.costing.clean.CostingSummary;
import com.tradecloud.domain.costing.clean.CostingSummarySearch;
import com.tradecloud.repository.CostingSummaryRepository;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Abstract implementation of the {@code CostingSummaryRepository} interface.
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public abstract class CostingSummaryRepositoryImpl<T extends CostingSummary> extends RepositoryBaseImpl<T, CostingSummarySearch> implements
CostingSummaryRepository<T> {
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(CostingSummaryRepositoryImpl.class);
/*
* (non-Javadoc)
* @see com.tradecloud.repository.CostingSummaryRepository#findByCostableId(java .lang.String)
*/
@Override
public List<T> search(CostingSummarySearch costingSummarySearch) {
if (log.isDebugEnabled()) {
log.debug("getPersistentClass(): " + getPersistentClass());
}
DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentClass());
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
if (CollectionUtils.isNotEmpty(costingSummarySearch.getCostableIdsSet())) {
criteria.createAlias("originalCostable", "costable");
criteria.add(Restrictions.in("costable.id", costingSummarySearch.getCostableIdsSet()));
}
// Let subclasses add specific criteria here
addSearchRestrictions(criteria, costingSummarySearch);
// criteria.add(Restrictions.sqlRestriction("xx"));
// return latest created first
criteria.addOrder(org.hibernate.criterion.Order.desc("created"));
@SuppressWarnings("unchecked")
List<T> results = criteria.getExecutableCriteria(getSessionCustom()).list();
// //todo change getCostLineSummaries to set, time not on my side for refactor now.
// if(results!=null) {
// for (T result : results) {
// List<CostLineSummary> costLineSummaries = result.getCostLineSummaries();
// List<CostLineSummary> collect = costLineSummaries.stream().distinct().collect(Collectors.toList());
// result.setCostLineSummaries(collect);
// }
// }
if (log.isDebugEnabled()) {
log.debug("Found " + results.size() + " results.");
}
return results;
}
/**
* Hook method that subclasses can override to apply specific search
* restrictions. This method is invoked by
* {@link #search(CostingSummarySearch)}.
*
* @param searchCriteria The {@code DetachedCriteria} to add restrictions to
* @param costingSummarySearch The {@code CostingSummarySearch} originating from the client
*/
public abstract void addSearchRestrictions(DetachedCriteria searchCriteria, CostingSummarySearch costingSummarySearch);
@Override
public long count(CostingSummarySearch costingSummarySearch) {
DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentClass());
// Let subclasses add specific criteria here
addSearchRestrictions(criteria, costingSummarySearch);
criteria.setProjection(Projections.rowCount());
List list = criteria.getExecutableCriteria(getSessionCustom()).list();
if (!list.isEmpty()) {
return (Long) list.get(0);
} else {
return 0;
}
}
protected abstract String getCostSummarySimpleName();
}