public void WriteDelimited(IEnumerable <dynamic> items, IEnumerable <dynamic> header, IEnumerable <dynamic> trailer, string path, SilverFileFormat properties, char delim = ',') { using (var writer = new StreamWriter(path)) { WriteFile(header, properties.header, delim, writer); WriteFile(items, properties.record, delim, writer); WriteFile(trailer, properties.trailer, delim, writer); } }
public Tuple <List <dynamic>, List <Row>, bool> ExtractPipe(string path, SilverFileFormat properties, char delim) { return(ExtractDelimited(path, properties, delim)); }
private Tuple <List <dynamic>, List <Row>, bool> ExtractDelimited(string path, SilverFileFormat properties, char v = ',') { var recordDictionary = properties.record.Select((s, i) => new { s, i }) .ToDictionary(x => x.i, x => x.s); var headerDictionary = properties.header.Select((s, i) => new { s, i }) .ToDictionary(x => x.i, x => x.s); var trailerDictionary = properties.trailer.Select((s, i) => new { s, i }) .ToDictionary(x => x.i, x => x.s); List <Row> failedFieldsList = new List <Row>(); bool successful = true; //string test = "a|b||c|||d"; int index = 0; var headerProperties = properties.header; int recordOffset = headerProperties.FindIndex(x => x.Source == "RECORD_COUNT"); int? totalRecords = null; string message = null; List <dynamic> record = new List <dynamic>(); List <dynamic> trailer = new List <dynamic>(); List <dynamic> header = new List <dynamic>(); foreach (var line in System.IO.File.ReadLines(path)) { int dictionaryIndex = 0; dynamic obj = new ExpandoObject(); List <IField> failedRecords = new List <IField>(); var delimited = line.Split(v); bool isValid = true; foreach (var item in delimited) { if (index == 0 && headerDictionary.Count > 0) { if (dictionaryIndex == recordOffset) { totalRecords = Convert.ToInt32(item); } AddProperty(obj, headerDictionary[dictionaryIndex].Destination, item); } else if (totalRecords.HasValue && index == totalRecords - 1) { AddProperty(obj, trailerDictionary[dictionaryIndex].Destination, item); } else { if (String.IsNullOrEmpty(recordDictionary[dictionaryIndex].Type)) { isValid = false; message = "Type not specified in meta data definition"; } else if (!String.IsNullOrEmpty(recordDictionary[dictionaryIndex].Function) && !String.IsNullOrEmpty(recordDictionary[dictionaryIndex].Format)) { Type thisType = this.GetType(); MethodInfo theMethod = thisType.GetMethod(recordDictionary[dictionaryIndex].Function); object[] parametersArray = { item, recordDictionary[dictionaryIndex].Format, recordDictionary[dictionaryIndex].Type }; var val = (Tuple <object, bool, string>)theMethod.Invoke(this, parametersArray); isValid = val.Item2; message = val.Item3; } AddProperty(obj, recordDictionary[dictionaryIndex].Destination, item); } if (!isValid) { successful = false; Field failedField = new Field() { Name = recordDictionary[dictionaryIndex].Destination, Message = message, Value = item, MetaData = recordDictionary[dictionaryIndex] }; failedRecords.Add(failedField); } dictionaryIndex++; } if (index == 0 && headerDictionary.Count > 0) { header.Add(obj); } else if (totalRecords.HasValue && index == totalRecords - 1) { trailer.Add(obj); } else { record.Add(obj); } if (failedRecords.Count > 0) { Row failedRow = new Row() { Fields = failedRecords, RowNumber = index }; failedFieldsList.Add(failedRow); } index++; } return(new Tuple <List <dynamic>, List <Row>, bool>(record, failedFieldsList, successful)); }
public void WritePipe(IEnumerable <dynamic> items, IEnumerable <dynamic> header, IEnumerable <dynamic> trailer, string path, SilverFileFormat properties) { WriteDelimited(items, header, trailer, path, properties, '|'); }