Esempio n. 1
0
        private void LoadRow(Dictionary <string, object> row, ref int errorCount)
        {
            string curTransformation = "";

            long rowNo = -1;

            if (row != null && row["#row"] != null)
            {
                rowNo = (long)row["#row"];
            }

            try
            {
                foreach (var tranKV in _transformationPipe)
                {
                    var tran = tranKV.Value;
                    curTransformation = tranKV.Key;
                    tran.Transform(row);

                    if (row.ContainsKey("#DROP") && (bool)row["#DROP"] == true)
                    {
                        _rowLogAction?.Invoke(false, true, rowNo, null);
                        return;
                    }
                }
                _rowLogAction?.Invoke(true, false, rowNo, null);
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("Error Transforming Row {0} in {2} : {1}", rowNo, ex.Message, curTransformation);

                _rowLogAction?.Invoke(false, false, rowNo, errMsg);

                Interlocked.Increment(ref errorCount);
                if (_errorsAllowed != -1 && errorCount >= _errorsAllowed)
                {
                    _logger.Fatal(string.Format("Pipe {0}: {1}", _pipeNumber, errMsg));
                    throw (new TransformationPipeException(string.Format("Number of Errors has Exceeded the limit {0}", _errorsAllowed)));
                }
                else
                {
                    _logger.Warn(string.Format("Pipe {0}: {1}", _pipeNumber, errMsg));
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputQueue"></param>
        /// <param name="errorCount"></param>
        /// <param name="ct"></param>
        /// <param name="logger"></param>
        /// <param name="rowLogAction"></param>
        public void Load(BlockingCollection <Dictionary <string, object> > inputQueue, ref int errorCount, CancellationToken ct, ILogger logger, RowLogAction rowLogAction)
        {
            using (CsvReader csv = new CsvReader(new StreamReader(_fileName), _hasHeader, _delimeter))
            {
                LookupFieldIndexFromName(csv);

                while (csv.ReadNextRecord() && !ct.IsCancellationRequested)
                {
                    try
                    {
                        var row = new Dictionary <string, object>();

                        foreach (var field in _fields)
                        {
                            try
                            {
                                row.Add(field.Map, field.Converter(csv[field.Index.Value]));
                            }
                            catch (Exception ex)
                            {
                                throw new FieldConversionException(string.Format("Invalid field conversion {0} ({1})", field.Map, field.Index.Value), ex);
                            }
                        }

                        row.Add("#row", csv.CurrentRecordIndex);

                        inputQueue.Add(row);
                    }
                    catch (Exception ex)
                    {
                        rowLogAction?.Invoke(false, false, csv.CurrentRecordIndex, ex.Message);

                        Interlocked.Increment(ref errorCount);
                        if (_errorsAllowed != -1 && errorCount >= _errorsAllowed)
                        {
                            throw (new ReaderException(string.Format("Number of Errors has Exceeded the limit {0}", _errorsAllowed)));
                        }
                    }
                }
            }
        }