public void Write(IEnumerable <IRow> rows)
        {
            ErrorMode errorMode;

            Enum.TryParse(_context.Connection.ErrorMode, true, out errorMode);

            FileHelperAsyncEngine engine;

            if (_context.Connection.Header == Constants.DefaultSetting)
            {
                var headerText = string.Join(_context.Connection.Delimiter, _context.OutputFields.Select(f => f.Label.Replace(_context.Connection.Delimiter, " ")));
                engine = new FileHelperAsyncEngine(_builder.CreateRecordClass())
                {
                    ErrorMode = errorMode, HeaderText = headerText
                };
            }
            else
            {
                engine = new FileHelperAsyncEngine(_builder.CreateRecordClass())
                {
                    ErrorMode = errorMode
                };
            }

            var file = Path.Combine(_context.Connection.Folder, _fileName ?? _context.Entity.OutputTableName(_context.Process.Name));

            _context.Debug(() => $"Writing {file}.");

            using (engine.BeginWriteFile(file)) {
                foreach (var row in rows)
                {
                    for (var i = 0; i < _context.OutputFields.Length; i++)
                    {
                        var field = _context.OutputFields[i];
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((byte[])row[field]);
                            //engine[i] = Utility.BytesToHexString((byte[]) row[field]);
                            //engine[i] = Encoding.UTF8.GetString((byte[])row[field]);
                            break;

                        case "string":
                            engine[i] = row[field];
                            break;

                        case "datetime":
                            engine[i] = ((DateTime)row[field]).ToString("o");
                            break;

                        default:
                            engine[i] = row[field].ToString();
                            break;
                        }
                    }
                    engine.WriteNextValues();
                }
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            ErrorMode errorMode;

            Enum.TryParse(_context.Connection.ErrorMode, true, out errorMode);

            FileHelperAsyncEngine engine;

            if (_context.Connection.Header == Constants.DefaultSetting)
            {
                var headerText = string.Join(_delimiter, _context.OutputFields.Select(f => f.Label.Replace(_delimiter, " ")));
                engine = new FileHelperAsyncEngine(_builder.CreateRecordClass())
                {
                    ErrorMode = errorMode, HeaderText = headerText
                };
            }
            else
            {
                engine = new FileHelperAsyncEngine(_builder.CreateRecordClass())
                {
                    ErrorMode = errorMode
                };
            }

            _context.Debug(() => "Writing to stream.");

            var writer = new StreamWriter(_stream);

            using (engine.BeginWriteStream(writer)) {
                foreach (var row in rows)
                {
                    for (var i = 0; i < _context.OutputFields.Length; i++)
                    {
                        var field = _context.OutputFields[i];
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((byte[])row[field]);
                            break;

                        case "string":
                            engine[i] = row[field];
                            break;

                        case "datetime":
                            engine[i] = row[field] is string?Convert.ToDateTime(row[field]).ToString("o") : ((DateTime)row[field]).ToString("o");

                            break;

                        default:
                            engine[i] = row[field].ToString();
                            break;
                        }
                    }
                    engine.WriteNextValues();
                }
            }
        }
Exemplo n.º 3
0
        public void RunTimeMultimpleInstances()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field1", "int?");
            cb.AddField("Field2", typeof(int?));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof(Nullable <int>));

            cb.CreateRecordClass();
            cb.CreateRecordClass();
        }
Exemplo n.º 4
0
        public static FileHelperAsyncEngine Create(OutputContext context)
        {
            var delimiter = string.IsNullOrEmpty(context.Connection.Delimiter) ? "," : context.Connection.Delimiter;

            var builder = new DelimitedClassBuilder(Utility.Identifier(context.Entity.OutputTableName(context.Process.Name)))
            {
                IgnoreEmptyLines = true,
                Delimiter        = delimiter,
                IgnoreFirstLines = 0
            };

            foreach (var field in context.OutputFields)
            {
                var fieldBuilder = builder.AddField(field.FieldName(), typeof(string));
                fieldBuilder.FieldQuoted   = true;
                fieldBuilder.QuoteChar     = context.Connection.TextQualifier;
                fieldBuilder.QuoteMode     = QuoteMode.OptionalForBoth;
                fieldBuilder.FieldOptional = field.Optional;
            }

            FileHelpers.ErrorMode errorMode;
            Enum.TryParse(context.Connection.ErrorMode, true, out errorMode);

            FileHelperAsyncEngine engine;

            if (context.Connection.Header == Constants.DefaultSetting)
            {
                var headerText = string.Join(delimiter, context.OutputFields.Select(f => f.Label.Replace(delimiter, " ")));
                engine = new FileHelperAsyncEngine(builder.CreateRecordClass())
                {
                    ErrorMode  = errorMode,
                    HeaderText = headerText,
                    FooterText = context.Connection.Footer
                };
            }
            else
            {
                engine = new FileHelperAsyncEngine(builder.CreateRecordClass())
                {
                    ErrorMode = errorMode
                };
                if (context.Connection.Header != string.Empty)
                {
                    engine.HeaderText = context.Connection.Header;
                }
                if (context.Connection.Footer != string.Empty)
                {
                    engine.FooterText = context.Connection.Footer;
                }
            }

            return(engine);
        }
