From 5f215d0029b2ae29bc30a14ffb03dcaa66e04cfc Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 3 Feb 2012 12:46:41 -0800 Subject: [PATCH] Adding admin controller for Update module --HG-- branch : autoroute --- .hgsubstate | 4 +- .../Modules/Orchard.Pages/Migrations.cs | 9 -- .../Modules/UpgrateTo14/AdminMenu.cs | 17 +++ .../Controllers/AdminController.cs | 143 ++++++++++++++++++ .../Modules/UpgrateTo14/Migrations.cs | 9 +- .../Modules/UpgrateTo14/UpgrateTo14.csproj | 7 +- .../ViewModels/MigrateViewModel.cs | 12 ++ .../UpgrateTo14/Views/Admin/Index.cshtml | 26 ++++ 8 files changed, 208 insertions(+), 19 deletions(-) create mode 100644 src/Orchard.Web/Modules/UpgrateTo14/AdminMenu.cs create mode 100644 src/Orchard.Web/Modules/UpgrateTo14/Controllers/AdminController.cs create mode 100644 src/Orchard.Web/Modules/UpgrateTo14/ViewModels/MigrateViewModel.cs create mode 100644 src/Orchard.Web/Modules/UpgrateTo14/Views/Admin/Index.cshtml diff --git a/.hgsubstate b/.hgsubstate index d7132bd1a..de1dbb07d 100644 --- a/.hgsubstate +++ b/.hgsubstate @@ -1,9 +1,9 @@ 81cb672c85fd980dd3db0515544b79a918e5eb69 src/Orchard.Web/Modules/Orchard.Alias -38ee40302540edd0f4671b8110788b295573c345 src/Orchard.Web/Modules/Orchard.Autoroute +5e43c516856ae311e2a4d35bb3c02df59c42e3d4 src/Orchard.Web/Modules/Orchard.Autoroute c54cb640d6bc14c51b9fb9bd78231bb0facec067 src/Orchard.Web/Modules/Orchard.Forms 2bf79a49eea7006847fc142891d1956882c5e285 src/Orchard.Web/Modules/Orchard.Projections a1ef39ba4e2d0cd78b3c91d6150e841793acb34b src/Orchard.Web/Modules/Orchard.Routable f2a3984789ebe5caf2822ccb9e1d2c953add9c35 src/Orchard.Web/Modules/Orchard.Rules ce578373f907c0a55fd91229a344f0755f290174 src/Orchard.Web/Modules/Orchard.TaskLease -2373765b3f1f4171b09b1458f7dce9e8b83db0a2 src/Orchard.Web/Modules/Orchard.Tokens +1a604b185be41e4e6a72436b4b84dabec4916f80 src/Orchard.Web/Modules/Orchard.Tokens d8a83a676cb3b9d0266ec83ac6a1e8562821d82b src/orchard.web/modules/Orchard.Fields diff --git a/src/Orchard.Web/Modules/Orchard.Pages/Migrations.cs b/src/Orchard.Web/Modules/Orchard.Pages/Migrations.cs index c0eaee5e8..40fdae464 100644 --- a/src/Orchard.Web/Modules/Orchard.Pages/Migrations.cs +++ b/src/Orchard.Web/Modules/Orchard.Pages/Migrations.cs @@ -22,14 +22,5 @@ namespace Orchard.Pages { ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg.WithPart("CommonPart", p => p.WithSetting("DateEditorSettings.ShowDateEditor", "true"))); return 2; } - - public int UpdateFrom2() { - // TODO: (PH:Autoroute) Copy routes/titles - ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg - .RemovePart("RoutePart") - .WithPart("TitlePart") - .WithPart("AutoroutePart")); - return 3; - } } } diff --git a/src/Orchard.Web/Modules/UpgrateTo14/AdminMenu.cs b/src/Orchard.Web/Modules/UpgrateTo14/AdminMenu.cs new file mode 100644 index 000000000..b0db21cab --- /dev/null +++ b/src/Orchard.Web/Modules/UpgrateTo14/AdminMenu.cs @@ -0,0 +1,17 @@ +using Orchard.Localization; +using Orchard.Security; +using Orchard.UI.Navigation; + +namespace UpgrateTo14 { + public class AdminMenu : INavigationProvider { + public Localizer T { get; set; } + + public string MenuName { + get { return "admin"; } + } + + public void GetNavigation(NavigationBuilder builder) { + builder.Add(T("Migrate Routes"), "0", item => item.Action("Index", "Admin", new { area = "UpgrateTo14" }).Permission(StandardPermissions.SiteOwner)); + } + } +} diff --git a/src/Orchard.Web/Modules/UpgrateTo14/Controllers/AdminController.cs b/src/Orchard.Web/Modules/UpgrateTo14/Controllers/AdminController.cs new file mode 100644 index 000000000..df580caea --- /dev/null +++ b/src/Orchard.Web/Modules/UpgrateTo14/Controllers/AdminController.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Transactions; +using System.Web.Mvc; +using Orchard; +using Orchard.Autoroute.Models; +using Orchard.Autoroute.Services; +using Orchard.ContentManagement; +using Orchard.ContentManagement.MetaData; +using Orchard.Core.Common.Models; +using Orchard.Core.Title.Models; +using Orchard.Data; +using Orchard.Environment.Configuration; +using Orchard.Localization; +using Orchard.Security; +using Orchard.UI.Notify; +using UpgrateTo14.ViewModels; + +namespace UpgrateTo14.Controllers { + public class AdminController : Controller { + private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly IOrchardServices _orchardServices; + private readonly ISessionFactoryHolder _sessionFactoryHolder; + private readonly ShellSettings _shellSettings; + private readonly IAutorouteService _autorouteService; + + public AdminController( + IContentDefinitionManager contentDefinitionManager, + IOrchardServices orchardServices, + ISessionFactoryHolder sessionFactoryHolder, + ShellSettings shellSettings, + IAutorouteService autorouteService) { + _contentDefinitionManager = contentDefinitionManager; + _orchardServices = orchardServices; + _sessionFactoryHolder = sessionFactoryHolder; + _shellSettings = shellSettings; + _autorouteService = autorouteService; + } + + public Localizer T { get; set; } + + public ActionResult Index() { + var viewModel = new MigrateViewModel { ContentTypes = new List() }; + foreach (var contentType in _contentDefinitionManager.ListTypeDefinitions().OrderBy(c => c.Name)) { + // only display routeparts + if (contentType.Parts.Any(x => x.PartDefinition.Name == "RoutePart")) { + viewModel.ContentTypes.Add(new ContentTypeEntry {ContentTypeName = contentType.Name}); + } + } + + if(!viewModel.ContentTypes.Any()) { + _orchardServices.Notifier.Warning(T("There are no content types with RoutePart")); + } + + return View(viewModel); + } + + [HttpPost, ActionName("Index")] + public ActionResult IndexPOST() { + if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate routes."))) + return new HttpUnauthorizedResult(); + + var viewModel = new MigrateViewModel { ContentTypes = new List() }; + + if(TryUpdateModel(viewModel)) { + var contentTypesToMigrate = viewModel.ContentTypes.Where(c => c.IsChecked).Select(c => c.ContentTypeName); + + var sessionFactory = _sessionFactoryHolder.GetSessionFactory(); + var session = sessionFactory.OpenSession(); + + foreach (var contentType in contentTypesToMigrate) { + // migrating pages + _contentDefinitionManager.AlterTypeDefinition(contentType, + builder => builder + .WithPart("AutoroutePart") + .WithPart("TitlePart")); + + var count = 0; + var isContainable = false; + IEnumerable contents; + + do { + contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100); + + foreach (dynamic content in contents) { + var autoroutePart = ((ContentItem) content).As(); + var titlePart = ((ContentItem) content).As(); + var commonPart = ((ContentItem) content).As(); + + if(commonPart != null && commonPart.Container != null) { + isContainable = true; + } + + using (new TransactionScope(TransactionScopeOption.Suppress)) { + var command = session.Connection.CreateCommand(); + command.CommandText = string.Format("SELECT Title, Path FROM {0} WHERE ContentItemRecord_Id = {1}", GetPrefixedTableName("Routable_RoutePartRecord"), autoroutePart.ContentItem.Id); + var reader = command.ExecuteReader(); + reader.Read(); + var title = reader.GetString(0); + var path = reader.GetString(1); + reader.Close(); + + autoroutePart.DisplayAlias = path; + titlePart.Title = title; + } + + _autorouteService.PublishAlias(autoroutePart); + count++; + } + + _orchardServices.ContentManager.Flush(); + // todo: _orchardServices.ContentManager.Clear(); + + } while (contents.Any()); + + _contentDefinitionManager.AlterTypeDefinition(contentType, builder => builder.RemovePart("RoutePart")); + + var typeDefinition = _contentDefinitionManager.GetTypeDefinition(contentType); + if (isContainable || typeDefinition.Parts.Any(x => x.PartDefinition.Name == "ContainablePart")) { + _autorouteService.CreatePattern(contentType, "Container and Title", "{Content.Container.Path}/{Content.Slug}", "my-container/a-sample-title", true); + } + else { + _autorouteService.CreatePattern(contentType, "Title", "{Content.Slug}", "my-sample-title", true); + } + + _orchardServices.Notifier.Information(T("{0} was migrated successfully", contentType)); + } + } + + return RedirectToAction("Index"); + } + + private string GetPrefixedTableName(string tableName) { + if (string.IsNullOrWhiteSpace(_shellSettings.DataTablePrefix)) { + return tableName; + } + + return _shellSettings.DataTablePrefix + "_" + tableName; + } + } +} diff --git a/src/Orchard.Web/Modules/UpgrateTo14/Migrations.cs b/src/Orchard.Web/Modules/UpgrateTo14/Migrations.cs index 6deda2781..fb3895830 100644 --- a/src/Orchard.Web/Modules/UpgrateTo14/Migrations.cs +++ b/src/Orchard.Web/Modules/UpgrateTo14/Migrations.cs @@ -1,38 +1,33 @@ using System.Transactions; -using Orchard.Alias; using Orchard.Autoroute.Models; using Orchard.Autoroute.Services; using Orchard.ContentManagement; using Orchard.ContentManagement.MetaData; -using Orchard.Core.Common.Models; using Orchard.Core.Title.Models; using Orchard.Data; using Orchard.Data.Migration; -using Orchard.Data.Migration.Schema; using Orchard.Environment.Configuration; namespace UpgrateTo14 { public class UpdateTo14DataMigration : DataMigrationImpl { private readonly IContentManager _contentManager; private readonly IAutorouteService _autorouteService; - private readonly IAliasService _aliasService; private readonly ISessionFactoryHolder _sessionFactoryHolder; private readonly ShellSettings _shellSettings; public UpdateTo14DataMigration( IContentManager contentManager, - IAutorouteService autorouteService, - IAliasService aliasService, + IAutorouteService autorouteService, ISessionFactoryHolder sessionFactoryHolder, ShellSettings shellSettings) { _contentManager = contentManager; _autorouteService = autorouteService; - _aliasService = aliasService; _sessionFactoryHolder = sessionFactoryHolder; _shellSettings = shellSettings; } public int Create() { + return 1; var sessionFactory = _sessionFactoryHolder.GetSessionFactory(); var session = sessionFactory.OpenSession(); diff --git a/src/Orchard.Web/Modules/UpgrateTo14/UpgrateTo14.csproj b/src/Orchard.Web/Modules/UpgrateTo14/UpgrateTo14.csproj index 5092ad51d..3f3805af7 100644 --- a/src/Orchard.Web/Modules/UpgrateTo14/UpgrateTo14.csproj +++ b/src/Orchard.Web/Modules/UpgrateTo14/UpgrateTo14.csproj @@ -88,12 +88,17 @@ + + + - + + +