Ejemplo n.º 1
0
        public ModelResult <PostComment> AddComment(Post post, CommentInputForImport commentInput)
        {
            ValidationStateDictionary validationState = new ValidationStateDictionary();

            validationState.Add(typeof(CommentInputForImport), validator.Validate(commentInput));

            if (!validationState.IsValid)
            {
                return(new ModelResult <PostComment>(validationState));
            }

            PostComment comment;

            using (TransactionScope transaction = new TransactionScope())
            {
                string commentSlug = generateUniqueCommentSlug(post.ToPostAddress());

                comment = commentInput.ToComment(commentSlug);

                comment = blogsCommentRepository.Save(comment, context.Site.ID, post.Blog.Name, post.Slug);

                invalidateCachedCommentDependencies(comment);

                transaction.Complete();
            }

            PostSmallReadOnly postProxy    = new PostSmallReadOnly(comment.Post);
            CommentReadOnly   commentProxy = new CommentReadOnly(comment, absolutePathHelper.GetAbsolutePath(comment));

            pluginEngine.ExecuteAll("CommentAddedFromImport", new { context, parent = postProxy, comment = commentProxy });

            return(new ModelResult <PostComment>(comment, validationState));
        }
Ejemplo n.º 2
0
        public virtual OxiteViewModel ImportSave(Blog blog, string slugPattern)
        {
            if (blog == null)
            {
                return(null);
            }

            ValidationStateDictionary validationState = new ValidationStateDictionary();
            XmlTextReader             reader          = null;
            bool modifiedSite = false;

            try
            {
                reader = new XmlTextReader(Request.Files[0].InputStream);

                BlogMLBlog         blogMLBlog = BlogMLSerializer.Deserialize(reader);
                Language           language   = languageService.GetLanguage(context.Site.LanguageDefault);
                BlogInputForImport blogInput  = new BlogInputForImport(blogMLBlog.SubTitle, blogMLBlog.SubTitle, blogMLBlog.DateCreated);
                ModelResult <Blog> results    = blogService.EditBlog(blog, blogInput);

                if (!results.IsValid)
                {
                    ModelState.AddModelErrors(results.ValidationState);

                    return(Import(blog));
                }

                if (!context.Site.HasMultipleBlogs)
                {
                    Site site = siteService.GetSite();

                    site.DisplayName = blog.DisplayName;
                    site.Description = blog.Description;

                    siteService.EditSite(site, out validationState);

                    if (!validationState.IsValid)
                    {
                        throw new Exception();
                    }

                    modifiedSite = true;
                }

                postService.RemoveAll(blog);

                foreach (BlogMLPost blogMLPost in blogMLBlog.Posts)
                {
                    if (string.IsNullOrEmpty(blogMLPost.Title) || string.IsNullOrEmpty(blogMLPost.Content.Text))
                    {
                        continue;
                    }

                    PostInputForImport postInput      = blogMLPost.ToImportPostInput(blogMLBlog, context.Site.CommentingDisabled | blog.CommentingDisabled, slugPattern, blogMLPost.Approved ? EntityState.Normal : EntityState.PendingApproval, context.User.Cast <User>());
                    ModelResult <Post> addPostResults = postService.AddPost(blog, postInput);

                    if (!addPostResults.IsValid)
                    {
                        ModelState.AddModelErrors(addPostResults.ValidationState);

                        return(Import(blog));
                    }

                    foreach (BlogMLComment blogMLComment in blogMLPost.Comments)
                    {
                        CommentInputForImport     commentInput      = blogMLComment.ToImportCommentInput(blogMLBlog, context.User.Cast <User>(), language);
                        ModelResult <PostComment> addCommentResults = commentService.AddComment(addPostResults.Item, commentInput);

                        if (!addCommentResults.IsValid)
                        {
                            ModelState.AddModelErrors(addCommentResults.ValidationState);

                            return(Import(blog));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelErrors(validationState);

                if (!string.IsNullOrEmpty(ex.Message))
                {
                    ModelState.AddModelError("ModelName", ex);
                }

                return(Import(blog));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            if (modifiedSite)
            {
                OxiteApplication.Load(ControllerContext.HttpContext);
            }

            return(new OxiteViewModel {
                Container = blog
            });
        }