Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

sdsdfsdfsdfsdfsdfsdfsdfsdf sdfsdf sdf

sdfsd

fsd

f

using System;
using System.Collections.Generic;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.ServiceLocation;
using System.Linq;
using Digizuite.Episerver.Extensions;
using Digizuite.Episerver.Models;
using Digizuite.Episerver.Models.Media;
using EPiServer.Core;
using EPiServer.Find;
using EPiServer.Find.Cms;
using EPiServer.Find.Framework;

namespace Digizuite.Episerver.Jobs
{
    [ScheduledPlugIn(DisplayName = "Digizuite Update Find", SortIndex = 1003)]
    public class RemoveAssetsFromFindJob : ScheduledJobBase
    {
        private bool _stopSignaled;

        public Injected<IDigizuiteClient> DigizuiteClient { get; set; }

        public RemoveAssetsFromFindJob()
        {
            IsStoppable = true;
        }

        /// <summary>
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// </summary>
        public override void Stop()
        {
            _stopSignaled = true;
        }

        /// <summary>
        /// Called when a scheduled job executes
        /// </summary>
        /// <returns>A status message to be stored in the database log and visible from admin mode</returns>
        public override string Execute()
        {
            // Call OnStatusChanged to periodically notify progress of job for manually started jobs
            OnStatusChanged($"Cleaning Digizuite assets from index {this.GetType()}");

            var allAssetsInDigizuiteList = DigizuiteClient.Service.GetAllAssetIds();
            if (allAssetsInDigizuiteList == null)
            {
                return $"Could not retrieve the assets from Digizuite, no action to the index has occurred.";
            }

            var allAssetsFromIndex = this.GetAllDigizuiteAssetsFromIndex();

            // Delete digizuite assets from the index, that's no longer available for the Episerver channel in Digizuite.
            foreach (var basicContent in allAssetsFromIndex)
            {
                var dzAsset = allAssetsInDigizuiteList.Find(x => x.AssetId == basicContent.ContentLink.ID);
                if (dzAsset == null)
                {
                    OnStatusChanged($"Remove asset from the index: AssetId:{basicContent.ContentLink.ID}");
                    SearchClient.Instance.Delete<BasicContent>(x => x.ContentLink.ID.Match(basicContent.ContentLink.ID));
                }

                if (_stopSignaled)
                {
                    return "Job was stopped";
                }
            }

            // Add missing digizuite assets to the index
            List<int> missingAssets = new List<int>();
            foreach (var asset in allAssetsInDigizuiteList)
            {
                var dzAsset = allAssetsFromIndex.Find(x => x.ContentLink.ID == asset.AssetId);
                if (dzAsset == null)
                {
                    missingAssets.Add(asset.AssetId);
                }
            }

            if (missingAssets.Count > 0)
            {
                // The asset does not exists in the index, add to index.
                List<IContent> assets = DigizuiteClient.Service.Search(new AssetQueryParameters { AssetIds = missingAssets }).ToList();
                if (assets.Count > 0)
                {
                    // Add to index
                    SearchClient.Instance.Index(assets);
                }
            }

            return $"Digizuite successfully cleaned up index";
        }

        private List<BasicContent> GetAllDigizuiteAssetsFromIndex()
        {
            var pageSize = 1000;
            var query = SearchClient.Instance.Search<BasicContent>().Filter(x => x.MatchType(typeof(Image)) | x.MatchType(typeof(Video)) | x.MatchType(typeof(DigizuiteFile))).Take(pageSize);

            var batch = query.GetContentResult();
            List<BasicContent> totalResult = new List<BasicContent>();
            if (batch.TotalMatching == 0)
            {
                return totalResult;
            }

            totalResult.AddRange(batch.Items);
            var totalMatching = batch.TotalMatching;
            var nextBatchFrom = pageSize;

            while (nextBatchFrom < totalMatching)
            {
                batch = query.Skip(nextBatchFrom).GetContentResult();
                totalResult.AddRange(batch.Items);
                nextBatchFrom += pageSize;
            }

            return totalResult;
        }
    }
}

  • No labels