Ejemplo n.º 1
0
        /// <summary>
        /// Adds the page to the database.
        /// </summary>
        /// <param name="summary">The summary details for the page.</param>
        /// <returns>A <see cref="PageSummary"/> for the newly added page.</returns>
        /// <exception cref="DatabaseException">An NHibernate (database) error occurred while saving.</exception>
        /// <exception cref="SearchException">An error occurred adding the page to the search index.</exception>
        public PageSummary AddPage(PageSummary summary)
        {
            try
            {
                string currentUser = RoadkillContext.Current.CurrentUsername;

                Page page = new Page();
                page.Title = summary.Title;
                page.Tags = summary.Tags.CleanTags();
                page.CreatedBy = AppendIpForAppHarbor(currentUser);
                page.CreatedOn = DateTime.Now;
                page.ModifiedOn = DateTime.Now;
                page.ModifiedBy = AppendIpForAppHarbor(currentUser);
                NHibernateRepository.Current.SaveOrUpdate<Page>(page);

                PageContent pageContent = new PageContent();
                pageContent.VersionNumber = 1;
                pageContent.Text = summary.Content;
                pageContent.EditedBy = AppendIpForAppHarbor(currentUser);
                pageContent.EditedOn = DateTime.Now;
                pageContent.Page = page;
                NHibernateRepository.Current.SaveOrUpdate<PageContent>(pageContent);

                // Update the lucene index
                try
                {
                    SearchManager.Current.Add(page.ToSummary());
                }
                catch (SearchException)
                {
                    // TODO: log
                }

                return page.ToSummary();
            }
            catch (HibernateException e)
            {
                throw new DatabaseException(e, "An error occurred while adding page '{0}' to the database", summary.Title);
            }
        }