public ImportQueue ImportQueueBulkImport(ImportQueue importQueue)
        {
            using (var conn = DbHelper.GetDBConnection())
            {
                using (var bulk = new SqlBulkCopy((SqlConnection)conn)
                {
                    BulkCopyTimeout = 600
                })
                {
                    var columnIndex = 0;
                    bulk.DestinationTableName = "dbo.Fdp_ImportData";
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "FdpImportId"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "LineNumber"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Pipeline Code"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Model Year Desc"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "NSC or Importer Description (Vista Market)"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Country Description"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Derivative Code"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Derivative Description"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Trim Pack Description"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Bff Feature Code"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex++, "Feature Description"));
                    bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnIndex, "Count of Specific Order No"));

                    bulk.WriteToServer(importQueue.ImportData);
                    bulk.Close();
                }
            }
            return ImportQueueGet(importQueue.ImportQueueId.GetValueOrDefault());
        }
        public ImportQueue ImportQueueBulkImportProcess(ImportQueue importQueue)
        {
            TakeRateSummary takeRateFile = new EmptyTakeRateSummary();

            using (var conn = DbHelper.GetDBConnection())
            {
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        var para = new DynamicParameters();
                        para.Add("@FdpImportId", importQueue.ImportId, DbType.Int32);
                        //para.Add("@FdpVolumeHeaderId", null, DbType.Int32, ParameterDirection.Output);

                        if (importQueue.LineNumber.HasValue)
                        {
                            para.Add("@LineNumber", importQueue.LineNumber, DbType.Int32);
                        }

                        var results = conn.Query<TakeRateSummary>("dbo.Fdp_ImportData_Process", para,
                            commandType: CommandType.StoredProcedure, commandTimeout: 600, transaction: tran);
                        var takeRateSummaries = results as IList<TakeRateSummary> ?? results.ToList();
                        if (results != null && takeRateSummaries.Any())
                        {
                            takeRateFile = takeRateSummaries.First();
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }
                }
            }
            var retVal = ImportQueueGet(importQueue.ImportQueueId.GetValueOrDefault());
            retVal.TakeRateId = takeRateFile.TakeRateId;
            return retVal;
        }
 Task<ImportStatus> IImportDataContext.GetProcessStatus(ImportQueue importItem)
 {
     throw new NotImplementedException();
 }
 public async Task<ImportQueue> GetProcessStatus(ImportQueue importItem)
 {
     var importQueueId = importItem.ImportQueueId.GetValueOrDefault();
     
     return await Task.FromResult(_importDataStore.ImportQueueGet(importQueueId));
 }
 public ImportQueue SaveImportQueue(ImportQueue importItem)
 {
     return _importDataStore.ImportQueueSave(importItem);
 }
 private ImportQueue ProcessImportData(ImportQueue importQueue)
 {
     return _importDataStore.ImportQueueBulkImportProcess(importQueue);
 }
        private ImportQueue BulkImportDataTableToDataStore(ImportQueue importQueue)
        {
            var importColumn = importQueue.ImportData.Columns.Add("FdpImportId", typeof(Int32));
            var lineNumberColumn = importQueue.ImportData.Columns.Add("LineNumber", typeof(Int32));

            importColumn.SetOrdinal(0);
            lineNumberColumn.SetOrdinal(1);

            var lineNumber = 0;

            foreach (DataRow row in importQueue.ImportData.Rows)
            {
                row[importColumn] = importQueue.ImportId;
                row[lineNumberColumn] = ++lineNumber;
            }

            return _importDataStore.ImportQueueBulkImport(importQueue);;
        }
 private static DataTable GetImportFileAsDataTable(ImportQueue queuedItem, ImportFileSettings settings)
 {
     return ExcelReader.ReadExcelAsDataTable(queuedItem.FilePath, settings);
 }
        public async Task<ImportResult> ProcessTakeRateData(ImportQueue queuedItem)
        {
            var result = new ImportResult();

            // Redo any worktray errors first
            queuedItem = ProcessImportData(queuedItem);
            if (queuedItem.HasErrors)
            {
                return result;
            }

            queuedItem = _importDataStore.ProcessTakeRateData(queuedItem);
            result.Status = queuedItem.ImportStatus;
            result.TakeRateId = queuedItem.TakeRateId;

            return result;
        }
 public ImportResult ReprocessImportQueue(ImportQueue queuedItem)
 {
     var result = new ImportResult();
     queuedItem = ProcessImportData(queuedItem);
     result.Status = queuedItem.ImportStatus;
     
     return result;
 }
        public ImportResult ProcessImportQueue(ImportQueue queuedItem, ImportFileSettings fileSettings)
        {
            var result = new ImportResult();

            queuedItem.ImportData = GetImportFileAsDataTable(queuedItem, fileSettings);
            
            // Validate the import data with some quick sanity checks

            var validator = new ImportDataValidator();
            var validationResult = validator.Validate(queuedItem);
            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            queuedItem = BulkImportDataTableToDataStore(queuedItem);
            queuedItem = ProcessImportData(queuedItem);

            //File.Delete(queuedItem.FilePath);

            result.Status = queuedItem.ImportStatus;
            result.TakeRateId = queuedItem.TakeRateId;

            return result;
        }
        private async Task<ImportResult> ReProcessException(ImportError exception)
        {
            if (!ConfigurationSettings.GetBoolean("ReprocessImportAfterHandleError"))
                return null;

            var queuedItem = new ImportQueue()
            {
                ImportId = exception.FdpImportId,
                ImportQueueId = exception.ImportQueueId,
                LineNumber = int.Parse(exception.LineNumber)
            };
            return await Task.FromResult(DataContext.Import.ReprocessImportQueue(queuedItem));
        }
 public static ImportQueue ToImportQueue(ImportQueueDataItem dataItem)
 {
     var queue = new ImportQueue
     {
         ImportQueueId = dataItem.FdpImportQueueId,
         ImportId = dataItem.FdpImportId,
         CreatedOn = dataItem.CreatedOn,
         CreatedBy = dataItem.CreatedBy,
         UpdatedOn = dataItem.UpdatedOn,
         OriginalFileName = dataItem.OriginalFileName,
         FilePath = dataItem.FilePath,
         ImportStatus = dataItem.ImportStatus,
         ImportType = dataItem.ImportType,
         Errors = dataItem.Errors,
         ProgrammeId = dataItem.ProgrammeId,
         Gateway = dataItem.Gateway,
         Document = dataItem.Document,
         VehicleName = dataItem.VehicleName,
         VehicleAKA = dataItem.VehicleAKA,
         ModelYear = dataItem.ModelYear,
         HasErrors = dataItem.HasErrors,
         ErrorCount = dataItem.ErrorCount,
         Error = dataItem.Error,
         DocumentId = dataItem.DocumentId,
         ErrorType = dataItem.ErrorType,
         ErrorSubType = dataItem.ErrorSubType,
         Uploaded = dataItem.Uploaded
     };
     return queue;
 }
        public ImportQueue ImportQueueSave(ImportQueue importQueue)
        {
            ImportQueue retVal;

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();

                    para.Add("@CDSId", CurrentCDSID, DbType.String, size: 16);
                    para.Add("@OriginalFileName", importQueue.OriginalFileName, DbType.String);
                    para.Add("@FilePath", importQueue.FilePath, DbType.String);
                    para.Add("@FdpImportTypeId", (int)importQueue.ImportType.ImportTypeDefinition, DbType.Int32);
                    para.Add("@FdpImportStatusId", (int)importQueue.ImportStatus.ImportStatusCode, DbType.Int32);
                    para.Add("@ProgrammeId", importQueue.ProgrammeId, DbType.Int32);
                    para.Add("@Gateway", importQueue.Gateway, DbType.String);
                    para.Add("@DocumentId", importQueue.DocumentId, DbType.Int32);

                    var result = conn
                        .Query<ImportQueueDataItem>("dbo.Fdp_ImportQueue_Save", para, commandType: CommandType.StoredProcedure)
                        .FirstOrDefault();

                    result.ImportType = ImportTypeDataItem.ToImportType(result);
                    result.ImportStatus = ImportStatusDataItem.ToImportStatus(result);
                    //HydrateImportErrors(result, conn);

                    retVal = ImportQueueDataItem.ToImportQueue(result);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }

            return retVal;

        }