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); }
public ActionResult IndexPOST() { if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate fields."))) 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); foreach (var contentType in contentTypesToMigrate) { _orchardServices.ContentManager.Flush(); _orchardServices.ContentManager.Clear(); var count = 0; IEnumerable<ContentItem> contents; do { contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100); foreach (ContentItem content in contents) { if((content.Record.Data ?? "").Length > (content.VersionRecord.Data ?? "").Length) { var draft = _orchardServices.ContentManager.Get(content.Id, VersionOptions.Draft); if(draft != null) { draft.VersionRecord.Data = content.Record.Data; } else { content.VersionRecord.Data = content.Record.Data; } } count++; } _orchardServices.ContentManager.Flush(); _orchardServices.ContentManager.Clear(); } while (contents.Any()); _orchardServices.Notifier.Information(T("{0} fields were migrated successfully", contentType)); } } return RedirectToAction("Index"); }
public ActionResult Index() { var viewModel = new MigrateViewModel { ContentTypes = new List<ContentTypeEntry>() }; foreach (var contentType in _contentDefinitionManager.ListTypeDefinitions().OrderBy(c => c.Name)) { // only display parts with fields if (contentType.Parts.Any(x => x.PartDefinition.Fields.Any())) { viewModel.ContentTypes.Add(new ContentTypeEntry {ContentTypeName = contentType.Name}); } } if(!viewModel.ContentTypes.Any()) { _orchardServices.Notifier.Warning(T("There are no content types with custom fields")); } if(!_featureManager.GetEnabledFeatures().Any(x => x.Id == "Orchard.Fields")) { _orchardServices.Notifier.Warning(T("You need to enable Orchard.Fields in order to migrate current fields. Then you can safely remove Contrib.DateTimeField and Contrib.MediaPickerField.")); } return View(viewModel); }
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)) { // 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 sessionFactory = _sessionFactoryHolder.GetSessionFactory(); var session = sessionFactory.OpenSession(); foreach (var contentType in contentTypesToMigrate) { _reportsCoordinator.Information("Migration", "Adding parts to " + contentType); // migrating parts _contentDefinitionManager.AlterTypeDefinition(contentType, builder => builder .WithPart("AutoroutePart") .WithPart("TitlePart")); // force the first object to be reloaded in order to get a valid AutoroutePart _orchardServices.ContentManager.Flush(); _orchardServices.ContentManager.Clear(); var count = 0; var isContainable = false; IEnumerable<ContentItem> contents; bool errors = false; do { contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100).ToList(); foreach (dynamic content in contents) { var autoroutePart = ((ContentItem)content).As<AutoroutePart>(); var titlePart = ((ContentItem) content).As<TitlePart>(); var commonPart = ((ContentItem) content).As<CommonPart>(); if(commonPart != null && commonPart.Container != null) { isContainable = true; } using (new TransactionScope(TransactionScopeOption.RequiresNew)) { var command = session.Connection.CreateCommand(); 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(); reader.Read(); try { 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; } } count++; } _orchardServices.ContentManager.Flush(); _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); } 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)); } } } return RedirectToAction("Index"); }