/// <summary>
        /// Update the specified article. Increment the version if required.
        /// </summary>
        /// <param name="article"></param>
        /// <param name="backupVersion">If true the previous article version is saved as a backup in the VersionedArticle and the current version is incremented.</param>
        public override void UpdateArticle(Article article, bool backupVersion)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                VersionedArticle versionedArticle = null;

                if (backupVersion)
                {
                    //Retrive the previous version (before saving the new instance) and save a versioned row

                    Article prevVersion = dataStore.FindByKey(article.Id);
                    if (prevVersion == null)
                    {
                        throw new ArticleNotFoundException(article.Id);
                    }

                    versionedArticle = new VersionedArticle(prevVersion);

                    VersionedArticleDataStore versionedStore = new VersionedArticleDataStore(transaction);
                    versionedStore.Insert(versionedArticle);

                    //Increment the current article version
                    article.IncrementVersion();
                }

                //flag the entity to be updated and attach the entity to the db
                // I must use InsertOrUpdateCopy because if backupVersion = true there is already a
                // persistent entity in the session and I must copy the values to this instance. The Update method in this case throw an exception
                article = dataStore.InsertOrUpdateCopy(article);

                transaction.Commit();
            }
        }
        public override IList <Article> GetArticles(Category category, ArticleStatus status, bool recursive)
        {
            IList <Article> articles = null;

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);
                articles = dataStore.FindByCategoryAndOwner(category, null, status);
                if (recursive)
                {
                    // Find children of this parent category.
                    CategoryDataStore ds = new CategoryDataStore(transaction);
                    IList <Category>  childCategories = ds.FindByChildOfCategory(category.Id);
                    foreach (Category subCategory in childCategories)
                    {
                        IList <Article> subArticles = dataStore.FindByCategoryAndOwner(subCategory, null, status);
                        foreach (Article subArticle in subArticles)
                        {
                            articles.Add(subArticle);
                        }
                    }
                }
            }
            return(articles);
        }
        public override Article CreateArticle(Category category, string owner,
                                              string name, string fileName, string title,
                                              string description, string body, bool isUpload, bool isEnabled, bool isNumbChaps,
                                              bool isAckRequired)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore articleStore = new ArticleDataStore(transaction);
                if (articleStore.FindByName(name) != null)
                {
                    throw new ArticleNameAlreadyExistsException(name);
                }

                CategoryDataStore dataStore = new CategoryDataStore(transaction);
                dataStore.Attach(category);

                Article article = new Article(category, name, fileName, owner, title, description, body, isUpload, isEnabled, isNumbChaps);
                article.Author = owner;
                //article.Tag = isAckRequired.ToString();
                article.RequiresAck = isAckRequired;

                if (category.AutoApprove)
                {
                    article.Approved = true;
                }

                articleStore.Insert(article);
                transaction.Commit();

                return(article);
            }
        }
        public override void DeleteCategory(Category category)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                CategoryDataStore dataStore = new CategoryDataStore(transaction);

                // Delete should only be allowed if the following conditions are true:
                // 1. There are no child categories.
                if (dataStore.FindByChildOfCategory(category.Id).Count > 0)
                {
                    throw new SchemaIntegrityException("Category has child categories");
                }

                // 2. There are no articles associated with this category.
                ArticleDataStore ads = new ArticleDataStore(transaction);
                if (ads.FindByCategory(category).Count > 0)
                {
                    throw new SchemaIntegrityException("Articles are associated with this category");
                }

                // Delete all access items.
                AccessLayer.AccessDataStore accessDs  = new AccessLayer.AccessDataStore(transaction);
                IList <AccessLayer.Access>  allAccess = accessDs.FindAllByItem(category.Id);

                foreach (AccessLayer.Access access in allAccess)
                {
                    access.Deleted = true;
                    accessDs.Update(access);
                }

                category.Deleted = true;
                dataStore.Update(category);
                transaction.Commit();
            }
        }
 public override IList <Article> GetAllArticles()
 {
     using (TransactionScope transaction = new TransactionScope(mConfiguration))
     {
         ArticleDataStore dsArticle = new ArticleDataStore(transaction);
         return(dsArticle.FindAllArticles());
     }
 }
 public override IList <Article> GetArticlesByTitle(string title)
 {
     using (TransactionScope transaction = new TransactionScope(mConfiguration))
     {
         ArticleDataStore dataStore = new ArticleDataStore(transaction);
         return(dataStore.FindByTitle(title));
     }
 }
        public override IList <Article> GetAllArticlesByOwner(string owner, ArticleStatus status)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                return(dataStore.FindByOwner(owner, status));
            }
        }
        /// <summary>
        /// Deletes the specified article.
        /// </summary>
        /// <param name="article"></param>
        public override string DeleteArticle(Article article)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);
                article.Deleted = true;
                article.Name   += DateTimeHelper.GetCurrentTimestamp();
                dataStore.Update(article);

                transaction.Commit();
                return(article.Category.Id);
            }
        }
        public override Article GetArticle(string id)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                Article article = dataStore.FindByKey(id);
                if (article == null)
                {
                    throw new ArticleNotFoundException(id);
                }

                return(article);
            }
        }
        public override Article GetArticleByName(string name, bool throwIfNotFound)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                Article article = dataStore.FindByName(name);
                if (article == null && throwIfNotFound)
                {
                    throw new ArticleNotFoundException(name);
                }
                else if (article == null)
                {
                    return(null);
                }

                return(article);
            }
        }
        public override IList <Article> FindArticles(Filter <string> categoryName,
                                                     Filter <string> searchFor,
                                                     Filter <string> author,
                                                     Filter <string> owner,
                                                     Filter <string> tag,
                                                     DateTime?fromDate, DateTime?toDate,
                                                     ArticleStatus status,
                                                     PagingInfo paging)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                return(dataStore.FindByFields(categoryName, searchFor,
                                              author, owner,
                                              tag,
                                              fromDate, toDate,
                                              status,
                                              paging));
            }
        }
        public override FileAttachment CreateFileAttachment(Article article, string name, string contentType, byte[] contentData)
        {
            FileAttachment attachment = new FileAttachment(article, name, contentType, contentData);

            //Check attachment
            if (attachment != null)
            {
                Attachment.FileHelper.CheckFile(attachment, article.Category.AttachExtensions, article.Category.AttachMaxSize);
            }

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);
                dataStore.Attach(article);

                FileAttachmentDataStore attachmentStore = new FileAttachmentDataStore(transaction);
                attachmentStore.Insert(attachment);

                transaction.Commit();

                return(attachment);
            }
        }