Example #1
0
        private void CopyRow(BatchJob batchJob, string[] sourceRow, DataTable targetTable, IEnumerable <FieldMapping> mappings, IBatchValidator validator)
        {
            try
            {
                // skip empty rows
                if (IsRowNull(sourceRow))
                {
                    return;
                }

                var targetRow = targetTable.NewRow();

                foreach (var mapping in mappings)
                {
                    if (!(mapping.Index.HasValue || mapping.Default.HasValue))
                    {
                        continue;
                    }

                    if (mapping.Default.HasValue)
                    {
                        var defaultValue = GetDefault(batchJob, mapping);
                        targetRow[mapping.Name] = defaultValue;
                    }
                    else
                    {
                        var sourceValue = sourceRow[mapping.Index.Value];

                        sourceValue.TryConvert(mapping.DataType, out var value);

                        var translator = _factory.ResolveTranslator(mapping.TranslatorType);
                        if (translator != null)
                        {
                            value = translator.Translate(mapping.TranslatorSource, value);
                        }

                        targetRow[mapping.Name] = value ?? DBNull.Value; // null must be set as DBNull
                    }
                }

                // ValidateRow should throw exception if can't continue
                validator?.ValidateRow(batchJob, targetRow);

                targetTable.Rows.Add(targetRow);
            }
            catch (DuplicateException dex)
            {
                _logger.LogError(dex, dex.Message);

                batchJob.Duplicates++;

                bool quit = batchJob.DuplicateHandling == BatchError.Quit;
                if (quit)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.GetBaseException().Message);

                batchJob.Errors++;

                bool quit = batchJob.ErrorHandling == BatchError.Quit ||
                            batchJob.Errors > batchJob.MaxErrors;

                if (quit)
                {
                    throw;
                }
            }
        }
        private void CopyRow(BatchJob batchJob, DataRow sourceRow, DataTable targetTable, IEnumerable<FieldMapping> mappings, IBatchValidator validator)
        {
            try
            {
                // skip empty rows
                if (IsRowNull(sourceRow))
                    return;

                var targetRow = targetTable.NewRow();

                foreach (var mapping in mappings)
                {
                    if (!(mapping.Index.HasValue || mapping.Default.HasValue))
                        continue;

                    var sourceValue = mapping.Index.HasValue
                        ? sourceRow[mapping.Index.Value]
                        : GetDefault(batchJob, mapping);

                    var translator = _factory.ResolveTranslator(mapping.TranslatorType);
                    if (translator != null)
                        sourceValue = translator.Translate(mapping.TranslatorSource, sourceValue);

                    targetRow[mapping.Name] = sourceValue ?? DBNull.Value; // null must be set as DBNull
                }

                // ValidateRow should throw exception if can't continue
                if (validator != null)
                    validator.ValidateRow(batchJob, targetRow);

                targetTable.Rows.Add(targetRow);
            }
            catch (DuplicateException dex)
            {
                Logger.Error()
                    .Message(dex.Message)
                    .Exception(dex)
                    .Write();

                batchJob.Duplicates++;

                bool quit = batchJob.DuplicateHandling == BatchError.Quit;
                if (quit)
                    throw;
            }
            catch (Exception ex)
            {
                Logger.Error()
                    .Message(ex.GetBaseException().Message)
                    .Exception(ex)
                    .Write();

                batchJob.Errors++;

                bool quit = batchJob.ErrorHandling == BatchError.Quit ||
                    batchJob.Errors > batchJob.MaxErrors;

                if (quit)
                    throw;
            }
        }