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
{
/// <summary>
/// Hibernate helper
/// </summary>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="10/15/2010 5:50 PM"
/// </RevisionHistory>
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
/// <summary>
/// Initializes the <see cref="NHibernateHelper"/> class.
/// </summary>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
/// </RevisionHistory>
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
/// <summary>
/// Gets or sets the configuration data.
/// </summary>
/// <value>The configuration data.</value>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="1/13/2011 9:54 AM"
/// </RevisionHistory>
public static NHibernate.Cfg.Configuration ConfigurationData
{
get
{
return _configuration;
}
set
{
_configuration = value;
}
}
#endregion
#region Public Methods
/// <summary>
/// Gets the session.
/// </summary>
/// <param name="enableAudit">if set to <c>true</c> [enable audit].</param>
/// <returns></returns>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="1/13/2011 9:54 AM"
/// </RevisionHistory>
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;
}
/// <summary>
/// Takes the specified ids.
/// </summary>
/// <param name="ids">The ids.</param>
/// <returns></returns>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="suman.m" Date="21-07-2012 15:45"
/// </RevisionHistory>
public static List<List<int>> PrepareInOperatorIds(List<int> ids)
{
int value = 1000;
List<List<int>> integers = new List<List<int>>();
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<int> 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
/// <summary>
/// Sets the Corresponding Database Configuration Properties to the NHibernate SessionFactory.
/// </summary>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
/// </RevisionHistory>
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);
}
}
/// <summary>
/// Adds the HBM file to the NHibernate Configuration.
/// </summary>
/// <param name="mappingPath">The mapping path.</param>
/// <param name="config">The config.</param>
/// <returns>NHibernate Configuration</returns>
/// <RevisionHistory>
/// Author="phani.Kumar" Date="20/07/2010 5:04 PM"
/// </RevisionHistory>
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;
/// <summary>
/// Gets the session.
/// </summary>
/// <param name="foldersList">The folders list.</param>
/// <param name="enableAudit">if set to <c>true</c> [enable audit].</param>
/// <returns></returns>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
/// </RevisionHistory>
public ISession GetSession(List<string> 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;
}
/// <summary>
/// Selects the DB.
/// </summary>
/// <param name="foldersList">The folders list.</param>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
/// </RevisionHistory>
private void SelectDB(List<string> 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);
}
}
/// <summary>
/// Adds the HBM file.
/// </summary>
/// <param name="mappingPath">The mapping path.</param>
/// <param name="foldersList">The folders list.</param>
/// <param name="config">The config.</param>
/// <returns></returns>
/// <remarks>
///
/// </remarks>
/// <RevisionHistory>
/// Revision Author="vamsi.gopu" Date="1/17/2011 4:10 PM"
/// </RevisionHistory>
private NHibernate.Cfg.Configuration AddHbmFile(string mappingPath, List<string> 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<string>();
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
}
}