示例#1
0
        /// <summary>
        /// The ReadData
        /// </summary>
        /// <returns>The <see cref="IFile[]"/></returns>
        public IFile[] ReadData()
        {
            try
            {
                var engine = new FileHelperEngine <CsvFile>();

                var detector = new FileHelpers.Detection.SmartFormatDetector();
                var formats  = detector.DetectFileFormat(filePath);
                if (ValidateFileHeaders.Validate(formats, this.GetType()))
                {
                    return(engine.ReadFile(filePath));
                }
                else
                {
                    MessageBox.Show("Nieprawidłowy plik .csv" + Environment.NewLine + filePath);
                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#2
0
        private void SmartDetect(string filename)
        {
            var detector = new FileHelpers.Detection.SmartFormatDetector();
            var formats  = detector.DetectFileFormat(filename);

            foreach (var format in formats)
            {
                Console.WriteLine("Format Detected, confidence:" + format.Confidence + "%");
                var delimited = format.ClassBuilderAsDelimited;

                Console.WriteLine("    Delimiter:" + delimited.Delimiter);
                Console.WriteLine("    Fields:");
                EntityStructure entityData = new EntityStructure();

                string sheetname;
                sheetname                       = FileName;
                entityData.Viewtype             = ViewType.File;
                entityData.DatabaseType         = DataSourceType.Text;
                entityData.DataSourceID         = FileName;
                entityData.DatasourceEntityName = FileName;
                entityData.Caption              = FileName;
                entityData.EntityName           = FileName;
                List <EntityField> Fields = new List <EntityField>();
                int y = 0;
                foreach (var field in delimited.Fields)
                {
                    Console.WriteLine("        " + field.FieldName + ": " + field.FieldType);


                    EntityField f = new EntityField();


                    //  f.tablename = sheetname;
                    f.fieldname = field.FieldName;
                    f.fieldtype = field.FieldType.ToString();
                    f.ValueRetrievedFromParent = false;
                    f.EntityName = sheetname;
                    f.FieldIndex = y;
                    Fields.Add(f);
                    y += 1;
                }

                entityData.Fields = new List <EntityField>();
                entityData.Fields.AddRange(Fields);
                Entities.Clear();
                EntitiesNames.Clear();
                EntitiesNames.Add(filename);
                Entities.Add(entityData);
            }
        }
示例#3
0
        //
        //-> FileIn:input.txt

        /*Id|Company Name|Representative|Position|Address|City|Country
         * ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany
         * ANATR|Emparedados y Helados|Ana Trujillo|Owner|Avda. Constitución 2222|México D.F.|Mexico
         * ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos  2312|México D.F.|Mexico
         * BERGS|Berglunds snabbköp|Christina Berglund|Administrator|Berguvsvägen  8|Luleå|Sweden
         * BLAUS|Blauer Delikatessen|Hanna Moos|Sales Rep|Forsterstr. 57|Mannheim|Germany
         * BOLID|Bólido Comidas preparadas|Martín Sommer|Owner|C/ Araquil, 67|Madrid|Spain*/
        //-> /File


        public override void Run()
        {
            //-> File:Example.cs

            var detector = new FileHelpers.Detection.SmartFormatDetector();
            var formats  = detector.DetectFileFormat("input.txt");

            foreach (var format in formats)
            {
                Console.WriteLine("Format Detected, confidence:" + format.Confidence + "%");
                var delimited = format.ClassBuilderAsDelimited;

                Console.WriteLine("    Delimiter:" + delimited.Delimiter);
                Console.WriteLine("    Fields:");

                foreach (var field in delimited.Fields)
                {
                    Console.WriteLine("        " + field.FieldName + ": " + field.FieldType);
                }
            }
            //-> /File
        }
示例#4
0
文件: Form1.cs 项目: Shock69/DipProg
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"d:\Home\Projects\DipProg\";

            if(ofd.ShowDialog() == DialogResult.OK)
            {
                DescriptionReader dr = new DescriptionReader(ofd.FileName+".xml");
                m_Descriptions = dr.GetDescriptions();

                var detector = new FileHelpers.Detection.SmartFormatDetector();
                var formats = detector.DetectFileFormat(ofd.FileName);
                var delimited = formats[0].ClassBuilderAsDelimited;

                m_Data = CommonEngine.CsvToDataTable(ofd.FileName, delimited.ClassName, delimited.Delimiter[0], false);

                dataGridView1.DataSource = m_Data;
                dataGridView1.AutoGenerateColumns = true;

                m_FuzzyData = m_Data.Copy();
                dataGridView2.AutoGenerateColumns = true;
                dataGridView2.DataSource = m_FuzzyData;
            }
        }
        //
        //-> FileIn:input.txt
        /*Id|Company Name|Representative|Position|Address|City|Country
ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany
ANATR|Emparedados y Helados|Ana Trujillo|Owner|Avda. Constitución 2222|México D.F.|Mexico
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos  2312|México D.F.|Mexico
BERGS|Berglunds snabbköp|Christina Berglund|Administrator|Berguvsvägen  8|Luleå|Sweden
BLAUS|Blauer Delikatessen|Hanna Moos|Sales Rep|Forsterstr. 57|Mannheim|Germany
BOLID|Bólido Comidas preparadas|Martín Sommer|Owner|C/ Araquil, 67|Madrid|Spain*/
        //-> /File


        public override void Run()
        {
            //-> File:Example.cs

            var detector = new FileHelpers.Detection.SmartFormatDetector();
            var formats = detector.DetectFileFormat("input.txt");

            foreach (var format in formats)
            {
                Console.WriteLine("Format Detected, confidence:" + format.Confidence + "%");
                var delimited = format.ClassBuilderAsDelimited;

                Console.WriteLine("    Delimiter:" + delimited.Delimiter);
                Console.WriteLine("    Fields:");

                foreach (var field in delimited.Fields)
                {
                   Console.WriteLine("        " + field.FieldName + ": " + field.FieldType);    
                }
                
                
            }
            //-> /File
        }
示例#6
0
        private static void RunOptionsAndReturnExitCode(Options opts)
        {
            if (!File.Exists(opts.InputFile) || !File.Exists(opts.MappingFile))
            {
                Console.WriteLine("Input and mapping definition files are required for processing");
                return;
            }

            if (string.IsNullOrEmpty(opts.Domain))
            {
                opts.Domain = Environment.UserDomainName;
                Console.WriteLine(" ==> Domain has been set to: " + opts.Domain);
            }

            Dictionary <string, string> fieldMappings = new Dictionary <string, string>();

            using (StreamReader sr = new StreamReader(opts.MappingFile))
            {
                while (!sr.EndOfStream)
                {
                    var line  = sr.ReadLine();
                    var split = line.Split('=');
                    if (split.Length == 2 && !string.IsNullOrEmpty(split[0]) && !string.IsNullOrEmpty(split[1]))
                    {
                        fieldMappings.Add(split[0], split[1]);
                    }
                }
            }

            var detector = new FileHelpers.Detection.SmartFormatDetector();

            detector.MaxSampleLines = 10;
            var  formats  = detector.DetectFileFormat(opts.InputFile);
            bool keyFound = false;
            DelimitedClassBuilder dcb;
            DelimitedFileEngine   fileEngine = null;

            foreach (var format in formats)
            {
                Console.WriteLine("File Format Detected, confidence:" + format.Confidence + "%");
                var delimited = format.ClassBuilderAsDelimited;
                Console.WriteLine("    Delimiter:" + delimited.Delimiter);
                dcb = new DelimitedClassBuilder("InputFile", delimited.Delimiter);
                Console.WriteLine("    Fields:");

                foreach (var field in delimited.Fields)
                {
                    dcb.AddField(field);
                    var mappedField = "<Ignored>";
                    if (fieldMappings.ContainsKey(field.FieldName))
                    {
                        mappedField = fieldMappings[field.FieldName];
                    }
                    var keyField = string.Empty;
                    if (field.FieldName == opts.Key)
                    {
                        keyField = "  << UNIQUE KEY >>";
                        keyFound = true;
                    }

                    Console.WriteLine("        " + field.FieldName + ": " + field.FieldType + " => " + mappedField + keyField);
                }

                fileEngine = new DelimitedFileEngine(dcb.CreateRecordClass());
            }

            if (!keyFound)
            {
                Console.WriteLine("The specified unique key field, " + opts.Key + ", has not been found in the input file - aborting.");
                return;
            }

            if (fileEngine == null)
            {
                return;
            }

            DirectoryEntry    ldapConnection = new DirectoryEntry("LDAP://" + opts.Domain, null, null, AuthenticationTypes.Secure);
            DirectorySearcher search         = new DirectorySearcher(ldapConnection);

            foreach (var mapping in fieldMappings)
            {
                search.PropertiesToLoad.Add(mapping.Value);
            }
            dynamic[] sourceFile = fileEngine.ReadFile(opts.InputFile);
            Console.WriteLine();
            Console.WriteLine("Input File loaded; " + (sourceFile.Length - 1) + " records");
            bool firstLineSkipped = false;
            int  count            = 1;

            foreach (dynamic rec in sourceFile)
            {
                if (firstLineSkipped)
                {
                    string key = Dynamic.InvokeGet(rec, opts.Key);
                    search.Filter = "(employeeID=" + key + ")";
                    SearchResultCollection result = search.FindAll();
                    if (result.Count == 1)
                    {
                        var user = result[0].GetDirectoryEntry();
                        Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": Checking user " + user.Name);

                        foreach (var mapping in fieldMappings)
                        {
                            if (mapping.Value == opts.Key)
                            {
                                continue;
                            }
                            bool   updateValue = false;
                            string oldValue    = string.Empty;
                            string newValue    = string.Empty;
                            newValue = Dynamic.InvokeGet(rec, mapping.Key);
                            if (user.Properties[mapping.Value].Value != null)
                            {
                                if (mapping.Value.ToLower() == "manager")
                                {
                                    if (!string.IsNullOrEmpty(Dynamic.InvokeGet(rec, mapping.Key)))
                                    {
                                        // Manager has to be treated differently, as its a DN reference to another AD object.
                                        string[] man = Dynamic.InvokeGet(rec, mapping.Key).Split(' ');

                                        // Lookup what the manager DN SHOULD be
                                        DirectorySearcher managerSearch = new DirectorySearcher(ldapConnection);
                                        managerSearch.PropertiesToLoad.Add("distinguishedName");
                                        managerSearch.Filter = "(&(givenName=" + man[0] + ")(sn=" + man[1] + "))";
                                        var manager = managerSearch.FindOne();
                                        if (manager != null)
                                        {
                                            newValue = manager.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();

                                            if (user.Properties[mapping.Value].Value.ToString() != newValue)
                                            {
                                                updateValue = true;
                                                oldValue    = user.Properties[mapping.Value].Value.ToString();
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (user.Properties[mapping.Value].Value.ToString() != Dynamic.InvokeGet(rec, mapping.Key))
                                    {
                                        updateValue = true;
                                        oldValue    = user.Properties[mapping.Value].Value.ToString();
                                    }
                                }
                            }
                            else
                            {
                                updateValue = true;
                            }

                            if (updateValue)
                            {
                                if (mapping.Value.ToLower() == "manager")
                                {
                                    if (!string.IsNullOrEmpty(Dynamic.InvokeGet(rec, mapping.Key)))
                                    {
                                        // Manager has to be treated differently, as its a DN reference to another AD object.
                                        string[] man = Dynamic.InvokeGet(rec, mapping.Key).Split(' ');

                                        // Lookup what the manager DN SHOULD be
                                        DirectorySearcher managerSearch = new DirectorySearcher(ldapConnection);
                                        managerSearch.PropertiesToLoad.Add("distinguishedName");
                                        managerSearch.Filter = "(&(givenName=" + man[0] + ")(sn=" + man[1] + "))";
                                        var manager = managerSearch.FindOne();
                                        if (manager != null)
                                        {
                                            newValue = manager.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
                                        }
                                    }
                                }

                                Console.WriteLine(" >> Value to be updated (" + mapping.Value + ") - " + oldValue + " != " + newValue);
                                if (!opts.WhatIf)
                                {
                                    user.Properties[mapping.Value].Clear();
                                    user.Properties[mapping.Value].Add(newValue);
                                }
                            }
                        }

                        if (!opts.WhatIf)
                        {
                            user.CommitChanges();
                            Console.WriteLine("Updated user " + user.Name);
                        }
                    }
                    else
                    {
                        if (result.Count > 1)
                        {
                            Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": -> Multiple entries returned; " + key);
                            continue;
                        }
                        else
                        {
                            Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": -> No record found for " + key);
                            if (opts.LooseMatching)
                            {
                                string userFullName = string.Empty;
                                string firstName    = string.Empty;
                                string lastName     = string.Empty;
                                foreach (var mapping in fieldMappings)
                                {
                                    if (mapping.Value == "givenName")
                                    {
                                        firstName = Dynamic.InvokeGet(rec, mapping.Key);
                                    }
                                    else if (mapping.Value == "sn")
                                    {
                                        lastName = Dynamic.InvokeGet(rec, mapping.Key);
                                    }
                                }
                                if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
                                {
                                    Console.WriteLine("No firstname and lastname values found mapped, can not loosely match - aborting run");
                                    return;
                                }
                                userFullName = firstName + " " + lastName;
                                Console.WriteLine(" -> Trying search for users full name " + userFullName);
                                search.Filter = "(cn=" + userFullName + ")";
                                result        = search.FindAll();
                                if (result.Count == 1)
                                {
                                    var user = result[0].GetDirectoryEntry();
                                    Console.WriteLine("Checking user " + user.Name);

                                    foreach (var mapping in fieldMappings)
                                    {
                                        bool updateValue = false;
                                        if (user.Properties[mapping.Value].Value != null)
                                        {
                                            if (user.Properties[mapping.Value].Value.ToString() != Dynamic.InvokeGet(rec, mapping.Key))
                                            {
                                                updateValue = true;
                                            }
                                        }
                                        else
                                        {
                                            updateValue = true;
                                        }

                                        if (updateValue)
                                        {
                                            Console.WriteLine("Value to be updated (" + mapping.Value + ") ");
                                            if (!opts.WhatIf)
                                            {
                                                user.Properties[mapping.Value].Clear();
                                                user.Properties[mapping.Value].Add(Dynamic.InvokeGet(rec, mapping.Key));
                                            }
                                        }
                                    }

                                    if (!opts.WhatIf)
                                    {
                                        user.CommitChanges();
                                        Console.WriteLine("Updated user " + user.Name);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine(" E -> Too many or no hits to for a loose match");
                                }
                            }
                        }
                    }
                    count++;
                }
                firstLineSkipped = true;
            }
        }