private void ValidateSpreadsheetRows(DataTable batchData, AllowedData allowedData, BatchUpload batchUpload)
        {
            batchUpload.ColumnsCount = batchData.Columns.Count;
            batchUpload.TotalDataRows = batchData.Rows.Count;

            //TODO FIN-841 check columns are all present

            for (int i = 0; i < batchData.Rows.Count; i++)
            {
                DataRow row = batchData.Rows[i];

                List<UploadValidationFailure> validationFailures = ValidateUploadedRow(row, i, allowedData);
                if (validationFailures.Count == 0)
                {
                    UploadDataModel upload = new BatchRowParser(row).GetUploadDataModel();
                    upload.RowNumber = i + 2;

                    batchUpload.DataToUpload.Add(upload);
                }
                else
                {
                    foreach (UploadValidationFailure error in validationFailures)
                        batchUpload.UploadValidationFailures.Add(error);
                }
            }
        }
        public DataTable RemoveDuplicatesInBatch(DataTable batchData)
        {
            var dataToBeUploaded = new List<UploadDataModel>();

            for (int i = 0; i < batchData.Rows.Count; i++)
            {
                var row = batchData.Rows[i];
                var rowParser = new BatchRowParser(row);

                dataToBeUploaded.Add(rowParser.GetUploadDataModel());
            }

            var noDupData =
                dataToBeUploaded.GroupBy(
                    x =>
                        new
                        {
                            x.IndicatorId,
                            x.Year,
                            x.YearRange,
                            x.Quarter,
                            x.Month,
                            x.AgeId,
                            x.SexId,
                            x.AreaCode,
                            x.CategoryTypeId,
                            x.CategoryId
                        })
                    .Select(y => y.First())
                    .ToList();

            // Create new table without duplicats
            var table = new DataTable();
            table.Columns.Add("IndicatorID", typeof(double));
            table.Columns.Add("Year", typeof(double));
            table.Columns.Add("YearRange", typeof(double));
            table.Columns.Add("Quarter", typeof(double));
            table.Columns.Add("Month", typeof(double));
            table.Columns.Add("AgeID", typeof(double));
            table.Columns.Add("SexID", typeof(double));
            table.Columns.Add("AreaCode", typeof(string));
            table.Columns.Add("Count", typeof(double));
            table.Columns.Add("Value", typeof(double));
            table.Columns.Add("LowerCI", typeof(double));
            table.Columns.Add("UpperCI", typeof(double));
            table.Columns.Add("Denominator", typeof(double));
            table.Columns.Add("Denominator_2", typeof(double));
            table.Columns.Add("ValueNoteId", typeof(double));
            table.Columns.Add("CategoryTypeId", typeof(double));
            table.Columns.Add("CategoryId", typeof(double));

            foreach (var r in noDupData)
            {
                table.Rows.Add(r.IndicatorId, r.Year, r.YearRange, r.Quarter, r.Month, r.AgeId, r.SexId, r.AreaCode,
                    r.Count, r.Value, r.LowerCi, r.UpperCi, r.Denominator, r.Denominator_2, r.ValueNoteId,
                    r.CategoryTypeId, r.CategoryId);
            }
            return table;
        }
 public void TestGetUploadDataModel()
 {
     var model = new BatchRowParser(DataRow()).GetUploadDataModel();
     Assert.AreEqual(indicatorId, model.IndicatorId);
     Assert.AreEqual(year, model.Year);
     Assert.AreEqual(yearRange, model.YearRange);
     Assert.AreEqual(quarter, model.Quarter);
     Assert.AreEqual(month, model.Month);
     Assert.AreEqual(ageId, model.AgeId);
     Assert.AreEqual(sexId, model.SexId);
     Assert.AreEqual(areaCode, model.AreaCode);
     Assert.AreEqual(count, model.Count);
     Assert.AreEqual(value, model.Value);
     Assert.AreEqual(lowerCI, model.LowerCi);
     Assert.AreEqual(upperCI, model.UpperCi);
     Assert.AreEqual(denominator, model.Denominator);
     Assert.AreEqual(denominator2, model.Denominator_2);
     Assert.AreEqual(valueNoteId, model.ValueNoteId);
     Assert.AreEqual(categoryTypeId, model.CategoryTypeId);
     Assert.AreEqual(categoryId, model.CategoryId);
 }
        public BatchUpload UploadData(DataTable indicators, UploadJob job)
        {
            var batchUpload = new BatchUpload();

            for (int i = 0; i < indicators.Rows.Count; i++)
            {
                var upload = new BatchRowParser(indicators.Rows[i]).GetUploadDataModel();
                _coreDataRepository.InsertCoreData(upload.ToCoreDataSet(), job.Guid);
                batchUpload.DataToUpload.Add(upload);
            }

            var dataToUpload = batchUpload.DataToUpload;

            int uploadId = _loggingRepository.InsertUploadAudit(job.Guid, job.Username, dataToUpload.Count, job.Filename,
                WorksheetNames.BatchPholio);

            batchUpload.ShortFileName = Path.GetFileName(job.Filename);
            batchUpload.TotalDataRows = dataToUpload.Count;
            batchUpload.UploadBatchId = job.Guid;
            batchUpload.Id = uploadId;

            return batchUpload;
        }