public void CreatePattern(string contentType, string name, string pattern, string description, bool makeDefault)
        {
            var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);

            if (contentDefinition == null) {
                throw new OrchardException(T("Unknown content type: {0}", contentType));
            }

            var settings = contentDefinition.Settings.GetModel<AutorouteSettings>();

            var routePattern = new RoutePattern {
                Description = description,
                Name = name,
                Pattern = pattern
            };

            var patterns = settings.Patterns;
            patterns.Add(routePattern);
            settings.Patterns = patterns;

            // define which pattern is the default
            if (makeDefault || settings.Patterns.Count == 1) {
                settings.DefaultPatternIndex = settings.Patterns.IndexOf(routePattern);
            }

            _contentDefinitionManager.AlterTypeDefinition(contentType, builder => builder.WithPart("AutoroutePart", settings.Build));
        }
        public int UpdateFrom2()
        {
            foreach (var contentTypeName in _contentTypesToUpdate)
            {
                var typeDef = ContentDefinitionManager.GetTypeDefinition(contentTypeName);
                if (typeDef == null) continue;

                var localizationPart = typeDef.Parts.Where(p=>p.PartDefinition.Name == LocalizationPartTypeName).FirstOrDefault();
                if (localizationPart == null)
                {
                    ContentDefinitionManager.AlterTypeDefinition(contentTypeName, c=>c.WithPart(LocalizationPartTypeName));
                    typeDef = ContentDefinitionManager.GetTypeDefinition(contentTypeName);
                }

                var autoroutePart = typeDef.Parts.Where(p => p.PartDefinition.Name == AutoroutePartTypeName).FirstOrDefault();
                if (autoroutePart == null) continue;

                var autoroutePartSettings = LoadAutorouteSettings(autoroutePart.Settings);

                var routePattern = autoroutePartSettings.Patterns.Where(p => p.Name == LocalizedTitleName).FirstOrDefault();
                if (routePattern == null)
                {
                    var patterns = autoroutePartSettings.Patterns.ToList();
                    var baseRoute = patterns.FirstOrDefault();
                    routePattern = new RoutePattern
                    { 
                        Name = LocalizedTitleName,
                        Pattern = baseRoute != null ? string.Format("{{Content.Culture}}/{0}", baseRoute.Pattern) : "{Content.Culture}/{Content.Slug}",
                        Description = baseRoute != null ? string.Format("en-us/{0}", baseRoute.Description) : "en-us/my-content-item"
                    };
                    patterns.Add(routePattern);
                    autoroutePartSettings.Patterns = patterns;
                }
                var defaultIndex = autoroutePartSettings.Patterns.IndexOf(routePattern);
                autoroutePartSettings.DefaultPatternIndex = defaultIndex;

                SetAutorouteSettings(autoroutePartSettings, autoroutePart.Settings);

                ContentDefinitionManager.StoreTypeDefinition(typeDef);
            }
            return 3;
        }
        public override IEnumerable <TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel)
        {
            if (builder.Name != "AutoroutePart")
            {
                yield break;
            }

            var settings = new AutorouteSettings {
                Patterns = new List <RoutePattern>()
            };

            // Get cultures
            settings.SiteCultures = _cultureManager.ListCultures().ToList();

            if (updateModel.TryUpdateModel(settings, "AutorouteSettings", null, null))
            {
                //TODO need to add validations client and/or server side here

                // If some default pattern is an empty pattern set it to the first pattern for the language
                var newDefaultPatterns = new List <DefaultPattern>();

                foreach (var defaultPattern in settings.DefaultPatterns)
                {
                    RoutePattern correspondingPattern = null;

                    if (string.IsNullOrEmpty(defaultPattern.Culture))
                    {
                        correspondingPattern = settings.Patterns.Where(x => String.IsNullOrEmpty(x.Culture)).ElementAt(Convert.ToInt32(defaultPattern.PatternIndex));
                    }
                    else
                    {
                        correspondingPattern = settings.Patterns.Where(x => String.Equals(x.Culture, defaultPattern.Culture, StringComparison.OrdinalIgnoreCase)).ElementAt(Convert.ToInt32(defaultPattern.PatternIndex));
                    }

                    if (String.IsNullOrWhiteSpace(correspondingPattern.Name) && String.IsNullOrWhiteSpace(correspondingPattern.Pattern) && String.IsNullOrWhiteSpace(correspondingPattern.Description))
                    {
                        newDefaultPatterns.Add(new DefaultPattern {
                            Culture = defaultPattern.Culture, PatternIndex = "0"
                        });
                    }
                    else
                    {
                        newDefaultPatterns.Add(defaultPattern);
                    }
                }

                settings.DefaultPatterns = newDefaultPatterns;

                // Remove empty patterns
                var patterns = settings.Patterns;
                patterns.RemoveAll(p => String.IsNullOrWhiteSpace(p.Name) && String.IsNullOrWhiteSpace(p.Pattern) && String.IsNullOrWhiteSpace(p.Description));

                // Adding a null culture for the culture neutral pattern
                var cultures = new List <string>();
                cultures.Add(null);
                cultures.AddRange(settings.SiteCultures);

                //If there is no pattern for some culture create a default one
                List <RoutePattern> newPatterns = new List <RoutePattern>();
                int current = 0;
                foreach (string culture in cultures)
                {
                    if (settings.Patterns.Any(x => String.Equals(x.Culture, culture, StringComparison.OrdinalIgnoreCase)))
                    {
                        foreach (RoutePattern routePattern in settings.Patterns.Where(x => String.Equals(x.Culture, culture, StringComparison.OrdinalIgnoreCase)))
                        {
                            newPatterns.Add(settings.Patterns[current]);
                            current++;
                        }
                    }
                    else
                    {
                        newPatterns.Add(new RoutePattern {
                            Name        = "Title",
                            Description = "my-title",
                            Pattern     = "{Content.Slug}",
                            Culture     = culture
                        });

                        _notifier.Warning(T("A default pattern has been added to AutoroutePart"));
                    }
                }

                settings.Patterns = newPatterns;

                // Update the settings builder
                settings.Build(builder);
            }

            yield return(DefinitionTemplate(settings));
        }