Fixing routes migration

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-02-23 16:36:47 -08:00
parent f6152fbfdc
commit 174e465d38

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Transactions; using System.Transactions;
using System.Web.Mvc; using System.Web.Mvc;
@@ -12,6 +13,7 @@ using Orchard.Core.Title.Models;
using Orchard.Data; using Orchard.Data;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
using Orchard.Localization; using Orchard.Localization;
using Orchard.Reports.Services;
using Orchard.Security; using Orchard.Security;
using Orchard.UI.Admin; using Orchard.UI.Admin;
using Orchard.UI.Notify; using Orchard.UI.Notify;
@@ -25,18 +27,21 @@ namespace UpgradeTo14.Controllers {
private readonly ISessionFactoryHolder _sessionFactoryHolder; private readonly ISessionFactoryHolder _sessionFactoryHolder;
private readonly ShellSettings _shellSettings; private readonly ShellSettings _shellSettings;
private readonly IAutorouteService _autorouteService; private readonly IAutorouteService _autorouteService;
private readonly IReportsCoordinator _reportsCoordinator;
public RouteController( public RouteController(
IContentDefinitionManager contentDefinitionManager, IContentDefinitionManager contentDefinitionManager,
IOrchardServices orchardServices, IOrchardServices orchardServices,
ISessionFactoryHolder sessionFactoryHolder, ISessionFactoryHolder sessionFactoryHolder,
ShellSettings shellSettings, ShellSettings shellSettings,
IAutorouteService autorouteService) { IAutorouteService autorouteService,
IReportsCoordinator reportsCoordinator) {
_contentDefinitionManager = contentDefinitionManager; _contentDefinitionManager = contentDefinitionManager;
_orchardServices = orchardServices; _orchardServices = orchardServices;
_sessionFactoryHolder = sessionFactoryHolder; _sessionFactoryHolder = sessionFactoryHolder;
_shellSettings = shellSettings; _shellSettings = shellSettings;
_autorouteService = autorouteService; _autorouteService = autorouteService;
_reportsCoordinator = reportsCoordinator;
} }
public Localizer T { get; set; } public Localizer T { get; set; }
@@ -65,6 +70,10 @@ namespace UpgradeTo14.Controllers {
var viewModel = new MigrateViewModel { ContentTypes = new List<ContentTypeEntry>() }; var viewModel = new MigrateViewModel { ContentTypes = new List<ContentTypeEntry>() };
if(TryUpdateModel(viewModel)) { if(TryUpdateModel(viewModel)) {
// creating report
_reportsCoordinator.Register("Migration", "UpgradeTo14", "Migrating " + string.Join(" ,", viewModel.ContentTypes.Where(x => x.IsChecked).Select(x => x.ContentTypeName).ToArray()));
var contentTypesToMigrate = viewModel.ContentTypes.Where(c => c.IsChecked).Select(c => c.ContentTypeName); var contentTypesToMigrate = viewModel.ContentTypes.Where(c => c.IsChecked).Select(c => c.ContentTypeName);
var sessionFactory = _sessionFactoryHolder.GetSessionFactory(); var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
@@ -72,6 +81,8 @@ namespace UpgradeTo14.Controllers {
foreach (var contentType in contentTypesToMigrate) { foreach (var contentType in contentTypesToMigrate) {
_reportsCoordinator.Information("Migration", "Adding parts to " + contentType);
// migrating parts // migrating parts
_contentDefinitionManager.AlterTypeDefinition(contentType, _contentDefinitionManager.AlterTypeDefinition(contentType,
builder => builder builder => builder
@@ -85,9 +96,10 @@ namespace UpgradeTo14.Controllers {
var count = 0; var count = 0;
var isContainable = false; var isContainable = false;
IEnumerable<ContentItem> contents; IEnumerable<ContentItem> contents;
bool errors = false;
do { do {
contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100); contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100).ToList();
foreach (dynamic content in contents) { foreach (dynamic content in contents) {
var autoroutePart = ((ContentItem)content).As<AutoroutePart>(); var autoroutePart = ((ContentItem)content).As<AutoroutePart>();
@@ -100,18 +112,34 @@ namespace UpgradeTo14.Controllers {
using (new TransactionScope(TransactionScopeOption.RequiresNew)) { using (new TransactionScope(TransactionScopeOption.RequiresNew)) {
var command = session.Connection.CreateCommand(); var command = session.Connection.CreateCommand();
command.CommandText = string.Format("SELECT Title, Path FROM {0} WHERE ContentItemRecord_Id = {1}", GetPrefixedTableName("Routable_RoutePartRecord"), autoroutePart.ContentItem.Id); command.CommandText = string.Format(@"
SELECT Title, Path FROM {0}
INNER JOIN {1} ON {0}.Id = {1}.Id
WHERE Latest = 1 AND {0}.ContentItemRecord_Id = {2}", GetPrefixedTableName("Routable_RoutePartRecord"), GetPrefixedTableName("Orchard_Framework_ContentItemVersionRecord"), autoroutePart.ContentItem.Id);
var reader = command.ExecuteReader(); var reader = command.ExecuteReader();
reader.Read(); reader.Read();
var title = reader.GetString(0);
var path = reader.GetString(1);
reader.Close();
autoroutePart.DisplayAlias = path; try {
titlePart.Title = title; var title = reader.GetString(0);
var path = reader.GetString(1);
reader.Close();
autoroutePart.DisplayAlias = path ?? String.Empty;
titlePart.Title = title;
_autorouteService.PublishAlias(autoroutePart);
}
catch(Exception e) {
if (!reader.IsClosed) {
reader.Close();
}
_reportsCoordinator.Error("Migration", "Migrating content item " + autoroutePart.ContentItem.Id + " failed with: " + e.Message);
errors = true;
}
} }
_autorouteService.PublishAlias(autoroutePart);
count++; count++;
} }
@@ -130,7 +158,12 @@ namespace UpgradeTo14.Controllers {
_autorouteService.CreatePattern(contentType, "Title", "{Content.Slug}", "my-sample-title", true); _autorouteService.CreatePattern(contentType, "Title", "{Content.Slug}", "my-sample-title", true);
} }
_orchardServices.Notifier.Information(T("{0} was migrated successfully", contentType)); if (errors) {
_orchardServices.Notifier.Warning(T("Some content items could not be imported. Please refer to the corresponding Report."));
}
else {
_orchardServices.Notifier.Information(T("{0} was migrated successfully", contentType));
}
} }
} }