コード スニペット: カスタム コネクタの実装

最終更新日: 2010年5月17日

適用対象: SharePoint Server 2010

この記事の内容
説明
この例を使用するには
コード

説明

以下のコード例は、Business Data Connectivity (BDC) service によって提供された ISystemUtility インターフェイスを実装して、カスタム コネクタを実装する方法を示しています。この例では、ファイル システムへのコネクタを提供します。必須インターフェイスは、ISystemUtility インターフェイスのみです。この例では、IConnectionManager インターフェイスを実装して、既定の接続マネージャーを上書きします。さらに IAdministrableSystem インターフェイスを実装して、管理 UI のプロパティ管理もサポートします。

この例を使用するには

  1. 新しい Microsoft Visual Studio クラス プロジェクトを開始し、「FileSystemConnector」という名前を付けます。

  2. .cs ファイルで自動生成されたコードを、以下の C# コードと置換します。

  3. アセンブリをコンパイルし、そのアセンブリを GAC に追加します。

  4. アセンブリの公開キー トークンを取得します。

  5. サンプル BDC モデル: カスタム ファイル システム コネクタに接続する の BDC モデル サンプルを XML ファイルにコピーします。それに、「FileSystemConnectorSample.xml」という名前を付けます。

  6. XML ファイルの 9 行目を新しいアセンブリ情報で更新します。

  7. XML ファイルの 22 行目を、ファイルが存在するディレクトリに一致するように更新します。

  8. XML ファイルを BDC にインポートします。

  9. この時点で、外部リストを作成して、ファイル システムのデータを参照できます。

コード

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.Runtime;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using System.Collections;



namespace MS.Internal.Motif.Office.Lobi.ConnectorTest
{
    class FileSystemConnector : ISystemUtility, IDisposable, IConnectionManager, IAdministrableSystem
      {
        #region private variables
        String DirectoryPath;
        String DirectoryPropertyName = "Directory";
        String FileIDName = "FileName";

        #endregion
        #region IAdministrableSystem Members

        public IList<AdministrableProperty> AdministrableLobSystemProperties
        {
            get
            {
                return new List<AdministrableProperty>()
                {
                    new AdministrableProperty("SystemUtilityTypeName","Name of the custom connector  utility",typeof(string),"SystemUtilityTypeName", typeof(string),true), 
                    new AdministrableProperty("SystemUtilityInstallDate","Date custom connector was installed",typeof(DateTime),"SystemUtilityInstallDate", typeof(DateTime),false)
                };
            }
        }

        public IList<AdministrableProperty> AdministrableLobSystemInstanceProperties
        {
            get
            {
                return new List<AdministrableProperty>()
                {
                   new AdministrableProperty("ServerName","Name of server where filesystem exists",typeof(string),"ServerName", typeof(string),false)  
                };
            }
        }
        #endregion

        #region ISystemUtility Members

        public void ExecuteStatic(IMethodInstance methodInstance, ILobSystemInstance lobSystemInstance, object[] methodSignatureArgs, IExecutionContext context)
        {
            if (methodInstance == null)
            {
                throw (new ArgumentNullException("methodInstance"));
            }
            if (lobSystemInstance == null)
            {
                throw (new ArgumentNullException("lobSystemInstance"));
            }
            if (methodSignatureArgs == null)
            {
                throw (new ArgumentNullException("args"));
            }

            Dictionary<String, object> propertyValues = new Dictionary<string, object>();

            if (context != null)
            {
                if (context.IsPropertyDefined("FullPath"))
                    propertyValues.Add("FullPath", context["FullPath"]);

                if (context.IsPropertyDefined("Count"))
                {
                    context["Count"] = (Int32)context["Count"] + 1;
                    propertyValues.Add("Count", context["Count"]);
                }

                if(context.IsPropertyDefined("CountInMethod"))
                    propertyValues.Add("CountInMethod", context["CountInMethod"]);
            }
                      
            DirectoryPath = GetFullPath(methodInstance);
            ExecuteInternal(DirectoryPath, methodInstance, methodSignatureArgs,propertyValues);

        }

        public virtual IEnumerator
            CreateEntityInstanceDataEnumerator(Object rawAdapterEntityInstanceStream, ISharedEntityState sharedEntityState)
        {
           
                return ((IEnumerable)rawAdapterEntityInstanceStream).GetEnumerator();
           
            
        }
        #endregion
        #region ITypeReflector
        public ITypeReflector DefaultTypeReflector
        {
            get
            {
                return null;
            }
        }

#endregion

