Share via


File-Based ECMA 2.2 Management Agent

Example: File-Based ECMA 2.2 Management Agent

The following code is an example of a File-Based ECMA 2.2 Management Agent

//********************************************************
//*                                                      *
//*   Copyright (C) Microsoft. All rights reserved.      *
//*                                                      *
//********************************************************

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.MetadirectoryServices;

namespace FimSync_Ezma
{
    public class EzmaExtension :
    //IMAExtensible2CallExport,
    //IMAExtensible2CallImport,
    IMAExtensible2FileImport,
    IMAExtensible2FileExport,
    //IMAExtensible2GetHierarchy,
    //IMAExtensible2GetSchema,
    IMAExtensible2GetCapabilities,
    IMAExtensible2GetParameters
    //IMAExtensible2GetPartitions
    {
        //
        // Constructor
        //
        public EzmaExtension()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public MACapabilities Capabilities
        {
            get
            {
                MACapabilities myCapabilities = new MACapabilities();

                myCapabilities.ConcurrentOperation = true;
                myCapabilities.ObjectRename = false;
                myCapabilities.DeleteAddAsReplace = true;
                myCapabilities.DeltaImport = true;
                myCapabilities.DistinguishedNameStyle = MADistinguishedNameStyle.None;
                myCapabilities.ExportType = MAExportType.AttributeUpdate;
                myCapabilities.NoReferenceValuesInFirstExport = false;
                myCapabilities.Normalizations = MANormalizations.None;

                return myCapabilities;
            }
        }

      

        public IList<ConfigParameterDefinition> GetConfigParameters(KeyedCollection<string, ConfigParameter> configParameters,
                                                             ConfigParameterPage page)
        {
            List<ConfigParameterDefinition> configParametersDefinitions = new List<ConfigParameterDefinition>();

            switch (page)
            {
                case ConfigParameterPage.Connectivity:


                    break;


                case ConfigParameterPage.Global:
                   
                   configParametersDefinitions.Add(ConfigParameterDefinition.CreateStringParameter("Full Import File", ""));
                   configParametersDefinitions.Add(ConfigParameterDefinition.CreateStringParameter("Delta Import File", ""));
                   configParametersDefinitions.Add(ConfigParameterDefinition.CreateStringParameter("Export File", ""));
                   configParametersDefinitions.Add(ConfigParameterDefinition.CreateDropDownParameter("Export Encoding", "ASCII,Unicode,UTF8", false, "UTF8"));
                    break;

                case ConfigParameterPage.Partition:
                    break;

                case ConfigParameterPage.RunStep:
                    configParametersDefinitions.Add(ConfigParameterDefinition.CreateCheckBoxParameter("Omit Xml Declaration", false));
                    break;
            }

            return configParametersDefinitions;
        }

        public ParameterValidationResult ValidateConfigParameters(KeyedCollection<string, ConfigParameter> configParameters,
                                                                   ConfigParameterPage page)
        {


            ParameterValidationResult myResults = new ParameterValidationResult();
            return myResults;

        }

        public WriteImportFileResults WriteImportFile(
                                             KeyedCollection<string, ConfigParameter> configParameters,
                                             Schema types,
                                             WriteImportFileRunStep importRunStep)
        {
            //
            // Write out the delimited file.
            //
            StreamWriter swImport = new StreamWriter(importRunStep.FilePath, true);

            swImport.WriteLine(
              "{0},{1},{2},{3},{4}",
              "objectclass",
              "delta",
              "anchor-attribute",
              "name",
              "email"
              );

            string inputxml = "";
            XmlDocument doc = new XmlDocument();

            if (importRunStep.ImportType == OperationType.Full)
            {
              inputxml = MAUtils.MAFolder + @“\” + configParameters["Full Import File"].Value;

            }

            if (importRunStep.ImportType == OperationType.Delta)
            {
              inputxml = MAUtils.MAFolder + @“\” + configParameters["Delta Import File"].Value;

            }

            doc.Load(inputxml);

            XmlNodeList xnList = doc.SelectNodes("/sample-objects/object");

            foreach (XmlNode node in xnList)
            {

                string objectclass = node["objectclass"].InnerText;
                string delta = node["delta"].InnerText;
                string anchor = node["anchor-attribute"].InnerText;
                string name = node["name"].InnerText;
                string email = node["email"].InnerText;

                string fullline = objectclass + "," + delta + "," + anchor + "," + name + "," + email;
                swImport.WriteLine(fullline);
            }

            swImport.WriteLine();

            swImport.Close();

            return new WriteImportFileResults();
        }


        public void ReadExportFile(KeyedCollection<string, ConfigParameter> configParameters,
                                     Schema types,
                                     ReadExportFileRunStep exportRunStep)
        {
            XmlWriter m_xmlWriterExport;
        
            StreamReader sr = new StreamReader(exportRunStep.FilePath);
            string lineContents = null;

            XmlWriterSettings xmlSettings = new XmlWriterSettings();

            // Determine encoding from Configuration Parmeters
            string encoding = configParameters["Export Encoding"].Value;
            if (encoding.Equals("ASCII"))
            {
                xmlSettings.Encoding = Encoding.ASCII;
            }
            else if (encoding.Equals("UTF8"))
            {
                xmlSettings.Encoding = Encoding.UTF8;
            }
            else
            {
                xmlSettings.Encoding = Encoding.Unicode;
            }

            // Use a run step config paramater to control Xml declaration
            string omitXmlDecl = configParameters["Omit Xml Declaration"].Value;
            if (omitXmlDecl.Equals("1"))
            {
                xmlSettings.OmitXmlDeclaration = true;
            }

            // Begin XML file
           string exportfile = configParameters["Export File"].Value.ToString();
            
            m_xmlWriterExport = XmlTextWriter.Create(MAUtils.MAFolder + @"\" + exportfile, xmlSettings);
            m_xmlWriterExport.WriteStartElement(Nodes.Root);
            m_xmlWriterExport.WriteAttributeString(Nodes.FullExport, (OperationType.Full == exportRunStep.ExportType) ? "true" : "false");

            // Include partition DN
            m_xmlWriterExport.WriteElementString(Nodes.PartitionDN, exportRunStep.StepPartition.DN);

            // Read from export file and create XML file
            while (null != (lineContents = sr.ReadLine()))
            {
                char[] commaEscape = new char[] { ',' };
                char[] quoteEscape = new char[] { '"' };
                string[] valueComponents = lineContents.Split(commaEscape);

                //
                // NOTE: In our sample, we assume that the order given to us is:
                //  objectclass,delta,anchor-attribute,name,email
                //

                //
                // Check the objectclass node and see if this object class is
                // something that we are interested in.
                //
                if (Nodes.ObjectClass == valueComponents[0].Trim(quoteEscape))
                {
                    continue;
                }

                //
                // This means that we are interested in this object class.
                // Populate the the comma-delimited file.
                //
                m_xmlWriterExport.WriteStartElement(Nodes.Object);

                m_xmlWriterExport.WriteElementString(
                    Nodes.ObjectClass,
                    valueComponents[0].Trim(quoteEscape)
                    );

                m_xmlWriterExport.WriteElementString(
                    Nodes.Delta,
                    valueComponents[1].Trim(quoteEscape)
                    );

                m_xmlWriterExport.WriteElementString(
                    Nodes.Anchor,
                    valueComponents[2].Trim(quoteEscape)
                    );

                m_xmlWriterExport.WriteElementString(
                    Nodes.Name,
                    valueComponents[3].Trim(quoteEscape)
                    );

                m_xmlWriterExport.WriteElementString(
                    Nodes.Email,
                    valueComponents[4].Trim(quoteEscape)
                    );

                m_xmlWriterExport.WriteEndElement();
            }

            m_xmlWriterExport.WriteEndElement();

            m_xmlWriterExport.Close();
        }



        struct Nodes
        {
            //
            // Struct used to keep track of the XML node names.
            // This is used when generating the XML file.
            //
            public const string Root = "sample-objects";
            public const string PartitionDN = "partition-dn";
            public const string FullExport = "full-export";
            public const string Object = "object";
            public const string ObjectClass = "objectclass";
            public const string Delta = "delta";
            public const string Anchor = "anchor-attribute";
            public const string Name = "name";
            public const string Email = "email";

        }
    };
}