public ContributionFile Read(TextReader stream) { var engine = new DelimitedFileEngine <ContributionRecord>(); engine.ErrorMode = ErrorMode.ThrowException; return(new ContributionFile(engine.ReadStream(stream))); }
public static void ImportInContractBSR(TrwBudgetPeriod period, IObjectSpace os, TextReader reader) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(InContractBsrRecord)); engine.Options.IgnoreFirstLines = 1; InContractBsrRecord[] records = (InContractBsrRecord[])engine.ReadStream(reader); os.Delete(period.InContractBSR); foreach (InContractBsrRecord rec in records) { if (!String.IsNullOrEmpty(rec.TrwSaleNomCode)) { TrwBudgetPeriodInContractBSR in_bsr = os.CreateObject <TrwBudgetPeriodInContractBSR>(); period.InContractBSR.Add(in_bsr); in_bsr.SaleNomCode = rec.TrwSaleNomCode.Trim(); in_bsr.SaleNomenclature = os.FindObject <TrwSaleNomenclature>(new BinaryOperator("TrwCode", rec.TrwSaleNomCode.Trim())); // if (in_bsr.SaleNomenclature == null) // throw new InvalidDataException("Unknow nomencalture: " + rec.TrwSaleNomCode); in_bsr.Period00 = rec.Period00 != null ? (Decimal)rec.Period00 : 0; in_bsr.Period01 = rec.Period01 != null ? (Decimal)rec.Period01 : 0; in_bsr.Period02 = rec.Period02 != null ? (Decimal)rec.Period02 : 0; in_bsr.Period03 = rec.Period03 != null ? (Decimal)rec.Period03 : 0; in_bsr.Period04 = rec.Period04 != null ? (Decimal)rec.Period04 : 0; in_bsr.Period05 = rec.Period05 != null ? (Decimal)rec.Period05 : 0; in_bsr.Period06 = rec.Period06 != null ? (Decimal)rec.Period06 : 0; in_bsr.Period07 = rec.Period07 != null ? (Decimal)rec.Period07 : 0; in_bsr.Period08 = rec.Period08 != null ? (Decimal)rec.Period08 : 0; in_bsr.Period09 = rec.Period09 != null ? (Decimal)rec.Period09 : 0; in_bsr.Period10 = rec.Period10 != null ? (Decimal)rec.Period10 : 0; in_bsr.Period11 = rec.Period11 != null ? (Decimal)rec.Period11 : 0; in_bsr.Period12 = rec.Period12 != null ? (Decimal)rec.Period12 : 0; } } }
public void ReadStreamWithExtraColumnsInBetween() { // arrange string data = "Field3,Field4,Field5,Inbetween1,Field1,Field2,More1,More2" + Environment.NewLine + "3,4,5,i,1,2,a,b" + Environment.NewLine; var engine = new DelimitedFileEngine <SampleType5Fields>(); // act engine.SetFieldOrder(new string[] { "Field3", "Field4", "Field5", "Inbetween1", "Field1", "Field2" }); TextReader textReader = new StringReader(data); SampleType5Fields[] res; res = engine.ReadStream(textReader, int.MaxValue); // assert Assert.AreEqual(1, res.Length); Assert.AreEqual(1, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("1", res[0].Field1); Assert.AreEqual("2", res[0].Field2); Assert.AreEqual("3", res[0].Field3); Assert.AreEqual("4", res[0].Field4); Assert.AreEqual("5", res[0].Field5); }
public void ReadStreamWithDifferentColumnOrder() { // arrange string data = "Field3,Field4,Field5,Field1,Field2" + Environment.NewLine + "3,4,5,1,2" + Environment.NewLine + "33,44,55,11,22" + Environment.NewLine; var engine = new DelimitedFileEngine <SampleType5Fields>(); // act engine.SetFieldOrder(new string[] { "Field3", "Field4", "Field5", "Field1", "Field2" }); TextReader textReader = new StringReader(data); SampleType5Fields[] res; res = engine.ReadStream(textReader, int.MaxValue); // assert Assert.AreEqual(2, res.Length); Assert.AreEqual(2, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("1", res[0].Field1); Assert.AreEqual("2", res[0].Field2); Assert.AreEqual("3", res[0].Field3); Assert.AreEqual("4", res[0].Field4); Assert.AreEqual("5", res[0].Field5); Assert.AreEqual("11", res[1].Field1); Assert.AreEqual("22", res[1].Field2); Assert.AreEqual("33", res[1].Field3); Assert.AreEqual("44", res[1].Field4); Assert.AreEqual("55", res[1].Field5); }
public void StandardImport() { // arrange var engine = new DelimitedFileEngine <FieldOrderType>(); engine.Options.IgnoreEmptyLines = true; engine.ErrorMode = ErrorMode.SaveAndContinue; // act var res = engine.ReadStream(new StringReader(data_standard), Int32.MaxValue).ToList(); // assert Assert.IsFalse(engine.ErrorManager.HasErrors); Assert.IsTrue(res.Count == 2); Validate_Data_Standard(res); }
public static void ImportInData(FmDocsFmInData doc, IObjectSpace os, TextReader reader) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(InDataImport)); engine.Options.IgnoreFirstLines = 1; InDataImport[] records = (InDataImport[])engine.ReadStream(reader); os.Delete(doc.Lines); fmCFAAccountSystem fact_system = os.FindObject <fmCFAAccountSystem>(new BinaryOperator("Code", "1000")); IList <fmCostItem> cost_items = os.GetObjects <fmCostItem>(); foreach (InDataImport rec in records) { FmDocsFmInData.Line line = doc.LinesCreate(); line.FactAccount = fact_system.Accounts.FirstOrDefault(x => x.Code == rec.AccountCode); line.FmOrder = os.FindObject <fmCOrder>(new BinaryOperator("Code", rec.OrderCode)); line.FmCostItem = cost_items.FirstOrDefault(x => x.Code == rec.CostItemCode); line.Summ = rec.Summ; } }
public static void ImportDocBSRLines(TrwBudgetPeriodDocBSR doc, IObjectSpace os, TextReader reader) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(DocBSRLineRecord)); engine.Options.IgnoreFirstLines = 1; DocBSRLineRecord[] records = (DocBSRLineRecord[])engine.ReadStream(reader); os.Delete(doc.DocBSRLines); foreach (DocBSRLineRecord rec in records) { if (!String.IsNullOrEmpty(rec.TrwSaleNomCode)) { String nom_code = rec.TrwSaleNomCode.Trim(); TrwBudgetPeriodDocBSR.LineBSR in_bsr = doc.DocBSRLines.FirstOrDefault(x => x.SaleNomCode == nom_code); if (in_bsr == null) { in_bsr = os.CreateObject <TrwBudgetPeriodDocBSR.LineBSR>(); in_bsr.SaleNomCode = nom_code; in_bsr.SaleNomenclature = os.FindObject <TrwSaleNomenclature>(new BinaryOperator("TrwCode", nom_code)); doc.DocBSRLines.Add(in_bsr); } // if (in_bsr.SaleNomenclature == null) // throw new InvalidDataException("Unknow nomencalture: " + rec.TrwSaleNomCode); in_bsr.Period00 += rec.Period00 != null ? (Decimal)rec.Period00 : 0; in_bsr.Period01 += rec.Period01 != null ? (Decimal)rec.Period01 : 0; in_bsr.Period02 += rec.Period02 != null ? (Decimal)rec.Period02 : 0; in_bsr.Period03 += rec.Period03 != null ? (Decimal)rec.Period03 : 0; in_bsr.Period04 += rec.Period04 != null ? (Decimal)rec.Period04 : 0; in_bsr.Period05 += rec.Period05 != null ? (Decimal)rec.Period05 : 0; in_bsr.Period06 += rec.Period06 != null ? (Decimal)rec.Period06 : 0; in_bsr.Period07 += rec.Period07 != null ? (Decimal)rec.Period07 : 0; in_bsr.Period08 += rec.Period08 != null ? (Decimal)rec.Period08 : 0; in_bsr.Period09 += rec.Period09 != null ? (Decimal)rec.Period09 : 0; in_bsr.Period10 += rec.Period10 != null ? (Decimal)rec.Period10 : 0; in_bsr.Period11 += rec.Period11 != null ? (Decimal)rec.Period11 : 0; in_bsr.Period12 += rec.Period12 != null ? (Decimal)rec.Period12 : 0; in_bsr.Period13 += rec.Period13 != null ? (Decimal)rec.Period13 : 0; } } }
protected void CheckAndProcessCsvFiles() { IEnumerable <string> filePaths = GetFilesMatchingBookOrderFileMask(); foreach (string filePath in filePaths) { if (FileExists(filePath)) { List <BookTitleRequest> bookTitleRequests = new List <BookTitleRequest>(); using (Stream stream = GetFileStream(filePath)) { var reader = new StreamReader(stream); var engine = new DelimitedFileEngine <BookTitleOrderModel>(); BookTitleOrderModel[] records = engine.ReadStream(reader); foreach (var record in records) { bookTitleRequests.Add(new BookTitleRequest( record.Title, record.Supplier, record.Price, record.Quantity)); } } // NOTE: Don't do this in production. There is no attempt here do even do this // in a transactional manner, and you should mark the file as processed once all // orders are placed correctly rather than delete it, and have some kind of support // for resuming at the last processed line if a fault occurs DeleteFile(filePath); foreach (BookTitleRequest bookTitleRequest in bookTitleRequests) { // Invoke the use case from the domain for each book order _addBookTitleRequestUseCase.Execute(bookTitleRequest); } } } }