package edu.wustl.common.util.dbManager;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

import edu.wustl.catissuecore.util.global.Variables;
import edu.wustl.common.util.logger.Logger;

/**
 * <p>Title: DBUtil Class>
 * <p>Description:  Utility class provides database specific utilities methods </p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author Kapil Kaveeshwar
 * @version 1.00
 */
public class DBUtil
{
	//A factory for DB Session which provides the Connection for client. 
	private static  SessionFactory m_sessionFactory;

	//ThreadLocal to hold the Session for the current executing thread. 
	private static final ThreadLocal session = new ThreadLocal();
	
	//Initialize the session Factory in the Static block.
	static 
	{
		try
		{
			File file = new File(Variables.catissueHome+System.getProperty("file.separator")+"hibernate.properties");
			Logger.out.info("File "+file);
			BufferedInputStream stram = new BufferedInputStream(new FileInputStream(file));
			Properties p = new Properties();
			p.load(stram);
			
			stram.close();
			Configuration cfg = new Configuration();
			cfg.setProperties(p);
			m_sessionFactory = cfg.configure().buildSessionFactory();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		    Logger.out.debug("Exception: "+ex.getMessage(),ex);
			throw new RuntimeException(ex.getMessage());
		}
	}

	/**
	 * Follows the singleton pattern and returns only current opened session.
	 * @return Returns the current db session.  
	 * */
	public static Session currentSession() throws HibernateException
	{
		Session s = (Session) session.get();
		
		//Open a new Session, if this Thread has none yet
		if (s == null)
		{
			s = m_sessionFactory.openSession();
			session.set(s);
		}
		return s;
	}

	/**
	 * Close the currently opened session.
	 * */
	public static void closeSession() throws HibernateException
	{
		Session s = (Session) session.get(); 
		session.set(null);
		if (s != null)
			s.close();
	}
	
//	public static String getTableName(Class classObj)
//	{
//		try
//		{
//			ClassMetadata classMetaData = m_sessionFactory.getClassMetadata(classObj);
//			//classMetaData.get
//		}
//		catch(Exception ex)
//		{
//			ex.printStackTrace();
//		}
//	}
}
@@ -121,32 +121,60 @@
 leInputStream;%0D%0A
+import java.util.Iterator;%0D%0A
 import java.util
@@ -187,16 +187,16 @@
 rties;%0D%0A
-
 %0D%0Aimport
@@ -353,18 +353,140 @@
 ation;%0D%0A
+import net.sf.hibernate.mapping.Column;%0D%0Aimport net.sf.hibernate.mapping.Property;%0D%0Aimport net.sf.hibernate.mapping.Table;
 %0D%0A
-
 import e
@@ -1136,19 +1136,16 @@
 cal();%0D%0A
-%09%0D%0A
 %09//Initi
@@ -1656,24 +1656,74 @@
 Factory();%0D%0A
+%09%09%09HibernateMetaData.initHibernateMetaData(cfg);%0D%0A
 %09%09%7D%0D%0A%09%09catch
@@ -2525,271 +2525,5 @@
 %09%7D%0D%0A
-%09%0D%0A//%09public static String getTableName(Class classObj)%0D%0A//%09%7B%0D%0A//%09%09try%0D%0A//%09%09%7B%0D%0A//%09%09%09ClassMetadata classMetaData = m_sessionFactory.getClassMetadata(classObj);%0D%0A//%09%09%09//classMetaData.get%0D%0A//%09%09%7D%0D%0A//%09%09catch(Exception ex)%0D%0A//%09%09%7B%0D%0A//%09%09%09ex.printStackTrace();%0D%0A//%09%09%7D%0D%0A//%09%7D%0D%0A
 %7D

Use of db metadataclass