private static AddQuestionResult UpdateQuestion(string website, SyndicationItem item) { if (!questionsCollection.ContainsKey(item.Id)) { Debug.WriteLine("{0} Skipped question.", item.Id); return(AddQuestionResult.None); } JsonObject questionObject = questionsCollection.GetNamedObject(item.Id); AddQuestionResult result = AddQuestionResult.None; string oldTitle = questionObject.GetNamedStringOrEmptyString("Title"); string newTitle = item.Title != null ? item.Title.Text : String.Empty; if (oldTitle != newTitle) { questionObject.SetNamedValue("Title", JsonValue.CreateStringValue(newTitle)); Debug.WriteLine("{0} Updated question. Different title.", item.Id); result = AddQuestionResult.Updated; } string oldSummary = questionObject.GetNamedStringOrEmptyString(SummaryKey); string newSummary = item.Summary != null ? item.Summary.Text : String.Empty; if (oldSummary != newSummary) { questionObject.SetNamedValue(SummaryKey, JsonValue.CreateStringValue(newTitle)); Debug.WriteLine("{0} Updated question. Different summary.", item.Id); result = AddQuestionResult.Updated; } Debug.WriteLineIf( result == AddQuestionResult.None, String.Format("{0} Skipped question. Up to date.", item.Id)); return(result); }
// This method can be call simultaneously. Make sure only one thread is touching it. public static AddQuestionsResult AddQuestions(string websiteUrl, SyndicationFeed feed, bool skipLatestPubDate) { AddQuestionsResult globalResult = new AddQuestionsResult(); DateTimeOffset latestPubDate = SettingsManager.GetLastestPubDate(websiteUrl); DateTimeOffset newLatestPubDate = DateTimeOffset.MinValue; Debug.WriteLine("{0} Current LastestPubDate is {1}.", websiteUrl, latestPubDate); // Wait until the event is set by another thread. addEvent.WaitOne(); try { CheckSettingsAreLoaded(); foreach (SyndicationItem item in feed.Items) { AddQuestionResult result = AddQuestionResult.None; if (skipLatestPubDate || DateTimeOffset.Compare(item.PublishedDate.DateTime, latestPubDate) > 0) { result = AddQuestion(websiteUrl, item); // Chances are we need to update the LatestPubDate. if (result == AddQuestionResult.Added && item.PublishedDate > newLatestPubDate) { newLatestPubDate = item.PublishedDate; Debug.WriteLine("{0} New LastestPubDate is {1}.", websiteUrl, newLatestPubDate); } } else { result = UpdateQuestion(websiteUrl, item); } switch (result) { case AddQuestionResult.Added: globalResult.AddedQuestions++; break; case AddQuestionResult.Updated: globalResult.UpdatedQuestions++; break; } } // If the quesiton list did not change, there should not be a new LatestPubDate. if (globalResult.AddedQuestions > 0) { SettingsManager.SetLastestPubDate(websiteUrl, newLatestPubDate); } return(globalResult); } finally { // Set the event, so other threads waiting on it can do their job. addEvent.Set(); } }