public static void UpdateNumberOfRowsInFile(UploadJob job, DataTable dataTable, UploadJobRepository repository,
            bool isSimpleUpload)
        {
            var rowCount = 0;

            if (isSimpleUpload)
            {
                for (var i = 0; i < dataTable.Rows.Count; i++)
                {
                    var row = dataTable.Rows[i];
                    var rowParser = new UploadSimpleRowParser(row);

                    if (rowParser.DoesRowContainData == false)
                    {
                        break;
                    }

                    rowCount++;
                }
            }
            else
            {
                rowCount = dataTable.Rows.Count;
            }

            job.TotalRows = rowCount;
            repository.UpdateJob(job);


        }
 public WorkerTestBase()
 {
     CoreDataRepository = new CoreDataRepository();
     LoggingRepository = new LoggingRepository();
     JobRepository = new UploadJobRepository();
     ErrorRepository = new UploadJobErrorRepository();
 }
 public void Init()
 {
     _uploadJobRepository = new UploadJobRepository();
     _guid = Guid.NewGuid();
 }
        public void ProcessJob(UploadJob job, IWorksheetNameValidator nameValidator,
            IBatchWorksheetDataProcessor processor, IExcelFileReader excelFileReader)
        {
            try
            {
                _jobRepository = new UploadJobRepository();
                _jobErrorRepository = new UploadJobErrorRepository();
                var batchUpload = ToBatchUpload(job);
                jobStatus = new StatusHelper(_jobRepository, _logger);

                _logger.Info("Job# {0} current status is {1} ", job.Guid, job.Status);

                // If user wants to override duplications 
                if (job.Status == UploadJobStatus.ConfirmationGiven)
                {
                    jobStatus.InProgress(job);
                    // Read indicators in datatable
                    var batchDataTable = GetBatchData(excelFileReader);
                    // Save the total number of rows in file
                    WorkerHelper.UpdateNumberOfRowsInFile(job, batchDataTable, _jobRepository, true);
                    // 
                    //Perform validation once again to get the list 
                    // of duplicate rows in database
                    //
                    processor.Validate(batchDataTable, batchUpload);
                    // Remove duplications in file
                    CheckDuplicateRowsInWorksheet(job, batchUpload, ref batchDataTable);
                    // Archive rows
                    processor.ArchiveDuplicates(batchUpload.DuplicateRowInDatabaseErrors, job);
                    // Upload data to core data set
                    UploadDataToCoreDataSet(job, processor, batchDataTable);
                }
                else // If we have a new job
                {
                    jobStatus.InProgress(job);

                    // Get worksheets from file
                    var worksheets = excelFileReader.GetWorksheets();

                    UpdateJobProgress(job, ProgressStage.ValidatingWorksheets);

                    // Check worksheet names are correct
                    var worksheetsOk = CheckWorksheets(job, nameValidator, worksheets);
                    if (!worksheetsOk) return;


                    var batchDataTable = GetBatchData(excelFileReader);

                    // Save the total number of rows in file
                    WorkerHelper.UpdateNumberOfRowsInFile(job, batchDataTable, _jobRepository, true);

                    UpdateJobProgress(job, ProgressStage.ValidatingData);

                    processor.Validate(batchDataTable, batchUpload);

                    var indicatorIdsInBatch = processor.GetIndicatorIdsInBatch();

                    UpdateJobProgress(job, ProgressStage.CheckingPermission);

                    // Check user permission for indicators
                    var permissionsOk = CheckPermission(job, indicatorIdsInBatch);
                    if (!permissionsOk) return;

                    UpdateJobProgress(job, ProgressStage.CheckingDuplicationInFile);

                    // Check for duplications in file
                    CheckDuplicateRowsInWorksheet(job, batchUpload, ref batchDataTable);

                    // Check validation errors
                    var validationOk = CheckValidationFailures(job, batchUpload);
                    if (!validationOk) return;

                    UpdateJobProgress(job, ProgressStage.CheckingDuplicationInDb);

                    // Check for duplications database rows
                    var haveDuplicates = CheckDuplicateRowsInDatabase(job, batchUpload);
                    if (haveDuplicates) return;

                    UploadDataToCoreDataSet(job, processor, batchDataTable);
                }
            }
            catch (Exception ex)
            {
                jobStatus.UnexpectedError(job);
                _logger.Error(ex);
            }
        }
 public StatusHelper(UploadJobRepository jobRepository, Logger logger)
 {
     _logger = logger;
     _jobRepository = jobRepository;
 }
        public void ProcessJob(UploadJob job, IWorksheetNameValidator nameValidator,
            ISimpleWorksheetDataProcessor processor, IExcelFileReader excelFileReader)
        {
            try
            {
                _jobRepository = new UploadJobRepository();
                _jobErrorRepository = new UploadJobErrorRepository();
                // Create SimpleUpload object from job
                var simpleUpload = ToSimpleUpload(job);
                jobStatus = new StatusHelper(_jobRepository, _logger);
                _logger.Info("Job# {0} current status is {1} ", job.Guid, job.Status);

                if (job.Status == UploadJobStatus.ConfirmationGiven)
                {
                    jobStatus.InProgress(job);
                    // Get indicator details worksheet as data table
                    var indicatorDetails = excelFileReader.GetIndicatorDetails();
                    // Get pholio data worksheet as data table
                    var pholioData = excelFileReader.GetPholioData();
                    // Save the total number of rows in file
                    WorkerHelper.UpdateNumberOfRowsInFile(job, pholioData, _jobRepository, true);
                    // Validate the Data            
                    processor.Validate(indicatorDetails, pholioData, simpleUpload);
                    // Remove duplicate rows
                    CheckDuplicateRowsInWorksheet(job, simpleUpload, ref pholioData);
                    // Archive rows
                    processor.ArchiveDuplicates(simpleUpload.DuplicateRowInDatabaseErrors, job);
                    // Upload data to core data set
                    UploadDataToCoreDataSet(job, processor, indicatorDetails, pholioData);
                }
                else
                {
                    // Update the job status to in progress            
                    jobStatus.InProgress(job);
                    // Get worksheets from file
                    var worksheets = excelFileReader.GetWorksheets();
                    // Check worksheet names are correct
                    var worksheetsOk = CheckWorksheets(job, worksheets, nameValidator);
                    if (!worksheetsOk) return;
                    //  Get indicator details worksheet as data table
                    var indicatorDetails = excelFileReader.GetIndicatorDetails();
                    // Get pholio data worksheet as data table
                    var pholioData = excelFileReader.GetPholioData();
                    // Save the total number of rows in file
                    WorkerHelper.UpdateNumberOfRowsInFile(job, pholioData, _jobRepository, true);
                    // Validate the Data            
                    processor.Validate(indicatorDetails, pholioData, simpleUpload);

                    // Check user permission for indicator
                    var indicatorIds = new List<int> { simpleUpload.IndicatorId };
                    var permissionsOk = CheckPermission(indicatorIds, job);
                    if (!permissionsOk) return;

                    // Check for duplications in file, if there will be any duplicate rows
                    // we will remove them.
                    CheckDuplicateRowsInWorksheet(job, simpleUpload, ref pholioData);

                    // Check validation errors
                    var validationOk = CheckValidationFailures(job, simpleUpload);
                    if (!validationOk) return;

                    // Check for duplications database rows
                    var haveDuplicates = CheckDuplicateRowsInDatabase(job, simpleUpload);
                    if (haveDuplicates) return;

                    // Upload to DB
                    UploadDataToCoreDataSet(job, processor, indicatorDetails, pholioData);
                }
            }
            catch (Exception ex)
            {
                jobStatus.UnexpectedError(job);
                _logger.Error(ex);
            }
        }