public void validateDisk() { Disk hdd = new Disk(); DiskValidator validator = new DiskValidator(); hdd.Description = "Dies ist ein Test"; hdd.Capacity = 50; hdd.Ssd = false; hdd.Inch = -3.5; Assert.AreEqual(false, validator.CheckConsistency(hdd)); }
/// <summary> /// Prüft die Konsistenz der Attribute der Entität 'Festplatte' /// </summary> /// <param name="entity">Das Objekt, welches geprüft wird</param> /// <returns>true: Objekt Konsistent, false: Objekt fehlerhaft</returns> public bool CheckConsistency(Disk entity) { bool result = true; if (entity.Capacity == 0) { result = false; } if(entity.Inch <= 0) { result = false; } return result; }
/// <summary> /// Verändert einen bestehenden Datensatz der Entität 'Festplatte' in der Datenbank. /// Ermittelt auch nicht mehr genutzte Referenzen und löscht diese aus der Beziehungstabelle. /// </summary> /// <param name="entity">Die veränderte Entität</param> public void Update(Disk entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); MySqlCommand interfaceCommand = connection.CreateCommand(); string usedInterfaces = ""; command.CommandText = "UPDATE `" + this.GetTableName() + "` SET `Beschreibung`='" + entity.Description + "', `Kapazität`=" + entity.Capacity + ", `SSD`='" + Convert.ToInt32(entity.Ssd) + "', `Zoll`='" + entity.Inch.ToString().Replace(',','.') + "', `ID_Hersteller`=" + entity.Producer.Id + " WHERE id = " + entity.Id; connection.Open(); for (int i = 0; i < entity.PhysicalInterfaces.Count; i++) { if (this.IsInterfaceInUse(entity.PhysicalInterfaces[i].PhysicalInterface, entity)) { interfaceCommand.CommandText = "UPDATE `" + this.GetTableName() + "_schnittstelle` SET `Anzahl`=" + entity.PhysicalInterfaces[i].Number + " WHERE `ID_Festplatte` = " + entity.Id + " AND `ID_Schnittstelle` = " + entity.PhysicalInterfaces[i].PhysicalInterface.Id; } else { interfaceCommand.CommandText = "INSERT INTO `" + this.GetTableName() + "_schnittstelle`(`ID_Festplatte`, `ID_Schnittstelle`, `Anzahl`) " + "VALUES (" + entity.Id + "," + entity.PhysicalInterfaces[i].PhysicalInterface.Id + "," + entity.PhysicalInterfaces[i].Number + ")"; } interfaceCommand.ExecuteNonQuery(); usedInterfaces += entity.PhysicalInterfaces[i].PhysicalInterface.Id; if (i != entity.PhysicalInterfaces.Count - 1) { usedInterfaces += ","; } } if (usedInterfaces.Length > 0) { interfaceCommand.CommandText = "DELETE FROM `" + this.GetTableName() + "_schnittstelle` WHERE `ID_Festplatte` = " + entity.Id + " AND `ID_Schnittstelle` NOT IN (" + usedInterfaces + ")"; } else { interfaceCommand.CommandText = "DELETE FROM `" + this.GetTableName() + "_schnittstelle` WHERE `ID_Festplatte` = " + entity.Id; } interfaceCommand.ExecuteNonQuery(); command.ExecuteNonQuery(); connection.Close(); }
/// <summary> /// Konstruktor: Setzt die Wert für die Initialisierung des Dialoges /// </summary> /// <param name="entity">Objekt einer Festplatte</param> public CreateDisk(Disk entity = null) { InitializeComponent(); this.DiskCapacityUnit.SelectedIndex = 0; this.GetProducers(); this.entity = entity; if (entity != null) { this.SetAllFields(); this.entity = entity; this.isAvailable = true; } else { this.entity = new Disk(); this.isAvailable = false; } }
/// <summary> /// Speichert ein Objekt der Entität 'Festplatte' in der Datenbank /// </summary> /// <param name="entity">Das Objekt, welches gespeichert wird</param> public void Save(Disk entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO `" + this.GetTableName() + "`(`Beschreibung`, `Kapazität`, `SSD`, `Zoll`, `ID_Hersteller`) " + "VALUES ('" + entity.Description + "'," + entity.Capacity + ",'" + Convert.ToInt32(entity.Ssd) + "','" + entity.Inch.ToString().Replace(',','.') + "'," + entity.Producer.Id + ")"; Console.WriteLine(command.CommandText); connection.Open(); command.ExecuteNonQuery(); connection.Close(); for (int i = 0; i < entity.PhysicalInterfaces.Count; i++) { command.CommandText = "INSERT INTO `" + this.GetTableName() + "_schnittstelle` (`ID_Festplatte`, `ID_Schnittstelle`, `Anzahl`) VALUES " + "(" + this.GetLastEntity<Disk>().Id + "," + entity.PhysicalInterfaces[i].PhysicalInterface.Id + "," + entity.PhysicalInterfaces[i].Number + ")"; connection.Open(); command.ExecuteNonQuery(); connection.Close(); } }
/// <summary> /// Prüft, ob ein Interface bereits der Entität zugewiesen ist /// </summary> /// <param name="physicalInterface">Die Schnittstelle auf welche geprüft wird</param> /// <param name="entity">Die Entität, welche geprüft wird</param> /// <returns>true, wenn eine Schnittstelle zugewiesen wurde</returns> private bool IsInterfaceInUse(PhysicalInterface physicalInterface, Disk entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); bool result; connection.Open(); command.CommandText = "SELECT * FROM `" + this.GetTableName() + "_schnittstelle` WHERE `ID_Festplatte` = " + entity.Id + " AND `ID_Schnittstelle` = " + physicalInterface.Id; result = command.ExecuteReader().HasRows; connection.Close(); return result; }
/// <summary> /// Liest alle Beziehungen zu der Entität 'Schnittstelle' aus der Datenbank /// </summary> /// <param name="entity">Die Festplatte, zu welcher die Schnittstellen ermittelt werden</param> /// <returns>Liste von PhysicalInterfaceWithCount</returns> private List<PhysicalInterfaceWithCount> GetPhysicalInterfaces(Disk entity) { List<PhysicalInterfaceWithCount> physicalInterfaces = new List<PhysicalInterfaceWithCount>(); PhysicalInterfaceDataAccess physicalInterfaceDataAccess = new PhysicalInterfaceDataAccess(); MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM `" + this.GetTableName() + "_schnittstelle` WHERE id_festplatte = " + entity.Id; connection.Open(); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { PhysicalInterface physicalInterface = physicalInterfaceDataAccess.GetEntityById<PhysicalInterface>(Int32.Parse(reader.GetValue(1).ToString())); uint count = uint.Parse(reader.GetValue(2).ToString()); physicalInterfaces.Add(new PhysicalInterfaceWithCount(physicalInterface, count)); } return physicalInterfaces; }
/// <summary> /// Mappt einen Datensatz aus der Datenbank auf ein Objekt vom Typ 'Festplatte' /// </summary> /// <param name="reader">Der Datensatz, welcher gemappt wird</param> /// <returns>Disk</returns> protected override object MapToEntity(MySqlDataReader reader) { Disk disk = new Disk(); ProducerDataAccess producerDataAccess = new ProducerDataAccess(); disk.Id = Int32.Parse(reader.GetValue(0).ToString()); disk.Description = reader.GetValue(1).ToString(); disk.Capacity = ulong.Parse(reader.GetValue(2).ToString()); disk.Ssd = Boolean.Parse(reader.GetValue(3).ToString()); disk.Inch = Double.Parse(reader.GetValue(4).ToString()); disk.Producer = producerDataAccess.GetEntityById<Producer>(Int32.Parse(reader.GetValue(5).ToString())); disk.PhysicalInterfaces = this.GetPhysicalInterfaces(disk); return disk; }
public void getDiskFromDatabase() { DiskDataAccess dataAccess = new DiskDataAccess(); ProducerDataAccess producerDataAccess = new ProducerDataAccess(); PhysicalInterfaceDataAccess physicalInterfaceDataAccess = new PhysicalInterfaceDataAccess(); Disk hdd = new Disk(); hdd.Description = "Dies ist ein Test"; hdd.Capacity = 1000; hdd.Ssd = false; hdd.Inch = 3.5; hdd.Producer = producerDataAccess.GetLastEntity<Producer>(); hdd.AddPhysicalInterface(new PhysicalInterfaceWithCount(physicalInterfaceDataAccess.GetLastEntity<PhysicalInterface>(), 3)); dataAccess.Save(hdd); Disk dbHDD = dataAccess.GetLastEntity<Disk>(); Assert.AreEqual(hdd.Capacity, dbHDD.Capacity); }
/// <summary> /// Wandelt ein Objekt der Entität 'Festplatte' in eine grafisches Objekt. /// Übersetzt englische Begriffe und zählt alle Schnittstellen in einer Liste auf. /// </summary> /// <param name="entity">Objekt vom Typ 'Festplatte'</param> public void MapFromEntity(Disk entity) { this.Id = entity.Id; this.Beschreibung = entity.Description; this.Kapazität = UnitConverter.ConvertToHigher(entity.Capacity); if (entity.Ssd) this.SSD = "Ja"; else this.SSD = "Nein"; this.Zoll = entity.Inch; this.Hersteller = entity.Producer.CompanyName; string schnittstellen = ""; for (int i = 0; i < entity.PhysicalInterfaces.Count; i++ ) { if (i > 0) schnittstellen += ", "; schnittstellen += entity.PhysicalInterfaces[i].PhysicalInterface.Name; schnittstellen += " (" + entity.PhysicalInterfaces[i].Number + ")"; } this.Schnittstellen = schnittstellen; }