チュートリアル: パイプライン ステップの実装

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

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

このチュートリアルは、テキスト ファイルのカスタム プロバイダーの実装 に関するチュートリアル シリーズのパート3であり、パイプライン ステップを実装する方法について説明します。パイプライン ステップは、データ同期プロセス中に実行されるロジックを表します。この例では、パイプライン ステップは、テキスト ファイル内の行を読み取る機能を表しています。

このチュートリアルでは、次の方法について説明します。

  • パイプライン ステップのテンプレートを作成する

  • パイプライン ステップ テンプレートにフィールドを追加する

  • パイプライン ステップ コンバーターを実装する

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

  • パイプライン ステップ テンプレートに既定値を設定する

パイプライン ステップのテンプレートを作成する

パイプライン ステップを使用して、テキスト ファイルからデータを読み取ります。読み取るテキストファイル(エンドポイント)を指定するには、テンプレートが必要です。

テンプレートを作成するには:

  1. コンテンツ エディターで、Templatesに移動します。

  2. メニュー リボンのHomeタブのInsertグループで、New Templateをクリックして新しいテンプレートを追加します。

    New Template option on the menu ribbon
  3. Select nameダイアログで、次の値を入力します。

    価値

    Name

    Read Text File Pipeline Step

    Base template

    Templates/Data Exchange/Framework/Pipeline Steps/Base Pipeline Steps/Base Pipeline Step

  4. Next」をクリックします。

  5. Locationダイアログで、「 Templates/Data Exchange/Providers/File System/Pipeline Steps」を選択します。Nextをクリックします。

  6. 確認ダイアログで、「 Close」をクリックします。

  7. 新しいテンプレートのアイコンを設定するには、コンテンツ ツリーでテンプレート ( Templates/Data Exchange/Providers/File System/Pipeline Steps/Read Text File Pipeline Stepなど) を選択します。

  8. 右側のペインのContentタブで、タイトル の横にあるアイコンをクリックします。

    Setting the icon for a pipeline step
  9. Change Iconダイアログの「Icon」フィールドに、次のように入力します。Office/32x32/element.png

パイプライン ステップ テンプレートにフィールドを追加する

パイプライン ステップ テンプレートでは、パイプライン ステップに使用するエンドポイントをSitecoreに指示する設定を構成する必要があります。これを行うには、テンプレート フィールドを追加します。

テンプレートフィールドを追加するには:

  1. コンテンツ ツリーで、パイプライン ステップ テンプレートを選択します。テンプレート セクションを追加し、Endpointsという名前を付けます。

  2. Endpointsで、テンプレート フィールドを追加し、EndpointFromという名前を付けます。次のフィールドに入力します。

    価値

    Name

    EndpointFrom

    Type

    Droptree

    Source

    query:./ancestor-or-self::*@@templateid='{327A381B-59F8-4E88-B331-BEBC7BD87E4E}'//descendant-or-self::*@@templateid='TEMPLATE-ID'

    Shared

    入選

    大事な

    Sourceフィールドで、TEMPLATE-IDFile System Endpoints Rootという名前のテンプレートのアイテムIDに置き換える必要があります。このテンプレートは、プロバイダーのエンドポイント項目を保存するために使用されるフォルダを表します。

    このテンプレートは、Data Exchange Framework SDKを使用してプロバイダを作成したときに自動的に作成されました。テンプレートは /sitecore/templates/Data Exchange/Providers/File System/Folders/File System Endpoints Rootにあります。IDはContentタブで確認できます。

    Sourceフィールドのクエリは、droptreeフィールドの種類を、このプロバイダーのエンドポイントの整理に使用されるフォルダー内の現在のテナント内のエンドポイントに制限します。

    ID {327A381B-59F8-4E88-B331-BEBC7BD87E4E} は、フレームワークに付属するテナント テンプレート用です。この値は変更しないでください。

  3. Sharedチェック ボックスをオンにします。アイテムを保存します。

    メモ

    アイテムを保存すると、アイテムに壊れたリンクが含まれていることを示すメッセージが表示されます。これは、ステップ2で設定した式に関連する予期される動作です。「 OK 」をクリックして続行します。

パイプライン ステップ コンバーターを実装する

コンバーターを使用して、パイプライン ステップ項目の設定をプラグインに変換する必要があります。プラグインは、パイプライン ステップが読み取るエンドポイントを識別します。

パイプライン ステップ コンバーターを実装するには:

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

    using Sitecore.DataExchange.Attributes;
    using Sitecore.DataExchange.Converters.PipelineSteps;
    using Sitecore.DataExchange.Models;
    using Sitecore.DataExchange.Plugins;
    using Sitecore.DataExchange.Repositories;
    using Sitecore.Services.Core.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Examples.DataExchange.Providers.FileSystem
    {
        [SupportedIds(ReadTextFileStepTemplateId)]
        public class ReadTextFileStepConverter : BasePipelineStepConverter
        {
            public const string ReadTextFileStepTemplateId = "[PIPELINE STEP TEMPLATE ID]";
            public const string TemplateFieldEndpointFrom = "EndpointFrom";
            public ReadTextFileStepConverter(IItemModelRepository repository) : base(repository)
            {
            }
            protected override void AddPlugins(ItemModel source, PipelineStep pipelineStep)
            {
                //
                //create the plugin
                var settings = new EndpointSettings
                {
                    //
                    //populate the plugin using values from the item
                    EndpointFrom = this.ConvertReferenceToModel<Endpoint>(source, TemplateFieldEndpointFrom)
                };
                //
                //add the plugin to the pipeline step
                pipelineStep.AddPlugin(settings);
            }
        }
    }
    大事な

    PIPELINE STEP TEMPLATE IDの値は、Read Text File Pipeline Stepという名前のパイプラインステップテンプレートのIDと一致するように変更する必要があります。

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

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

BasePipelineStepConverterから継承することで、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から継承することで、エンドポイントからのデータの読み取りを容易にするメソッドにアクセスできます。

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

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

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

パイプライン ステップ テンプレートに既定値を設定する

パイプラインステップテンプレートを使用する場合は、テンプレートにデフォルト値を追加する必要があります。

テンプレートにデフォルト値を追加するには:

  1. コンテンツ エディターで、Templates/Data Exchange/Providers/File System/Endpoints/Endpointに移動します。

  2. 右側のペインで、Builderタブをクリックします。

  3. メニュー リボンのBuilder OPTIONSタブのTemplateグループで、Standard valuesをクリックします。

    Standard values in Templates group on menu ribbon
  4. 新しいアイテムを選択します。

    Standard values in content tree
  5. 右側のペインのContentタブで、次の既定のフィールド値を設定します。

    価値

    Converter Type

    Examples.DataExchange.Providers.FileSystem.ReadTextFileStepConverter, Examples.DataExchange.Providers.FileSystem

    Processor Type

    Examples.DataExchange.Providers.FileSystem.ReadTextFileStepProcessor, Examples.DataExchange.Providers.FileSystem

    手記

    Converter TypeフィールドはData Exchange Frameworkセクションにあります。フィールドを表示する前に、セクションを展開する必要がある場合があります。

  6. アイテムを保存します。

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