Adding upgrade steps for Content Picker

--HG--
branch : 1.x
extra : rebase_source : c58efb226ce34208a2b73341d5ef3a0cb5afcafa
This commit is contained in:
Sebastien Ros
2013-06-21 16:05:35 -07:00
parent 5e6fe8e253
commit 66b297c4db
12 changed files with 240 additions and 64 deletions

View File

@@ -15,7 +15,13 @@ namespace Orchard.ContentPicker.Handlers {
Filters.Add(new ActivatingFilter<ContentMenuItemPart>("ContentMenuItem"));
Filters.Add(StorageFilter.For(repository));
OnLoading<ContentMenuItemPart>((context, part) => part._content.Loader(p => contentManager.Get(part.Record.ContentMenuItemRecord.Id)));
OnLoading<ContentMenuItemPart>((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) {

View File

@@ -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;
}
}

View File

@@ -85,7 +85,9 @@
<Content Include="Styles\orchard-websearch-admin.css" />
<Content Include="Styles\orchard-mediaproviders-admin.css" />
<Content Include="Styles\orchard-medialibrary-admin.css" />
<Content Include="Web.config" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Views\Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />

View File

@@ -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))
);
}
}

View File

@@ -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");
}
}
}

View File

@@ -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");
}
}

View File

@@ -9,4 +9,4 @@ Category: Developer
Features:
Upgrade:
Description: Provides actions for upgrading Orchard instances
Dependencies: Orchard.Autoroute, Title, Navigation
Dependencies: Orchard.Autoroute, Title, Navigation, Orchard.MediaLibrary

View File

@@ -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<IDataReader, IDbConnection> action);
string GetPrefixedTableName(string tableName);
}
}

View File

@@ -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<IDictionary<string, object>>();
ExecuteReader(selectStatement, (reader, conn) => {
var parameters = new SortedDictionary<string, object>();
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<IDataReader, IDbConnection> 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;
}
}
}

View File

@@ -69,6 +69,7 @@
<Content Include="Views\Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Compile Include="Controllers\ContentPickerController.cs" />
<Compile Include="Controllers\TaxonomyController.cs" />
<Compile Include="Controllers\MediaController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -109,6 +110,8 @@
<Compile Include="Controllers\MenuController.cs" />
<Compile Include="Controllers\FieldController.cs" />
<Compile Include="Controllers\RouteController.cs" />
<Compile Include="Services\IUpgradeService.cs" />
<Compile Include="Services\UpgradeService.cs" />
<Compile Include="ViewModels\MigrateViewModel.cs" />
</ItemGroup>
<ItemGroup>
@@ -127,6 +130,9 @@
<ItemGroup>
<Content Include="Views\Taxonomy\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\ContentPicker\Index.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,14 @@
@using Orchard.Utility.Extensions
@{ Layout.Title = T("Migrate Content Picker").ToString(); }
@using (Html.BeginFormAntiForgeryPost()) {
Html.ValidationSummary();
<fieldset>
<legend>@T("Migrating Content Picker:")</legend>
<span class="hint">@T("This migration step will move you content picker menu items to its new location.")</span>
</fieldset>
<fieldset>
<button type="submit">@T("Migrate")</button>
</fieldset>
}

View File

@@ -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();
<fieldset>
<legend>@T("Migrating Media Files:")</legend>
<span class="hint">@T("This migration step will create and organize new Media Content Items based on the media files in your ~/Media folder.")</span>
<legend>@T("Migrating Taxonomies:")</legend>
<span class="hint">@T("This migration step will convert the Contrib.Taxonomies content items to Orchard.Taxonomies.")</span>
</fieldset>
<fieldset>
<button type="submit">@T("Migrate")</button>