Example #1
0
        public void SaveChanges(bool SaveChildren = true)
        {
            bool ShouldSaveChildren = SaveChildren || SavedAlready;

            // Add catch for uniqueness across url and site, no duplicates, how to handle?  revert to node alias path with or without document culture?
            if (!SavedAlready)
            {
                // Check itself for changes and save, then children
                foreach (NodeUrlSlug UrlSlug in UrlSlugs)
                {
                    if (UrlSlug.IsNewOrUpdated)
                    {
                        if (ValidationHelper.GetGuid(UrlSlug.ExistingNodeSlugGuid, Guid.Empty) != Guid.Empty)
                        {
                            // Now Update the Url Slug if it's not custom
                            var ExistingSlug = UrlSlugInfoProvider.GetUrlSlugInfo(UrlSlug.ExistingNodeSlugGuid);
                            if (!ExistingSlug.UrlSlugIsCustom)
                            {
                                ExistingSlug.UrlSlug = UrlSlug.UrlSlug;
                                UrlSlugInfoProvider.SetUrlSlugInfo(ExistingSlug);
                            }
                        }
                        else
                        {
                            // Check for existing Url Slug that matches the new Url
                            var MatchingUrlSlug = UrlSlugInfoProvider.GetUrlSlugs()
                                                  .WhereEquals("UrlSlug", UrlSlug.UrlSlug)
                                                  .WhereNotEquals("UrlSlugNodeID", NodeID)
                                                  .FirstOrDefault();
                            if (MatchingUrlSlug != null)
                            {
                                if (Settings.LogConflicts)
                                {
                                    var CurDoc          = DocumentHelper.GetDocument(NodeID, UrlSlug.CultureCode, new TreeProvider());
                                    var ExistingSlugDoc = DocumentHelper.GetDocument(MatchingUrlSlug.UrlSlugNodeID, MatchingUrlSlug.UrlSlugCultureCode, new TreeProvider());

                                    // Log Conflict
                                    EventLogProvider.LogEvent("W", "DynamicRouting", "UrlSlugConflict", eventDescription: string.Format("Cannot create a new Url Slug {0} for Document {1} [{2}] because it exists already for {3} [{4}]",
                                                                                                                                        UrlSlug.UrlSlug,
                                                                                                                                        CurDoc.NodeAliasPath,
                                                                                                                                        CurDoc.DocumentCulture,
                                                                                                                                        ExistingSlugDoc.NodeAliasPath,
                                                                                                                                        ExistingSlugDoc.DocumentCulture
                                                                                                                                        ));
                                }
                            }
                            else
                            {
                                var newSlug = new UrlSlugInfo()
                                {
                                    UrlSlug            = UrlSlug.UrlSlug,
                                    UrlSlugNodeID      = NodeID,
                                    UrlSlugCultureCode = UrlSlug.CultureCode,
                                    UrlSlugIsCustom    = false
                                };
                                UrlSlugInfoProvider.SetUrlSlugInfo(newSlug);
                            }
                        }
                    }
                }
                // Mark this as saved already
                SavedAlready = true;
            }

            // Now Call save children on children if they are built.
            if (ShouldSaveChildren)
            {
                foreach (NodeItem Child in Children)
                {
                    Child.SaveChanges();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Saves any inserts, updates or deletes to the database
        /// </summary>
        /// <param name="SaveChildren">If the children should be saved as well.</param>
        public void SaveChanges(bool SaveChildren = true)
        {
            bool ShouldSaveChildren = SaveChildren || SavedAlready;

            // Add catch for uniqueness across url and site, no duplicates, how to handle?  revert to node alias path with or without document culture?
            if (!SavedAlready)
            {
                // Check itself for changes and save, then children
                foreach (NodeUrlSlug UrlSlug in UrlSlugs)
                {
                    // Handle Deletes
                    if (UrlSlug.Delete)
                    {
                        UrlSlugInfoProvider.DeleteUrlSlugInfo(UrlSlug.ExistingNodeSlugGuid);
                        continue;
                    }
                    if (UrlSlug.IsNewOrUpdated)
                    {
                        // Check for existing Url Slug that matches the new Url
                        var MatchingUrlSlug = UrlSlugInfoProvider.GetUrlSlugs()
                                              .WhereEquals("UrlSlug", UrlSlug.UrlSlug)
                                              .WhereNotEquals("UrlSlugNodeID", NodeID)
                                              .Where($"UrlSlugNodeID in (Select NodeID from CMS_Tree where NodeSiteID = {UrlSlug.SiteID})")
                                              .FirstOrDefault();
                        if (MatchingUrlSlug != null)
                        {
                            if (DynamicRouteInternalHelper.AppendPostFixOnConflict())
                            {
                                bool ExistingFound = true;
                                int  AppendCount   = 0;
                                // Loop till no match found
                                while (ExistingFound)
                                {
                                    string PreviousAppendex = $"-({AppendCount})";
                                    AppendCount++;
                                    string NewAppendex = $"-({AppendCount})";
                                    if (UrlSlug.UrlSlug.Contains(PreviousAppendex))
                                    {
                                        UrlSlug.UrlSlug = UrlSlug.UrlSlug.Replace(PreviousAppendex, NewAppendex);
                                    }
                                    else
                                    {
                                        UrlSlug.UrlSlug += NewAppendex;
                                    }
                                    ExistingFound = UrlSlugInfoProvider.GetUrlSlugs()
                                                    .WhereEquals("UrlSlug", UrlSlug.UrlSlug)
                                                    .WhereNotEquals("UrlSlugNodeID", NodeID)
                                                    .Where($"UrlSlugNodeID in (Select NodeID from CMS_Tree where NodeSiteID = {UrlSlug.SiteID})")
                                                    .FirstOrDefault() != null;
                                }
                            }
                            else
                            {
                                // Technically should never hit this as the action should have either been cancelled or logged with an error
                                throw new Exception("Conflict Found on Save and Url Slug Conflict Behavior Setting is not 'Append Post Fix' so could not complete action.");
                            }
                        }

                        if (ValidationHelper.GetGuid(UrlSlug.ExistingNodeSlugGuid, Guid.Empty) != Guid.Empty)
                        {
                            // Now Update the Url Slug if it's not custom
                            var ExistingSlug = UrlSlugInfoProvider.GetUrlSlugInfo(UrlSlug.ExistingNodeSlugGuid);
                            if (!ExistingSlug.UrlSlugIsCustom)
                            {
                                ExistingSlug.UrlSlug = UrlSlug.UrlSlug;
                                UrlSlugInfoProvider.SetUrlSlugInfo(ExistingSlug);
                            }
                        }
                        else
                        {
                            var newSlug = new UrlSlugInfo()
                            {
                                UrlSlug            = UrlSlug.UrlSlug,
                                UrlSlugNodeID      = NodeID,
                                UrlSlugCultureCode = UrlSlug.CultureCode,
                                UrlSlugIsCustom    = false
                            };
                            UrlSlugInfoProvider.SetUrlSlugInfo(newSlug);
                        }
                    }
                }
                // Mark this as saved already
                SavedAlready = true;
            }

            // Now Call save children on children if they are built.
            if (ShouldSaveChildren)
            {
                foreach (NodeItem Child in Children)
                {
                    Child.SaveChanges();
                }
            }
        }