예제 #1
0
        /// <summary>
        /// Publish the post
        /// Draft posts will merge the comments
        /// New posts will get a unique file id
        /// OldCatID, DraftType etc will be removed
        /// </summary>
        /// <param name="post">
        /// Draft Post
        /// </param>
        /// <returns>
        /// Post ID
        /// </returns>
        public string Publish(DraftPost post)
        {
            if (post.Type == PostType.Draft)
            {
                string draftPath = this.GetFilePath(post.DraftID);
                XElement root = null;
                root = XElement.Load(draftPath);
                XAttribute attr = root.Attribute("OldCatID");
                if (attr != null)
                {
                    attr.Remove();
                }

                XAttribute attr2 = root.Attribute("Type");
                if (attr2 != null)
                {
                    attr2.Remove();
                }

                // Get a copy of the post to merge comments
                PostData data = new PostData();
                Post existPost = data.Load(post.FileID);

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                string publishPath = this.GetFilePath(post.FileID);
                XmlWriter writer = XmlWriter.Create(publishPath, settings);
                root.Save(writer);
                writer.Close();

                // Merge comments
                Post publishPost = data.Load(post.FileID);
                publishPost.Comments = existPost.Comments;
                data.Save(publishPost);

                System.IO.File.Delete(draftPath);
            }

            if (post.Type == PostType.New)
            {
                string filePath = this.GetFilePath(post.DraftID);
                XElement root = null;
                root = XElement.Load(filePath);

                XAttribute attr2 = root.Attribute("Type");
                if (attr2 != null)
                {
                    attr2.Remove();
                }

                post.FileID = this.GetUniqueFileID(post.Title);

                root.Attribute("FileID").Value = post.FileID;

                string publishPath = this.GetFilePath(post.FileID);
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(publishPath, settings);
                root.Save(writer);
                writer.Close();
                System.IO.File.Delete(filePath);
            }

            // Newly published posts will get their file ID here.
            return post.FileID;
        }
예제 #2
0
        /// <summary>
        /// Refreshes the comment XML from comments from the post
        /// Not called by application
        /// </summary>
        public void Refresh()
        {
            List<Comment> comments = new List<Comment>();
            XElement root = null;

            try
            {
                root = XElement.Load(this._path);
            }
            catch (Exception ex)
            {
                Logger.Log(NO_FILE_ERROR, ex);
                throw new ApplicationException(NO_FILE_ERROR, ex);
            }

            root.RemoveAll();
            root.SetAttributeValue("TotalComments", 0);
            root.Save(this._path);

            BlogData blogData = new BlogData();
            List<PostInfo> posts = blogData.GetBlogItems();

            foreach (PostInfo post in posts)
            {
                PostData postData = new PostData();
                Post post2 = postData.Load(post.FileID);
                comments.AddRange(post2.Comments);
            }

            var qry = from cmnt in comments orderby cmnt.Time ascending select cmnt;

            List<Comment> comments2 = qry.ToList<Comment>();
            Dictionary<string, List<Comment>> dic = new Dictionary<string, List<Comment>>();

            foreach (Comment comment in comments2)
            {
                comment.ID = this.Insert(comment);
                if (!dic.ContainsKey(comment.FileID))
                {
                    dic[comment.FileID] = new List<Comment>();
                }

                dic[comment.FileID].Add(comment);
            }

            foreach (string fileID in dic.Keys)
            {
                PostData postData = new PostData();
                Post post = postData.Load(fileID);
                post.Comments = dic[fileID];
                postData.Save(post);
            }
        }