示例#1
0
        public MigrateItemPipelineArgs Run(ItemModel source)
        {
            var args = new MigrateItemPipelineArgs {
                Source = source
            };

            try
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();

                CorePipeline.Run("migrateItem", args, "OneNorth.Migrator");

                stopWatch.Stop();

                if (args.Item == null)
                {
                    Sitecore.Diagnostics.Log.Warn(string.Format("[FieldMigrator] (MigrateItemPipeline) Did not migrate: {0} {1} in {2}", source.Id, source.FullPath(x => x.Parent, x => x.Name), stopWatch.Elapsed), this);
                }
                else
                {
                    Sitecore.Diagnostics.Log.Info(string.Format("[FieldMigrator] (MigrateItemPipeline) Migrated: {0} {1} in {2}", args.Item.ID, args.Item.Paths.FullPath, stopWatch.Elapsed), this);
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error(string.Format("[FieldMigrator] (MigrateItemPipeline) {0} {1}", source.Id, source.FullPath(x => x.Parent, x => x.Name)), ex, this);
            }
            return(args);
        }
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null ||
                args.Item != null)
            {
                return;
            }

            var source = args.Source;

            var id     = (Overrides.ContainsKey(source.Id)) ? Overrides[source.Id] : source.Id;
            var itemId = new ID(id);

            args.Item = Sitecore.Context.Database.GetItem(itemId);

            if (args.Item != null)
            {
                if (KeepExistingMedia && args.Item.Paths.IsMediaItem && args.Item.TemplateID != TemplateIDs.MediaFolder)
                {
                    args.AbortPipeline();
                }

                return;
            }

            var results = _createItemPipeline.Run(source, itemId);

            if (results.Aborted)
            {
                args.AbortPipeline();
                return;
            }

            args.Item = results.Item;
        }
