insert Save /update/delete by nhibernate

 


 
namespace Sana.Commerce.Customization.Common
{
    public class ExtendedOfflineCommonProvider : OfflineCommonProviderIExtendedCommonProvider
    {
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        /// Delete all product types.
        /// </summary>
        public virtual void DeleteAllProductTypes()
        {
            WrapDataAccessException(() =>
            {
                using (var sm = CreateSessionManager(false))
                {
                    var productTypes = GetProductTypes();
                    foreach (var productType in productTypes)
                    {
                        sm.Session.Delete(productType);
                    }
                    sm.Commit();
                }
            }, "Cannot delete product types for {0} website".FormatWith(Context.WebsiteId));
        }
 
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        ///  Get product types.
        /// </summary>
        /// <returns></returns>
        public virtual IList<IProductType> GetProductTypes()
        {
            using (var sm = CreateSessionManager(true))
            {
                var productTypes = sm.Session.CreateCriteria(GetRegisteredType<IProductType>())
                    .Add(NHibernate.Criterion.Restrictions.Eq("WebsiteId"CommerceFrameworkBase.Context.WebsiteId))
                    .List<IProductType>();
                return productTypes;
            }
        }
 
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        /// Save product types.
        /// </summary>
        /// <param name="productTypes"></param>
        public virtual void SaveProductTypes(IList<IProductType> productTypes)
        {
            WrapDataAccessException(() =>
            {
                using (var sm = CreateSessionManager(false))
                {
                    productTypes.Each(c => sm.Session.SaveOrUpdate(c));
                    sm.Commit();
                }
            }, "Cannot save product types");
        }
 
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        /// Save product finder types
        /// </summary>
        /// <param name="productFinderTypes"></param>
        public virtual void SaveProductFinderTypes(IList<IProductFinderTypeModel> productFinderTypes)
        {
            // Delete data for Website and product type ID,Then save 
 
            if (productFinderTypes != null)
            {
                DeleteProductFinderTypesByProductTypeCode(productFinderTypes[0].ProductTypeCode);
            }
            WrapDataAccessException(() =>
            {
                using (var sm = CreateSessionManager(false))
                {
                    productFinderTypes.Each(c => sm.Session.SaveOrUpdate(c));
                    sm.Commit();
                }
            }, "Cannot save product finder types");
        }
 
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        /// Get product finder types by product type code
        /// </summary>
        /// <param name="typeCode">product type Code</param>
        /// <returns></returns>
        public virtual IList<IProductFinderTypeModel> GetProductFinderTypesByProductTypeCode(string typeCode)
        {
            return WrapDataAccessException(() =>
               {
                   using (var sm = CreateSessionManager(true))
                   {
                       var criteria = sm.Session.CreateCriteria(GetRegisteredType<IProductFinderTypeModel>())
                          .Add(Expression.Eq("WebsiteId", Context.WebsiteId))
                          .Add(Expression.Eq("ProductTypeCode", typeCode));
                       return criteria.List<IProductFinderTypeModel>();
                   }
               }, "Cannot get related products set for products with Ids = {0} and website = {1}.", typeCode, Context.WebsiteId);
        }
 
        /// Ticket 100006: [Topmedia] 3.1. Search – Product Finder
        /// <summary>
        ///  Delete product finder types by product type code
        /// </summary>
        /// <param name="typeCode">product type Code</param>
        public virtual void DeleteProductFinderTypesByProductTypeCode(string typeCode)
        {            
            WrapDataAccessException(() =>
            {
                using (var sm = CreateSessionManager(false))
                {
                    var productFinderTypes = GetProductFinderTypesByProductTypeCode(typeCode);
                    foreach (var productFinderType in productFinderTypes)
                    {
                        sm.Session.Delete(productFinderType);
                    }
                    sm.Commit();
                }
            }, "Cannot delete product types for {0} website".FormatWith(Context.WebsiteId));
        }  
    }
}