Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1041 → Rev 1042

/hostadmiral/trunk/src/ak/hostadmiral/util/hibernate/HibernateUtil.java
0,0 → 1,278
package ak.hostadmiral.util.hibernate;
 
import java.util.Iterator;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.type.Type;
 
import ak.hostadmiral.util.ModelStoreException;
 
public class HibernateUtil
{
public static final int DATABASE_VERSION = 2;
 
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, ModelStoreException
{
synchronized(HibernateUtil.class) {
if(validated) return;
 
Collection versions = currentSession().find("from DatabaseVersion");
 
if(versions == null || versions.size() == 0)
throw new ModelStoreException("Database structure version not found");
 
if(versions.size() > 1)
throw new ModelStoreException(
"Too much entries in database structure version table");
 
int version = ((DatabaseVersion)versions.iterator().next()).getMajor();
if(version != DATABASE_VERSION)
throw new ModelStoreException("Expected database structure version "
+ DATABASE_VERSION + ", found " + version);
 
validated = true;
}
}
 
public static void configure(String driver, String userName, String password,
String url, String dialect)
throws HibernateException
{
Configuration c = getConfiguration();
c.setProperty("hibernate.connection.driver_class", driver);
c.setProperty("hibernate.connection.username", userName);
c.setProperty("hibernate.connection.password", password);
c.setProperty("hibernate.connection.url", url);
c.setProperty("hibernate.dialect", dialect);
}
 
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, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null)
throw new ModelStoreException("No session found for this thread");
 
hibernateBean.set(null);
hb.session.close();
}
 
public static void beginTransaction()
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb != null && hb.transaction != null)
throw new ModelStoreException("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, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
return (hb != null) && (hb.transaction != null);
}
 
public static void commitTransaction()
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelStoreException("No open transaction");
 
hb.transaction.commit();
hb.transaction = null;
}
 
public static void rollbackTransaction()
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelStoreException("No open transaction");
 
hb.transaction.rollback();
hb.transaction = null;
}
 
public static List sqlQuery(String query, Object[] values)
throws HibernateException, ModelStoreException
{
Connection con = currentSession().connection();
PreparedStatement stmt;
 
try {
stmt = con.prepareStatement(query);
}
catch(SQLException ex) {
throw new ModelStoreException(ex);
}
 
try {
if(values != null) {
for(int i = 0; i < values.length; i++)
stmt.setObject(i+1, values[i]);
}
 
List res = new ArrayList();
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
res.add(rs.getObject(1));
}
return res;
}
catch(SQLException ex)
{
throw new ModelStoreException(ex);
}
finally {
try {
stmt.close();
}
catch(SQLException ex) {
ex.printStackTrace();
}
}
}
 
public static List pageableListSql(int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws HibernateException, ModelStoreException
{
Query hq = 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 selectFirstClassColumn(hq.list());
// FIXME: really no other way in Hibernate?
}
 
public static List pageableList(int pageSize, int pageNumber,
String query, Object[] values, Type[] types)
throws HibernateException, ModelStoreException
{
Query hq = currentSession().createQuery(query);
 
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();
}
 
protected static List selectFirstClassColumn(List list)
{
List res = new ArrayList();
 
for(Iterator i = list.iterator(); i.hasNext(); ) {
res.add(((Object[])i.next())[0]);
}
 
return res;
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelStoreException
{
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 ModelStoreException(
"Field for sorting key " + sortingKeys[i] + " not found");
 
buf.append(field);
}
 
return buf.toString();
}
 
static class HibernateBean
{
public Session session;
public Transaction transaction;
}
}