Beispiel #1
0
        /// <summary>
        /// Retrieves the page by its id.
        /// </summary>
        /// <param name="id">The id of the page</param>
        /// <returns>A <see cref="PageViewModel"/> for the page.</returns>
        /// <exception cref="DatabaseException">An databaseerror occurred while getting the page.</exception>
        public PageViewModel GetById(int id, bool loadContent = false)
        {
            try
            {
                PageViewModel pageModel = _pageViewModelCache.Get(id);
                if (pageModel != null)
                {
                    return(pageModel);
                }
                else
                {
                    Page page = Repository.GetPageById(id);

                    if (page == null)
                    {
                        return(null);
                    }
                    else
                    {
                        // If object caching is enabled, ignore the "loadcontent" parameter as the cache will be
                        // used on the second call anyway, so performance isn't an issue.
                        if (ApplicationSettings.UseObjectCache)
                        {
                            pageModel = new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter);
                        }
                        else
                        {
                            if (loadContent)
                            {
                                pageModel = new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter);
                            }
                            else
                            {
                                pageModel = new PageViewModel(page);
                            }
                        }

                        _pageViewModelCache.Add(id, pageModel);
                        return(pageModel);
                    }
                }
            }
            catch (DatabaseException ex)
            {
                throw new DatabaseException(ex, "An error occurred getting the page with id '{0}' from the database", id);
            }
        }
        /// <summary>
        /// Compares a page version to the previous version.
        /// </summary>
        /// <param name="mainVersionId">The id of the version to compare</param>
        /// <returns>Returns a IEnumerable of two versions, where the 2nd item is the previous version.
        /// If the current version is 1, or a previous version cannot be found, then the 2nd item will be null.</returns>
        /// <exception cref="HistoryException">An database error occurred while comparing the two versions.</exception>
        public IEnumerable <PageViewModel> CompareVersions(Guid mainVersionId)
        {
            try
            {
                List <PageViewModel> versions = new List <PageViewModel>();

                PageContent mainContent = Repository.GetPageContentById(mainVersionId);
                versions.Add(new PageViewModel(mainContent, _markupConverter));

                if (mainContent.VersionNumber == 1)
                {
                    versions.Add(null);
                }
                else
                {
                    PageViewModel model = _pageViewModelCache.Get(mainContent.Page.Id, mainContent.VersionNumber - 1);

                    if (model == null)
                    {
                        PageContent previousContent = Repository.GetPageContentByPageIdAndVersionNumber(mainContent.Page.Id, mainContent.VersionNumber - 1);
                        if (previousContent == null)
                        {
                            model = null;
                        }
                        else
                        {
                            model = new PageViewModel(previousContent, _markupConverter);
                            _pageViewModelCache.Add(mainContent.Page.Id, mainContent.VersionNumber - 1, model);
                        }
                    }

                    versions.Add(model);
                }

                return(versions);
            }
            catch (ArgumentNullException ex)
            {
                throw new HistoryException(ex, "An ArgumentNullException occurred comparing the version history for version id {0}", mainVersionId);
            }
            catch (DatabaseException ex)
            {
                throw new HistoryException(ex, "A HibernateException occurred comparing the version history for version id {0}", mainVersionId);
            }
        }