Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2004-2005 Revolution Systems Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.revolsys.orm.hibernate.dao;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Blob;
import java.sql.Clob;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LobHelper;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.revolsys.collection.ResultPager;
import com.revolsys.orm.core.NamedQueryParameter;
import com.revolsys.orm.hibernate.callback.HibernateNamedQueryCallback;
import com.revolsys.util.ExceptionUtil;
import com.revolsys.util.JavaBeanUtil;
public class HibernateDaoHandler extends HibernateDaoSupport implements
InvocationHandler {
/** The class definition of the DataAcessObject interface. */
private final Class> daoInterface;
/** The class definition of the entities persisted by this Data Access Object. */
private final Class> objectClass;
/** The class name of the entities persisted by this Data Access Object. */
private final String objectClassName;
public HibernateDaoHandler(final Class> daoInterface,
final Class> objectClass) {
this.daoInterface = daoInterface;
this.objectClass = objectClass;
this.objectClassName = objectClass.getName();
}
/**
* Clear all objects loaded from persistent storage from the cache. After
* invoking this method the in memory Java objects will be disconnected from
* the persistent storage and any changes to them will not be saved.
*/
public void clearCache() {
final HibernateTemplate hibernateTemplate = getHibernateTemplate();
hibernateTemplate.clear();
}
public Blob createBlob(final byte[] bytes) {
final Session session = getSession();
final LobHelper lobHelper = session.getLobHelper();
return lobHelper.createBlob(bytes);
}
public Blob createBlob(final InputStream in, final long length) {
final Session session = getSession();
final LobHelper lobHelper = session.getLobHelper();
return lobHelper.createBlob(in, length);
}
public Clob createClob(final String string) {
final Session session = getSession();
final LobHelper lobHelper = session.getLobHelper();
return lobHelper.createClob(string);
}
public Object createInstance() {
try {
return objectClass.newInstance();
} catch (final Exception e) {
return ExceptionUtil.throwCauseException(e);
}
}
public Object delete(
final Method method,
final String queryName,
final Object[] args) {
final Query query = getQuery(method, queryName, args);
return query.executeUpdate();
}
/**
* Evict the object from cache of managed objects. After evicting an object
* not further changes to the object will be saved to the persistent storage.
*
* @param object The object to evict.
* @return null.
*/
public Object evict(final Object object) {
final HibernateTemplate hibernateTemplate = getHibernateTemplate();
hibernateTemplate.evict(object);
return null;
}
public List find(
final Method method,
final String queryName,
final Object[] args) {
try {
final Query query = getQuery(method, queryName, args);
return query.list();
} catch (final HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
public List findMax(
final Method method,
final String queryName,
final int limit,
final Object[] args) {
try {
final Query query = getQuery(method, queryName, args);
query.setMaxResults(limit);
return query.list();
} catch (final HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
/**
* Flush all changes to the persistent storage.
*/
public void flush() {
getHibernateTemplate().flush();
}
public Object get(
final Method method,
final String queryName,
final Object[] args) {
try {
final Query query = getQuery(method, queryName, args);
final Iterator resultIter = query.iterate();
if (resultIter.hasNext()) {
return resultIter.next();
} else {
return null;
}
} catch (final HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
}
private Query getQuery(
final Method method,
final String queryName,
final Object... args) {
Object[] arguments;
if (args == null) {
arguments = new Object[0];
} else {
arguments = args;
}
final String fullQueryName = getQueryName(queryName);
final HibernateCallback callback = new HibernateNamedQueryCallback(
fullQueryName);
final HibernateTemplate hibernateTemplate = getHibernateTemplate();
final Query query = hibernateTemplate.execute(callback);
for (int i = 0; i < arguments.length; i++) {
final String name = getQueryParameterName(method, i);
final Object value = arguments[i];
if (name == null) {
query.setParameter(i, value);
} else {
if (value instanceof Collection>) {
query.setParameterList(name, (Collection>)value);
} else if (value instanceof Object[]) {
query.setParameterList(name, (Object[])value);
} else if (value instanceof Enum>) {
query.setParameter(name, value.toString());
} else {
query.setParameter(name, value);
}
}
}
return query;
}
public String getQueryName(final String methodName) {
final String queryName = methodName.replaceFirst(
"\\A(get|page|findMax|find|iterate|delete|update)", "");
return objectClassName + "." + queryName;
}
private String getQueryParameterName(final Method method, final int index) {
final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (final Annotation annotation : parameterAnnotations[index]) {
if (annotation.annotationType().equals(NamedQueryParameter.class)) {
final String parameterName = ((NamedQueryParameter)annotation).value();
return parameterName;
}
}
return null;
}
public Object invoke(
final Object proxy,
final Method method,
final Object[] args) throws Throwable {
final SessionFactory sessionFactory = getSessionFactory();
boolean participate = false;
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
} else {
logger.debug("Creating Hibernate Session");
final Session session = SessionFactoryUtils.getSession(sessionFactory,
true);
session.setFlushMode(FlushMode.AUTO);
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(session));
}
try {
final String methodName = method.getName();
try {
final Class>[] paramTypes = method.getParameterTypes();
final Method localMethod = getClass().getMethod(methodName, paramTypes);
return localMethod.invoke(this, args);
} catch (final InvocationTargetException e) {
throw e.getCause();
} catch (final SecurityException e) {
throw e;
} catch (final NoSuchMethodException e) {
if (methodName.equals("removeAll")) {
return removeAll((Collection