using System;
using SiberTek.Xenta.Data;
using SiberTek.Xenta.Data.Providers;
using SiberTek.Xenta.Entities;
using SiberTek.Xenta.Entities.Collections;
using SiberTek.Xenta.Enums;
using SiberTek.Xenta.Events;
using SiberTek.Xenta.Exceptions;
using SiberTek.Xenta.Utils;
namespace SiberTek.Xenta.Managers
{
/// <summary>
/// Contains methods to manage currencies
/// </summary>
public class CurrencyManager : IManager
{
#region Fields
private static CurrencyManager _instance = null;
private ICurrencyDataProvider _dataProvider;
private bool _initialized;
#endregion
#region Constructors
private CurrencyManager()
{
_dataProvider = null;
_initialized = false;
}
#endregion
#region Events
public event EntityEventHandler CurrencyCreated;
public event EntityEventHandler CurrencyUpdated;
public event EntityEventHandler CurrencyDeleted;
#endregion
#region Properties
public static CurrencyManager Instance
{
get
{
if(_instance == null)
{
_instance = new CurrencyManager();
}
return _instance;
}
}
private ICurrencyDataProvider DataProvider
{
get
{
return _dataProvider;
}
}
#endregion
#region Methods
public void Initialize()
{
if(!_initialized)
{
_dataProvider = DataAccessManager.GetProviderInstance<ICurrencyDataProvider>("CurrencyDataProvider");
_initialized = true;
}
}
public CurrencyInfo CreateCurrency(string name, string displayName, string priceFormat, bool isActive)
{
if(String.IsNullOrEmpty(name))
{
throw new XentaException("Currency name is not valid", (int)CoreError.CurrencyNameIsNotValid);
}
name = StringHelper.TrimToLength(name, 3);
if(GetCurrencyByName(name) != null)
{
throw new XentaException("Currency already exists", (int)CoreError.DuplicateCurrency);
}
CurrencyInfo currency = null;
int currencyID;
displayName = StringHelper.TrimToLength(displayName, 50);
priceFormat = StringHelper.TrimToLength(priceFormat, 20);
if(DataProvider.InsertCurrency(name, displayName, priceFormat, isActive, DateTime.UtcNow, DateTime.UtcNow, out currencyID))
{
currency = GetCurrency(currencyID);
FireCurrencyCreated(currency);
}
return currency;
}
public CurrencyInfo GetCurrency(int currencyID)
{
return DataMapper.Map(DataProvider.GetCurrency(currencyID));
}
public CurrencyInfo GetCurrencyByName(string name)
{
if(String.IsNullOrEmpty(name))
{
return null;
}
name = StringHelper.TrimToLength(name, 3);
return DataMapper.Map(DataProvider.GetCurrencyByName(name));
}
public CurrencyInfoCollection GetAllCurrencies()
{
return GetAllCurrencies(false);
}
public CurrencyInfoCollection GetAllCurrencies(bool showHidden)
{
return GetAllCurrencies(null, null, showHidden);
}
public CurrencyInfoCollection GetAllCurrencies(DateTime? createdOnStart, DateTime? createdOnEnd, bool showHidden)
{
return DataMapper.Map(DataProvider.GetAllCurrencies(createdOnStart, createdOnEnd, showHidden));
}
public CurrencyInfo GetDefaultCurrency()
{
return GetCurrency(SettingManager.Instance.GetParam<Int32>("Currency.Default"));
}
public CurrencyInfo UpdateCurrency(int currencyID, string name, string displayName, string priceFormat, bool isActive)
{
if(String.IsNullOrEmpty(name))
{
throw new XentaException("Currency name is not valid", (int)CoreError.CurrencyNameIsNotValid);
}
CurrencyInfo currency = GetCurrency(currencyID);
if(currency == null)
{
throw new XentaException("Currency does not exist", (int)CoreError.CurrencyDoesNotExist);
}
name = StringHelper.TrimToLength(name, 3);
if(!currency.Name.Equals(name) && GetCurrencyByName(name) != null)
{
throw new XentaException("Currency already exists", (int)CoreError.DuplicateCurrency);
}
displayName = StringHelper.TrimToLength(displayName, 50);
priceFormat = StringHelper.TrimToLength(priceFormat, 20);
if(DataProvider.UpdateCurrency(currencyID, name, displayName, priceFormat, isActive, currency.CreatedOn, DateTime.UtcNow))
{
currency = GetCurrency(currencyID);
FireCurrencyUpdated(currency);
}
return currency;
}
public void DeleteCurrency(int currencyID)
{
if(currencyID == SettingManager.Instance.GetParam<Int32>("Currency.Default"))
{
throw new XentaException("Cannot delete default currency", (int)CoreError.CannotDeleteDefaultCurrency);
}
CurrencyInfo currency = GetCurrency(currencyID);
if(currency != null)
{
FireCurrencyDeleted(currency);
DataProvider.DeleteCurrency(currencyID);
}
}
#endregion
#region Utilties
private void FireCurrencyCreated(CurrencyInfo currency)
{
if(CurrencyCreated != null)
{
CurrencyCreated(this, new EntityEventArgs(currency));
}
}
private void FireCurrencyUpdated(CurrencyInfo currency)
{
if(CurrencyUpdated != null)
{
CurrencyUpdated(this, new EntityEventArgs(currency));
}
}
private void FireCurrencyDeleted(CurrencyInfo currency)
{
if(CurrencyDeleted != null)
{
CurrencyDeleted(this, new EntityEventArgs(currency));
}
}
#endregion
}
}