Ejemplo n.º 1
0
        public virtual OxiteModel BlogMLSave(Area areaInput, string slugPattern, User currentUser)
        {
            //TODO: (erikpo) Change this to be user selectable in the multiple blogs case if the user is a site owner
            Area area = areaService.GetArea("Blog");

            if (area == null)
            {
                return(null);
            }

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

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

                BlogMLBlog blog     = BlogMLSerializer.Deserialize(reader);
                Language   language = languageService.GetLanguage(site.LanguageDefault);

                area.Description = blog.SubTitle;
                area.DisplayName = blog.Title;
                area.Created     = blog.DateCreated;
                areaService.EditArea(area, out validationState);

                if (!site.HasMultipleAreas)
                {
                    site.DisplayName = area.DisplayName;
                    site.Description = area.Description;

                    siteService.EditSite(site, out validationState);

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

                    modifiedSite = true;
                }

                postService.RemoveAll(area);

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

                    Post post = blogMLPost.ToPost(blog, currentUser, slugPattern);

                    postService.AddPost(post, currentUser, false, out validationState, out post);

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

                    foreach (BlogMLComment blogMLComment in blogMLPost.Comments)
                    {
                        Comment comment = blogMLComment.ToComment(blog, currentUser, language);

                        postService.ValidateComment(comment, out validationState);

                        if (validationState.IsValid)
                        {
                            postService.AddCommentWithoutMessages(area, post, comment, comment.Creator, false, out validationState, out comment);

                            if (!validationState.IsValid)
                            {
                                throw new Exception();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelErrors(validationState);

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

                return(BlogML(areaInput));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

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

            return(new OxiteModel {
                Container = area
            });
        }