示例#3
0
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null ||
                args.Item == null)
            {
                return;
            }

            // Sort decending by version and Group by language
            var languages = args.Source.Versions
                            .OrderBy(x => x.Language)
                            .ThenByDescending(x => x.Version)
                            .GroupBy(x => x.Language);

            var versionModels = new List <VersionModel>();

            foreach (var language in languages)
            {
                VersionModel latestVersion  = null;
                VersionModel latestApproved = null;

                // Find latest version that is publishable and is approved
                foreach (var version in language)
                {
                    if (latestVersion == null)
                    {
                        latestVersion = version;
                    }

                    if (version.HasWorkflow && version.WorkflowState == WorkflowState.NonFinal)
                    {
                        continue;
                    }

                    latestApproved = version;

                    break;
                }

                // Take the latest approved version.  If there is no approved version take the latest version.
                if (latestApproved != null)
                {
                    versionModels.Add(latestApproved);
                }
                else if (latestVersion != null)
                {
                    versionModels.Add(latestVersion);
                    Sitecore.Diagnostics.Log.Info(string.Format("[FieldMigrator] Approved version not found: {0}, {1}, {2}", args.Source.Id, latestVersion.Language, args.Source.FullPath(x => x.Parent, x => x.Name)), this);
                }
            }

            // Update each version
            foreach (var versionModel in versionModels)
            {
                _migrateVersionPipeline.Run(versionModel, args.Item);
            }
        }
        private Item EnsureMediaFolders(MigrateItemPipelineArgs args)
        {
            var source = args.Source;

            // Determine if the parent already exists.  If it does, there is nothing to do.
            var parent = Sitecore.Context.Database.GetItem(new ID(source.ParentId));

            if (parent != null)
            {
                return(parent);
            }

            // Work backwards ensuring folders exist, skipping the current item
            var firstVersion = source.Versions.First();
            var path         = _hardRockWebServiceProxy.GetFullPath(source.Id, firstVersion.Language, firstVersion.Version);

            // Find first ancestor that exists
            var count = path.Count;
            var index = 2; //Skip the first 2 (self and parent)

            while (index < count)
            {
                parent = Sitecore.Context.Database.GetItem(new ID(path[index].Id));
                if (parent != null)
                {
                    break;
                }

                index++;
            }

            // Move to the first missing parent
            index--;

            // Create the parents, furthest to closest
            while (index > 0)
            {
                var folder = path[index];

                // Create the folder.  This becomes the parent for the next item
                parent = CreateItem(folder.Name, parent, TemplateIDs.MediaFolder, new ID(folder.Id));

                Sitecore.Diagnostics.Log.Debug(string.Format("[FieldMigrator] (CreateMediaItem): {0}", parent.Paths.FullPath), this);

                index--;
            }

            return(parent);
        }
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null)
            {
                return;
            }

            if (_processed.Contains(args.Source.Id))
            {
                args.AbortPipeline();
                return;
            }

            _processed.Add(args.Source.Id);
        }
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null ||
                args.Item == null)
            {
                return;
            }

            var source = args.Source;
            var item   = args.Item;

            // Confirm we are processing a Media Item
            if (!item.Paths.IsMediaItem || item.TemplateID == TemplateIDs.MediaFolder)
            {
                return;
            }

            // Confirm the target is an unversioned media item
            if (!item.Template.FullName.Contains("Unversioned"))
            {
                return;
            }

            var media = MediaManager.GetMedia(item);

            var blobField = source.Versions.SelectMany(x => x.Fields).First(x => string.Equals(x.Name, "Blob", StringComparison.OrdinalIgnoreCase));

            if (string.IsNullOrEmpty(blobField.Value))
            {
                media.ReleaseStream();
            }
            else
            {
                // Grab extension
                var extensionField = source.Versions.SelectMany(x => x.Fields).FirstOrDefault(x => string.Equals(x.Name, "Extension", StringComparison.OrdinalIgnoreCase));
                if (extensionField == null)
                {
                    return;
                }

                using (var stream = new MemoryStream(_hardRockWebServiceProxy.MediaDownloadAttachment(source.Id)))
                {
                    media.SetStream(stream, extensionField.Value);
                }
            }
        }
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null)
            {
                return;
            }

            var source = args.Source;

            if (_configuration.TemplateExcludes.Any(x => x.SourceTemplateId == source.TemplateId))
            {
                args.AbortPipeline();
                return;
            }

            // If configured to include media, allow all media through
            if (IncludeMedia)
            {
                if (source.IsMediaItem)
                {
                    return;
                }
            }

            // If template is registered, we can process.
            var templateInclude = _configuration.TemplateIncludes.FirstOrDefault(x => x.SourceTemplateId == source.TemplateId);

            if (templateInclude != null)
            {
                return;
            }

            // Check to see if a parent template is set to include all children.
            var parentTemplateIncludes = source.Parents(x => x.Parent)
                                         .Select(parent => _configuration.TemplateIncludes.FirstOrDefault(x => x.SourceTemplateId == parent.TemplateId))
                                         .Where(x => x != null);

            if (parentTemplateIncludes.Any(x => x.IncludeAllDescendants))
            {
                return;
            }

            // Skip item
            args.AbortPipeline();
        }
        public virtual void Process(MigrateItemPipelineArgs args)
        {
            if (args.Source == null)
            {
                return;
            }

            var source = args.Source;

            // Confirm we are processing a Media Item
            if (!source.IsMediaItem)
            {
                return;
            }

            // Check if we should process due to KeepExisting
            var item = Sitecore.Context.Database.GetItem(new ID(source.Id));

            if (KeepExisting && item != null)
            {
                args.AbortPipeline();
                return;
            }

            // Grab extension
            var extensionField = source.Versions.SelectMany(x => x.Fields).FirstOrDefault(x => string.Equals(x.Name, "Extension", StringComparison.OrdinalIgnoreCase));

            if (extensionField == null)
            {
                return;
            }

            var extension = extensionField.Value;
            var versioned = Sitecore.Configuration.Settings.Media.UploadAsVersionableByDefault;

            // Create dummy item that is a place holder
            if (item == null)
            {
                var parent     = EnsureMediaFolders(args);
                var templateId = MediaManager.Config.GetTemplate(extension, versioned);
                var template   = parent.Database.Templates[templateId];
                item = CreateItem(source.Name, parent, template.ID, new ID(source.Id));
            }

            // Get Alternate Text
            string alternateText = null;
            var    altField      = source.Versions.SelectMany(x => x.Fields).FirstOrDefault(x => string.Equals(x.Name, "Alt", StringComparison.OrdinalIgnoreCase));

            if (altField != null)
            {
                alternateText = altField.Value;
            }

            var fileName = item.Name + "." + extensionField.Value;

            var mediaCreatorOptions = new MediaCreatorOptions
            {
                AlternateText = alternateText,
                Database      = item.Database,
                Destination   = item.Paths.FullPath,
                FileBased     = Sitecore.Configuration.Settings.Media.UploadAsFiles,
                IncludeExtensionInItemName = false,
                KeepExisting = KeepExisting,
                Language     = LanguageManager.DefaultLanguage,
                Versioned    = versioned
            };

            using (var stream = new MemoryStream(_hardRockWebServiceProxy.MediaDownloadAttachment(source.Id)))
            {
                args.Item = MediaManager.Creator.CreateFromStream(stream, fileName, mediaCreatorOptions);
            }
        }