Exemplo n.º 5
0
        static void RunTimeRecords()
        {
            Console.WriteLine("Run Time Records now =) ...");
            Console.WriteLine();

            DelimitedClassBuilder cb = new DelimitedClassBuilder("Customer", ",");

            cb.AddField("CustId", typeof(Int32));
            cb.AddField("Name", typeof(string));
            cb.AddField("Balance", typeof(Decimal));
            cb.AddField("AddedDate", typeof(DateTime));
            cb.LastField.Converter.Kind = ConverterKind.Date;
            cb.LastField.Converter.Arg1 = "ddMMyyyy";


            // Estas dos lineas son el uso de la librería
            FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable dt = engine.ReadFileAsDT(@"..\Data\CustomersDelimited.txt");

            // Aqui es donde ustedes agregan su código
            foreach (DataRow dr in dt.Rows)
            {
                Console.WriteLine("Customer: " + dr[0].ToString() + " - " + dr[1].ToString());
            }
        }
Exemplo n.º 6
0
        protected virtual void PrepareType(Entity entity)
        {
            var builder = new DelimitedClassBuilder("Tfl" + entity.OutputName())
            {
                IgnoreEmptyLines = true, Delimiter = _connection.Delimiter.ToString(CultureInfo.InvariantCulture), IgnoreFirstLines = 0
            };

            foreach (var f in _fileFields)
            {
                var field = new DelimitedFieldBuilder(f.Identifier, f.SystemType);
                if (f.SimpleType.Equals("datetime"))
                {
                    field.Converter.Kind = ConverterKind.Date;
                    field.Converter.Arg1 = _connection.DateFormat;
                }
                if (f.SimpleType.Equals("guid"))
                {
                    field.Converter.TypeName = "Transformalize.Operations.Load.GuidConverter";
                }
                if (f.SimpleType.Equals("byte[]") || f.SimpleType.Equals("rowversion"))
                {
                    field.Converter.TypeName = "Transformalize.Operations.Load.ByteArrayConverter";
                }
                if (_isCsv)
                {
                    field.FieldQuoted    = true;
                    field.QuoteChar      = '"';
                    field.QuoteMode      = QuoteMode.OptionalForBoth;
                    field.QuoteMultiline = MultilineMode.NotAllow;
                }
                builder.AddField(field);
            }
            Type = builder.CreateRecordClass();
        }
Exemplo n.º 7
0
        //public bool Export(Guid id, PriceImportLogStatus pilStatus, string path)
        //{
        //    DeleteExportFile(path);
        //    PriceImport pi = GetById(id);

        //    // TODO: Convert to MemoryStream.
        //    // TODO: Move hardcoded to translation file
        //    List<int> lst = GetIndexsForExport(id, pilStatus);

        //    DelimitedClassBuilder cb = CreateClassBuilder(pi.SeparationChar, pi.HaveHeader);
        //    FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());
        //    object[] items = engine.ReadFile(path + pi.ID + ".csv");
        //    object[] erroritems = new object[lst.Count];
        //    int i = 0;
        //    foreach (int error in lst)
        //    {
        //        erroritems[i] = items[error];
        //        i++;
        //    }
        //    engine.WriteFile(path + Resource.Business.GetString("ExportFile"), erroritems);


        //    return true;
        //}

        public MemoryStream Export(Guid id, PriceImportLogStatus pilStatus, string path)
        {
            PriceImport pi = GetById(id);

            List <int> lst = GetIndexsForExport(id, pilStatus);

            DelimitedClassBuilder cb     = CreateClassBuilder(pi.SeparationChar, pi.HaveHeader);
            FileHelperEngine      engine = new FileHelperEngine(cb.CreateRecordClass());

            object[] items      = engine.ReadFile(path + pi.ID + ".csv");
            object[] erroritems = new object[lst.Count];
            int      i          = 0;

            foreach (int error in lst)
            {
                erroritems[i] = items[error];
                i++;
            }

            MemoryStream stream = new MemoryStream();
            StreamWriter sw     = new StreamWriter(stream);

            engine.WriteStream(sw, erroritems);

            return(stream);
        }
Exemplo n.º 8
0
        public override void ProcessFeed()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("TmpOutfitUpdater", Separator);

            if (HaveHeader)
            {
                cb.IgnoreFirstLines = 1;
            }
            if (HaveFooter)
            {
                cb.IgnoreLastLines = 1;
            }

            AddFields(cb);

            cb.IgnoreEmptyLines = true;

            FileHelperAsyncEngine engine = new FileHelperAsyncEngine(cb.CreateRecordClass());

            engine.BeginReadFile(Path.Combine(ImportPath, this.FileName));

            while (engine.ReadNext() != null)
            {
                LineRead(ConvertDataToLine(engine.LastRecord));
            }
        }
Exemplo n.º 9
0
        public void TestingNameAndTypes()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");

            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof(DateTime));
            cb.LastField.TrimMode       = TrimMode.Both;
            cb.LastField.QuoteMode      = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Field2", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar   = '"';

            cb.AddField("Field3", typeof(int));

            engine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable dt = engine.ReadFileAsDT(TestCommon.GetPath("Good", "Test2.txt"));

            Assert.AreEqual("Field1", dt.Columns[0].ColumnName);
            Assert.AreEqual(typeof(DateTime), dt.Columns[0].DataType);

            Assert.AreEqual("Field2", dt.Columns[1].ColumnName);
            Assert.AreEqual(typeof(string), dt.Columns[1].DataType);

            Assert.AreEqual("Field3", dt.Columns[2].ColumnName);
            Assert.AreEqual(typeof(int), dt.Columns[2].DataType);
        }
