Ejemplo n.º 1
0
        private IList <ElementItem> GetElements(IExcelFileReader reader, IList <DesignItem> designs)
        {
            var result = new List <ElementItem>();

            reader.SetActiveSheet("ElementData");

            while (reader.ReadRow())
            {
                if (reader.GetColumnCount() < 3)
                {
                    continue;
                }

                int material_id = 0;
                int design_id   = 0;
                int max_usage   = 0;

                if (!reader.ReadColumnAsInt(0, out material_id))
                {
                    continue;
                }

                if (!reader.ReadColumnAsInt(1, out design_id))
                {
                    continue;
                }

                if (!reader.ReadColumnAsInt(2, out max_usage))
                {
                    continue;
                }

                result.Add(new ElementItem()
                {
                    MaterialID            = material_id,
                    MaxUsage              = max_usage,
                    DesignAndOrientations = designs.Where(x => x.DesignID == design_id)
                });
            }

            return(result);
        }
Ejemplo n.º 2
0
        private List <DesignItem> GetDesignItems(IExcelFileReader reader)
        {
            reader.SetActiveSheet("DesignData");

            var designs = new List <DesignItem>();

            while (reader.ReadRow())
            {
                if (reader.GetColumnCount() < 7)
                {
                    continue;
                }

                int     design_id = 0;
                string  blname    = "";
                int     size_x    = 0;
                int     size_z    = 0;
                string  transform = "";
                decimal offset_x  = 0;
                decimal offset_z  = 0;

                if (!reader.ReadColumnAsInt(0, out design_id))
                {
                    continue;
                }

                if (!reader.ReadColumnAsText(1, out blname))
                {
                    continue;
                }

                if (!reader.ReadColumnAsInt(2, out size_x))
                {
                    continue;
                }

                if (!reader.ReadColumnAsInt(3, out size_z))
                {
                    continue;
                }

                if (!reader.ReadColumnAsText(4, out transform))
                {
                    continue;
                }

                if (!reader.ReadColumnAsDecimal(5, out offset_x))
                {
                    continue;
                }

                if (!reader.ReadColumnAsDecimal(6, out offset_z))
                {
                    continue;
                }

                designs.Add(new DesignItem()
                {
                    DesignID      = design_id,
                    BricklinkName = blname,
                    SizeX         = size_x,
                    SizeZ         = size_z,
                    Transform     = transform,
                    OffsetX       = (float)offset_x,
                    OffsetZ       = (float)offset_z,
                });

                if (reader.GetColumnCount() >= 10)
                {
                    if (!reader.ReadColumnAsText(7, out transform))
                    {
                        continue;
                    }

                    if (!reader.ReadColumnAsDecimal(8, out offset_x))
                    {
                        continue;
                    }

                    if (!reader.ReadColumnAsDecimal(9, out offset_z))
                    {
                        continue;
                    }

                    designs.Add(new DesignItem()
                    {
                        DesignID      = design_id,
                        BricklinkName = blname,
                        SizeX         = size_z,
                        SizeZ         = size_x, // Flipped 90 degree
                        Transform     = transform,
                        OffsetX       = (float)offset_x,
                        OffsetZ       = (float)offset_z,
                    });
                }
            }

            return(designs);
        }
        private static DataTable GetBatchData(IExcelFileReader excelFileReader)
        {
            // Get batch indicator data from worksheet as data table 
            DataTable batchDataTable = excelFileReader.GetBatchData();

            // Delete non-valid rows
            foreach (var row in batchDataTable.Select("IndicatorId is null"))
            {
                row.Delete();
            }

            batchDataTable.AcceptChanges();
            return batchDataTable;
        }
        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 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);
            }
        }