Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DraftPost"/> class.
        /// </summary>
        /// <param name="post">
        /// The post.
        /// </param>
        public DraftPost(Post post)
        {
            this._fileID = post.FileID;
            this._type = PostType.Draft;

            this._catID = post.CatID;
            this._oldCatID = this._catID;

            this._title = post.Title;
            this._author = post.Author;
            this._time = post.Time;

            this._comments = post.Comments;
            this._docs = post.Documents;
            this._contents = post.Contents;
        }
Beispiel #2
0
        /// <summary>
        /// Saves the content of the post in XML
        /// Called during post creation / editing / AutoSave
        /// </summary>
        /// <param name="post">
        /// The Post
        /// </param>
        public void Save(Post post)
        {
            XElement postElem = null;
            string path = this.GetFilePath(post.FileID);

            string template = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            template += "<Post Title=\"{0}\" FileID=\"{1}\" Author=\"{2}\" Time=\"{3}\" CatID=\"{4}\" Comments=\"{5}\">";
            template += "<Documents></Documents>";
            template += "<Content></Content>";
            template += "<Comments></Comments></Post>";

            template = string.Format(
                template,
                post.Title,
                post.FileID,
                post.Author,
                post.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture),
                post.CatID,
                post.Comments.Count);

            try
            {
                postElem = XElement.Parse(template);
            }
            catch (Exception ex)
            {
                Logger.Log(XML_FORMAT_ERROR, ex);
                throw new ApplicationException(XML_FORMAT_ERROR, ex);
            }

            try
            {
                XElement docsElem = postElem.Element("Documents");

                foreach (Document document in post.Documents)
                {
                    XElement docElem = new XElement("Document", new XAttribute("Path", document.Path));
                    docsElem.Add(docElem);
                }

                string contents2 = HttpContext.Current.Server.HtmlEncode(post.Contents);

                // postElem.Element("Content").Value = contents2;
                postElem.Element("Content").Value = post.Contents;

                XElement commentsElem = postElem.Element("Comments");
                commentsElem.RemoveAll();

                foreach (Comment comment in post.Comments)
                {
                    XElement commentElem = new XElement(
                        "Comment",
                        new XAttribute("ID", comment.ID),
                        new XAttribute("Name", comment.Name),
                        new XAttribute("Url", comment.Url),
                        new XAttribute("IsAuthor", comment.IsAuthor),
                        new XAttribute(
                            "Time", comment.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture)),
                        new XAttribute("Ip", comment.Ip));

                    // commentElem.Value = HttpContext.Current.Server.HtmlEncode(comment.Text);
                    commentElem.Value = comment.Text;
                    commentsElem.Add(commentElem);
                }

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(path, settings);
                postElem.Save(writer);
                writer.Close();
            }
            catch (Exception ex)
            {
                Logger.Log(XML_FORMAT_ERROR, ex);
                throw new ApplicationException(XML_FORMAT_ERROR, ex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the post data
        /// Called when displaying a post
        /// </summary>
        /// <param name="fileID">
        /// Post ID
        /// </param>
        /// <returns>
        /// The Post
        /// </returns>
        public Post Load(string fileID)
        {
            string path = this.GetFilePath(fileID);

            XElement root = null;

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

            Post post = new Post();
            XElement postElem = root;

            try
            {
                post.FileID = postElem.Attribute("FileID").Value;
                post.Title = postElem.Attribute("Title").Value;
                post.Author = postElem.Attribute("Author").Value;
                post.CatID = postElem.Attribute("CatID").Value;

                try
                {
                    post.Time = DateTime.ParseExact(
                        postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
                }
                catch (Exception ex)
                {
                    string msg = string.Format(DATE_FORMAT_ERROR, fileID);
                    Logger.Log(msg, ex);
                    post.Time = DateTime.MinValue;
                }

                XElement docsElem = postElem.Element("Documents");
                if (docsElem != null)
                {
                    foreach (XElement docElem in docsElem.Elements("Document"))
                    {
                        Document document = new Document();
                        document.Path = docElem.Attribute("Path").Value;
                        post.Documents.Add(document);
                    }
                }

                string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content")));
                post.Contents = contents;

                XElement commentsElem = postElem.Element("Comments");
                if (commentsElem != null)
                {
                    foreach (XElement commentElem in commentsElem.Elements("Comment"))
                    {
                        Comment comment = new Comment();
                        comment.FileID = post.FileID;
                        comment.ID = commentElem.Attribute("ID").Value;
                        comment.Name = commentElem.Attribute("Name").Value;
                        comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem));

                        // comment.Text = HttpContext.Current.Server.HtmlDecode(comment.Text);
                        comment.Url = commentElem.Attribute("Url").Value;
                        comment.Time = DateTime.ParseExact(
                            commentElem.Attribute("Time").Value,
                            DataContext.DateTimeFormat,
                            CultureInfo.InvariantCulture);
                        comment.Ip = commentElem.Attribute("Ip").Value;
                        if (commentElem.Attribute("IsAuthor") != null)
                        {
                            comment.IsAuthor = bool.Parse(commentElem.Attribute("IsAuthor").Value);
                        }

                        post.Comments.Add(comment);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(XML_FORMAT_ERROR, ex);
                throw new ApplicationException(XML_FORMAT_ERROR, ex);
            }

            return post;
        }
Beispiel #4
0
 /// <summary>
 /// The save.
 /// </summary>
 /// <param name="post">
 /// The post.
 /// </param>
 public static void Save(Post post)
 {
     IPostData data = ConfigHelper.DataContext.PostData;
     data.Save(post);
 }