no.abmu.common.persistence.hibernate3.AbstractQueryFinderSpecification.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.common.persistence.hibernate3.AbstractQueryFinderSpecification.java

Source

/*$Id: AbstractQueryFinderSpecification.java 12146 2008-12-08 00:42:14Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2006 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.common.persistence.hibernate3;

import no.abmu.common.persistence.BaseQueryFinderSpecification;
import no.abmu.common.persistence.FinderSpecification;
import no.abmu.common.persistence.FinderSpecificationBean;
import no.abmu.util.string.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * AbstractQueryFinderSpecification.
 * 
 * @author Erik Romson, erik@zenior.no
 * @author $Author: jens $
 * @version $Rev: 12146 $
 * @date $Date: 2008-12-08 01:42:14 +0100 (Mon, 08 Dec 2008) $
 * @copyright ABM-Utvikling
 */
public abstract class AbstractQueryFinderSpecification extends BaseQueryFinderSpecification
        implements FinderSpecification {

    /**
     * HQL select string for counting number of occurrence (hit) in a search. 
     */
    public static final String HQL_SELECT_COUNT = "select count(*) ";

    private static final Log logger = (Log) LogFactory.getLog(AbstractQueryFinderSpecification.class);

    public AbstractQueryFinderSpecification(FinderSpecificationBean finderSpecificationBean) {
        super(finderSpecificationBean);
    }

    public Query createQuery(Session session) {

        String queryString;

        if (StringUtil.isEmpty(getOrderBy())) {
            queryString = getQueryString();
        } else {
            queryString = getQueryString() + getOrderBy();
        }

        if (logger.isDebugEnabled()) {
            logger.debug("QueryString: [" + queryString + "]");
        }

        Query query = session.createQuery(queryString);

        if (usePropertiesBean()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Using propertiesBean: " + getPropertiesBean());
            }
            query.setProperties(getPropertiesBean());
        }

        if (isPaged()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Paging with firstElement " + getFirstElement() + ", and pageSize " + getPageSize());
            }
            query.setFirstResult(getFirstElement());
            query.setMaxResults(getPageSize());
        }

        if (isCaching()) {
            String cacheRegion = getCacheRegion();
            if (logger.isDebugEnabled()) {
                logger.debug("Caching is on in cacheRegion [" + cacheRegion + "]");
            }
            Query query2 = query.setCacheable(true).setCacheRegion(cacheRegion);
            return query2;
        }

        return query;
    }

    public Query createQueryCount(Session session) {

        String countQueryString = getCountString();
        if (logger.isDebugEnabled()) {
            logger.debug("QueryCountString: " + countQueryString);
        }

        Query query = session.createQuery(countQueryString);
        if (usePropertiesBean()) {
            query.setProperties(getPropertiesBean());
        }

        return query;
    }

    public int hashCode() {
        return super.hashCode();
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        if (!super.equals(obj)) {
            return false;
        }
        return true;
    }

    protected final String getCountSelect() {
        return HQL_SELECT_COUNT;
    }

}