org.beangle.commons.orm.hibernate.BaseDaoHibernate.java Source code

Java tutorial

Introduction

Here is the source code for org.beangle.commons.orm.hibernate.BaseDaoHibernate.java

Source

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.commons.orm.hibernate;

import java.util.List;
import java.util.Map;

import org.beangle.commons.collection.page.Page;
import org.beangle.commons.collection.page.PageLimit;
import org.beangle.commons.collection.page.SinglePage;
import org.beangle.commons.dao.EntityDao;
import org.beangle.commons.lang.Strings;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.impl.CriteriaImpl;

/**
 * @deprecated use HibernateEntityDao
 * @author chaostone
 */
public class BaseDaoHibernate extends HibernateEntityDao {

    protected EntityDao entityDao;

    public void setEntityDao(EntityDao entityDao) {
        this.entityDao = entityDao;
    }

    @SuppressWarnings("unchecked")
    public <T> Page<T> paginateCriteria(Criteria criteria, PageLimit limit) {
        CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
        int totalCount = 0;
        List<T> targetList = null;
        if (null == criteriaImpl.getProjection()) {
            criteria.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize())
                    .setMaxResults(limit.getPageSize());
            targetList = criteria.list();
            Projection projection = null;
            criteria.setFirstResult(0).setMaxResults(1);
            projection = Projections.rowCount();
            totalCount = ((Number) criteria.setProjection(projection).uniqueResult()).intValue();
        } else {
            List<T> list = criteria.list();
            totalCount = list.size();
            criteria.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize())
                    .setMaxResults(limit.getPageSize());
            targetList = criteria.list();
        }
        // 
        return new SinglePage<T>(limit.getPageNo(), limit.getPageSize(), totalCount, targetList);
    }

    /**
     * @param query
     * @param names
     * @param values
     * @param pageNo
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> Page<T> paginateQuery(Query query, Map<String, Object> params, PageLimit limit) {
        QuerySupport.setParameter(query, params);
        query.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize()).setMaxResults(limit.getPageSize());
        List<T> targetList = query.list();

        String queryStr = buildCountQueryStr(query);
        Query countQuery = null;
        if (query instanceof SQLQuery) {
            countQuery = getSession().createSQLQuery(queryStr);
        } else {
            countQuery = getSession().createQuery(queryStr);
        }
        QuerySupport.setParameter(countQuery, params);
        // 
        return new SinglePage<T>(limit.getPageNo(), limit.getPageSize(),
                ((Number) (countQuery.uniqueResult())).intValue(), targetList);
    }

    /**
     * 
     * 
     * @param query
     * @return
     */
    private String buildCountQueryStr(Query query) {
        String queryStr = "select count(*) ";
        if (query instanceof SQLQuery) {
            queryStr += "from (" + query.getQueryString() + ")";
        } else {
            String lowerCaseQueryStr = query.getQueryString().toLowerCase();
            String selectWhich = lowerCaseQueryStr.substring(0, query.getQueryString().indexOf("from"));
            int indexOfDistinct = selectWhich.indexOf("distinct");
            int indexOfFrom = lowerCaseQueryStr.indexOf("from");
            // ?distinct
            if (-1 != indexOfDistinct) {
                if (Strings.contains(selectWhich, ",")) {
                    queryStr = "select count("
                            + query.getQueryString().substring(indexOfDistinct, query.getQueryString().indexOf(","))
                            + ")";

                } else {
                    queryStr = "select count(" + query.getQueryString().substring(indexOfDistinct, indexOfFrom)
                            + ")";
                }
            }
            queryStr += query.getQueryString().substring(indexOfFrom);
        }
        return queryStr;
    }
}