using System;
using System.Linq;
using System.Collections.Generic;
using NHibernate;
using System.IO;
using Symphony.Phoenix.Hibernate.DataAccess;
using Symphony.Phoenix.ExceptionHandling;
namespace Symphony.Phoenix.Hibernate.ServiceImpl
{
///
/// Hibernate helper
///
///
///
///
///
/// Revision Author="vamsi.gopu" Date="10/15/2010 5:50 PM"
///
public class HibernateHelper
{
#region Private Member Variables
static ISessionFactory _sessionFactory;
ISession applicationSession;
string _namedBlock = string.Empty;
private static NHibernate.Cfg.Configuration _configuration = new NHibernate.Cfg.Configuration();
AuditLogInterceptor auditLogInterceptor = new AuditLogInterceptor();
#endregion
#region Public Constructor
///
/// Initializes the class.
///
///
///
///
///
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
///
public HibernateHelper()
{
string namedBlock = "Constructor Begin";
try
{
if (_sessionFactory == null)
{
_namedBlock = "HibernateHelper Constructor Before Calling Select DB Method";
SelectDB();
applicationSession = null;
}
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", HibernateHelper," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
}
#endregion
#region Public Properties
///
/// Gets or sets the configuration data.
///
/// The configuration data.
///
///
///
///
/// Revision Author="vamsi.gopu" Date="1/13/2011 9:54 AM"
///
public static NHibernate.Cfg.Configuration ConfigurationData
{
get
{
return _configuration;
}
set
{
_configuration = value;
}
}
#endregion
#region Public Methods
///
/// Gets the session.
///
/// if set to true [enable audit].
///
///
///
///
///
/// Revision Author="vamsi.gopu" Date="1/13/2011 9:54 AM"
///
public ISession GetSession(bool enableAudit = true)
{
applicationSession = null;
string namedBlock = "Method Start";
try
{
if (enableAudit)
{
applicationSession = _sessionFactory.OpenSession(auditLogInterceptor);
auditLogInterceptor.SetSession(applicationSession);
return applicationSession;
}
else
{
applicationSession = _sessionFactory.OpenSession();
return applicationSession;
}
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", GetSession," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
return applicationSession;
}
///
/// Takes the specified ids.
///
/// The ids.
///
///
///
///
///
/// Revision Author="suman.m" Date="21-07-2012 15:45"
///
public static List
- > PrepareInOperatorIds(List
{
int value = 1000;
List
- > integers = new List
- >();
string _namedBlock = "Method Start";
try
{
int totalCount = ids.Count;
double fraction = ((double)totalCount / (double)value);
fraction = Math.Ceiling(fraction);
for (int counter = 0; counter < fraction; counter++)
{
int index = (counter * value);
int elementsCount = value;
if (counter == fraction - 1)
{
elementsCount = totalCount - (value * (int)(fraction - 1));
}
List tempList = ids.GetRange(index, elementsCount);
integers.Add(tempList);
}
}
catch (Exception exception)
{
bool reThrow = false;
exception.Source = _namedBlock;
reThrow = SymphonyPhoenixExceptionPolicy.HandleException(exception, AppLayer.DataAccessLayer, true);
if (!reThrow)
{
throw;
}
}
return integers;
}
#endregion
#region Private Methods
///
/// Sets the Corresponding Database Configuration Properties to the NHibernate SessionFactory.
///
///
///
///
///
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
///
private void SelectDB()
{
string namedBlock = "Method Start";
try
{
//get the common mapping path
string mappingPathCommon = string.Empty;
if (System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains(Constants.COMMONMAPPINGFILES))
{
mappingPathCommon = System.Configuration.ConfigurationManager.AppSettings[Constants.COMMONMAPPINGFILES];
}
// get the mapping path
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string mappingPath = baseDirectory + Constants.BINMAPPINGXML;
if (mappingPath.Contains(Constants.BINDEBUGBIN) || mappingPath.Contains(Constants.BINRELEASE))
{
mappingPath = AppDomain.CurrentDomain.BaseDirectory + Constants.MAPPING;
}
if (mappingPath.Contains(Constants.BINDEBUGSLASH) || mappingPath.Contains(Constants.BINRELEASESLASH))
{
// Windows service mapping path
mappingPath = AppDomain.CurrentDomain.BaseDirectory + Constants.MAPPINGPATH;
}
// Create the instance of configuration
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
// set the properties of provider information
config.SetProperty(Constants.CONNECTIONPROVIDER, System.Configuration.ConfigurationManager.AppSettings[Constants.PROVIDER]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
// get the database type from web.config file and switch to the particular database and set the configuration
switch (System.Configuration.ConfigurationManager.AppSettings[Constants.DATABASETYPE])
{
case Constants.SQLDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLCONNECTIONSTRING]);
break;
case Constants.ORACLEDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLEDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLEDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLECONNECTIONSTRING]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
break;
case Constants.MYSQLDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLCONNECTIONSTRING]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
break;
}
_namedBlock = "Before Adding Hbm Files in Select DB Method";
// Add all HBM files to the configuration
config = AddHbmFile(mappingPath, mappingPathCommon, config);
_namedBlock = "After Adding Hbm Files in Select DB Method";
ConfigurationData = config;
// set the configuration
//NHibernateHelper.ConfigurationData = config;
_namedBlock = "Before BuildSessionFactory Method Calling in Select DB Method";
// Build the configuration and set the session factory
_sessionFactory = config.BuildSessionFactory();
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", SelectDB," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
}
///
/// Adds the HBM file to the NHibernate Configuration.
///
/// The mapping path.
/// The config.
/// NHibernate Configuration
///
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
///
private NHibernate.Cfg.Configuration AddHbmFile(string mappingPath, string mappingPathCommon, NHibernate.Cfg.Configuration config)
{
NHibernate.Cfg.Configuration cfg = null;
string namedBlock = "Method Start";
try
{
NHibernate.Cfg.Configuration configuration = config;
_namedBlock = "While Adding HBM Files in AddHbmFile Metods";
// Get all the files in given directory
string[] filePaths = Directory.GetFiles(mappingPath, Constants.HBM, SearchOption.AllDirectories);
// Add the files to configuration
foreach (string fileName in filePaths)
{
configuration.AddFile(fileName);
}
if (mappingPathCommon.Trim() != string.Empty)
{
// Get all common files in a given directory
string[] fileCommonPaths = Directory.GetFiles(mappingPathCommon, Constants.HBM, SearchOption.AllDirectories);
// Add the common files to configuration
foreach (string fileName in fileCommonPaths)
{
configuration.AddFile(fileName);
}
}
// Returns the configuration
cfg= configuration;
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", AddHbmFile," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
return cfg;
}
#endregion
#region Folder Configuration
ISession _session;
///
/// Gets the session.
///
/// The folders list.
/// if set to true [enable audit].
///
///
///
///
///
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
///
public ISession GetSession(List foldersList, bool enableAudit = true)
{
_session = null;
string namedBlock = "Method Start";
try
{
SelectDB(foldersList);
if (enableAudit)
{
AuditLogInterceptor auditLogInterceptor = new AuditLogInterceptor();
_session = null;
_session = _sessionFactory.OpenSession(auditLogInterceptor);
auditLogInterceptor.SetSession(applicationSession);
}
else
{
_session = _sessionFactory.OpenSession();
}
return _session;
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", GetSession," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
return _session;
}
///
/// Selects the DB.
///
/// The folders list.
///
///
///
///
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
///
private void SelectDB(List foldersList)
{
string namedBlock = "Method Start";
try
{
// get the mapping path
string mappingPath = AppDomain.CurrentDomain.BaseDirectory + Constants.BINMAPPINGXML;
if (mappingPath.Contains(Constants.BINDEBUGBIN) || mappingPath.Contains(Constants.BINRELEASE))
{
mappingPath = AppDomain.CurrentDomain.BaseDirectory + Constants.MAPPING;
}
// Jagadish.gurram
if (mappingPath.Contains(Constants.BINDEBUGSLASH) || mappingPath.Contains(Constants.BINRELEASESLASH))
{
// Windows service mapping path
mappingPath = AppDomain.CurrentDomain.BaseDirectory + Constants.MAPPINGPATH;
}
// Create the instance of configuration
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
// set the properties of provider information
config.SetProperty(Constants.CONNECTIONPROVIDER, System.Configuration.ConfigurationManager.AppSettings[Constants.PROVIDER]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
// get the database type from web.config file and switch to the particular database and set the configuration
switch (System.Configuration.ConfigurationManager.AppSettings[Constants.DATABASETYPE])
{
case Constants.SQLDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.SQLCONNECTIONSTRING]);
break;
case Constants.ORACLEDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLEDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLEDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.ORACLECONNECTIONSTRING]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
break;
case Constants.MYSQLDBTYPE:
config.SetProperty(Constants.CONFIGDIALECT, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLDIALECT]);
config.SetProperty(Constants.CONNECTIONDRIVERCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLDRIVERCLASS]);
config.SetProperty(Constants.CONNECTIONSTRING, System.Configuration.ConfigurationManager.AppSettings[Constants.MYSQLCONNECTIONSTRING]);
config.SetProperty(Constants.PROXYFACTORYCLASS, System.Configuration.ConfigurationManager.AppSettings[Constants.FACTORYCLASS]);
break;
}
// Add all HBM files to the configuration
config = AddHbmFile(mappingPath, foldersList, config);
// Build the configuration and set the session factory
_sessionFactory = config.BuildSessionFactory();
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", SelectDB," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
}
///
/// Adds the HBM file.
///
/// The mapping path.
/// The folders list.
/// The config.
///
///
///
///
///
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
///
private NHibernate.Cfg.Configuration AddHbmFile(string mappingPath, List foldersList, NHibernate.Cfg.Configuration config)
{
NHibernate.Cfg.Configuration cfg=null;
string namedBlock = "Method Start";
try
{
NHibernate.Cfg.Configuration configuration = config;
string[] filePaths;
DirectoryInfo directoryInfo = new DirectoryInfo(mappingPath);
foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
{
string value = (from folder in foldersList where folder.ToLower().Equals(directory.Name.ToLower()) select folder).FirstOrDefault();
if (!string.IsNullOrEmpty(value))
{
string newMappingPath = mappingPath + value;
filePaths = Directory.GetFiles(newMappingPath, Constants.HBM, SearchOption.AllDirectories);
foreach (string fileName in filePaths)
{
cfg= configuration.AddFile(fileName);
}
}
}
}
catch (Exception exception)
{
exception.Source = this.GetType().Name + ", AddHbmFile," + namedBlock + " |" + exception.Source;
SymphonyPhoenixExceptionPolicy.ManageException(exception, AppLayer.BusinessLayer);
}
return cfg;
}
#endregion
}
}

Replies
Know the answer? Post it — somebody with the same question will find it here.