📄 queryservicehibernateimpl.java
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.hibernate;import java.util.ArrayList;import java.util.Calendar;import java.util.Collection;import java.util.Date;import java.util.GregorianCalendar;import java.util.Iterator;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.irunninglog.businessobject.IBusinessObject;import net.sf.irunninglog.service.IQueryService;import net.sf.irunninglog.service.ServiceException;import net.sf.irunninglog.transaction.IUnitOfWork;import net.sf.irunninglog.transaction.TransactionalSupport;/** * Hibernate implementation of the <code>IQuerySercvice</code> interface. * This class will use the Hibernate API to service query requests. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.3 $ $Date: 2005/06/30 22:46:37 $ * @since iRunningLog 1.0 */public final class QueryServiceHibernateImpl implements IQueryService { /** <code>Log</code> instance for this class. */ private static final Log LOG = LogFactory.getLog(QueryServiceHibernateImpl.class); /** Name of the (helper class) method used to perform queries. */ private static final String METHOD_NAME = "executeQuery"; /** Hibernate query to find all Shoes for a Runner. */ private static final String SHOES_FOR_RUNNER = "ShoesForRunner"; /** Hibernate query to find all Routes for a Runner. */ private static final String ROUTES_FOR_RUNNER = "RoutesForRunner"; /** Hibernate query to find all Run Types for a Runner. */ private static final String RUN_TYPES_FOR_RUNNER = "RunTypesForRunner"; /** Hibernate query to find all Run Data for a Runner in a given month.*/ private static final String RUN_DATA_FOR_RUNNER_BY_MONTH = "RunDataForRunnerByMonth"; /** * Create a new instance. This has private visibility, as instances * should be obtained by calling the * <code>ServiceFactory.newService</code> method. * * @see net.sf.irunninglog.service.ServiceFactory#newService(String) */ private QueryServiceHibernateImpl() { } /** * Retrieve all of the Shoes associated with a particular Runner. This * will return a <code>Collection</code> of <code>DTO</code>s representing * the shoes for the Runner whose <em>id</em> matches the method parameter. * * @param runnerId The <em>id</em> of the Runner whose shoes are to be * retrieved * @return The <code>Collection</code> of <code>DTO</code>s representing * the Runner's Shoes * @throws ServiceException If there is an error while performing the query */ public Collection findShoesForRunner(String runnerId) throws ServiceException { return executeQuery(SHOES_FOR_RUNNER, new String [] {runnerId}); } /** * Retrieve all of the Routes associated with a particular Runner. This * will return a <code>Collection</code> of <code>DTO</code>s representing * the routes for the Runner whose <em>id</em> matches the method parameter. * * @param runnerId The <em>id</em> of the Runner whose routes are to be * retrieved * @return The <code>Collection</code> of <code>DTO</code>s representing * the Runner's Routes * @throws ServiceException If there is an error while performing the query */ public Collection findRoutesForRunner(String runnerId) throws ServiceException { return executeQuery(ROUTES_FOR_RUNNER, new String [] {runnerId}); } /** * Retrieve all of the Run Types associated with a particular Runner. This * will return a <code>Collection</code> of <code>DTO</code>s representing * the run types for the Runner whose <em>id</em> matches the method * parameter. * * @param runnerId The <em>id</em> of the Runner whose run types are to be * retrieved * @return The <code>Collection</code> of <code>DTO</code>s representing * the Runner's Run Types * @throws ServiceException If there is an error while performing the query */ public Collection findRunTypesForRunner(String runnerId) throws ServiceException { return executeQuery(RUN_TYPES_FOR_RUNNER, new String [] {runnerId}); } /** * Retrieve all of the Run Data associated with a particular runner for a * given month/year combination. This will return a <code>Collection</code> * of <code>DTOs</code>s represeting the run data for the Runner whose * <em>id</em> matches the method parameter (for the month/year specified). * * @param runnerId The <em>id</em> of the Runner whose run data is to be * retrieved * @param month The month for which run data should be retrieved * @param year The year for which run data should be retrieved * @return The <code>Collection</code> of <code>DTO</code>s representing * the Runner's Run Data * @throws ServiceException If there is an error while performing the query */ public Collection findRunDataForRunnerByMonth(String runnerId, int month, int year) throws ServiceException { Calendar begin = GregorianCalendar.getInstance(); begin.set(Calendar.HOUR_OF_DAY, 0); begin.set(Calendar.MINUTE, 0); begin.set(Calendar.SECOND, 0); begin.set(Calendar.MILLISECOND, 0); begin.set(Calendar.MONTH, month); begin.set(Calendar.YEAR, year); begin.set(Calendar.DAY_OF_MONTH, 1); Calendar end = (Calendar) begin.clone(); end.add(Calendar.MONTH, 1); Date startDate = begin.getTime(); Date endDate = end.getTime(); if (LOG.isDebugEnabled()) { LOG.debug("findRunDataForRunnerByMonth: Runner id - " + runnerId); LOG.debug("findRunDataForRunnerByMonth: Start date - " + startDate); LOG.debug("findRunDataForRunnerByMonth: End date - " + endDate); } return executeQuery(RUN_DATA_FOR_RUNNER_BY_MONTH, new Object [] {runnerId, startDate, endDate}); } /** * Execute a query using Hibernate. This method will use the supplied * query string and replacement parameters to invoke the <code>execute * </code> method to invoke a query operation. Any exceptions encountered * will be caught and re-thrown as <code>ServiceException</code>s. * * @param queryString The Hibernate query string * @param params Parameters to be used to represent query values * @return The Collection of objects retrieved by the query * @throws ServiceException If there is an error performing the query */ private Collection executeQuery(String queryString, Object [] params) throws ServiceException { try { if (LOG.isDebugEnabled()) { LOG.debug("executeQuery: Executing the following query " + queryString); LOG.debug("executeQuery: Using the following parameters" + params); } return execute(queryString, params); } catch (Exception ex) { throw new ServiceException(ex); } } /** * Execute a Hibernate query within a transaction. This method will begin * a transaction, perform a Hibernate query, then either commit the * transaction (if the query succeeded) or roll it back (if the query * fails). * * @param queryString The Hibernate query string * @param params Parameters to be used to represent query values * @return The Collection of objects retrieved by the query * @throws Exception If there is an error performing the query */ private Collection execute(String queryString, Object [] params) throws Exception { IUnitOfWork unitOfWork = null; try { if (LOG.isDebugEnabled()) { LOG.debug("execute: Beginning a transaction"); } unitOfWork = TransactionalSupport.beginTransaction(); Collection bos = unitOfWork.find(queryString, params); Collection dtos = new ArrayList(); IBusinessObject bo = null; if (LOG.isDebugEnabled()) { LOG.debug("execute: Found " + bos.size() + " business objects"); LOG.debug("execute: Converting to DTOs"); } for (Iterator i = bos.iterator(); i.hasNext();) { bo = (IBusinessObject) i.next(); dtos.add(bo.getValues()); } if (LOG.isDebugEnabled()) { LOG.debug("execute: Have " + dtos.size() + " DTOs"); LOG.debug("execute: Committing the transaction"); } TransactionalSupport.commitTransaction(); return dtos; } catch (Exception ex) { LOG.error("execute: Caught the following exception", ex); if (unitOfWork != null) { if (LOG.isDebugEnabled()) { LOG.debug("execute: Rolling back the transaction"); } TransactionalSupport.rollbackTransaction(); } else { if (LOG.isDebugEnabled()) { LOG.debug("execute: No unit of work - do nothing"); } } throw ex; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -