public void validateMotherboard() { Motherboard motherboard = new Motherboard(); MotherboardValidator validator = new MotherboardValidator(); motherboard.Description = "Dies ist ein Test"; motherboard.Inch = 24.2; motherboard.Socket = "!$%&§&"; Assert.AreEqual(false, validator.CheckConsistency(motherboard)); }
/// <summary> /// Prüft die Konsistenz der Attribute der Entität 'Hauptplatine' /// </summary> /// <param name="entity">Das Objekt, welches geprüft wird</param> /// <returns>true: Objekt Konsistent, false: Objekt fehlerhaft</returns> public bool CheckConsistency(Motherboard entity) { bool result = true; Regex socketReg = new Regex(this.socketPattern); if (!socketReg.Match(entity.Socket).Success) { result = false; } if (entity.Inch <= 0) { result = false; } return result; }
/// <summary> /// Konstruktor: Setzt die Wert für die Initialisierung des Dialoges /// </summary> /// <param name="entity">Objekt eines Mainboards</param> public CreateMotherboard(Motherboard entity = null) { InitializeComponent(); this.GetProducers(); this.entity = entity; if (entity != null) { this.SetAllFields(); this.entity = entity; this.isAvailable = true; } else { this.entity = new Motherboard(); this.isAvailable = false; } }
/// <summary> /// Verändert einen bestehenden Datensatz der Entität 'Hauptplatine' 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(Motherboard entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); MySqlCommand interfaceCommand = connection.CreateCommand(); string usedInterfaces = ""; command.CommandText = "UPDATE `" + this.GetTableName() + "` SET `Beschreibung`='" + entity.Description + "', `Zoll`='" + entity.Inch.ToString().Replace(',','.') + "', `Sockel`='" + entity.Socket + "', `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_Hauptplatine` = " + entity.Id + " AND `ID_Schnittstelle` = " + entity.PhysicalInterfaces[i].PhysicalInterface.Id; } else { interfaceCommand.CommandText = "INSERT INTO `" + this.GetTableName() + "_schnittstelle`(`ID_Hauptplatine`, `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_Hauptplatine` = " + entity.Id + " AND `ID_Schnittstelle` NOT IN (" + usedInterfaces + ")"; } else { interfaceCommand.CommandText = "DELETE FROM `" + this.GetTableName() + "_schnittstelle` WHERE `ID_Hauptplatine` = " + entity.Id; } interfaceCommand.ExecuteNonQuery(); command.ExecuteNonQuery(); connection.Close(); }
/// <summary> /// Speichert ein Objekt der Entität 'Hauptplatine' in der Datenbank /// </summary> /// <param name="entity">Das Objekt, welches gespeichert wird</param> public void Save(Motherboard entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO `" + this.GetTableName() + "`(`Beschreibung`, `Zoll`, `Sockel`, `ID_Hersteller`) " + "VALUES ('" + entity.Description + "','" + entity.Inch.ToString().Replace(',','.') + "','" + entity.Socket + "'," + entity.Producer.Id + ")"; connection.Open(); command.ExecuteNonQuery(); connection.Close(); for (int i = 0; i < entity.PhysicalInterfaces.Count; i++) { command.CommandText = "INSERT INTO `" + this.GetTableName() + "_schnittstelle`(`ID_Hauptplatine`, `ID_Schnittstelle`, `Anzahl`) " + "VALUES (" + this.GetLastEntity<Motherboard>().Id + "," + entity.PhysicalInterfaces[i].PhysicalInterface.Id + "," + entity.PhysicalInterfaces[i].Number + ")"; connection.Open(); command.ExecuteNonQuery(); connection.Close(); } }
public void getMotherboardFromDatabase() { MotherboardDataAccess dataAccess = new MotherboardDataAccess(); ProducerDataAccess producerDataAccess = new ProducerDataAccess(); PhysicalInterfaceDataAccess physicalInterfaceDataAccess = new PhysicalInterfaceDataAccess(); Motherboard motherboard = new Motherboard(); motherboard.Description = "Dies ist ein Test"; motherboard.Inch = 24.2; motherboard.Socket = "PGA 988B"; motherboard.Producer = producerDataAccess.GetLastEntity<Producer>(); motherboard.AddPhysicalInterface(new PhysicalInterfaceWithCount(physicalInterfaceDataAccess.GetLastEntity<PhysicalInterface>(), 3)); dataAccess.Save(motherboard); Motherboard dbMotherboard = dataAccess.GetLastEntity<Motherboard>(); Assert.AreEqual(motherboard.Socket, dbMotherboard.Socket); }
/// <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, Motherboard entity) { MySqlConnection connection = this.CreateConnection(); MySqlCommand command = connection.CreateCommand(); bool result; connection.Open(); command.CommandText = "SELECT * FROM `" + this.GetTableName() + "_schnittstelle` WHERE `ID_Hauptplatine` = " + 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 Hauptplatine, zu welcher die Schnittstellen ermittelt werden</param> /// <returns>Liste von PhysicalInterfaceWithCount</returns> private List<PhysicalInterfaceWithCount> GetPhysicalInterfaces(Motherboard 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_hauptplatine = " + 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 'Hauptplatine' /// </summary> /// <param name="reader">Der Datensatz, welcher gemappt wird</param> /// <returns>Motherboard</returns> protected override object MapToEntity(MySqlDataReader reader) { Motherboard motherboard = new Motherboard(); ProducerDataAccess producerDataAccess = new ProducerDataAccess(); motherboard.Id = Int32.Parse(reader.GetValue(0).ToString()); motherboard.Description = reader.GetValue(1).ToString(); motherboard.Inch = Double.Parse(reader.GetValue(2).ToString()); motherboard.Socket = reader.GetValue(3).ToString(); motherboard.Producer = producerDataAccess.GetEntityById<Producer>(Int32.Parse(reader.GetValue(4).ToString())); motherboard.PhysicalInterfaces = this.GetPhysicalInterfaces(motherboard); return motherboard; }
/// <summary> /// Wandelt ein Objekt der Entität 'Hauptplatine' in eine grafisches Objekt. /// Übersetzt englische Begriffe und zählt alle Schnittstellen in einer Liste auf. /// </summary> /// <param name="entity">Objekt vom Typ 'Hauptplatine'</param> public void MapFromEntity(Motherboard entity) { this.Id = entity.Id; this.Beschreibung = entity.Description; this.Zoll = entity.Inch; this.Sockel = entity.Socket; 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; }