パイプライン ステップ プロセッサを実装する

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

プロセッサを使用して、コンポーネントのビジネスロジックを実装します。この例では、パイプライン ステップに割り当てられたエンドポイントに対応するテキスト ファイルからデータを読み取るパイプライン ステップのビジネス ロジックを実装するためにプロセッサが必要です。

パイプライン ステップは真空中には存在しません。これは、データ同期プロセスの一部となるように設計されています。つまり、パイプライン ステップのプロセッサは、実行時に特定の条件が存在する必要がある場合があります。この例では、プロセッサは、エンドポイントのプラグインコレクションにTextFileSettingsプラグインが含まれている必要があります。

プロセッサは、後続のパイプライン ステップで必要な条件を作成することもできます。この例では、プロセッサはプラグインIterableDataSettingsをパイプライン コンテキストのプラグイン コレクションに追加します。

パイプライン・ステップ・プロセッサーを実装するには、次のようにします。

  1. Visual Studioで、次のクラスを追加します。

    using Sitecore.DataExchange.Attributes;
    using Sitecore.DataExchange.Contexts;
    using Sitecore.DataExchange.Models;
    using Sitecore.DataExchange.Plugins;
    using Sitecore.DataExchange.Processors.PipelineSteps;
    using Sitecore.Services.Core.Diagnostics;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Examples.DataExchange.Providers.FileSystem
    {
        [RequiredEndpointPlugins(typeof(TextFileSettings))]
        public class ReadTextFileStepProcessor : BaseReadDataStepProcessor
        {
            public ReadTextFileStepProcessor()
            {
            }
            protected override void ReadData(
                Endpoint endpoint,
                PipelineStep pipelineStep,
                PipelineContext pipelineContext,
                ILogger logger)
            {
                if (endpoint == null)
                {
                    throw new ArgumentNullException(nameof(endpoint));
                }
                if (pipelineStep == null)
                {
                    throw new ArgumentNullException(nameof(pipelineStep));
                }
                if (pipelineContext == null)
                {
                    throw new ArgumentNullException(nameof(pipelineContext));
                }
                if (logger == null)
                {
                    throw new ArgumentNullException(nameof(logger));
                }
                //
                //get the file path from the plugin on the endpoint
                var settings = endpoint.GetTextFileSettings();
                if (settings == null)
                {
                    return;
                }
                if (string.IsNullOrWhiteSpace(settings.Path))
                {
                    logger.Error(
                        "No path is specified on the endpoint. " +
                        "(pipeline step: {0}, endpoint: {1})",
                        pipelineStep.Name, endpoint.Name);
                    return;
                }
                if (!File.Exists(settings.Path))
                {
                    logger.Error(
                        "The path specified on the endpoint does not exist. " +
                        "(pipeline step: {0}, endpoint: {1}, path: {2})",
                        pipelineStep.Name, endpoint.Name, settings.Path);
                    return;
                }
                //
                //add the data that was read from the file to a plugin
                var data = this.GetIterableData(settings);
                var dataSettings = new IterableDataSettings(data);
                //
                //add the plugin to the pipeline context
                pipelineContext.AddPlugin(dataSettings);
            }
            protected virtual IEnumerable<string[]> GetIterableData(TextFileSettings settings)
            {
                //
                //read the file, one line at a time
                var separator = new string[] { settings.ColumnSeparator };
                using (var reader = new StreamReader(File.OpenRead(settings.Path)))
                {
                    var firstLine = true;
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        if (firstLine && settings.ColumnHeadersInFirstLine)
                        {
                            firstLine = false;
                            continue;
                        }
                        if (string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }
                        //
                        //split the line into an array, using the separator
                        var values = line.Split(separator, StringSplitOptions.None);
                        yield return values;
                    }
                }
            }
        }
    }
    メモ

    BaseReadDataStepProcessorから継承することで、エンドポイントからのデータの読み取りを容易にするメソッドにアクセスできます。

    基本クラスは、読み取られたデータで何が起こるかを指定しません。代わりに、データはIterableDataSettingsプラグインに追加されます。後続のパイプライン ステップ プロセッサを実装して、データを反復処理し、データの処理方法を指定できます。

  2. プロジェクトをコンパイルします。

  3. Examples.DataExchange.Providers.FileSystem.dllをSitecoreサーバーにデプロイします。

この記事を改善するための提案がある場合は、 お知らせください!