        #region DefaultConnectionManager
        public IConnectionManager DefaultConnectionManager
        {
            get
            {
                return null;
            }
        }
        #endregion

        #region IConnectionManager Members

        public void CloseConnection(object connection)
        {

        }

        public void FlushConnections()
        {

        }

        public object GetConnection()
        {
            return null;
        }

        public void Initialize(ILobSystemInstance properties)
        {

        }

        #endregion
        #region IDisposable Members

        public void Dispose()
        {
       
            
        }
        #endregion

        #region private methods
        private String GetFullPath(IMethodInstance methodInstance)
        {
            IDataClass dataClass = methodInstance.GetMethod().GetDataClass();
            return (string)dataClass.GetProperties()[DirectoryPropertyName];
        }

        private void ExecuteInternal(
            string directory, /* SP List Name */
           IMethodInstance methodInstance,
           object[] args,
           Dictionary<String,object> propertyBag)
        {
            String methodName = methodInstance.GetMethod().Name;
            String path;
            DynamicType dynamicParameter = new DynamicType();

            switch (methodName)
            {
                case "ReadFile":
                    String[] fields = GetFieldValues(methodInstance);
                    path = directory + @"\" + args[0];
                    if (!File.Exists(path))
                        throw new Exception(" The file does not exist for the FindSpecific");
                    else
                    {
                        foreach (String field in fields)
                        {
                            switch (field)
                            {
                                case "FileName":
                                    if (propertyBag.ContainsKey("FullPath"))
                                    {
                                        if ((Boolean)propertyBag["FullPath"])
                                            dynamicParameter.Add("FileName", path);
                                        else
                                            dynamicParameter.Add("FileName", args[0]);
                                    }
                                    else
                                        dynamicParameter.Add("FileName", args[0]);
                                    break;

                                case "Date Modified":
                                    dynamicParameter.Add("Date Modified", File.GetLastWriteTime(path));
                                    break;

                                case "Attributes":
                                    FileAttributes attributes = File.GetAttributes(path);                    
                                    
                                    dynamicParameter.Add("Attributes", attributes);
                                    break;

                                case "Date Created":
                                    dynamicParameter.Add("Date Created", File.GetCreationTime(path));
                                    break;

                                case "Date Accessed":
                                    dynamicParameter.Add("Date Accessed",File.GetLastAccessTime(path));
                                    break;

                                case "Type":
                                    FileStream stream = File.Open(path, FileMode.Open);
                                    dynamicParameter.Add("Type", stream.GetType().ToString());
                                    stream.Close();
                                    stream.Dispose();
                                    break;

                                default:
                                    throw new Exception(" Field does not belong to File");

                            }

                        }
                    }  
                    args[1] = dynamicParameter;
                    break;

               case "CreateFile":
                    Dictionary<string, object> createValues = new Dictionary<string, object>();
                    int count = 0;

                    //
                    // Get the method parameters.
                    //
                    IParameterCollection parameterCollection = methodInstance.GetMethod().GetParameters();
                    foreach (IParameter parameter in parameterCollection)
                    {
                        if (parameter.Direction == DirectionType.In
                            || parameter.Direction == DirectionType.InOut)
                        {
                            createValues.Add(parameter.Name, args[count]);
                        }
                        count++;
                    }

                    path = directory + @"\" + args[0];
                    FileStream stream1 = File.Create(path);
                    stream1.Close();
                    stream1.Dispose();
                    args[args.Length - 1] = args[0];

                    break;
                    
                case "DeleteFile":
                    path = directory + @"\" + args[0];
                    if (!File.Exists(path))
                        throw new Exception("File does not exist to Delete");
                    File.Delete(path);
                    break;

                case "ReadFiles":
                    String[] fieldValues = GetFieldValues(methodInstance);
                    List<DynamicType> returnParameters = new List<DynamicType>();
                   // path = directory + @"\" + args[0];
                    if (!Directory.Exists(directory))
                        throw new Exception(" The directory does not exist for the Finder");
                    else
                    {
                        string[] Files = Directory.GetFiles(directory);
                        foreach(string FileName in Files)
                        {
                            DynamicType paraValue = new DynamicType();                                                      
                            foreach (String fieldValue in fieldValues)
                            {
                                
                                switch (fieldValue)
                                {
                                       
                                    case "FileName":
                                        FileInfo info = new FileInfo(FileName);
                                      
                                        paraValue.Add("FileName", info.Name);
                                        break;

                                    case "Date Modified":
                                        paraValue.Add("Date Modified", File.GetLastWriteTime(FileName));
                                        break;

                                    case "Attributes":
                                        FileAttributes attributes = File.GetAttributes(FileName);

                                        paraValue.Add("Attributes", attributes);
                                        break;

                                    case "Date Created":
                                        paraValue.Add("Date Created", File.GetCreationTime(FileName));
                                        break;

                                    case "Date Accessed":
                                        paraValue.Add("Date Accessed", File.GetLastAccessTime(FileName));
                                        break;

                                    case "Type":
                                        FileStream stream = File.Open(FileName, FileMode.Open);
                                        paraValue.Add("Type", stream.GetType().ToString());
                                        stream.Close();
                                        stream.Dispose();
                                        break;

                                    default:
                                        throw new Exception(" Field does not belong to File");

                                }
                            }
                            returnParameters.Add(paraValue);

                        }
                    }
                    args[1] = returnParameters.ToArray();
                    break;

                case "UpdateFile":
                   
                    Microsoft.BusinessData.Runtime.DynamicType updatedValues = args[0] as Microsoft.BusinessData.Runtime.DynamicType;
                    path = directory + @"\" + updatedValues[FileIDName];
                    if (!File.Exists(path))
                        throw new Exception("File does not exist to update");
                    foreach (KeyValuePair<string, object> kvp in updatedValues)
                    {
                        switch(kvp.Key)
                        {
                            case "FileName":                                 
                                break;

                            case "Date Modified":
                                File.SetLastWriteTime(path,(DateTime) kvp.Value);
                                break;

                            case "Attributes":
                                FileAttributes updatedAttribs = (FileAttributes)kvp.Value;
                                File.SetAttributes(path, updatedAttribs);                                
                                break;

                            case "Date Created":
                                File.SetCreationTime(path,(DateTime)kvp.Value);
                                break;

                            case "Date Accessed":
                                File.SetLastWriteTime(path,(DateTime)kvp.Value);
                                break;

                           default:
                                throw new Exception(" Field does not belong to File");



                        }
                    }
                    break; 

                default:
                    throw new Exception("Method Not Implemented called");
                }
            } 
     
