using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NHibernate;
using NHibernate.Linq;
using Sleis.Models;
using Sleis.Infrastructure;
using log4net;
using Sleis.Utility;
using System.Linq.Expressions;
using Spring.Data.Generic;
using Spring.Data.Common;
namespace Sleis.Data
{
public enum DbProviderType { Oracle, MsSql }
public abstract class BaseData : AdoDaoSupport, IBaseData
{
internal static ILog Log;
private ISessionFactory _sessionFactory;
private ISession _currentSession;
public IPropertyPlaceholder Properties { get; set; }
///
/// Session factory for sub-classes.
///
public ISessionFactory SessionFactory
{
protected get { return _sessionFactory; }
set { _sessionFactory = value; }
}
///
/// Get's the current active session. Will retrieve session as managed by the
/// Open Session In View module if enabled.
///
/*protected ISession CurrentSession
{
get { return _sessionFactory.GetCurrentSession(); }
}
*/
public BaseData()
{
Log = LogManager.GetLogger(this.GetType());
//if (AdoTemplate == null) { throw new NullReferenceException("AdoTemplate not configured"); }
}
public DbProviderType DbProviderType
{
get
{
if(DbProvider.DbMetadata.ProductName.Contains("Oracle"))
{
return DbProviderType.Oracle;
}
return DbProviderType.MsSql;
}
}
public ISession GetSession()
{
Log.Debug("Getting Session...");
//Injected via spring
//return SessionFactory.OpenSession();
if (SessionFactory != null)
{
return SessionFactory.OpenSession();
}
throw new ArgumentNullException("Session is null.");
}
public virtual void Init()
{
ArgumentValidationUtility.ThrowOnNull(Properties, "Properties");
}
#region AdoTemplate Param Helper Functions
public object GetDateTimeDbParam(DateTime val)
{
return (val == DateTime.MinValue) ? (object)DBNull.Value : val;
}
public string CreateParameterName(string name)
{
return AdoTemplate.DbProvider.CreateParameterName(name);
}
public object GetOutParamValue(string name, IDbParameters args)
{
//for whatever reason, MS Based SQL params have to be accessed by @Name, while Oracle just uses non prefixed name....not sure about other DBs yet.
if (DbProviderType != DbProviderType.Oracle)
return args[CreateParameterName(name)].Value;
return args[name].Value;
}
public object GetNullableDbParam(object val)
{
return (val == null) ? (object)DBNull.Value : val;
}
public object GetNullableDbStringParam(string val)
{
return (String.IsNullOrEmpty(val) || String.IsNullOrEmpty(val.Trim()) || val.Trim().ToLower().Equals("null")) ? (object)DBNull.Value : val;
}
#endregion
public override string ToString()
{
return Utility.ReflectionUtility.GetPublicPropertiesString(this);
}
public virtual IQueryable Query() where T : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query();
}
}
public int Count(Expression> predicate) where Obj : Sleis.Models.BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().Count(predicate);
}
}
public IList GetAll()
{
using (ISession session = GetSession())
{
return (from d in session.Query()
select d).ToList();
}
}
public Obj GetSingle(ISession session, Expression> predicate) where Obj : BaseIdentityModel
{
return session.Query().SingleOrDefault(predicate);
}
public Obj GetSingle(Expression> predicate) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().SingleOrDefault(predicate);
}
}
public Obj GetSingle(Expression> predicate, Expression> selectPredicate) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().Select(selectPredicate).SingleOrDefault(predicate);
}
}
public IList Get(Expression> predicate, Expression> selectPredicate) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().Where(predicate).Select(selectPredicate).ToList();
}
}
public IList Get(Expression> predicate) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().Where(predicate).ToList();
}
}
public IQueryable Get(Expression> predicate, ISession session) where Obj : BaseIdentityModel
{
return session.Query().Where(predicate);
}
public IList Get(Expression> predicate, Action> order, int pageNum, int pageSize) where Obj : BaseIdentityModel
{
if (pageSize < 1) { pageSize = GetDefaultPageSize(); }
using (ISession session = GetSession())
{
return Fetch(predicate, order, session)
.Skip(pageNum * pageSize)
.Take(pageSize)
.ToList();
}
}
public virtual IQueryable Fetch(Expression> predicate, Action> order, ISession session) where T: BaseIdentityModel
{
var orderable = new Orderable(Get(predicate, session));
order(orderable);
return orderable.Queryable;
}
public Obj Create(Obj obj) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
session.SaveOrUpdate(obj);
return obj;
}
}
public Obj GetById(int id) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
return session.Query().SingleOrDefault(o => o.Id == id);
}
}
public bool Exists(Expression> predicate, ISession session) where Obj : IIdentifiable
{
return session.Query().Any(predicate);
}
public Obj Create(Obj obj, ISession session) where Obj : BaseIdentityModel
{
//session.Evict(obj);
session.Save(obj);
return obj;
}
public void Update(Obj obj) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
using (var txn = session.BeginTransaction())
{
session.Update(obj);
txn.Commit();
}
}
}
public void Update(Obj obj, ISession session) where Obj : BaseIdentityModel
{
session.Update(obj);
}
public void SaveOrUpdate(Obj obj, ISession session) where Obj : BaseIdentityModel
{
session.SaveOrUpdate(obj);
}
public void SaveOrUpdate(Obj obj) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
session.SaveOrUpdate(obj);
session.Flush();
}
}
public void Delete(int id) where Obj : BaseIdentityModel
{
using (ISession session = GetSession())
{
using (var txn = session.BeginTransaction())
{
session.Delete(GetById(id));
txn.Commit();
}
}
}
public void Delete(int id, ISession session) where Obj : BaseIdentityModel
{
session.Delete(GetById(id));
}
public void Delete(Obj obj, ISession session) where Obj : BaseIdentityModel
{
session.Delete(obj);
}
private int GetDefaultPageSize()
{
int size = 0;
Int32.TryParse(Properties.Get(Constants.DefaultPageSize), out size);
return size;
}
}
public abstract class BaseData : BaseData, Sleis.Data.IBaseData, IBaseData where T : BaseIdentityModel
{
public IList GetAll()
{
using (ISession session = GetSession())
{
return (from d in session.Query()
select d).ToList();
}
}
public T GetSingle(Expression> predicate)
{
using (ISession session = GetSession())
{
return session.Query().SingleOrDefault(predicate);
}
}
public IList Get(Expression> predicate)
{
using (ISession session = GetSession())
{
return session.Query().Where(predicate).ToList();
}
}
public T GetById(int id)
{
using (ISession session = GetSession())
{
var entities = from d in session.Query()
where d.Id == id
select d;
return entities.FirstOrDefault();
}
}
public T Create(T obj)
{
using (ISession session = GetSession())
{
session.SaveOrUpdate(obj);
return obj;
}
}
public T Create(T obj, ISession session)
{
session.SaveOrUpdate(obj);
return obj;
}
public void Update(T obj)
{
using (ISession session = GetSession())
{
using (var txn = session.BeginTransaction())
{
session.Update(obj);
txn.Commit();
}
}
}
public void Update(T obj, ISession session)
{
session.Update(obj);
}
public void SaveOrUpdate(T obj, ISession session)
{
session.SaveOrUpdate(obj);
}
public void SaveOrUpdate(T obj)
{
using (ISession session = GetSession())
{
session.SaveOrUpdate(obj);
session.Flush();
}
}
public void Delete(int id)
{
using (ISession session = GetSession())
{
using (var txn = session.BeginTransaction())
{
session.Delete(GetById(id));
txn.Commit();
}
}
}
public void Delete(int id, ISession session)
{
session.Delete(GetById(id));
}
public void Delete(T obj, ISession session)
{
session.Delete(obj);
}
}
}