using System.Web;

using BeWo.Data.Entities;

using BS.Shared;

using NHibernate;
using NHibernate.Criterion;

namespace BeWo.Data.Access
{
    public abstract class AbstractBaseDAO
    {
        protected ISession Session
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    // WCF Request
                    return WCFHibernateSessionManager.CurrentSession;
                }
                else
                {
                    // ASP Request
                    return HttpContext.Current.Items["hibernateSession"] as ISession;
                }
            }
        }

        protected ICriteria CreateCriteria<T>() where T : BeWoEntityBase
        {
            return this.Session.CreateCriteria(typeof(T));
        }

        protected ICriteria CreateCriteriaIsActive<T>() where T : BeWoEntityBase
        {
            return this.Session.CreateCriteria(typeof(T)).Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active));
        }

        protected ICriteria CreateCriteriaIsActiveOrArchived<T>() where T : BeWoEntityBase
        {
            return this.Session.CreateCriteria(typeof(T)).Add(Restrictions.Or(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active), Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Archived)));
        }

        private void CheckSession()
        {
            //WORKAROUND: Bei geleichzeitigen Asynchronen Calls kann e
        }
    }
}