public void Save(IEnumerable <TrackbackOutbound> trackbacks) { if (trackbacks != null && trackbacks.Count() > 0) { context.oxite_TrackbackOutbounds.InsertAllOnSubmit( trackbacks.Select( tb => new oxite_TrackbackOutbound { TrackbackOutboundID = Guid.NewGuid(), TargetUrl = tb.TargetUrl, PostID = tb.PostID, PostUrl = tb.PostUrl, PostAreaTitle = tb.PostAreaTitle, PostTitle = tb.PostTitle, PostBody = tb.PostBody, RemainingRetryCount = (byte)tb.RemainingRetryCount, LastAttemptDate = null, SentDate = null, IsSending = false } ) ); context.SubmitChanges(); } }
public void Save(IBackgroundService backgroundService, NameValueCollection settings) { oxite_Plugin foundPlugin = backgroundService.ID != Guid.Empty ? ( from p in context.oxite_Plugins where p.SiteID == siteID && p.PluginID == backgroundService.ID select p ).FirstOrDefault() : null; if (foundPlugin != null) { foundPlugin.PluginName = backgroundService.Name; foundPlugin.PluginCategory = backgroundService.Category; foundPlugin.Enabled = backgroundService.Enabled; //TODO: (erikpo) Add new fields here } else { context.oxite_Plugins.InsertOnSubmit( new oxite_Plugin() { SiteID = siteID, PluginID = backgroundService.ID != Guid.Empty ? backgroundService.ID : Guid.NewGuid(), PluginName = backgroundService.Name, PluginCategory = backgroundService.Category, Enabled = backgroundService.Enabled //TODO: (erikpo) Add new fields here } ); } if (settings != null) { foreach (string name in settings.AllKeys) { oxite_PluginSetting setting = getSetting(backgroundService.ID, name); if (setting != null) { setting.PluginSettingValue = settings[name]; } else { context.oxite_PluginSettings.InsertOnSubmit( new oxite_PluginSetting() { SiteID = siteID, PluginID = backgroundService.ID, PluginSettingName = name, PluginSettingValue = settings[name] } ); } } //TODO: (erikpo) Cleanup settings that aren't needed anymore } context.SubmitChanges(); }
public void Save(IPlugin plugin, NameValueCollection settings) { oxite_Plugin foundPlugin = ( from p in context.oxite_Plugins where p.SiteID == siteID && p.PluginID == plugin.ID select p ).FirstOrDefault(); if (foundPlugin != null) { foundPlugin.PluginName = plugin.Name; foundPlugin.PluginCategory = plugin.Category; foundPlugin.Enabled = plugin.Enabled; } else { context.oxite_Plugins.InsertOnSubmit( new oxite_Plugin() { SiteID = siteID, PluginID = plugin.ID, PluginName = plugin.Name, PluginCategory = plugin.Category, Enabled = plugin.Enabled } ); } if (settings != null) { foreach (string name in settings.AllKeys) { oxite_PluginSetting setting = getSetting(plugin.ID, name); if (setting != null) { setting.PluginSettingValue = settings[name]; } else { context.oxite_PluginSettings.InsertOnSubmit( new oxite_PluginSetting() { SiteID = siteID, PluginID = plugin.ID, PluginSettingName = name, PluginSettingValue = settings[name] } ); } } //TODO: (erikpo) Cleanup settings that aren't needed anymore } context.SubmitChanges(); }
public void Save(Language language) { oxite_Language dbLanguage = null; Guid languageID = language.ID; if (languageID != Guid.Empty) { dbLanguage = (from l in context.oxite_Languages where l.LanguageID == languageID select l).FirstOrDefault(); } if (dbLanguage == null) { if (languageID == Guid.Empty) { languageID = Guid.NewGuid(); } dbLanguage = new oxite_Language(); context.oxite_Languages.InsertOnSubmit(dbLanguage); } dbLanguage.LanguageName = language.Name; dbLanguage.LanguageDisplayName = language.DisplayName; context.SubmitChanges(); language.ID = languageID; }
public void Save(Site site) { oxite_Site dbSite = null; if (site.ID != Guid.Empty) { dbSite = (from s in context.oxite_Sites where s.SiteID == site.ID || s.SiteName == site.Name select s).FirstOrDefault(); } if (dbSite == null) { dbSite = new oxite_Site(); context.oxite_Sites.InsertOnSubmit(dbSite); if (site.ID == Guid.Empty) { dbSite.SiteID = Guid.NewGuid(); } else { dbSite.SiteID = site.ID; } makeFieldsMatch(dbSite, site); } else { makeFieldsMatch(dbSite, site); var siteRedirectsQuery = from sr in context.oxite_SiteRedirects where sr.SiteID == dbSite.SiteID select sr; if (siteRedirectsQuery.Count() > 0) { context.oxite_SiteRedirects.DeleteAllOnSubmit(siteRedirectsQuery); } } if (site.HostRedirects != null && site.HostRedirects.Count > 0) { context.oxite_SiteRedirects.InsertAllOnSubmit( site.HostRedirects.Select( hr => new oxite_SiteRedirect { SiteID = dbSite.SiteID, SiteRedirect = hr.ToString() } ) ); } context.SubmitChanges(); }
public IEnumerable <TrackbackOutbound> GetNextOutbound(bool executeOnAll, TimeSpan interval) { IEnumerable <TrackbackOutbound> trackbacks = Enumerable.Empty <TrackbackOutbound>(); using (TransactionScope transaction = new TransactionScope()) { IQueryable <oxite_TrackbackOutbound> query = from t in context.oxite_TrackbackOutbounds where !t.IsSending && t.RemainingRetryCount > 0 && (!t.LastAttemptDate.HasValue || t.LastAttemptDate.Value.Add(interval) <= DateTime.Now.ToUniversalTime()) select t; if (!executeOnAll) { query = query.Take(1); } trackbacks = query.Select( tb => new TrackbackOutbound() { ID = tb.TrackbackOutboundID, TargetUrl = tb.TargetUrl, PostTitle = tb.PostTitle, PostAreaTitle = tb.PostAreaTitle, PostBody = tb.PostBody, PostUrl = tb.PostUrl, RemainingRetryCount = tb.RemainingRetryCount } ).ToList(); if (query.Count() > 0) { query.ToList().ForEach(tb => tb.IsSending = true); } context.SubmitChanges(); transaction.Complete(); } return(trackbacks); }
public IEnumerable <MessageOutbound> GetNextOutbound(bool executeOnAll, TimeSpan interval) { IEnumerable <MessageOutbound> messages = Enumerable.Empty <MessageOutbound>(); using (TransactionScope transaction = new TransactionScope()) { IQueryable <oxite_MessageOutbound> query = from mo in context.oxite_MessageOutbounds where !mo.IsSending && mo.RemainingRetryCount > 0 && (!mo.LastAttemptDate.HasValue || mo.LastAttemptDate.Value.Add(interval) <= DateTime.Now.ToUniversalTime()) select mo; if (!executeOnAll) { query = query.Take(1); } messages = query.Select( m => new MessageOutbound() { ID = m.MessageOutboundID, To = m.MessageTo, Subject = m.MessageSubject, Body = m.MessageBody, RemainingRetryCount = m.RemainingRetryCount } ).ToList(); if (query.Count() > 0) { query.ToList().ForEach(mo => mo.IsSending = true); } context.SubmitChanges(); transaction.Complete(); } return(messages); }
public void Save(IEnumerable <MessageOutbound> messages) { if (messages != null && messages.Count() > 0) { context.oxite_MessageOutbounds.InsertAllOnSubmit( messages.Select( m => new oxite_MessageOutbound { MessageOutboundID = Guid.NewGuid(), MessageTo = m.To, MessageSubject = m.Subject, MessageBody = m.Body, RemainingRetryCount = (byte)m.RemainingRetryCount, LastAttemptDate = null, SentDate = null, IsSending = false } ) ); context.SubmitChanges(); } }
public void Save(Area area, Site site) { oxite_Area dbArea = null; Guid areaID = area.ID; if (area.ID != Guid.Empty) { dbArea = (from a in context.oxite_Areas where a.SiteID == siteID && a.AreaID == area.ID select a).FirstOrDefault(); } if (dbArea == null) { if (area.ID == Guid.Empty) { areaID = Guid.NewGuid(); } dbArea = new oxite_Area(); dbArea.SiteID = site != null && site.ID != Guid.Empty ? site.ID : siteID; dbArea.AreaID = areaID; if (area.Created.HasValue) { dbArea.CreatedDate = area.Created.Value; } else { dbArea.CreatedDate = DateTime.UtcNow; } context.oxite_Areas.InsertOnSubmit(dbArea); } dbArea.CommentingDisabled = area.CommentingDisabled; dbArea.AreaName = area.Name; dbArea.Description = area.Description ?? ""; dbArea.DisplayName = area.DisplayName ?? ""; dbArea.ModifiedDate = DateTime.UtcNow; context.SubmitChanges(); area.ID = areaID; }
public void Save(User user) { oxite_User dbUser = null; Guid userID = user.ID; if (userID != Guid.Empty) { dbUser = (from u in context.oxite_Users where u.UserID == userID select u).FirstOrDefault(); } if (dbUser == null) { if (userID == Guid.Empty) { userID = Guid.NewGuid(); } dbUser = new oxite_User(); dbUser.UserID = userID; context.oxite_Users.InsertOnSubmit(dbUser); } dbUser.DefaultLanguageID = (from l in context.oxite_Languages where l.LanguageName == (site.LanguageDefault ?? "en") select l).First().LanguageID; dbUser.Username = user.Name; dbUser.DisplayName = user.DisplayName; dbUser.Email = user.Email; dbUser.HashedEmail = user.HashedEmail; dbUser.Password = user.Password; dbUser.PasswordSalt = user.PasswordSalt; dbUser.Status = user.Status; context.SubmitChanges(); user.ID = userID; }
public void Save(Page page) { oxite_Post postToSave = null; oxite_PostRelationship parentPostRelationship = null; if (page.ID != Guid.Empty) { postToSave = context.oxite_Posts.Where(p => p.PostID == page.ID).FirstOrDefault(); parentPostRelationship = context.oxite_PostRelationships.Where(pr => pr.SiteID == siteID && pr.PostID == page.ID).FirstOrDefault(); } if (postToSave == null) { postToSave = new oxite_Post { PostID = Guid.NewGuid() }; context.oxite_Posts.InsertOnSubmit(postToSave); } postToSave.Body = page.Body; postToSave.BodyShort = page.BodyShort ?? ""; postToSave.CreatedDate = page.Created ?? DateTime.UtcNow; postToSave.ModifiedDate = DateTime.UtcNow; postToSave.PublishedDate = page.Published ?? SqlDateTime.MaxValue.Value; postToSave.Slug = page.Slug; postToSave.State = (byte)page.State; postToSave.Title = page.Title; postToSave.SearchBody = page.Body; // set the parent page if (page.Parent == null || page.Parent.ID == Guid.Empty) { if (parentPostRelationship != null) { if (parentPostRelationship.ParentPostID != page.ID) { context.oxite_PostRelationships.DeleteOnSubmit(parentPostRelationship); parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = postToSave.PostID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { if (parentPostRelationship != null) { if (parentPostRelationship.ParentPostID != page.Parent.ID) { context.oxite_PostRelationships.DeleteOnSubmit(parentPostRelationship); parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.Parent.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.Parent.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } // The associated user but not changes to the user itself oxite_User user = context.oxite_Users.Where(u => u.Username.ToLower() == page.Creator.Name.ToLower()).FirstOrDefault(); if (user == null) { throw new InvalidOperationException(string.Format("User {0} could not be found", page.Creator.Name)); } if (postToSave.CreatorUserID != user.UserID) { postToSave.oxite_User = user; } context.SubmitChanges(); }
public void Save(Post post) { using (TransactionScope scope = new TransactionScope()) { oxite_Post postToSave = null; bool postIsNew = false; Guid postID = post.ID; if (post.ID != Guid.Empty) { postToSave = context.oxite_Posts.Where(p => p.PostID == post.ID).FirstOrDefault(); } if (postToSave == null) { if (post.ID == Guid.Empty) { postID = Guid.NewGuid(); } postToSave = new oxite_Post { PostID = postID }; context.oxite_Posts.InsertOnSubmit(postToSave); postIsNew = true; } postToSave.Body = post.Body; postToSave.BodyShort = post.BodyShort; postToSave.CreatedDate = post.Created ?? DateTime.UtcNow; postToSave.ModifiedDate = DateTime.UtcNow; postToSave.PublishedDate = post.Published ?? SqlDateTime.MaxValue.Value; postToSave.Slug = post.Slug; postToSave.State = (byte)post.State; postToSave.Title = post.Title; postToSave.CommentingDisabled = post.CommentingDisabled; // Tags: Use existing, create new ones if needed. Don't edit old tags foreach (Tag tag in post.Tags) { string normalizedName = normalizeTagName(tag.Name); oxite_Tag persistenceTag = context.oxite_Tags.Where(t => t.TagName.ToLower() == normalizedName.ToLower()).FirstOrDefault(); if (persistenceTag == null) { Guid newTagID = Guid.NewGuid(); persistenceTag = new oxite_Tag { TagName = normalizedName, CreatedDate = tag.Created.HasValue ? tag.Created.Value : DateTime.UtcNow, TagID = newTagID, ParentTagID = newTagID }; context.oxite_Tags.InsertOnSubmit(persistenceTag); } if (!context.oxite_PostTagRelationships.Where(pt => pt.PostID == postToSave.PostID && pt.TagID == persistenceTag.TagID).Any()) { context.oxite_PostTagRelationships.InsertOnSubmit(new oxite_PostTagRelationship { PostID = postToSave.PostID, TagID = persistenceTag.TagID, TagDisplayName = tag.DisplayName ?? tag.Name }); } } var updatedTagNames = post.Tags.Select(t => normalizeTagName(t.Name).ToLower()); var tagsRemoved = from t in context.oxite_Tags join pt in context.oxite_PostTagRelationships on t.TagID equals pt.TagID where pt.PostID == postToSave.PostID && !updatedTagNames.Contains(t.TagName.ToLower()) select pt; context.oxite_PostTagRelationships.DeleteAllOnSubmit(tagsRemoved); // Files: Use existing, create new ones if needed. Edit display name and mimetype of old files if (post.Files != null) { foreach (File file in post.Files) { oxite_File persistenceFile = context.oxite_Files.Where(f => f.PostID == postToSave.PostID && string.Compare(f.Url, file.Url.ToString(), true) == 0).FirstOrDefault(); if (persistenceFile == null) { persistenceFile = new oxite_File() { PostID = postToSave.PostID, Url = file.Url.ToString(), ID = Guid.NewGuid() }; context.oxite_Files.InsertOnSubmit(persistenceFile); } persistenceFile.DisplayName = file.DisplayName; persistenceFile.Length = file.SizeInBytes; persistenceFile.MimeType = file.MimeType; } var updatedFileUrls = post.Files.Select(f => f.Url.ToString()); var filesRemoved = from f in context.oxite_Files where f.PostID == post.ID && !updatedFileUrls.Contains(f.Url) select f; context.oxite_Files.DeleteAllOnSubmit(filesRemoved); } // The area associated with the post but not changes to the area itself oxite_Area area = post.Area.ID == Guid.Empty ? context.oxite_Areas.Where(a => a.AreaName.ToLower() == post.Area.Name.ToLower()).FirstOrDefault() : context.oxite_Areas.Where(a => a.AreaID == post.Area.ID).FirstOrDefault(); if (area == null) { throw new InvalidOperationException(string.Format("Area {0} could not be found.", post.Area.Name ?? post.Area.ID.ToString())); } if (postIsNew && (from p in context.oxite_Posts join ap in context.oxite_PostAreaRelationships on p.PostID equals ap.PostID where ap.AreaID == area.AreaID && p.Slug == postToSave.Slug select p).Any()) { throw new InvalidOperationException(string.Format("There is already a post with slug {0} in area {1}.", post.Slug, area.AreaName)); } if (postToSave.oxite_PostAreaRelationships.Count == 0) { context.oxite_PostAreaRelationships.InsertOnSubmit(new oxite_PostAreaRelationship { AreaID = area.AreaID, PostID = postToSave.PostID }); } else { oxite_PostAreaRelationship areaMapping = context.oxite_PostAreaRelationships.Where(pa => pa.PostID == postToSave.PostID).FirstOrDefault(); areaMapping.AreaID = area.AreaID; } // The associated user but not changes to the user itself oxite_User user = context.oxite_Users.Where(u => u.Username.ToLower() == post.Creator.Name.ToLower()).FirstOrDefault(); if (user == null) { throw new InvalidOperationException(string.Format("User {0} could not be found", post.Creator.Name)); } if (postToSave.CreatorUserID != user.UserID) { postToSave.oxite_User = user; } postToSave.SearchBody = postToSave.Title + postToSave.Body + postToSave.oxite_User.Username + postToSave.oxite_User.DisplayName + string.Join("", post.Tags.Select(t => t.Name + t.DisplayName).ToArray()); context.SubmitChanges(); scope.Complete(); post.ID = postID; } }