Exemplo n.º 10
0
        public void RunTimeEmptyGuidProperties()
        {
            var builder = new DelimitedClassBuilder("EntityWithGuid", "\t");

            builder.AddField("Name", typeof(string));
            builder.AddField("Id", typeof(Guid));
            builder.LastField.FieldNullValue = Guid.Empty;

            var engine = new FileHelperEngine(builder.CreateRecordClass())
            {
                Options =
                {
                    IgnoreFirstLines = 1
                }
            };

            const string inputValue = @"Name	Id
first	
second	";

            var records = engine.ReadString(inputValue);

            Check.That(records.Length).IsEqualTo(2);

            dynamic record = records[0];

            Check.That(((Guid)record.Id)).IsEqualTo(Guid.Empty);
            Check.That(((string)record.Name)).IsEqualTo("first");

            record = records[1];
            Check.That(((Guid)record.Id)).IsEqualTo(Guid.Empty);
            Check.That(((string)record.Name)).IsEqualTo("second");
        }
Exemplo n.º 11
0
        public void FullClassBuilding()
        {
            var cb = new DelimitedClassBuilder("Customers", ",");

            cb.IgnoreFirstLines = 1;
            cb.IgnoreEmptyLines = true;

            cb.AddField("Field1", typeof(DateTime));
            cb.LastField.TrimMode       = TrimMode.Both;
            cb.LastField.QuoteMode      = QuoteMode.AlwaysQuoted;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Field2", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar   = '"';

            cb.AddField("Field3", typeof(int));

            engine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable dt = engine.ReadFileAsDT(TestCommon.GetPath("Good", "Test2.txt"));

            Assert.AreEqual(4, dt.Rows.Count);
            Assert.AreEqual(4, engine.TotalRecords);
            Assert.AreEqual(0, engine.ErrorManager.ErrorCount);

            Assert.AreEqual("Field1", dt.Columns[0].ColumnName);
            Assert.AreEqual("Field2", dt.Columns[1].ColumnName);
            Assert.AreEqual("Field3", dt.Columns[2].ColumnName);

            Assert.AreEqual("Hola", dt.Rows[0][1]);
            Assert.AreEqual(DateTime.Today, dt.Rows[2][0]);
        }
Exemplo n.º 12
0
        public static FileHelperAsyncEngine Create(InputContext context)
        {
            var identifier = Utility.Identifier(context.Entity.OutputTableName(context.Process.Name));
            var delimiter  = string.IsNullOrEmpty(context.Connection.Delimiter) ? "," : context.Connection.Delimiter;

            var builder = new DelimitedClassBuilder(identifier)
            {
                IgnoreEmptyLines = true,
                Delimiter        = delimiter,
                IgnoreFirstLines = context.Connection.Start
            };

            foreach (var field in context.InputFields)
            {
                var fieldBuilder = builder.AddField(field.FieldName(), typeof(string));
                fieldBuilder.FieldQuoted   = true;
                fieldBuilder.QuoteChar     = context.Connection.TextQualifier;
                fieldBuilder.QuoteMode     = QuoteMode.OptionalForBoth;
                fieldBuilder.FieldOptional = field.Optional;
            }

            FileHelpers.ErrorMode errorMode;
            Enum.TryParse(context.Connection.ErrorMode, true, out errorMode);

            var engine = new FileHelperAsyncEngine(builder.CreateRecordClass());

            engine.ErrorManager.ErrorMode  = errorMode;
            engine.ErrorManager.ErrorLimit = context.Connection.ErrorLimit;

            return(engine);
        }
Exemplo n.º 13
0
        protected override void PrepareType(Entity entity)
        {
            var builder = new DelimitedClassBuilder("Tfl" + entity.OutputName())
            {
                IgnoreEmptyLines = true, Delimiter = " ", IgnoreFirstLines = 0
            };

            builder.AddField(_htmlField, typeof(string));
            Type = builder.CreateRecordClass();
        }
Exemplo n.º 14
0
        public ReadUsersFromFileDynamic()
        {
            var userRecordClassBuilder = new DelimitedClassBuilder("UserRecord", "\t");

            userRecordClassBuilder.IgnoreFirstLines = 1;
            userRecordClassBuilder.AddField("Id", typeof(Int32));
            userRecordClassBuilder.AddField("Name", typeof(String));
            userRecordClassBuilder.AddField("Email", typeof(String));
            _tblClass = userRecordClassBuilder.CreateRecordClass();
        }
Exemplo n.º 15
0
        public void ReadAsDataTable1()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.IgnoreEmptyLines   = true;
            cb.GenerateProperties = true;

            cb.AddField("FirstName", typeof(string));
            cb.LastField.TrimMode    = FileHelpers.TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("LastName", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetNumber", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetAddress", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Unit", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("City", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("State", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Zip", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            var engine = new FileHelperEngine(cb.CreateRecordClass());

            string source = "Alex & Jen,Bouquet,1815,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Mark & Lisa K ,Arlinghaus,1817,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Ed & Karen S ,Craycraft,1819,Bell Rd,, Batavia,OH,45103" + Environment.NewLine;

            DataTable contactData = engine.ReadStringAsDT(source);

            Assert.AreEqual(3, contactData.Rows.Count);
            Assert.AreEqual(8, contactData.Columns.Count);

            Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
            Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

            // new DelimitedClassBuilder("", ",");
        }
