예제 #1
0
    /// <summary>
    /// Validates the Records for Errors
    /// </summary>
    /// <param name="row">Row which needs to be validated</param>
    /// <returns>Returns the Row State(Valid or Invalid), also updates the Error Message accordingly</returns>
    public static bool ValidateData(Record row)
    {
        if(string.IsNullOrWhiteSpace(row.Account))
            {
                row.ErrorMessages = "Account Column is Empty";
            }

            if(string.IsNullOrWhiteSpace(row.Description))
            {
                row.ErrorMessages = string.Join(",", row.ErrorMessages, "Description Column is Empty");
            }

            if(string.IsNullOrWhiteSpace(row.CurrencyCode))
            {
                row.ErrorMessages = string.Join(",", row.ErrorMessages, "Currency Code Column is Empty");
            }

            if(string.IsNullOrWhiteSpace(row.Amount))
            {
                row.ErrorMessages = string.Join(",", row.ErrorMessages, "Amount Column is Empty");
            }

            decimal temp;
            if(!decimal.TryParse(row.Amount, out temp))
            {
                row.ErrorMessages = string.Join(",", row.ErrorMessages, "Amount must be a Decimal Value");
            }

            if(row.CurrencyCode.Length != 3 && CurrencyCodes.Value.FirstOrDefault(i => string.Equals(row.CurrencyCode, i, StringComparison.OrdinalIgnoreCase)) == null)
            {
                row.ErrorMessages = string.Join(",", row.ErrorMessages, "Invalide Currency Code");
            }

            if(!string.IsNullOrWhiteSpace(row.ErrorMessages))
            {
                row.ErrorMessages = row.ErrorMessages.TrimStart(',');
            }

            return string.IsNullOrWhiteSpace(row.ErrorMessages);
    }
예제 #2
0
        /// <summary>
        /// Performance the Import action based on the parser supplied
        /// </summary>
        /// <returns>Tuple with Success Count, Fail Count and List of Failed Elements</returns>
        public async Task<Tuple<int, int, List<Record>>> Process()
        {
            var ErrorList = new List<Record>();
            int createdCount = 0, failedCount = 0;
            var dataTable = CreateDataTable(_targetTableName);
            using(SqlConnection sqlConnection = new SqlConnection(_connectionString))
            {
                sqlConnection.Open();

                var sqlBulkCopy = CreateSqlBulkCopy(sqlConnection, _targetTableName);

                foreach(var item in _parser.Readline())
                {
                    Record row = new Record();
                    try
                    {
                        if(item.Count() != 4)
                        {
                            throw new ApplicationException();
                        }
                        row = new Record()
                        {
                            Account = item[0],
                            Description = item[1],
                            CurrencyCode = item[2],
                            Amount = item[3]
                        };
                    }
                    catch(Exception)
                    {
                        row = new Record()
                        {
                            ErrorMessages = string.Format("Error Processing data @ Row: {0}", createdCount + failedCount)
                        };
                        ErrorList.Add(row);
                        failedCount++;
                        continue;
                    }
                    if(DataValidator.ValidateData(row))
                    {
                        dataTable.Rows.Add(item.ToArray());
                    }
                    else
                    {
                        row.ErrorMessages += ", Row No: " + (createdCount + failedCount);
                        ErrorList.Add(row);
                        failedCount++;
                        continue;
                    }

                    createdCount++;

                    if(createdCount % _batchCount == 0)
                    {
                        await InsertDataTable(sqlBulkCopy, dataTable);
                    }
                }

                if(dataTable.Rows.Count > 0)
                {
                    await InsertDataTable(sqlBulkCopy, dataTable);
                }
            }
            return new Tuple<int, int, List<Record>>(createdCount, failedCount, ErrorList);
        }