       private String[] GetFieldValues(IMethodInstance methodInstance)
        {
           List<String> fieldsList = new List<string>();
            IMethod method = methodInstance.GetMethod();
            ITypeDescriptor typeDescriptor;
                
               typeDescriptor = method.GetParameters()[1].GetRootTypeDescriptor();
               ITypeDescriptor childTypeDescriptor = typeDescriptor.GetChildTypeDescriptors()[0];

               if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
               {
                   foreach (ITypeDescriptor childDescpritor in childTypeDescriptor.GetChildTypeDescriptors())
                   {

                       fieldsList.Add(childDescpritor.Name);

                   }
               }
               else
               {

                   foreach (ITypeDescriptor childDescpritor in typeDescriptor.GetChildTypeDescriptors())
                   {
                       fieldsList.Add(childDescpritor.Name);
                   }
               }
            return fieldsList.ToArray();
        }

        private String[] GetUpdateFieldValues(IMethodInstance methodInstance)
        {
            List<String> fieldsList = new List<string>();
            IMethod method = methodInstance.GetMethod();
            ITypeDescriptor typeDescriptor = method.GetParameters()[0].GetRootTypeDescriptor();

            foreach (ITypeDescriptor childDescpritor in typeDescriptor.GetChildTypeDescriptors())
            {
                fieldsList.Add(childDescpritor.Name);
            }
            return fieldsList.ToArray();
        }

       
        }

        #endregion

    }

関連項目

参照

AdministrableProperty

ISystemUtility

ITypeReflector

IConnectionManager

IAdministrableSystem

IMethodInstance

GetMethod()

IDataClass

ILobSystemInstance

IExecutionContext

IsPropertyDefined(String)

ISharedEntityState

DynamicType

Add(String, Object)

IParameterCollection

GetParameters()

IParameterCollection

IParameter

Direction

GetRootTypeDescriptor()

ITypeDescriptor

GetChildTypeDescriptors()

MethodInstanceType

MethodInstanceType

概念

サンプル BDC モデル: カスタム ファイル システム コネクタに接続する