Exemplo n.º 16
0
        protected void Page_Load(object sender, EventArgs e)
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("Prices", ";");

            // cb.IgnoreFirstLines = 1;
            cb.AddField("CodGrundfos", typeof(string));
            cb.AddField("CodProv", typeof(string));
            cb.AddField("Modelo", typeof(string));
            cb.AddField("Descripcion", typeof(string));
            cb.AddField("Proveedor", typeof(string));
            cb.AddField("Frecuencia", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("TP");
            cb.LastField.FieldType      = "Nullable < System.Decimal >";
            cb.LastField.FieldNullValue = null;
            cb.LastField.FieldOptional  = true;
            cb.LastField.Converter.Kind = ConverterKind.AnyDecimal;
            cb.AddField("MonedaTP", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("GRP", typeof(string));
            cb.LastField.FieldType      = "Nullable < System.Decimal >";
            cb.LastField.FieldNullValue = null;
            cb.LastField.FieldOptional  = true;
            cb.LastField.Converter.Kind = ConverterKind.AnyDecimal;
            cb.AddField("MonedaGRP", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("PL", typeof(string));
            cb.LastField.FieldType      = "Nullable < System.Decimal >";
            cb.LastField.FieldNullValue = null;
            cb.LastField.FieldOptional  = true;
            cb.LastField.Converter.Kind = ConverterKind.AnyDecimal;
            cb.AddField("MonedaPL", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("Familia", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("Tipo", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.AddField("Linea", typeof(string));
            cb.LastField.FieldOptional = true;
            cb.IgnoreEmptyLines        = true;

            FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());

            object[] items = engine.ReadFile(Server.MapPath("~/TesterSeba2.csv"));

            object o = items[0];

            Response.Write(o.GetType().GetField("CodGrundfos").GetValue(o));
            Response.Write("<br>");
            Response.Write(o.GetType().GetField("TP").GetValue(o));
            Response.Write("<br>");

            o = items[1];
            Response.Write(o.GetType().GetField("TP").GetValue(o));
        }
Exemplo n.º 17
0
        public void RunTimeGenerics()
        {
            var cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.AddField("Field2", typeof(Dictionary <int, List <string> >));
            cb.AddField("Field1", "System.Collections.Generic.List<int>");
            cb.AddField("Field7", typeof(List <int>));
            cb.AddField("Field3", "Nullable<int>");
            cb.AddField("Field4", typeof(Nullable <int>));

            cb.CreateRecordClass();
        }
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            DelimitedClassBuilder classBuilder    = ExamineGridForColumnOrder(",");
            OpenFileDialog        openFileDialog1 = new OpenFileDialog
            {
                Filter =
                    "csv files (*.csv)|*.csv|Excel files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*",
                FilterIndex = 3
            };

            if (openFileDialog1.ShowDialog() != true)
            {
                return;
            }
            try
            {
                DataTable dt;
                if (Path.GetExtension(openFileDialog1.FileName).ToLower().Equals(".csv"))
                {
                    FileHelperEngine engine = new FileHelperEngine(classBuilder.CreateRecordClass());
                    dt = engine.ReadFileAsDT(openFileDialog1.FileName);
                }
                else if (Path.GetExtension(openFileDialog1.FileName).ToLower().Equals(".xls") ||
                         Path.GetExtension(openFileDialog1.FileName).ToLower().Equals(".xlsx"))
                {
                    ExcelStorage storage = new ExcelStorage(classBuilder.CreateRecordClass(), openFileDialog1.FileName, 1, 1);

                    dt = storage.ExtractRecordsAsDT();
                }
                else
                {
                    return;
                }
                PasteDataTable(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
Exemplo n.º 19
0
        public void LoopingFields()
        {
            var cb = new DelimitedClassBuilder("MyClass", ",");

            string[] lst = { "fieldOne", "fieldTwo", "fieldThree" };

            for (int i = 0; i < lst.Length; i++)
            {
                cb.AddField(lst[i].ToString(), typeof(string));
            }

            var engineTemp = new FileHelperEngine(cb.CreateRecordClass());
        }
Exemplo n.º 20
0
        public void ReadAsDataTable2()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ",");

            cb.IgnoreEmptyLines   = true;
            cb.GenerateProperties = true;

            cb.AddField("FirstName", typeof(string));
            cb.LastField.TrimMode    = FileHelpers.TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("LastName", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetNumber", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("StreetAddress", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Unit", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("City", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("State", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            cb.AddField("Zip", typeof(string));
            cb.LastField.TrimMode    = TrimMode.Both;
            cb.LastField.FieldQuoted = false;

            var engine = new FileHelperEngine(cb.CreateRecordClass());

            DataTable contactData = engine.ReadFileAsDT(TestCommon.GetPath("Good", "ReadAsDataTable.txt"));

            Assert.AreEqual(3, contactData.Rows.Count);
            Assert.AreEqual(8, contactData.Columns.Count);

            Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
            Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

            // new DelimitedClassBuilder("", ",");
        }
Exemplo n.º 21
0
        private void ClassBuilder(int ignoreLines, IDictionary <int, string> fieldNames, char[] separator)
        {
            var builder = new DelimitedClassBuilder("UserRecord", separator[0].ToString())
            {
                IgnoreFirstLines = ignoreLines
            };


            foreach (var field in fieldNames.OrderBy(x => x.Key))
            {
                builder.AddField(field.Value, typeof(string));
            }

            InputType = builder.CreateRecordClass();
        }
        private void PasteFromClip()
        {
            DelimitedClassBuilder classBuilder = ExamineGridForColumnOrder("\t");

            try
            {
                FileHelperEngine engine = new FileHelperEngine(classBuilder.CreateRecordClass());
                DataTable        dt     = engine.ReadStringAsDT(Clipboard.GetText());
                PasteDataTable(dt);
            }
            catch (Exception ex)
            {
                throw ex;
                //addInBase.MessageService.LogMessage(new ApplicationMessage(ex, ApplicationMessageType.ErrorNoPopup));
            }
        }
        private static void Main(string[] args)
        {
            var cb = new DelimitedClassBuilder("Customer", ",");

            cb.AddField("SomeField", typeof(String));
            cb.LastField.Converter.TypeName = typeof(OtherNameSpace.MyConverter).ToString();

            Type recordClass = cb.CreateRecordClass();

            var engine  = new FileHelperEngine(recordClass);
            var records = engine.ReadStringAsDT("aaa");

            Assert.AreEqual("aaa", records.Rows[0].Field <string>(0));

            Console.WriteLine("All OK");
            Console.ReadKey();
        }
Exemplo n.º 24
0
        public IEnumerable <IRow> Read()
        {
            ErrorMode errorMode;

            Enum.TryParse(_context.Connection.ErrorMode, true, out errorMode);

            var engine = new FileHelperAsyncEngine(_builder.CreateRecordClass());

            engine.ErrorManager.ErrorMode  = errorMode;
            engine.ErrorManager.ErrorLimit = _context.Connection.ErrorLimit;

            _context.Debug(() => $"Reading {_fileInfo.Name}.");

            using (engine.BeginReadFile(_fileInfo.FullName)) {
                foreach (var record in engine)
                {
                    var values = engine.LastRecordValues;
                    var row    = _rowFactory.Create();
                    for (var i = 0; i < _context.InputFields.Length; i++)
                    {
                        var field = _context.InputFields[i];
                        if (field.Type == "string")
                        {
                            row[field] = values[i] as string;
                        }
                        else
                        {
                            row[field] = field.Convert(values[i]);
                        }
                    }
                    if (_rowCondition.Eval(row))
                    {
                        yield return(row);
                    }
                }

                if (engine.ErrorManager.HasErrors)
                {
                    foreach (var error in engine.ErrorManager.Errors)
                    {
                        _context.Error(error.ExceptionInfo.Message);
                    }
                }
            }
        }
Exemplo n.º 25
0
        public void ReadAsDataTableWithCustomConverter()
        {
            var fields = new[] {
                "FirstName",
                "LastName",
                "StreetNumber",
                "StreetAddress",
                "Unit",
                "City",
                "State",
            };
            var cb = new DelimitedClassBuilder("ImportContact", ",");

            // Add assembly reference
            cb.AdditionalReferences.Add(typeof(MyCustomConverter).Assembly);

            foreach (var f in fields)
            {
                cb.AddField(f, typeof(string));
                cb.LastField.TrimMode    = TrimMode.Both;
                cb.LastField.FieldQuoted = false;
            }

            cb.AddField("Zip", typeof(int?));
            cb.LastField.Converter.TypeName = "FileHelpers.Tests.Dynamic.DelimitedClassBuilderTests.MyCustomConverter";

            mEngine = new FileHelperEngine(cb.CreateRecordClass());

            string source = "Alex & Jen,Bouquet,1815,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
                            "Mark & Lisa K ,Arlinghaus,1817,Bell Rd,, Batavia,OH,NaN" + Environment.NewLine +
                            "Ed & Karen S ,Craycraft,1819,Bell Rd,, Batavia,OH,45103" + Environment.NewLine;

            var contactData = mEngine.ReadString(source);

            Assert.AreEqual(3, contactData.Length);
            var zip = mEngine.RecordType.GetFields()[7];

            Assert.AreEqual("Zip", zip.Name);
            Assert.IsNull(zip.GetValue(contactData[1]));
            Assert.AreEqual((decimal)45103, zip.GetValue(contactData[2]));
        }
Exemplo n.º 26
0
        private Type GetDynamicMasterType()
        {
            var masterCb = new DelimitedClassBuilder("CustomDynamicType", ";");

            //masterCb.IgnoreFirstLines = 1;
            masterCb.IgnoreEmptyLines = true;

            masterCb.AddField("Code", typeof(string));

            masterCb.AddField("Field1", typeof(DateTime));
            masterCb.LastField.TrimMode       = TrimMode.Both;
            masterCb.LastField.QuoteMode      = QuoteMode.AlwaysQuoted;
            masterCb.LastField.FieldNullValue = DateTime.Today;

            masterCb.AddField("Field2", typeof(string));
            masterCb.LastField.FieldQuoted = true;
            masterCb.LastField.QuoteChar   = '"';

            masterCb.AddField("Field3", typeof(int));
            return(masterCb.CreateRecordClass());
        }
        public void Create()
        {
            InitializePerformanceCounters();
            if (ramCounter.NextValue() < (ramTotal * MemorySafe))
            {
                WriteInfo(string.Format("Safe Memory Limit"), string.Format("Available = {0} < Safe Limit = {1}MB", getAvailableRAM(), ramTotal * MemorySafe));
                return;
            }

            DateTime startTime    = DateTime.Now;
            int      totalRecords = 0;

            lines = new List <OUImportationLine>();

            // TODO: Fetch keywords to make it faster on the loops.
            trends         = new List <Trend>(trendRepository.GetAll());
            silouhettes    = new List <Silouhette>(silouhetteRepository.GetAll());
            patterns       = new List <Pattern>(patternRepository.GetAll());
            colorFamilies  = new List <ColorFamily>(colorFamilyRepository.GetAll());
            outfitUpdaters = new List <OutfitUpdater>(outfitUpdaterRepository.GetFor(Partner));
            wordsForDiscard.Add(" men");
            wordsForDiscard.Add(" mens");
            wordsForDiscard.Add(" men's");
            wordsForDiscard.Add(" men´s");
            wordsForDiscard.Add("kid");
            wordsForDiscard.Add("kids");
            wordsForDiscard.Add("kid's");
            wordsForDiscard.Add("kid´s");

            // Retrieve Keywords
            // TODO: Limit by partner
            colorFamilyKeywords = new Repository <ColorFamilyKeywordsByPartner>().GetAll();
            patternKeywords     = new Repository <PatternKeywordsByPartner>().GetAll();
            silouhetteKeywords  = new Repository <SilouhetteKeywordsByPartner>().GetAll();

            Write("--------------------------------------------------------------------------------");
            Write(string.Format("{0} - {1}\t\t{2}\t\t{3}\t{4}\t{5}", "Time", "Process Type", "Action", "CPU Usage", "RAM Available(MB)", "Records OK"));
            WriteFullInfo("Start");

            //REVIEW: The ZapposClassBuilder should read the file and create an event for the loop, returning an
            //REVIEW: understandable object that we can interchange when we have another builder. This way we can not
            //REVIEW: change the builder in a truly manner. Right now, is kind of complex and not clear how to change it.
            //REVIEW: That way is simple to change from reading from a file or other providers.
            //REVIEW: This class should worry about checking if the line is valid and import it in our system.
            DelimitedClassBuilder cb = ouImportationClassBuilder.CreateClassBuilder(Separator, HaveHeader);

            //REVIEW: Why is these here? We always read line at line.
            if (Sync)
            {
                FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());
                object[]         items  = engine.ReadFile(path + Filename);
                totalRecords = items.Length;
                WriteFullInfo("Ready");

                for (int i = 0; i < items.Length; i++)
                {
                    actualLine = i;
                    if (HaveHeader)
                    {
                        actualLine++;
                    }

                    ProcessOUImportationLine(items[i]);

                    CheckLimits();
                }
            }
            else
            {
                FileHelperAsyncEngine engine = new FileHelperAsyncEngine(cb.CreateRecordClass());
                engine.BeginReadFile(path + Filename);
                WriteFullInfo("Ready");

                while (engine.ReadNext() != null)
                {
                    actualLine = totalRecords + 1;
                    if (HaveHeader)
                    {
                        actualLine++;
                    }

                    ProcessOUImportationLine(engine.LastRecord);

                    CheckLimits();
                    totalRecords++;
                }

                ProcessList();
            }

            DateTime endTime = DateTime.Now;
            TimeSpan span    = endTime - startTime;

            WriteFullInfo("Finish");
            Write("--------------------------------------------------------------------------------");
            Write(string.Format("{0} - {1} {2} {3} records in {4} seconds", endTime.ToLongTimeString(), Process, "Finish", totalRecords, span.TotalSeconds.ToString("0")));
            WriteInfo("Total lines added/modified", linesOk.Count.ToString());
            WriteInfo("Total errors", TotalErrors.ToString());
            //WriteInfo("Lines with errors", string.Join(",", linesWithErrors.ToArray()));
            Write("--------------------------------------------------------------------------------");
        }
Exemplo n.º 28
0
        private static void Main(string[] args)
        {
            /*var provider = new ExcelStorage(typeof(RaRecord)) {
             *  StartRow = 2,
             *  StartColumn = 1,
             *  SheetName = "Sheet2",
             *  FileName = "test.xlsx"
             * };*/

            //Dynamic Records
            var cb = new DelimitedClassBuilder("Customer", "|")
            {
                IgnoreFirstLines = 1,
                IgnoreEmptyLines = true
            };

            cb.AddField("BirthDate", typeof(DateTime));
            cb.LastField.TrimMode       = TrimMode.Both;
            cb.LastField.FieldNullValue = DateTime.Today;

            cb.AddField("Name", typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar   = '"';

            cb.AddField("Age", typeof(int));

            var providerWithDynamicRecord = new ExcelNPOIStorage(cb.CreateRecordClass())
            {
                FileName = Directory.GetCurrentDirectory() + @"\testDynamicRecords.xlsx"
            };

            providerWithDynamicRecord.StartRow    = 1;
            providerWithDynamicRecord.StartColumn = 1;

            dynamic dynamicRecord = Activator.CreateInstance(providerWithDynamicRecord.RecordType);

            dynamicRecord.Name      = "Jonh";
            dynamicRecord.Age       = 1;
            dynamicRecord.BirthDate = DateTime.Now;

            var valuesList = new List <dynamic>
            {
                dynamicRecord,
                dynamicRecord
            };

            var columnsHeaders = ((System.Reflection.TypeInfo)(dynamicRecord.GetType())).DeclaredFields.Select(x => x.Name).ToList();

            providerWithDynamicRecord.ColumnsHeaders = columnsHeaders;
            providerWithDynamicRecord.InsertRecords(valuesList.ToArray());

            //General export of excel with date time columns
            var provider = new ExcelNPOIStorage(typeof(RaRecord))
            {
                FileName = Directory.GetCurrentDirectory() + @"\test.xlsx"
            };

            provider.StartRow    = 0;
            provider.StartColumn = 0;

            var records = new List <RaRecord>();

            records.Add(new RaRecord()
            {
                Level     = 123.123m,
                Name      = "Dickie",
                Startdate = DateTime.Now
            });

            var values = new List <int>
            {
                1,
                2,
                3
            };

            records.Add(new RaRecord()
            {
                Level     = null,
                Name      = "Bavo",
                Project   = "too many",
                Startdate = DateTime.Now,
                ListOfIds = string.Join(",", values.Select(n => n.ToString(CultureInfo.InvariantCulture)).ToArray())
            });

            provider.HeaderRows = 4;

            provider.InsertRecords(records.ToArray());

            var res = (RaRecord[])provider.ExtractRecords();
        }
Exemplo n.º 29
0
        public override IEnumerable <Row> Execute(IEnumerable <Row> rows)
        {
            var cb = new DelimitedClassBuilder("Tfl" + _entity.OutputName())
            {
                IgnoreEmptyLines = true,
                Delimiter        = _delimiter.ToString(CultureInfo.InvariantCulture),
                IgnoreFirstLines = _ignoreFirstLines
            };

            foreach (var field in _fields)
            {
                if (field.IsQuoted())
                {
                    cb.AddField(new DelimitedFieldBuilder(field.Identifier, typeof(string))
                    {
                        FieldQuoted   = true,
                        QuoteChar     = field.QuotedWith,
                        QuoteMode     = QuoteMode.OptionalForRead,
                        FieldOptional = field.Optional
                    });
                }
                else
                {
                    cb.AddField(new DelimitedFieldBuilder(field.Identifier, typeof(string))
                    {
                        FieldOptional = field.Optional
                    });
                }
            }

            Logger.Info("Reading Delimited {0}", _name);

            var conversionMap = Common.GetObjectConversionMap();

            if (_top > 0)
            {
                using (var file = new FluentFile(cb.CreateRecordClass()).From(_fullName).OnError(_errorMode)) {
                    foreach (var row in from object obj in file select Row.FromObject(obj))
                    {
                        if (_counter == 0)
                        {
                            try {
                                ProcessRow(row, _fields, _fullName, conversionMap);
                            } catch (Exception ex) {
                                if (!_warned)
                                {
                                    Logger.EntityWarn(_entity.Alias, "First row from '{0}' failed to process.  You may have headers.  If so, set start=\"2\" on connection. Note: start is 1-based. {0}", _name, ex.Message);
                                    _warned = true;
                                }
                            }
                        }
                        else
                        {
                            ProcessRow(row, _fields, _fullName, conversionMap);
                        }
                        if (_counter < _top)
                        {
                            yield return(row);
                        }
                        else
                        {
                            yield break;
                        }
                    }
                    Interlocked.Increment(ref _counter);
                    HandleErrors(file);
                }
            }
            else
            {
                using (var file = new FluentFile(cb.CreateRecordClass()).From(_fullName).OnError(_errorMode)) {
                    foreach (var row in from object obj in file select Row.FromObject(obj))
                    {
                        if (_counter == 0)
                        {
                            try {
                                ProcessRow(row, _fields, _fullName, conversionMap);
                            } catch (Exception ex) {
                                Logger.EntityWarn(_entity.Alias, "First row failed to process.  You may have headers.  If so, set start-index=\"2\" on connection. {0}", ex.Message);
                            }
                        }
                        else
                        {
                            ProcessRow(row, _fields, _fullName, conversionMap);
                        }
                        yield return(row);

                        Interlocked.Increment(ref _counter);
                    }
                    HandleErrors(file);
                }
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Create  a new PriceImport object based on the input file.
        /// </summary>
        /// <param name="filename">File with the CSV to import</param>
        /// <param name="description">Description for the new import process</param>
        /// <param name="haveHeader">Indicates if the file has header line</param>
        /// <param name="separationChar">Indicates the separation character in the CSV file</param>
        /// <param name="path">Indicates the path of the CSV file</param>
        /// <returns></returns>
        public PriceImport Create(string newfilename, string description, bool haveHeader, char separationChar, string path, string originalfilename)
        {
            Utils.GetLogger().Debug(string.Format("[[Product Import]] Start {0}", description));

            DelimitedClassBuilder cb     = CreateClassBuilder(separationChar, haveHeader);
            FileHelperEngine      engine = new FileHelperEngine(cb.CreateRecordClass());

            object[] items = engine.ReadFile(path + newfilename);

            if (items.Length <= 0)
            {
                throw new EmptyImportationFileException("No se encontraron registros para ingresar.");
            }

            Utils.GetLogger().Debug(string.Format("[[Product Import]] Starting to get database data {0}", description));

            // TODO: We should only get the necessary fields for each object and not the whole object.
            provlist = ControllerManager.Provider.GetActives() as List <Provider>;
            catlist  = ControllerManager.CategoryBase.GetAll() as List <CategoryBase>;
            currlist = ControllerManager.Currency.GetAll() as List <Currency>;

            // Get Needed PriceBase Data
            IQuery q = NHibernateSession.CreateQuery("select lower(trim(P.Code)), lower(trim(PB.ProviderCode)), PB.Provider.ID, PB.ID FROM PriceBase PB JOIN PB.Product P");

            q.SetResultTransformer(new NHibernate.Transform.AliasToBeanConstructorResultTransformer(typeof(PriceBaseFound).GetConstructors()[0]));
            List <PriceBaseFound> lst = q.List <PriceBaseFound>() as List <PriceBaseFound>;

            // Clear the memory
            NHibernateSession.Flush();
            NHibernateSession.Clear();

            Utils.GetLogger().Info(string.Format("[[Product Import]] Ready {0}", description));

            List <PriceImportLog> lstDuplicates = new List <PriceImportLog>(items.Length);

            // Start the transaction
            this.BeginTransaction();

            // Create the PriceImport item
            PriceImport pi = new PriceImport();

            pi.ImportStatus   = ImportStatus.Invalid;
            pi.File           = originalfilename;
            pi.Description    = description;
            pi.DateImported   = DateTime.Now;
            pi.HaveHeader     = haveHeader;
            pi.SeparationChar = separationChar;
            Save(pi);

            bool error           = false;
            bool atLeastOneValid = false;

            for (int i = 0; i < items.Length; i++)
            {
                string originalline = "";

                // Create the item
                PriceImportLog lr = new PriceImportLog();
                lr.CodGrundfos  = GetValue("CodGrundfos", items[i]).ToString();
                lr.CodProvider  = GetValue("CodProv", items[i]).ToString();
                lr.Model        = GetValue("Modelo", items[i]).ToString();
                lr.Description  = GetValue("Descripcion", items[i]).ToString();
                lr.Provider     = GetValue("Proveedor", items[i]).ToString();
                lr.Frequency    = GetValue("Frecuencia", items[i]).ToString();
                lr.TP           = (decimal?)GetValue("TP", items[i]);
                lr.TPCurrency   = GetValue("MonedaTP", items[i]).ToString();
                lr.GRP          = (decimal?)GetValue("GRP", items[i]);
                lr.GRPCurrency  = GetValue("MonedaGRP", items[i]).ToString();
                lr.PL           = (decimal?)GetValue("PL", items[i]);
                lr.PLCurrency   = GetValue("MonedaPL", items[i]).ToString();
                lr.Cat1         = GetValue("Familia", items[i]).ToString();
                lr.Cat2         = GetValue("Tipo", items[i]).ToString();
                lr.Cat3         = GetValue("Linea", items[i]).ToString();
                lr.OriginalLine = originalline;
                lr.FileIndex    = i;

                // Check if at least the required fields are completed, otherwise ignore.
                if (string.IsNullOrEmpty(lr.Provider) || (string.IsNullOrEmpty(lr.CodGrundfos) && string.IsNullOrEmpty(lr.CodProvider)))
                {
                    NHibernateSession.Evict(lr);
                    error = true;
                    continue;
                }

                // Check the whole item data
                CheckLogResultItem(lr);

                if (lr.CodGrundfos == string.Empty || lr.CodProvider == string.Empty)
                {
                    if (lstDuplicates.Exists(delegate(PriceImportLog record)
                    {
                        if ((record.CodGrundfos != string.Empty && record.CodGrundfos == lr.CodGrundfos && record.Provider == lr.Provider) || (record.CodProvider != string.Empty && record.CodProvider == lr.CodProvider && record.Provider == lr.Provider))
                        {
                            return(true);
                        }
                        return(false);
                    }))
                    {
                        SetErrorOnItem(lr, Resource.Business.GetString("DuplicatedProduct"));
                    }
                }

                lstDuplicates.Add(lr);

                if (lr.Status != PriceImportLogStatus.Error)
                {
                    string codGrundFos = lr.CodGrundfos.Trim();
                    string codProvider = lr.CodProvider.Trim();

                    Provider       prov = FindInMemory(provlist, lr.Provider);
                    PriceBaseFound pb;

                    if (!string.IsNullOrEmpty(codGrundFos))
                    {
                        codGrundFos = codGrundFos.ToLower();
                        pb          = lst.Find(delegate(PriceBaseFound record)
                        {
                            if (record.Code == codGrundFos && record.ProviderId == prov.ID)
                            {
                                return(true);
                            }
                            return(false);
                        });
                    }
                    else
                    {
                        codProvider = codProvider.ToLower();
                        pb          = lst.Find(delegate(PriceBaseFound record)
                        {
                            if (record.CodeProvider == codProvider && record.ProviderId == prov.ID)
                            {
                                return(true);
                            }
                            return(false);
                        });
                    }
                    if (pb != null)
                    {
                        lr.Status          = PriceImportLogStatus.Modify;
                        lr.ParsedPriceBase = new PriceBase(pb.PriceBaseId);
                        if (string.IsNullOrEmpty(GetValue("Frecuencia", items[i]).ToString()))
                        {
                            lr.Frequency       = string.Empty;
                            lr.ParsedFrequency = null;
                        }
                        if (string.IsNullOrEmpty(GetValue("MonedaTP", items[i]).ToString()))
                        {
                            lr.TPCurrency = string.Empty;
                        }
                        if (string.IsNullOrEmpty(GetValue("MonedaGRP", items[i]).ToString()))
                        {
                            lr.GRPCurrency = string.Empty;
                        }
                        if (string.IsNullOrEmpty(GetValue("MonedaPL", items[i]).ToString()))
                        {
                            lr.PLCurrency = string.Empty;
                        }
                    }
                }

                // Save the item
                lr.PriceImport = pi;
                NHibernateSession.Save(lr);

                if (lr.Status != PriceImportLogStatus.Error)
                {
                    atLeastOneValid = true;
                }
                else
                {
                    error = true;
                }
            }

            // Update the status of the PriceImport item
            if (error)
            {
                pi.ImportStatus = (atLeastOneValid) ? ImportStatus.VerifiedSomeInvalid : ImportStatus.Invalid;
            }
            else
            {
                pi.ImportStatus = ImportStatus.Verified;
            }

            // Commit Changes
            CommitChanges();

            Utils.GetLogger().Info(string.Format("[[Product Import]] Finished with Identifier {0} and status {1}", pi.ID, pi.ImportStatus));
            File.Move(@path + newfilename, @path + pi.ID + ".csv");

            return(pi);
        }