/// <summary> /// Loads statistics data into a Stat object /// Called by Application Start event /// </summary> /// <returns>The Stat object</returns> public Stat Load() { Stat stat = new Stat(); List<Tuple<PostInfo, int>> popular = new List<Tuple<PostInfo, int>>(); XElement root = null; try { root = XElement.Load(this._path); stat.Path = this._path; } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } try { List<XElement> feedElems = new List<XElement>(); List<XElement> delPostElems = new List<XElement>(); // BlogData blogData = new BlogData(); // List<PostInfo> posts = blogData.GetBlogItems(); foreach (XElement statElem in root.Elements("Stat")) { string id = statElem.Attribute("id").Value; int count = int.Parse(statElem.Attribute("Count").Value); // get feed nodes first if (Stat.IsFeedKey(id)) { feedElems.Add(statElem); stat.Feeds[id] = count; continue; } switch (id) { case "Visits": stat.Visits = count; break; case "Hits": stat.Hits = count; break; } } foreach (XElement feedElem in feedElems) { feedElem.Remove(); } BlogData blogData = new BlogData(); stat.PageVisits = blogData.GetBlogItems().OrderByDescending(p => p.Views).ToDictionary(p => p.FileID); } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } root.Save(this._path); return stat; }
/// <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); } }