2012-02-04 04:46:41 +08:00
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 ;
2012-02-04 04:51:09 +08:00
using UpgradeTo14.ViewModels ;
2012-02-04 04:46:41 +08:00
2012-02-04 04:51:09 +08:00
namespace UpgradeTo14.Controllers {
2012-02-04 04:46:41 +08:00
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 < ContentTypeEntry > ( ) } ;
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 < ContentTypeEntry > ( ) } ;
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 ) {
2012-02-08 10:01:43 +08:00
// migrating parts
2012-02-04 04:46:41 +08:00
_contentDefinitionManager . AlterTypeDefinition ( contentType ,
2012-02-08 10:01:43 +08:00
builder = > builder
. WithPart ( "AutoroutePart" )
. WithPart ( "TitlePart" ) ) ;
2012-02-04 04:46:41 +08:00
2012-02-10 04:42:35 +08:00
// force the first object to be reloaded in order to get a valid AutoroutePart
_orchardServices . ContentManager . Flush ( ) ;
_orchardServices . ContentManager . Clear ( ) ;
2012-02-04 04:46:41 +08:00
var count = 0 ;
var isContainable = false ;
IEnumerable < ContentItem > contents ;
do {
contents = _orchardServices . ContentManager . HqlQuery ( ) . ForType ( contentType ) . Slice ( count , 100 ) ;
foreach ( dynamic content in contents ) {
2012-02-10 04:42:35 +08:00
var autoroutePart = ( ( ContentItem ) content ) . As < AutoroutePart > ( ) ;
2012-02-04 04:46:41 +08:00
var titlePart = ( ( ContentItem ) content ) . As < TitlePart > ( ) ;
var commonPart = ( ( ContentItem ) content ) . As < CommonPart > ( ) ;
if ( commonPart ! = null & & commonPart . Container ! = null ) {
isContainable = true ;
}
2012-02-10 04:42:35 +08:00
using ( new TransactionScope ( TransactionScopeOption . RequiresNew ) ) {
2012-02-04 04:46:41 +08:00
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 ( ) ;
2012-02-10 04:42:35 +08:00
_orchardServices . ContentManager . Clear ( ) ;
2012-02-04 04:46:41 +08:00
} 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 ;
}
}
}