private void RunPendingStatements()
        {
            var session = _sessionLocator.For(typeof(ContentItemRecord));

            try {
                foreach (var sqlStatement in _sqlStatements)
                {
                    Logger.Debug(sqlStatement);

                    using (var command = session.Connection.CreateCommand()) {
                        command.CommandText = sqlStatement;
                        session.Transaction.Enlist(command);
                        command.ExecuteNonQuery();
                    }

                    _reportsCoordinator.Information("Data Migration", String.Format("Executing SQL Query: {0}", sqlStatement));
                }
            }
            finally {
                _sqlStatements.Clear();
            }
        }
Beispiel #2
0
        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", "Upgrade", "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.Clear();

                    var count         = 0;
                    var isContainable = false;
                    IEnumerable <ContentItem> contents;
                    bool errors = false;

                    do
                    {
                        contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).ForVersion(VersionOptions.Latest).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.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"));
        }