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 }); }
public ModelResult <Blog> EditBlog(Blog blog, BlogInputForImport blogInput) { return(editBlog(blog, blogInput, b => b.Apply(blogInput))); }
public static Blog Apply(this Blog blog, BlogInputForImport input) { return(new Blog(blog.Site.ID, blog.CommentingDisabled, input.Created, input.Description, input.DisplayName, blog.ID, input.Created, blog.Name)); }