/// <summary>
        /// Deletes a collection of product options
        /// </summary>
        /// <param name="options">
        /// The collection of product options to be deleted
        /// </param>
        /// <param name="raiseEvents">
        /// Optional boolean indicating whether or not to raise events.
        /// </param>
        /// <remarks>
        /// This performs a check to ensure the option is valid to be deleted
        ///
        /// THIS is INTERNAL due to sharing policies
        ///
        /// </remarks>
        internal void Delete(IEnumerable <IProductOption> options, bool raiseEvents = true)
        {
            var optionsArray = options as IProductOption[] ?? options.ToArray();

            if (raiseEvents)
            {
                Deleting.RaiseEvent(new DeleteEventArgs <IProductOption>(optionsArray), this);
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();

                using (var repository = RepositoryFactory.CreateProductOptionRepository(uow))
                {
                    foreach (var option in optionsArray)
                    {
                        repository.Delete(option);
                    }

                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Deleted.RaiseEvent(new DeleteEventArgs <IProductOption>(optionsArray), this);
            }
        }
 /// <summary>
 /// Gets all the product options.
 /// </summary>
 /// <returns>
 /// The <see cref="IEnumerable{IProductOption}"/>.
 /// </returns>
 internal IEnumerable <IProductOption> GetAll()
 {
     using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.GetAll());
     }
 }
 /// <summary>
 /// Gets the number of occurrences that an option has been shared.
 /// </summary>
 /// <param name="option">
 /// The option.
 /// </param>
 /// <returns>
 /// The count of option shares.
 /// </returns>
 public int GetProductOptionShareCount(IProductOption option)
 {
     using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.GetSharedProductOptionCount(option.Key));
     }
 }
 /// <summary>
 /// Gets a collection of <see cref="IProductOption"/> by a list of keys.
 /// </summary>
 /// <param name="keys">
 /// The keys.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable{IProductOption}"/>.
 /// </returns>
 public IEnumerable <IProductOption> GetByKeys(IEnumerable <Guid> keys)
 {
     using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.GetAll(keys.ToArray()));
     }
 }
 /// <summary>
 /// Gets a <see cref="IProductOption"/> by it's key.
 /// </summary>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="IProductOption"/>.
 /// </returns>
 public IProductOption GetByKey(Guid key)
 {
     using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.Get(key));
     }
 }
        /// <summary>
        /// Deletes a product option
        /// </summary>
        /// <param name="option">
        /// The option to be deleted
        /// </param>
        /// <param name="raiseEvents">
        /// Optional boolean indicating whether or not to raise events.
        /// </param>
        /// <remarks>
        /// This performs a check to ensure the option is valid to be deleted
        /// </remarks>
        public void Delete(IProductOption option, bool raiseEvents = true)
        {
            if (!EnsureSafeOptionDelete(option))
            {
                MultiLogHelper.Warn <ProductOptionService>("A ProductOption delete attempt was aborted.  The option cannot be deleted due to it being shared with one or more products.");
                return;
            }

            if (raiseEvents)
            {
                if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs <IProductOption>(option), this))
                {
                    ((ProductOption)option).WasCancelled = true;
                    return;
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();
                using (var repository = RepositoryFactory.CreateProductOptionRepository(uow))
                {
                    repository.Delete(option);
                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Deleted.RaiseEvent(new DeleteEventArgs <IProductOption>(option), this);
            }
        }
        /// <summary>
        /// Saves a single product option.
        /// </summary>
        /// <param name="option">
        /// The option to be saved
        /// </param>
        /// <param name="raiseEvents">
        /// Optional boolean indicating whether or not to raise events.
        /// </param>
        public void Save(IProductOption option, bool raiseEvents = true)
        {
            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IProductOption>(option), this))
                {
                    ((ProductOption)option).WasCancelled = true;
                    return;
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();
                using (var repository = RepositoryFactory.CreateProductOptionRepository(uow))
                {
                    repository.AddOrUpdate(option);
                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Saved.RaiseEvent(new SaveEventArgs <IProductOption>(option), this);
            }
        }
        /// <summary>
        /// Ensures the option is safe to delete.
        /// </summary>
        /// <param name="option">
        /// The option.
        /// </param>
        /// <returns>
        /// A value indicating whether or not the option can be deleted.
        /// </returns>
        private bool EnsureSafeOptionDelete(IProductOption option)
        {
            using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
            {
                var count = repository.GetSharedProductOptionCount(option.Key);

                return(option.Shared ? count == 0 : count == 1);
            }
        }
 /// <summary>
 /// Updates an attribute.
 /// </summary>
 /// <param name="attribute">
 /// The attribute.
 /// </param>
 internal void Save(IProductAttribute attribute)
 {
     using (new WriteLock(Locker))
     {
         var uow = UowProvider.GetUnitOfWork();
         using (var repository = RepositoryFactory.CreateProductOptionRepository(uow))
         {
             repository.UpdateAttribute(attribute);
             uow.Commit();
         }
     }
 }
 /// <summary>
 /// Gets a page of <see cref="IProductOption"/>.
 /// </summary>
 /// <param name="term">
 /// A search term to filter by
 /// </param>
 /// <param name="page">
 /// The page.
 /// </param>
 /// <param name="itemsPerPage">
 /// The items per page.
 /// </param>
 /// <param name="sortBy">
 /// The sort by.
 /// </param>
 /// <param name="sortDirection">
 /// The sort direction.
 /// </param>
 /// <param name="sharedOnly">
 /// Limit to only shared options.
 /// </param>
 /// <returns>
 /// The <see cref="Page{IProductOption}"/>.
 /// </returns>
 public Page <IProductOption> GetPage(
     string term,
     long page,
     long itemsPerPage,
     string sortBy = "",
     SortDirection sortDirection = SortDirection.Descending,
     bool sharedOnly             = true)
 {
     using (var repository = RepositoryFactory.CreateProductOptionRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.GetPage(term, page, itemsPerPage, sortBy, sortDirection, sharedOnly));
     }
 }