} //prevent any incomplete objects public SimulatorAssignment(string simulatorId, SimulatorAssignmentsManager.SimulatorAssignments assignment, SimulatorAssignmentsManager.SimulatorPositions position, SimulatorAssignmentsManager.SimulationMattresses mattress, string customName) { this.SimulatorId = simulatorId; this.Assignment = assignment; this.Position = position; this.Mattress = mattress; this.CustomAssignmentName = customName; }
/// <summary> /// Loads all known assignments from the *csv file and adds them to the assignedSimulators-dictionary. /// </summary> private static Exception LoadAssignments() { if (simulatorAssignments == null) { #region Load assignments simulatorAssignments = new List <SimulatorAssignment>(); Exception ex; DataTable dtAssignments = Logging.CsvHandler.ReadCSVFile(myPathToSimulatorAssignments, out ex); if (dtAssignments == null || ex != null) { return(new Exception("Interner Fehler beim Laden der Simulatorzuweisungen.")); } if ((string)dtAssignments.Columns[1].ColumnName != "simulatorAssignments" || dtAssignments.Columns.Count != assignmentsFileHeader.Split(';').Count()) //the file is outdated --> delete all assignments and notify the user that they have to be made again { ResetAssignments(); simulatorAssignments.Clear(); } try { foreach (DataRow row in dtAssignments.Rows) //parse all assignments (there can be only one assignment per simulator id) { string simId = row[0].ToString(); SimulatorAssignmentsManager.SimulatorAssignments assignment = (SimulatorAssignmentsManager.SimulatorAssignments)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorAssignments), row[1].ToString()); SimulatorAssignmentsManager.SimulatorPositions position = (SimulatorAssignmentsManager.SimulatorPositions)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorPositions), row[2].ToString()); SimulatorAssignmentsManager.SimulationMattresses mattress = (SimulatorAssignmentsManager.SimulationMattresses)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulationMattresses), row[3].ToString()); string customName = row[4].ToString(); if (simulatorAssignments.Where(a => a.SimulatorId == simId).Count() > 0) { return(new Exception("Fehler beim Lesen der Simulatorzuweisungen: der Simulator mit der Seriennummer " + simId + " kommt mehrfach vor!")); } SimulatorAssignment newAssignment = new SimulatorAssignment(simId, assignment, position, mattress, customName); simulatorAssignments.Add(newAssignment); } } catch (Exception exc) { simulatorAssignments.Clear(); ResetAssignments(); return(exc); } #endregion } return(null); }
/// <summary> /// Validates and saves the assignments. /// </summary> private void bSave_Click(object sender, EventArgs e) { #region Validation //only 1 simulator may have the pressure mapping assignment int pressureMappingCnt = 0; if ((string)cbDeviceAssignment1.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung.ToString() || (string)cbDeviceAssignment1.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_Kaltschaum.ToString() || (string)cbDeviceAssignment1.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_NaturLatex.ToString()) { pressureMappingCnt++; } if ((string)cbDeviceAssignment2.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung.ToString() || (string)cbDeviceAssignment2.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_Kaltschaum.ToString() || (string)cbDeviceAssignment2.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_NaturLatex.ToString()) { pressureMappingCnt++; } if ((string)cbDeviceAssignment3.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung.ToString() || (string)cbDeviceAssignment3.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_Kaltschaum.ToString() || (string)cbDeviceAssignment3.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_NaturLatex.ToString()) { pressureMappingCnt++; } if ((string)cbDeviceAssignment4.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung.ToString() || (string)cbDeviceAssignment4.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_Kaltschaum.ToString() || (string)cbDeviceAssignment4.Text == SimulatorAssignmentsManager.SimulatorAssignments.Druckmessung_und_Simulation_NaturLatex.ToString()) { pressureMappingCnt++; } if (pressureMappingCnt != 1 && pressureMappingCnt != 2) { MessageBox.Show("Bitte weisen Sie maximal zwei Simulatoren die Funktion \"Druckmessung\" bzw. \"Druckmessung_und_Simulation_Kaltschaum\" und \"Druckmessung_und_Simulation_NaturLatex\" zu!", "Zuweisungen nicht korrekt", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } //check if a simulator has been assigned multiple times List <string> simulatorSerialNbrs = new List <string>() { (string)cbSerialNbr1.Text, (string)cbSerialNbr2.Text, (string)cbSerialNbr3.Text, (string)cbSerialNbr4.Text }; //delete any "Nicht verwendet" selections simulatorSerialNbrs = simulatorSerialNbrs.Where(s => s != COMBOBOX_ITEM_NOT_USED).ToList(); if (simulatorSerialNbrs.Count != simulatorSerialNbrs.Distinct().Count()) { MessageBox.Show("Es wurde mindestens ein Simulator doppelt eingerichtet. Bitte stellen Sie sicher, dass jede Seriennummer nur einmal verwendet wird!", "Simulator doppelt", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } //if a serial number was selected, all other settings have to be made as well #endregion #region Save assignments bool success = true; List <SimulatorAssignment> unchangedAssignments = SimulatorAssignmentsManager.GetSimulatorAssignments(); if (cbSerialNbr1.SelectedIndex > 0) { string simId = cbSerialNbr1.Text; SimulatorAssignmentsManager.SimulatorAssignments assignment = (SimulatorAssignmentsManager.SimulatorAssignments)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorAssignments), cbDeviceAssignment1.Text); SimulatorAssignmentsManager.SimulatorPositions position = (SimulatorAssignmentsManager.SimulatorPositions)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorPositions), cbPosition1.Text); SimulatorAssignmentsManager.SimulationMattresses mattress = (SimulatorAssignmentsManager.SimulationMattresses)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulationMattresses), cbMattress1.Text); string customName = tbCustomName1.Text; SimulatorAssignment newAssignment = new SimulatorAssignment(simId, assignment, position, mattress, customName); success &= SimulatorAssignmentsManager.AddOrUpdateAssignment(newAssignment); if (unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr1.Text).Count() == 1) //remove the assignment from the list if it was existing before { unchangedAssignments.Remove(unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr1.Text).FirstOrDefault()); } } if (cbSerialNbr2.SelectedIndex > 0) { string simId = cbSerialNbr2.Text; SimulatorAssignmentsManager.SimulatorAssignments assignment = (SimulatorAssignmentsManager.SimulatorAssignments)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorAssignments), cbDeviceAssignment2.Text); SimulatorAssignmentsManager.SimulatorPositions position = (SimulatorAssignmentsManager.SimulatorPositions)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorPositions), cbPosition2.Text); SimulatorAssignmentsManager.SimulationMattresses mattress = (SimulatorAssignmentsManager.SimulationMattresses)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulationMattresses), cbMattress2.Text); string customName = tbCustomName2.Text; SimulatorAssignment newAssignment = new SimulatorAssignment(simId, assignment, position, mattress, customName); success &= SimulatorAssignmentsManager.AddOrUpdateAssignment(newAssignment); if (unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr2.Text).Count() == 1) //remove the assignment from the list if it was existing before { unchangedAssignments.Remove(unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr2.Text).FirstOrDefault()); } } if (cbSerialNbr3.SelectedIndex > 0) { string simId = cbSerialNbr3.Text; SimulatorAssignmentsManager.SimulatorAssignments assignment = (SimulatorAssignmentsManager.SimulatorAssignments)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorAssignments), cbDeviceAssignment3.Text); SimulatorAssignmentsManager.SimulatorPositions position = (SimulatorAssignmentsManager.SimulatorPositions)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorPositions), cbPosition3.Text); SimulatorAssignmentsManager.SimulationMattresses mattress = (SimulatorAssignmentsManager.SimulationMattresses)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulationMattresses), cbMattress3.Text); string customName = tbCustomName3.Text; SimulatorAssignment newAssignment = new SimulatorAssignment(simId, assignment, position, mattress, customName); success &= SimulatorAssignmentsManager.AddOrUpdateAssignment(newAssignment); if (unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr3.Text).Count() == 1) //remove the assignment from the list if it was existing before { unchangedAssignments.Remove(unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr3.Text).FirstOrDefault()); } } if (cbSerialNbr4.SelectedIndex > 0) { string simId = cbSerialNbr4.Text; SimulatorAssignmentsManager.SimulatorAssignments assignment = (SimulatorAssignmentsManager.SimulatorAssignments)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorAssignments), cbDeviceAssignment4.Text); SimulatorAssignmentsManager.SimulatorPositions position = (SimulatorAssignmentsManager.SimulatorPositions)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulatorPositions), cbPosition4.Text); SimulatorAssignmentsManager.SimulationMattresses mattress = (SimulatorAssignmentsManager.SimulationMattresses)Enum.Parse(typeof(SimulatorAssignmentsManager.SimulationMattresses), cbMattress4.Text); string customName = tbCustomName4.Text; SimulatorAssignment newAssignment = new SimulatorAssignment(simId, assignment, position, mattress, customName); success &= SimulatorAssignmentsManager.AddOrUpdateAssignment(newAssignment); if (unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr4.Text).Count() == 1) //remove the assignment from the list if it was existing before { unchangedAssignments.Remove(unchangedAssignments.Where(a => a.SimulatorId == cbSerialNbr4.Text).FirstOrDefault()); } } if (success) { //check if an assignment has been deleted if (unchangedAssignments.Count > 0) //if there are assignments left in this list it means that the user selected "Nicht verwendet" combobox item --> delete these assignments { foreach (var assignment in unchangedAssignments) { SimulatorAssignmentsManager.DeleteAssignment(assignment); } } MessageBox.Show("Die Simulatorzuweisungen wurden erfolgreich übernommen.", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Fehler beim Speichern der Simulatorzuweisungen!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); } #endregion }