/*
* Namespace Summary
* Copyright (C) 2005+ Bogdan Damian Constantin
* E-Mail: damianbcpetro@gmail.com
* WEB: http://www.sourceforge.net/projects/dataholder
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License 2.1 or later, as
* published by the Free Software Foundation. See the included License.txt
* or http://www.gnu.org/copyleft/lesser.html for details.
*
*/
using System;
using System.Data;
using DataHolder.Containers;
using DataHolder.DataPersistence;
using DataHolder.DataPersistence.DBAProvider;
namespace DataHolder.DataPersistence.Template{
/// <summary>
/// This is an abstract class to be used in order to save or load data
/// </summary>
public class GeneralDBActionTemplate : IDisposable
{
protected IDbConnection Connection;
protected IDbTransaction Transaction;
protected CommandProvider l_CommandProvider;
protected DatabasePersistor l_DBPersistor;
protected virtual void OpenConnection()
{
Connection.Open();
}
protected virtual object GetPersistenceInfo()
{
return null;
}
#region LoadData
public virtual GenericDataCollection LoadData(Type CollectionType, SelectCommandOption SelCmdOption, string PropertiesIdentifier)
{
bool connectionIsAllreadyOpened = (Connection.State == ConnectionState.Open);
if(!connectionIsAllreadyOpened)
Connection.Open();
try
{
GenericDataCollection gecoll = (GenericDataCollection)Activator.CreateInstance(CollectionType);
GenericDataPropertiesHolder [] gdphs = PersistenceManager.GetAllProperties(gecoll.GenericDataType, PropertiesIdentifier);
l_DBPersistor.LoadDates(Connection, Transaction, l_CommandProvider, gdphs, gecoll, SelCmdOption, GetPersistenceInfo());
gecoll.AcceptChanges();
if(!connectionIsAllreadyOpened)
Connection.Close();
return gecoll;
}
catch(Exception ex)
{
if(!connectionIsAllreadyOpened && Connection.State == ConnectionState.Open)
Connection.Close();
throw new ApplicationException("error Loading data from database", ex);
}
}
public GenericDataCollection LoadData(Type CollectionType, SelectCommandOption SelCmdOption)
{
return LoadData(CollectionType, SelCmdOption, null);
}
#endregion
#region LoadDataPagedData
public virtual GenericDataCollection LoadDataPagedData(out int RowsCount, Type CollectionType, FilterSentence sentence,
OrderByInfoCollection ordInfo, string PropertiesIdentifier, int PageSize, int CurrentPageIndex, int EmptySpacesInPage)
{
RowsCount = 0;
bool connectionIsAllreadyOpened = (Connection.State == ConnectionState.Open);
if(!connectionIsAllreadyOpened)
Connection.Open();
try
{
GenericDataCollection gecoll = (GenericDataCollection)Activator.CreateInstance(CollectionType);
GenericDataPropertiesHolder gdph = null;
if(PropertiesIdentifier != null)
gdph = PersistenceManager.GetProperty(gecoll.GenericDataType, PropertiesIdentifier);
else
gdph = PersistenceManager.GetProperty(gecoll.GenericDataType);
IDbCommand SelectCommand = l_CommandProvider.GetSelectCommand(gdph, sentence, ordInfo, CommandLockType.Nolock, null);
SelectCommand.Connection = Connection;
RowsCount = l_DBPersistor.LoadPagedDates(SelectCommand, ref gdph, ref gecoll, PageSize, CurrentPageIndex, EmptySpacesInPage);
gecoll.AcceptChanges();
if(!connectionIsAllreadyOpened)
Connection.Close();
return gecoll;
}
catch(Exception ex)
{
if(!connectionIsAllreadyOpened && Connection.State == ConnectionState.Open)
Connection.Close();
throw ex;
}
}
public GenericDataCollection LoadDataPagedData(out int RowsCount, Type CollectionType, FilterSentence sentence,
OrderByInfoCollection ordercols,int PageSize, int CurrentPageIndex, int EmptySpacesInPage)
{
RowsCount = 0;
return LoadDataPagedData(out RowsCount, CollectionType, sentence, ordercols, null, PageSize, CurrentPageIndex, EmptySpacesInPage);
}
#endregion
#region SaveData
protected virtual void BeforeBeginTransactionAction()
{
//TODO
}
protected virtual void AfterBeginTransactionAction()
{
//TODO
}
protected virtual void BeforeCommitTransactionAction()
{
//TODO
}
protected virtual void ExecuteSaveCommands(GenericDataCollection gecoll, GenericDataPropertiesHolder [] gdphs)
{
l_DBPersistor.SaveDates(Connection, Transaction, l_CommandProvider, gdphs, true, gecoll, GetPersistenceInfo());
}
protected virtual void ExecuteSaveCommands(GenericData ge, GenericDataPropertiesHolder [] gdphs)
{
l_DBPersistor.SaveData(Connection, Transaction, l_CommandProvider, gdphs, true, ge, GetPersistenceInfo());
}
public void SaveData(GenericData ge, string PropertiesIdentifier)
{
try
{
OpenConnection();
GenericDataPropertiesHolder [] gdphs = PersistenceManager.GetAllProperties(ge.GetType(), PropertiesIdentifier);
BeforeBeginTransactionAction();
Transaction = Connection.BeginTransaction();
AfterBeginTransactionAction();
ExecuteSaveCommands(ge, gdphs);
BeforeCommitTransactionAction();
Transaction.Commit();
ge.AcceptChanges();
Transaction = null;
Connection.Close();
}
catch(Exception ex)
{
if(Connection.State != ConnectionState.Closed)
{
if(Transaction != null)
Transaction.Rollback();
Transaction = null;
Connection.Close();
}
throw ex;
}
}
public void SaveData(GenericDataCollection gecoll, string PropertiesIdentifier)
{
try
{
OpenConnection();
GenericDataPropertiesHolder [] gdphs = PersistenceManager.GetAllProperties(gecoll.GenericDataType, PropertiesIdentifier);
BeforeBeginTransactionAction();
Transaction = Connection.BeginTransaction();
AfterBeginTransactionAction();
ExecuteSaveCommands(gecoll, gdphs);
BeforeCommitTransactionAction();
Transaction.Commit();
gecoll.AcceptChanges();
Transaction = null;
Connection.Close();
}
catch(Exception ex)
{
if(Connection.State != ConnectionState.Closed)
{
if(Transaction != null)
Transaction.Rollback();
Transaction = null;
Connection.Close();
}
throw ex;
}
}
public void SaveData(GenericDataCollection gecoll)
{
SaveData(gecoll, null);
}
#endregion
#region IDisposable Members
public void Dispose()
{
if(Transaction != null)
{
Transaction.Rollback();
Transaction = null;
}
if(Connection != null)
{
if(Connection.State == System.Data.ConnectionState.Open)
Connection.Close();
Connection.Dispose();
}
}
#endregion
}
}
|