From 66b297c4dbb020e28fa1a9563c2e31f40067f611 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 21 Jun 2013 16:05:35 -0700 Subject: [PATCH] Adding upgrade steps for Content Picker --HG-- branch : 1.x extra : rebase_source : c58efb226ce34208a2b73341d5ef3a0cb5afcafa --- .../Handlers/ContentMenuItemPartHandler.cs | 8 +- .../Orchard.ContentPicker/Migrations.cs | 50 +------- .../Orchard.MediaLibrary.csproj | 4 +- src/Orchard.Web/Modules/Upgrade/AdminMenu.cs | 11 +- .../Controllers/ContentPickerController.cs | 58 +++++++++ .../Upgrade/Controllers/TaxonomyController.cs | 23 +++- src/Orchard.Web/Modules/Upgrade/Module.txt | 2 +- .../Upgrade/Services/IUpgradeService.cs | 11 ++ .../Upgrade/Services/UpgradeService.cs | 111 ++++++++++++++++++ .../Modules/Upgrade/Upgrade.csproj | 6 + .../Upgrade/Views/ContentPicker/Index.cshtml | 14 +++ .../Upgrade/Views/Taxonomy/Index.cshtml | 6 +- 12 files changed, 240 insertions(+), 64 deletions(-) create mode 100644 src/Orchard.Web/Modules/Upgrade/Controllers/ContentPickerController.cs create mode 100644 src/Orchard.Web/Modules/Upgrade/Services/IUpgradeService.cs create mode 100644 src/Orchard.Web/Modules/Upgrade/Services/UpgradeService.cs create mode 100644 src/Orchard.Web/Modules/Upgrade/Views/ContentPicker/Index.cshtml diff --git a/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentMenuItemPartHandler.cs b/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentMenuItemPartHandler.cs index 9357241e0..e3c154bfa 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentMenuItemPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentMenuItemPartHandler.cs @@ -15,7 +15,13 @@ namespace Orchard.ContentPicker.Handlers { Filters.Add(new ActivatingFilter("ContentMenuItem")); Filters.Add(StorageFilter.For(repository)); - OnLoading((context, part) => part._content.Loader(p => contentManager.Get(part.Record.ContentMenuItemRecord.Id))); + OnLoading((context, part) => part._content.Loader(p => { + if (part.Record.ContentMenuItemRecord != null) { + return contentManager.Get(part.Record.ContentMenuItemRecord.Id); + } + + return null; + })); } protected override void GetItemMetadata(GetContentItemMetadataContext context) { diff --git a/src/Orchard.Web/Modules/Orchard.ContentPicker/Migrations.cs b/src/Orchard.Web/Modules/Orchard.ContentPicker/Migrations.cs index 0f17691b8..7a1e50af4 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentPicker/Migrations.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentPicker/Migrations.cs @@ -1,28 +1,9 @@ -using System; -using Orchard.ContentManagement.MetaData; -using Orchard.ContentManagement.Records; -using Orchard.ContentPicker.Models; +using Orchard.ContentManagement.MetaData; using Orchard.Core.Contents.Extensions; -using Orchard.Data; using Orchard.Data.Migration; -using Orchard.Environment.Configuration; -using Orchard.Logging; namespace Orchard.ContentPicker { public class Migrations : DataMigrationImpl { - private readonly ISessionLocator _sessionLocator; - private readonly ShellSettings _shellSettings; - - public Migrations( - ISessionLocator sessionLocator, - ShellSettings shellSettings) { - _sessionLocator = sessionLocator; - _shellSettings = shellSettings; - - Logger = NullLogger.Instance; - } - - public ILogger Logger { get; set; } public int Create() { @@ -46,35 +27,6 @@ namespace Orchard.ContentPicker { .Attachable() .WithDescription("Allows the management of Content Menu Items associated with a Content Item.")); - // copying records from previous version of ContentMenuItemPartRecord which was in Core - var session = _sessionLocator.For(typeof (ContentItemRecord)); - - var tablePrefix = String.IsNullOrEmpty(_shellSettings.DataTablePrefix) ? "" : _shellSettings.DataTablePrefix + "_"; - - try { - if (null != session.CreateSQLQuery("SELECT COUNT(*) FROM " + tablePrefix + "Navigation_ContentMenuItemPartRecord").UniqueResult()) { - // if no exception is thrown, we need to upgrade previous data - var records = session.CreateSQLQuery("SELECT * FROM " + tablePrefix + "Navigation_ContentMenuItemPartRecord").List(); - - foreach (dynamic record in records) { - try { - - session.CreateSQLQuery("INSERT INTO " + tablePrefix + "Orchard_ContentPicker_ContentMenuItemPartRecord (Id, ContentMenuItemRecord_id) VALUES (:id, :cid)") - .SetInt32("id", (int)record.Id) - .SetInt32("cid", (int)record.ContentMenuItemRecord_id) - .ExecuteUpdate(); - } - catch (Exception e) { - Logger.Error(e, "Could not migrate ContentMenuItemRecord with Id: {0}", (int)record.Id); - } - } - } - - } - catch { - // silently ignore exception as it means there is no content to migrate - } - return 1; } } diff --git a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj index aa1803c33..b4ce3605e 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj +++ b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj @@ -85,7 +85,9 @@ - + + Designer + diff --git a/src/Orchard.Web/Modules/Upgrade/AdminMenu.cs b/src/Orchard.Web/Modules/Upgrade/AdminMenu.cs index 12a88d519..f67acfbf9 100644 --- a/src/Orchard.Web/Modules/Upgrade/AdminMenu.cs +++ b/src/Orchard.Web/Modules/Upgrade/AdminMenu.cs @@ -13,11 +13,12 @@ namespace Upgrade { public void GetNavigation(NavigationBuilder builder) { builder .Add(T("Upgrade to 1.7"), "0", menu => menu.Action("Index", "Route", new { area = "Upgrade" }) - .Add(T("Migrate Media"), "1", item => item.Action("Index", "Media", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) - .Add(T("Migrate Taxonomies"), "2", item => item.Action("Index", "Taxonomy", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) - .Add(T("Migrate Routes"), "3", item => item.Action("Index", "Route", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) - .Add(T("Migrate Fields"), "4", item => item.Action("Index", "Field", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) - .Add(T("Migrate Menu"), "5", item => item.Action("Index", "Menu", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Media (1.7)"), "1", item => item.Action("Index", "Media", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Taxonomies (1.7)"), "2", item => item.Action("Index", "Taxonomy", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Content Picker (1.7)"), "2", item => item.Action("Index", "ContentPicker", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Fields (1.5)"), "3", item => item.Action("Index", "Field", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Menu (1.5)"), "4", item => item.Action("Index", "Menu", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) + .Add(T("Routes (1.4)"), "5", item => item.Action("Index", "Route", new { area = "Upgrade" }).LocalNav().Permission(StandardPermissions.SiteOwner)) ); } } diff --git a/src/Orchard.Web/Modules/Upgrade/Controllers/ContentPickerController.cs b/src/Orchard.Web/Modules/Upgrade/Controllers/ContentPickerController.cs new file mode 100644 index 000000000..ab7ba1a5f --- /dev/null +++ b/src/Orchard.Web/Modules/Upgrade/Controllers/ContentPickerController.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using System.Web.Mvc; +using Orchard; +using Orchard.Environment.Features; +using Orchard.Localization; +using Orchard.Logging; +using Orchard.Security; +using Orchard.UI.Admin; +using Orchard.UI.Notify; +using Upgrade.Services; + +namespace Upgrade.Controllers { + [Admin] + public class ContentPickerController : Controller { + private readonly IUpgradeService _upgradeService; + private readonly IOrchardServices _orchardServices; + private readonly IFeatureManager _featureManager; + + public ContentPickerController( + IUpgradeService upgradeService, + IOrchardServices orchardServices, + IFeatureManager featureManager) { + _upgradeService = upgradeService; + _orchardServices = orchardServices; + _featureManager = featureManager; + } + + public Localizer T { get; set; } + public ILogger Logger { get; set; } + + public ActionResult Index() { + if(_featureManager.GetEnabledFeatures().All(x => x.Id != "Orchard.ContentPicker")) { + _orchardServices.Notifier.Warning(T("You need to enable Orchard.ContentPicker in order to migrate Content Picker items to Orchard.ContentPicker.")); + } + + return View(); + } + + [HttpPost, ActionName("Index")] + public ActionResult IndexPOST() { + if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate Orchard.ContentPicker."))) + return new HttpUnauthorizedResult(); + try { + _upgradeService.CopyTable("Navigation_ContentMenuItemPartRecord", "Orchard_ContentPicker_ContentMenuItemPartRecord"); + + _orchardServices.Notifier.Information(T("Content Picker menu items were migrated successfully.")); + } + catch(Exception e) { + Logger.Error(e, "Unexpected error while migrating to Orchard.ContentPicker. Please check the log."); + _orchardServices.Notifier.Error(T("Unexpected error while migrating to Orchard.ContentPicker. Please check the log.")); + + } + + return RedirectToAction("Index"); + } + } +} diff --git a/src/Orchard.Web/Modules/Upgrade/Controllers/TaxonomyController.cs b/src/Orchard.Web/Modules/Upgrade/Controllers/TaxonomyController.cs index c3fde69cb..1ff357c2f 100644 --- a/src/Orchard.Web/Modules/Upgrade/Controllers/TaxonomyController.cs +++ b/src/Orchard.Web/Modules/Upgrade/Controllers/TaxonomyController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Orchard; @@ -6,31 +7,37 @@ using Orchard.ContentManagement; using Orchard.ContentManagement.MetaData; using Orchard.Environment.Features; using Orchard.Localization; +using Orchard.Logging; using Orchard.Security; using Orchard.UI.Admin; using Orchard.UI.Notify; +using Upgrade.Services; using Upgrade.ViewModels; namespace Upgrade.Controllers { [Admin] public class TaxonomyController : Controller { + private readonly IUpgradeService _upgradeService; private readonly IContentDefinitionManager _contentDefinitionManager; private readonly IOrchardServices _orchardServices; private readonly IFeatureManager _featureManager; public TaxonomyController( + IUpgradeService upgradeService, IContentDefinitionManager contentDefinitionManager, IOrchardServices orchardServices, IFeatureManager featureManager) { + _upgradeService = upgradeService; _contentDefinitionManager = contentDefinitionManager; _orchardServices = orchardServices; _featureManager = featureManager; } public Localizer T { get; set; } + public ILogger Logger { get; set; } public ActionResult Index() { - if(!_featureManager.GetEnabledFeatures().Any(x => x.Id == "Orchard.Taxonomies")) { + if(_featureManager.GetEnabledFeatures().All(x => x.Id != "Orchard.Taxonomies")) { _orchardServices.Notifier.Warning(T("You need to enable Orchard.Taxonomies in order to migrate Contrib.Taxonomies to Orchard.Taxonomies.")); } @@ -41,9 +48,17 @@ namespace Upgrade.Controllers { public ActionResult IndexPOST() { if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate Contrib.Taxonomies."))) return new HttpUnauthorizedResult(); + try { + _upgradeService.CopyTable("Contrib_Taxonomies_TermPartRecord", "Orchard_Taxonomies_TermPartRecord"); + + _orchardServices.Notifier.Information(T("Taxonomies were migrated successfully.")); + } + catch(Exception e) { + Logger.Error(e, "Unexpected error while migrating to Orchard.Taxonomies. Please check the log."); + _orchardServices.Notifier.Error(T("Unexpected error while migrating to Orchard.Taxonomies. Please check the log.")); + + } - _orchardServices.Notifier.Information(T("Taxonomies were migrated successfully.")); - return RedirectToAction("Index"); } } diff --git a/src/Orchard.Web/Modules/Upgrade/Module.txt b/src/Orchard.Web/Modules/Upgrade/Module.txt index 34a77b8fb..176b1f8f1 100644 --- a/src/Orchard.Web/Modules/Upgrade/Module.txt +++ b/src/Orchard.Web/Modules/Upgrade/Module.txt @@ -9,4 +9,4 @@ Category: Developer Features: Upgrade: Description: Provides actions for upgrading Orchard instances - Dependencies: Orchard.Autoroute, Title, Navigation \ No newline at end of file + Dependencies: Orchard.Autoroute, Title, Navigation, Orchard.MediaLibrary \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Upgrade/Services/IUpgradeService.cs b/src/Orchard.Web/Modules/Upgrade/Services/IUpgradeService.cs new file mode 100644 index 000000000..ce0f3823a --- /dev/null +++ b/src/Orchard.Web/Modules/Upgrade/Services/IUpgradeService.cs @@ -0,0 +1,11 @@ +using System; +using System.Data; +using Orchard; + +namespace Upgrade.Services { + public interface IUpgradeService : IDependency { + void CopyTable(string fromTableName, string toTableName); + void ExecuteReader(string sqlStatement, Action action); + string GetPrefixedTableName(string tableName); + } +} diff --git a/src/Orchard.Web/Modules/Upgrade/Services/UpgradeService.cs b/src/Orchard.Web/Modules/Upgrade/Services/UpgradeService.cs new file mode 100644 index 000000000..a44eb37f5 --- /dev/null +++ b/src/Orchard.Web/Modules/Upgrade/Services/UpgradeService.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Transactions; +using Orchard.Data; +using Orchard.Environment.Configuration; +using Orchard.Logging; + +namespace Upgrade.Services { + public class UpgradeService : IUpgradeService { + private readonly ISessionFactoryHolder _sessionFactoryHolder; + private readonly ShellSettings _shellSettings; + + public UpgradeService(ISessionFactoryHolder sessionFactoryHolder, ShellSettings shellSettings) { + _sessionFactoryHolder = sessionFactoryHolder; + _shellSettings = shellSettings; + Logger = NullLogger.Instance; + } + + public ILogger Logger { get; set; } + + public void CopyTable(string fromTableName, string toTableName) { + var fromPrefixedTableName = GetPrefixedTableName(fromTableName); + var toPrefixedTableName = GetPrefixedTableName(toTableName); + + var selectStatement = String.Format(@"SELECT * FROM {0}", fromPrefixedTableName); + var values = new List>(); + + ExecuteReader(selectStatement, (reader, conn) => { + var parameters = new SortedDictionary(); + + for (int i = 0; i < reader.FieldCount; i++) { + parameters.Add(reader.GetName(i), reader.GetValue(i)); + } + + values.Add(parameters); + }); + + var sessionFactory = _sessionFactoryHolder.GetSessionFactory(); + var session = sessionFactory.OpenSession(); + + var connection = session.Connection; + + foreach (var record in values) { + var command = connection.CreateCommand(); + var statement = String.Format("INSERT INTO {0} (", toPrefixedTableName); + + foreach (var keyValuePair in record) { + statement += keyValuePair.Key; + if (keyValuePair.Key != record.Last().Key) { + statement += ", "; + } + } + + statement += ") VALUES ( "; + + foreach (var keyValuePair in record) { + + var parameter = command.CreateParameter(); + parameter.ParameterName = "@" + keyValuePair.Key; + parameter.Value = keyValuePair.Value; + + statement += parameter.ParameterName; + if (keyValuePair.Key != record.Last().Key) { + statement += ", "; + } + + command.Parameters.Add(parameter); + } + + statement += ")"; + + command.CommandText = statement; + + command.ExecuteNonQuery(); + } + } + + public void ExecuteReader(string sqlStatement, Action action) { + var sessionFactory = _sessionFactoryHolder.GetSessionFactory(); + var session = sessionFactory.OpenSession(); + + var command = session.Connection.CreateCommand(); + command.CommandText = string.Format(sqlStatement); + + var reader = command.ExecuteReader(); + + while (reader != null && reader.Read()) { + try { + action(reader, session.Connection); + } + catch (Exception e) { + Logger.Error(e, "Error while executing custom SQL Statement in Upgrade."); + } + } + + if (reader != null && !reader.IsClosed) { + reader.Close(); + } + } + + public string GetPrefixedTableName(string tableName) { + if (string.IsNullOrWhiteSpace(_shellSettings.DataTablePrefix)) { + return tableName; + } + + return _shellSettings.DataTablePrefix + "_" + tableName; + } + } +} diff --git a/src/Orchard.Web/Modules/Upgrade/Upgrade.csproj b/src/Orchard.Web/Modules/Upgrade/Upgrade.csproj index 3f7026c2f..0d87001e2 100644 --- a/src/Orchard.Web/Modules/Upgrade/Upgrade.csproj +++ b/src/Orchard.Web/Modules/Upgrade/Upgrade.csproj @@ -69,6 +69,7 @@ + @@ -109,6 +110,8 @@ + + @@ -127,6 +130,9 @@ + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/src/Orchard.Web/Modules/Upgrade/Views/ContentPicker/Index.cshtml b/src/Orchard.Web/Modules/Upgrade/Views/ContentPicker/Index.cshtml new file mode 100644 index 000000000..d12647cfa --- /dev/null +++ b/src/Orchard.Web/Modules/Upgrade/Views/ContentPicker/Index.cshtml @@ -0,0 +1,14 @@ +@using Orchard.Utility.Extensions + +@{ Layout.Title = T("Migrate Content Picker").ToString(); } + +@using (Html.BeginFormAntiForgeryPost()) { + Html.ValidationSummary(); +
+ @T("Migrating Content Picker:") + @T("This migration step will move you content picker menu items to its new location.") +
+
+ +
+} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Upgrade/Views/Taxonomy/Index.cshtml b/src/Orchard.Web/Modules/Upgrade/Views/Taxonomy/Index.cshtml index 16c84a094..221a15d8f 100644 --- a/src/Orchard.Web/Modules/Upgrade/Views/Taxonomy/Index.cshtml +++ b/src/Orchard.Web/Modules/Upgrade/Views/Taxonomy/Index.cshtml @@ -1,12 +1,12 @@ @using Orchard.Utility.Extensions -@{ Layout.Title = T("Migrate Media Files").ToString(); } +@{ Layout.Title = T("Migrate Taxonomies").ToString(); } @using (Html.BeginFormAntiForgeryPost()) { Html.ValidationSummary();
- @T("Migrating Media Files:") - @T("This migration step will create and organize new Media Content Items based on the media files in your ~/Media folder.") + @T("Migrating Taxonomies:") + @T("This migration step will convert the Contrib.Taxonomies content items to Orchard.Taxonomies.")