/// <summary> /// Parses a file into Measurements, checks the database for existence, if records are new uploads to database. /// </summary> /// <param name="filename"></param> /// <param name="columnIdentifiers"></param> /// <returns></returns> private static async Task <bool> UploadMeasurementsFromFileToDatabaseAsync(string filename, [Optional] IntervalHierarchyNames columnIdentifiers) { var measurements = await MeasurementHandler.GetMeasurementsFromFileAsync(filename, columnIdentifiers).ConfigureAwait(true); ICollection <Measurement> measurementsToRemove = new HashSet <Measurement>(); using (DescDBContext dBContext = new DescDBContext()) { foreach (var measurement in measurements) { var measurementExists = await DatabaseWorkflowHandler.FindMeasurementInDatabase(dBContext, measurement).ConfigureAwait(true); if (measurementExists) { measurementsToRemove.Add(measurement); } } } foreach (var measurement in measurementsToRemove) { measurements.Remove(measurement); } if (measurements.Count == 0) { return(false); } bool isDataUploaded; foreach (var measurement in measurements) { measurement.MeasurementData.Clear(); //Not uploading measurement report data at the moment } using (DescDBContext dbContext = new DescDBContext()) { isDataUploaded = await DatabaseWorkflowHandler.AddMeasurementsToDataBaseAsync(dbContext, measurements).ConfigureAwait(true); } return(isDataUploaded); }
public static async Task <bool> GetMeasurementIDForMeasurementFile(string file, string exportFilePath) { ICollection <Measurement> measurements = await MeasurementHandler.GetMeasurementsFromFileAsync(file).ConfigureAwait(true); using (DescDBContext dBContext = new DescDBContext()) { foreach (var measurement in measurements) { measurement.ID = await DatabaseWorkflowHandler.GetMeasurementIDAsync(dBContext, measurement).ConfigureAwait(true); } } //Column Names HashSet <string> columns = measurements.First().MeasurementData.Select(x => x.ColumnName).ToHashSet(); //Check if all measurements have the same Columns foreach (var measurement in measurements) { var compareColumns = measurement.MeasurementData.Select(x => x.ColumnName).ToHashSet(); if (!columns.SetEquals(compareColumns)) { return(false); } } //TODO: Construct new Datatable using (DataTable dataTable = new DataTable()) { foreach (var column in columns) { dataTable.Columns.Add(column); } dataTable.Columns.Add("MeasurementID").SetOrdinal(0); int currentRow = 0; foreach (var measurement in measurements) { dataTable.ImportRow(measurement.DataRow); var row = dataTable.Rows[currentRow]; row.BeginEdit(); row["MeasurementID"] = measurement.ID; row.EndEdit(); currentRow++; } //AddID column //Export measurements to File Importer.ExportDataTableAsNewFile(exportFilePath, dataTable); } return(true); }