6.0

パイプラインステッププロセッサの実装

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

このページの翻訳は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 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サーバーにデプロイしてください。

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