/// <summary> /// Update the data entry area. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lbxDataSets_SelectedIndexChanged(object sender, EventArgs e) { MovingAverageDS ds = lbxDataSets.SelectedItem as MovingAverageDS; if (ds == null) { activeId = -1; } else { activeId = ds.id; } UpdateValues(ds == null); if (ds != null) { txtName.Text = ds.name; nudWindowSize.Value = ds.windowSize; lbxValues_SelectedIndexChanged(this, null); } else { ClearDisplay(); } }
/// <summary> /// Calculate the selected dataset. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCalculate_Click(object sender, EventArgs e) { if (activeId == -1) { MessageBox.Show("No data selected.", "Cannot open data", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string outputFileName; sfdSave.ShowDialog(); outputFileName = sfdSave.FileName; MovingAverageDS ds = DSAccess.Operation(DSOperation.Find, new MovingAverageDS { id = activeId }, DSFindBy.Id).FirstOrDefault(); try { File.WriteAllText(outputFileName, CalculateDSToString.OutputCalculation(ds)); } catch (Exception ex) { string title = "Could not save output."; MessageBox.Show(ex.Message, title, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Handle a database operation for the datasets. /// </summary> /// <param name="operation">The operation to perform</param> /// <param name="data">The data to be used for the operation. Ignored by GetAll operation</param> /// <param name="findBy">The method by which to find the data. Only used by the Find operation</param> /// <returns>The output of the operation</returns> public static MovingAverageDS[] Operation(DSOperation operation, MovingAverageDS data = null, DSFindBy findBy = DSFindBy.Id) { MovingAverageDS[] toReturn = null; using (DatasetContext db = new DatasetContext()) { switch (operation) { case DSOperation.Add: toReturn = new MovingAverageDS[] { Add(data, db) }; break; case DSOperation.Delete: Delete(data, db); break; case DSOperation.Modify: Modify(data, db); break; case DSOperation.Find: toReturn = Find(data, findBy, db); break; case DSOperation.GetAll: toReturn = GetAll(db); break; } db.SaveChanges(); } return(toReturn); }
/// <summary> /// Find all matching datasets within the database based on the /// given dataset and the DSFindBy method. /// </summary> /// <param name="data">The dataset is used as the sought value</param> /// <param name="findBy">The method to search by</param> /// <param name="db">The active database context to seach</param> /// <returns>The datasets that match</returns> private static MovingAverageDS[] Find(MovingAverageDS data, DSFindBy findBy, DatasetContext db) { MovingAverageDS[] toReturn = null; switch (findBy) { case DSFindBy.Data: toReturn = (from MovingAverageDS ds in db.dataSets where data.data == ds.data select ds).ToArray(); break; case DSFindBy.Id: toReturn = (from MovingAverageDS ds in db.dataSets where data.id == ds.id select ds).ToArray(); break; case DSFindBy.WindowSize: toReturn = (from MovingAverageDS ds in db.dataSets where data.windowSize == ds.windowSize select ds).ToArray(); break; case DSFindBy.Name: toReturn = (from MovingAverageDS ds in db.dataSets where data.name == ds.name select ds).ToArray(); break; } return(toReturn); }
/// <summary> /// Get the average of a dataset and format it to be human readable. /// </summary> /// <param name="ds">The dataset to calculate</param> /// <returns>The human readable output of the calculation</returns> public static string OutputCalculation(MovingAverageDS ds) { StringBuilder toReturn = new StringBuilder(); OutputCalculation(ds, toReturn); return(toReturn.ToString()); }
/// <summary> /// Get the average of a dataset and format it to be human readable. /// </summary> /// <param name="ds">The dataset to calculate</param> /// <param name="builder">The StringBuilder to output to</param> public static void OutputCalculation(MovingAverageDS ds, StringBuilder builder) { double[] output = MovingAverage.GetMovingAverage(ds.windowSize, ds.data); builder.Append("Name: "); builder.AppendLine(ds.name); builder.Append("Window Size: "); builder.AppendLine(ds.windowSize.ToString()); builder.Append("Data: "); ArrayToString(ds.data, builder); builder.AppendLine(); builder.Append("Averages: "); ArrayToString(output, builder); builder.AppendLine(); }
/// <summary> /// Modify the database. Uses the data's ID to determine which entry /// to modify in the database. /// </summary> /// <param name="data">The data to use for the modification</param> /// <param name="db">The active db context to modify the data from</param> private static void Modify(MovingAverageDS data, DatasetContext db) { MovingAverageDS toChange = (from d in db.dataSets where (d.id == data.id) select d).FirstOrDefault(); toChange.data = data.data; toChange.windowSize = data.windowSize; toChange.name = data.name; }
/// <summary> /// Delete a dataset from the database. /// </summary> /// <param name="data">The dataset to delete. Only reads the ID</param> /// <param name="db">The active db context to delete the data from</param> private static void Delete(MovingAverageDS data, DatasetContext db) { MovingAverageDS ds = Find(data, DSFindBy.Id, db)[0]; if (ds == null) { } else { db.dataSets.Remove(ds); } }
/// <summary> /// Delete the currently selected dataset. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, EventArgs e) { if (activeId != -1) { MovingAverageDS ds = lbxDataSets.SelectedItem as MovingAverageDS; if (ds != null) { DSAccess.Operation(DSOperation.Delete, ds); ClearDisplay(); activeId = -1; UpdateDatasets(); } else { MessageBox.Show("No data selected...", "Delete Operation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Save the currently selected dataset. If no dataset is /// selected, uses the data entry area to create a dataset. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { MovingAverageDS ds = new MovingAverageDS(); ds.name = txtName.Text; ds.windowSize = (int)nudWindowSize.Value; ds.data = _values.ToArray(); ds.id = activeId; if (activeId == -1) { ds = DSAccess.Operation(DSOperation.Add, ds)[0]; SelectDSById(ds.id); } else { DSAccess.Operation(DSOperation.Modify, ds); } UpdateDatasets(); }
/// <summary> /// Add the given data to the database. /// </summary> /// <param name="data">The data to add to the database</param> /// <param name="db">The active db context to add the data to</param> /// <returns>The data added</returns> private static MovingAverageDS Add(MovingAverageDS data, DatasetContext db) { return(db.dataSets.Add(data)); }