---
title: "パイプラインステッププロセッサの実装"
slug: "implement-a-pipeline-step-processor"
abstract: "Sitecore Data Exchange Frameworkでビジネスロジックを実装するためのパイプラインステッププロセッサの作成方法。"
---
# パイプラインステッププロセッサの実装

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

パイプラインステップは真空状態で存在するわけではありません。それはデータ同期プロセスの一部として設計されています。つまり、パイプラインステップを実行する際にプロセッサが特定の条件を要求する場合があります。この例では、プロセッサはエンドポイントのプラグインコレクションに*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](https://doc.sitecore.com/xp/ja/developers/def/60/data-exchange-framework/pipeline-step-processors.html "Pipeline step processors")から継承することで、エンドポイントからデータを読み取るのを容易にするメソッドにアクセスできます。
    > 
    > ベースクラスは読み取ったデータに何が起こるかを指定していません。代わりに、データは*IterableDataSettings*プラグインに追加されます。その後のパイプラインステッププロセッサを実装してデータを反復し、処理方法を指定することができます。
    
2.  プロジェクトをコンパイルします。
    
3.  Examples.DataExchange.Providers.FileSystem.dllをSitecoreサーバーにデプロイしてください。