com.quhaodian.data.core.CriteriaDaoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discover-hibernate-common Show documentation
Show all versions of discover-hibernate-common Show documentation
discover-hibernate_common is a lib for hibernate
The newest version!
package com.quhaodian.data.core;
import com.quhaodian.data.page.Filter;
import com.quhaodian.data.page.Filter.Operator;
import com.quhaodian.data.page.Order;
import com.quhaodian.data.page.Order.Direction;
import com.quhaodian.data.page.Page;
import com.quhaodian.data.page.Pageable;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
@SuppressWarnings("rawtypes")
public abstract class CriteriaDaoImpl extends BaseDaoImpl
implements CriteriaDao {
public Page findPage(Pageable pageable) {
Criteria criteriaBuilder = createCriteria();
return findPage(criteriaBuilder, pageable);
}
public List findList(Integer first, Integer count, List filters, List orders) {
Criteria criteriaBuilder = createCriteria();
addRestrictions(criteriaBuilder, filters);
addOrders(criteriaBuilder, orders);
if (first != null) {
criteriaBuilder.setFirstResult(first);
}
if (count != null) {
criteriaBuilder.setMaxResults(count);
}
return criteriaBuilder.list();
}
private void addOrders(Criteria criteriaQuery, List orders) {
if (criteriaQuery == null || orders == null || orders.isEmpty()) {
return;
}
for (Order order : orders) {
if (order.getDirection() == Direction.asc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.asc(order.getProperty()));
} else if (order.getDirection() == Direction.desc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.desc(order.getProperty()));
}
}
}
protected Page findPage(Criteria criteriaQuery, Pageable pageable) {
if (pageable == null) {
pageable = new Pageable();
}
Criteria countQuery = createCriteria();
if (StringUtils.isNotEmpty(pageable.getSearchProperty()) && StringUtils.isNotEmpty(pageable.getSearchValue())) {
countQuery.add(Restrictions.like(pageable.getSearchProperty(), "%" + pageable.getSearchValue() + "%"));
}
Long total = count(countQuery, pageable.getFilters());
if (total == null) {
total = 0l;
}
if (StringUtils.isNotEmpty(pageable.getSearchProperty()) && StringUtils.isNotEmpty(pageable.getSearchValue())) {
criteriaQuery.add(Restrictions.like(pageable.getSearchProperty(), "%" + pageable.getSearchValue() + "%"));
}
addRestrictions(criteriaQuery, pageable.getFilters());
int totalPages = (int) Math.ceil((double) total / (double) pageable.getPageSize());
if (totalPages < pageable.getPageNumber()) {
pageable.setPageNumber(totalPages);
}
if (StringUtils.isNotEmpty(pageable.getSearchAliasProperty()) && pageable.getSearchAliasValue() == null) {
criteriaQuery.createAlias(pageable.getSearchAliasProperty().split("[.]")[0],
pageable.getSearchAliasProperty().split("[.]")[0])
// .add(Restrictions.eq(pageable.getSearchAliasProperty(), pageable.getSearchAliasValue()))
.add(Restrictions.isNotNull(pageable.getSearchAliasProperty()));
}
if (StringUtils.isNotEmpty(pageable.getSearchAliasProperty()) && pageable.getSearchAliasValue() != null) {
criteriaQuery.createAlias(pageable.getSearchAliasProperty().split("[.]")[0],
pageable.getSearchAliasProperty().split("[.]")[0])
.add(Restrictions.eq(pageable.getSearchAliasProperty(), pageable.getSearchAliasValue()));
}
addOrders(criteriaQuery, pageable);
criteriaQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
criteriaQuery.setMaxResults(pageable.getPageSize());
return new Page(criteriaQuery.list(), total, pageable);
}
public long count(Filter... filters) {
Criteria criteriaBuilder = createCriteria();
return count(criteriaBuilder, filters != null ? Arrays.asList(filters) : null);
}
private void addOrders(Criteria criteriaQuery, Pageable pageable) {
if (criteriaQuery == null || pageable == null) {
return;
}
if (StringUtils.isNotEmpty(pageable.getOrderProperty()) && pageable.getOrderDirection() != null) {
if (pageable.getOrderDirection() == Direction.asc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.asc(pageable.getOrderProperty()));
} else if (pageable.getOrderDirection() == Direction.desc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.desc(pageable.getOrderProperty()));
}
}
if (pageable.getOrders() != null) {
for (Order order : pageable.getOrders()) {
if (order.getDirection() == Direction.asc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.asc(order.getProperty()));
} else if (order.getDirection() == Direction.desc) {
criteriaQuery.addOrder(org.hibernate.criterion.Order.desc(order.getProperty()));
}
}
}
}
protected Long count(Criteria criteriaQuery, List filters) {
addRestrictions(criteriaQuery, filters);
criteriaQuery.setProjection(Projections.rowCount());
return (Long) criteriaQuery.uniqueResult();
}
private void addRestrictions(Criteria criteriaQuery, List filters) {
if (criteriaQuery == null || filters == null || filters.isEmpty()) {
return;
}
for (Filter filter : filters) {
if (filter == null || StringUtils.isEmpty(filter.getProperty())) {
continue;
}
if (filter.getOperator() == Operator.eq && filter.getValue() != null) {
criteriaQuery.add(Restrictions.eq(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.ne && filter.getValue() != null) {
criteriaQuery.add(Restrictions.ne(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.gt && filter.getValue() != null) {
criteriaQuery.add(Restrictions.gt(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.lt && filter.getValue() != null) {
criteriaQuery.add(Restrictions.lt(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.ge && filter.getValue() != null) {
criteriaQuery.add(Restrictions.ge(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.le && filter.getValue() != null) {
criteriaQuery.add(Restrictions.le(filter.getProperty(), filter.getValue()));
} else if (filter.getOperator() == Operator.like && filter.getValue() != null
&& filter.getValue() instanceof String) {
criteriaQuery.add(Restrictions.like(filter.getProperty(), (String) filter.getValue(), MatchMode.ANYWHERE));
} else if (filter.getOperator() == Operator.in && filter.getValue() != null) {
if (filter.getValue() instanceof Collection) {
criteriaQuery.add(Restrictions.in(filter.getProperty(), (Collection) filter.getValue()));
}
} else if (filter.getOperator() == Operator.isNull) {
criteriaQuery.add(Restrictions.isNull(filter.getProperty()));
} else if (filter.getOperator() == Operator.isNotNull) {
criteriaQuery.add(Restrictions.isNotNull(filter.getProperty()));
}
}
// criteriaQuery.where(restrictions);
}
}