Subversion Repositories general

Compare Revisions

Problem with comparison.

Ignore whitespace Rev HEAD → Rev 1020

/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
0,0 → 1,206
package ak.hostadmiral.util;
 
import java.util.Collection;
import java.util.Map;
 
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.type.Type;
 
public class HibernateUtil
{
public static final int DATABASE_VERSION = 1;
 
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal hibernateBean = new ThreadLocal();
private static boolean validated = false;
 
private static void validate()
throws HibernateException, ModelException
{
synchronized(HibernateUtil.class) {
if(validated) return;
 
Collection versions = currentSession().find("from DatabaseVersion");
 
if(versions == null || versions.size() == 0)
throw new ModelException("Database structure version not found");
 
if(versions.size() > 1)
throw new ModelException("Too much entries in database structure version table");
 
int version = ((DatabaseVersion)versions.iterator().next()).getVersion();
if(version != DATABASE_VERSION)
throw new ModelException("Expected database structure version "
+ DATABASE_VERSION + ", found " + version);
 
validated = true;
}
}
 
public static Configuration getConfiguration()
throws HibernateException
{
if(configuration == null)
configuration = new Configuration();
 
return configuration;
}
 
public static SessionFactory getSessionFactory()
throws HibernateException
{
if(sessionFactory == null)
sessionFactory = getConfiguration().configure().buildSessionFactory();
 
return sessionFactory;
}
 
private static HibernateBean currentBean()
throws HibernateException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null) {
hb = new HibernateBean();
hb.session = getSessionFactory().openSession();
hibernateBean.set(hb);
}
return hb;
}
 
public static Session currentSession()
throws HibernateException
{
return currentBean().session;
}
 
public static void closeSession()
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null)
throw new ModelException("No session found for this thread");
 
hibernateBean.set(null);
hb.session.close();
}
 
public static void beginTransaction()
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb != null && hb.transaction != null)
throw new ModelException("Transaction is already open");
 
currentBean().transaction = currentSession().beginTransaction();
 
// validate database structure version
if(!validated) // just try to speed up by avoiding synchronization
validate();
}
 
public static boolean isTransactionOpen()
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
return (hb != null) && (hb.transaction != null);
}
 
public static void commitTransaction()
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelException("No open transaction");
 
hb.transaction.commit();
hb.transaction = null;
}
 
public static void rollbackTransaction()
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelException("No open transaction");
 
hb.transaction.rollback();
hb.transaction = null;
}
 
public static Collection pageableList(CollectionInfo info, int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws ModelException
{
try {
// find the collection size if needed
if(info != null) {
Query sizeq = HibernateUtil.currentSession().createSQLQuery(
"select count(*) as {c} from " + query,
"c", Integer.class);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
sizeq.setParameter(i, values[i], types[i]);
}
 
info.setSize(((Integer)sizeq.iterate().next()).intValue());
}
 
// find the collection
Query hq = HibernateUtil.currentSession().createSQLQuery(
query, returnAliases, returnClasses);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
 
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return hq.list();
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelException
{
if(sortingKeys == null || sortingKeys.length == 0) return "";
 
StringBuffer buf = new StringBuffer(" order by ");
 
for(int i = 0; i < sortingKeys.length; i++) {
if(i > 0) buf.append(",");
 
String field = (String)fieldMap.get(sortingKeys[i]);
if(field == null)
throw new ModelException(
"Field for sorting key " + sortingKeys[i] + " not found");
 
buf.append(field);
}
 
return buf.toString();
}
 
static class HibernateBean
{
public Session session;
public Transaction transaction;
}
}