📄 crudservicehibernateimpl.java
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.hibernate;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.irunninglog.businessobject.BusinessObjectFactory;import net.sf.irunninglog.businessobject.IBusinessObject;import net.sf.irunninglog.service.ICRUDService;import net.sf.irunninglog.service.ServiceException;import net.sf.irunninglog.transaction.IUnitOfWork;import net.sf.irunninglog.transaction.TransactionalSupport;import net.sf.irunninglog.util.DTO;import net.sf.irunninglog.validation.ValidationException;/** * Hibernate implementation of the <code>ICRUDSercvice</code> interface. * This class will use the Hibernate API to service create, read, update, * and delete 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 * @see ICRUDService */public final class CRUDServiceHibernateImpl implements ICRUDService { /** <code>Log</code> instance for this class. */ private static final Log LOG = LogFactory.getLog(CRUDServiceHibernateImpl.class); /** Name of the <em>create</em> method. */ private static final String CREATE = "create"; /** Name of the <em>read</em> method. */ private static final String READ = "read"; /** Name of the <em>update</em> method. */ private static final String UPDATE = "update"; /** Name of the <em>delete</em> method. */ private static final String DELETE = "delete"; /** * 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 CRUDServiceHibernateImpl() { } /** * Create a new business object. * * @param valueObject Transfer object containing values for * use in creating the new object * @return Transfer object containing the newly created * object's values * @throws ServiceException If an unrecoverable error is encountered while * executing the service */ public DTO create(DTO valueObject) throws ServiceException { return (DTO) executeAction(CREATE, valueObject); } /** * Read (select) a business object's values. * * @param valueObject Transfer object containing information used * to find the object whose values are to be read * @return Transfer object containing the values read from the object * @throws ServiceException If an unrecoverable error is encountered while * executing the service */ public DTO read(DTO valueObject) throws ServiceException { return (DTO) executeAction(READ, valueObject); } /** * Update a business object's values. * * @param valueObject Transfer object containing values used to * find and update an object * @return Transfer object containing the updated object's values * @throws ServiceException If an unrecoverable error is encountered while * executing the service */ public DTO update(DTO valueObject) throws ServiceException { return (DTO) executeAction(UPDATE, valueObject); } /** * Delete a business object. * * @param valueObject Transfer object containing values used to find the * object to delete * @return Transfer object containing the deleted object's values * @throws ServiceException If an unrecoverable error is encountered while * executing the service */ public DTO delete(DTO valueObject) throws ServiceException { return (DTO) executeAction(DELETE, valueObject); } /** * Execte a CRUD action. This method will make use of the * <code>CRUDHelper</code> class to execute the appropriate action, and * to deal with any exceptions, validation errors, etc. * * @param methodName The name of the action (method) to invoke * @param valueObject The transfer object containing values to be used in * performing the action * @return A transfer object representing the result of the action * @throws ServiceException If an unhandled exception ocurrs dring * execution * @see #executeUnitOfWork(String, DTO) */ private DTO executeAction(String methodName, DTO valueObject) throws ServiceException { try { if (LOG.isDebugEnabled()) { LOG.debug("executeAction: Executing action '" + methodName + "' on the following object " + valueObject); } CRUDHelper helper = new CRUDHelper(); valueObject = helper.execute(methodName, valueObject); if (LOG.isDebugEnabled()) { LOG.debug("executeAction: Result of executing the action is " + valueObject); } return valueObject; } catch (InvocationTargetException ex) { if (ex.getCause() instanceof ValidationException) { ValidationException vEx = (ValidationException) ex.getCause(); valueObject.addErrors(vEx.getErrors()); if (LOG.isDebugEnabled()) { LOG.debug("executeAction: Execution led to a " + "ValidationException containing " + vEx.getErrors().size() + " error(s)"); LOG.debug("executeAction: Result of executing the action is" + " " + valueObject); } return valueObject; } else { LOG.error("executeAction: Encountered the following unexpected" + " exception", ex); throw new ServiceException(ex); } } catch (Exception ex) { LOG.error("executeAction: Encountered the following unexpected" + " exception", ex); throw new ServiceException(ex); } } /** * Private helper class used to service CRUD requests. This class is used * to perform the actual work of servicing requests - methods in this class * are invoked by the service implementation. This class handles the work * of managaing the transaction and interacting with the current unit of * work. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @since iRunningLog 1.0 */ private final class CRUDHelper { /** Create a new helper instance. */ private CRUDHelper() { super(); } /** * Execute a CRUD action. This will reflectively call one of the * private methods to perfom the action. Any work needed to manage * the transaction is performed in this method. * * @param methodName The name of the action (method) to invoke * @param valueObject Transfer object to be used in performing the * action * @return A transfer object representing the results of the action * @throws Exception If there is an error performing the action */ private DTO execute(String methodName, DTO valueObject) throws Exception { IUnitOfWork unitOfWork = null; try { if (LOG.isDebugEnabled()) { LOG.debug("execute: Beginning a transaction"); } unitOfWork = TransactionalSupport.beginTransaction(); Class [] classes = new Class [] {valueObject.getClass()}; Method method = getClass().getDeclaredMethod(methodName, classes); method.setAccessible(true); DTO result = (DTO) method.invoke(this, new Object [] {valueObject}); if (LOG.isDebugEnabled()) { LOG.debug("execute: Committing the transaction"); } TransactionalSupport.commitTransaction(); return result; } catch (Exception ex) { if (LOG.isDebugEnabled()) { // Leave it to executeAction to log if needed LOG.debug("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; } } /** * Create a new business object. * * @param valueObject Transfer object containing values used to create * the business object * @return Transfer object containing the created object's values * @throws Exception If there is an error creating the object */ private DTO create(DTO valueObject) throws Exception { IUnitOfWork unitOfWork = TransactionalSupport.getUnitOfWork(); String canonicalId = valueObject.getCanonicalId(); IBusinessObject bo = BusinessObjectFactory.newInstance(canonicalId); bo.setValues(valueObject); unitOfWork.create(bo); return bo.getValues(); } /** * Read a business object's values. * * @param valueObject Transfer object containing values used to find the * object to read * @return Transfer object containing the business object's values * @throws Exception If there is an error reading the object */ private DTO read(DTO valueObject) throws Exception { String canonicalId = valueObject.getCanonicalId(); IBusinessObject bo = BusinessObjectFactory.newInstance(canonicalId); bo.setPrimaryKey(valueObject); IUnitOfWork unitOfWork = TransactionalSupport.getUnitOfWork(); bo = (IBusinessObject) unitOfWork.load(bo.getClass(), bo.getPrimaryKey()); return bo.getValues(); } /** * Update a business object's values. * * @param valueObject Transfer object containing values used to find the * object to update * @return Transfer object containing the updated object's values * @throws Exception If there is an error updating the object */ private DTO update(DTO valueObject) throws Exception { String canonicalId = valueObject.getCanonicalId(); IBusinessObject bo = BusinessObjectFactory.newInstance(canonicalId); bo.setPrimaryKey(valueObject); IUnitOfWork unitOfWork = TransactionalSupport.getUnitOfWork(); bo = (IBusinessObject) unitOfWork.load(bo.getClass(), bo.getPrimaryKey()); bo.setValues(valueObject); unitOfWork.update(bo); return bo.getValues(); } /** * Delete a business object. * * @param valueObject Transfer object containing values used to find the * object to delete * @return Transfer object containing the deleted object's values * @throws Exception If there is an error deleting the object */ private DTO delete(DTO valueObject) throws Exception { String canonicalId = valueObject.getCanonicalId(); IBusinessObject bo = BusinessObjectFactory.newInstance(canonicalId); bo.setPrimaryKey(valueObject); IUnitOfWork unitOfWork = TransactionalSupport.getUnitOfWork(); bo = (IBusinessObject) unitOfWork.load(bo.getClass(), bo.getPrimaryKey()); unitOfWork.delete(bo); return bo.getValues(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -