/// <summary> /// Applies changes on the similar object stored on the DB with the object in parameter /// </summary> /// <param name="Obj">Object with the changes</param> /// <returns>Returns true if done, false else</returns> public override int ChangeElement(object Obj) { int Count; Marques Brand = (Marques)(Obj); Marques BrandFound = Manager.GetMarque(Id: Brand.Id); if (BrandFound != null) { if (Manager.GetMarque(Brand.Nom) == null) // Check if the brand already exist or not { Count = Manager.UpdateMarque(Brand); if (Count != 1) { throw new Exception("Une erreur liée à la base de données à empêcher la modification de la marque " + Brand.Nom); } } else { throw new Exception("La marque " + Brand.Nom + " existe déjà dans la base"); } } else { throw new Exception("La marque " + Brand.Nom + " n'existe pas dans la base"); } return(Count); }
/// <summary> /// Constructor of the class /// </summary> /// <param name="ControllerBrand">Controller to use</param> /// <param name="Marque">The brand to modify or null if none</param> public AddUpdateBrand(ControllerView_Brand ControllerBrand, Marques Marque = null) { this.ControllerBrand = ControllerBrand; this.Marque = Marque; InitializeComponent(); InitializeGraphics(); }
/// <summary> /// Get one brand by name or id /// </summary> /// <param name="Name"> The name of the brand to get </param> /// <param name="Id"> The id of the brand to get </param> /// <returns> The brand searched </returns> public Marques GetMarque(string Name = "", int Id = -1) { Marques Marque = new Marques(); SQLiteCommand Sql; if (Name.CompareTo("") != 0) { Sql = new SQLiteCommand("SELECT * FROM Marques WHERE Nom = @name", Conn); Sql.Parameters.AddWithValue("@name", Name); } else { Sql = new SQLiteCommand("SELECT * FROM Marques WHERE RefMarque = @idMarque", Conn); Sql.Parameters.AddWithValue("@idMarque", Id); } SQLiteDataReader Reader = Sql.ExecuteReader(); if (Reader.Read()) { Marque.ConvertDataReaderToMarques(Reader); return(Marque); } else { return(null); } }
/// <summary> /// Check if the brand contains a mistake spelling /// </summary> /// <param name="Name"> The name of the brand to check </param> /// <returns> The brand fixed or null if no brand have been found </returns> protected Marques CheckSpellingMarques(string Name) { int BestDistance = 255, TempDistance; Marques Marque = null; Dictionary <int, Marques> ListMarque = DbManager.GetAllMarques(); // Retrieve all brands of the database foreach (KeyValuePair <int, Marques> Entry in ListMarque) { TempDistance = DistanceLevenshtein(Name, Entry.Value.Nom); // Compute the distance if (TempDistance < BestDistance) { BestDistance = TempDistance; Marque = Entry.Value; } } if (BestDistance <= 2) // Here we decide the degree of tolerance { return(Marque); } else { return(null); } }
/// <summary> /// If not exist, check if spelling mistake, if not, create new one. /// </summary> private void TreatMarque() { Marques Marque = DbManager.GetMarque(Node.SelectSingleNode("marque").InnerText); if (Marque == null) { Marque = CheckSpellingMarques(Node.SelectSingleNode("marque").InnerText); if (Marque == null) { NewMarque(); } else { UpdateListView(TypeMessage.Avertissement, SubjectMessage.Erreur_orthographe, "La marque de l'article " + Article.Reference + " est \"" + Node.SelectSingleNode("marque").InnerText + "\". Elle a été remplacée par \"" + Marque.Nom + "\""); Article.IdMarque = Marque.Id; Node.SelectSingleNode("marque").InnerText = Marque.Nom; // Change the text of the XML to correct the spelling mistake } } else { Article.IdMarque = Marque.Id; } }
/// <summary> /// Insert new brand to the database /// </summary> protected void NewMarque() { Marques Marque = new Marques(); Marque.Nom = Node.SelectSingleNode("marque").InnerText; Article.IdMarque = DbManager.InsertMarque(Marque); // Insert return the last id of the brand added. UpdateListView(TypeMessage.Succès, SubjectMessage.Ajouter_marque, "La marque " + Marque.Nom + " a été créée"); }
/// <summary> /// Adds a new element in the DB /// </summary> /// <param name="Obj">Object to add in the DB.</param> public override void AddElement(object Obj) { Marques Brand = (Marques)Obj; Marques ResBrand = Manager.GetMarque(Brand.Nom); if (ResBrand == null) { Manager.InsertMarque(Brand); } else { throw new Exception("La marque " + ResBrand.Nom + " existe déja dans la base"); } }
/// <summary> /// Get all brands /// </summary> /// <returns>A dictionnary of all brands</returns> public Dictionary <int, Marques> GetAllMarques() { Dictionary <int, Marques> ListMarque = new Dictionary <int, Marques>(); SQLiteCommand Sql = new SQLiteCommand("SELECT * FROM Marques", Conn); SQLiteDataReader Reader = Sql.ExecuteReader(); while (Reader.Read()) { Marques Marque = new Marques(); Marque.ConvertDataReaderToMarques(Reader); ListMarque.Add(Marque.Id, Marque); } return(ListMarque); }
/// <summary> /// Update value of the article /// </summary> private void UpdateArticle() { Article.Description = Node.SelectSingleNode("description").InnerText; // Update the description Familles Famille = DbManager.GetFamille(Node.SelectSingleNode("famille").InnerText); if (Famille == null) { NewFamille(); } else { Article.IdFamille = Famille.Id; // Set the new id of the of famille } SousFamilles SousFamille = DbManager.GetSousFamille(Node.SelectSingleNode("sousFamille").InnerText); if (SousFamille == null) { NewSousFamille(); } else { if (DbManager.ExistSousFamilleInFamille(SousFamille.Id, Article.IdFamille)) { Article.IdSousFamille = SousFamille.Id; // Set the new id of the sub family } else { // Generate error because a sousFamille don't belong to twice family. (this sub family has already a family) UpdateListView(TypeMessage.Erreur, SubjectMessage.Modifier_famille, "L'article " + Article.Reference + ". Sa famille n'a pas été mis à jour car sa sous famille ne correspond pas avec la nouvelle famille"); } } Marques Marque = DbManager.GetMarque(Node.SelectSingleNode("marque").InnerText); if (Marque == null) { NewMarque(); } else { Article.IdMarque = Marque.Id; // Set the new id of the brand } Article.PrixHT = Convert.ToDouble(Node.SelectSingleNode("prixHT").InnerText); // Update prixHT DbManager.UpdateArticle(Article); // Update information to the database }
/// <summary> /// Set the new brand to the database /// </summary> /// <param name="Brand"> The brand to update </param> /// <returns>The number of line modified </returns> public int UpdateMarque(Marques Brand) { SQLiteCommand Sql = new SQLiteCommand("UPDATE Marques SET Nom = @nom WHERE RefMarque = @reference", Conn); Sql.Parameters.AddWithValue("@nom", Brand.Nom); Sql.Parameters.AddWithValue("@reference", Brand.Id); try { return(Sql.ExecuteNonQuery()); } catch (Exception Ex) { throw new Exception(Ex.Message); } }
/// <summary> /// Insert a new brand to the database /// </summary> /// <param name="Marque"> The new brand to add </param> /// <returns> Id of the new brand added </returns> public int InsertMarque(Marques Marque) { SQLiteCommand Sql = new SQLiteCommand( "INSERT INTO Marques (RefMarque, Nom) VALUES((SELECT ifnull((SELECT RefMarque FROM Marques ORDER BY RefMarque DESC LIMIT 1) + 1, 1)), @nom);", Conn); Sql.Parameters.AddWithValue("@nom", Marque.Nom); try { Sql.ExecuteNonQuery(); return(Convert.ToInt32(Conn.LastInsertRowId)); } catch (Exception Ex) { throw new Exception(Ex.Message); } }
/// <summary> /// Add of modify the brand when the user has completed the form /// </summary> /// <param name="Sender"></param> /// <param name="E"></param> private void Btn_Valider_Click(object Sender, EventArgs E) { if (!CheckEntries()) { MessageBox.Show("Certains champs ne sont pas valides !", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string NameMessage = ""; try { if (Marque == null)//ajout { NameMessage = "L'ajout "; Marque = new Marques( Tbx_Marque.Text ); ControllerBrand.AddElement(Marque); this.DialogResult = DialogResult.OK; } else//modification { NameMessage = "La modification "; Marque.Nom = Tbx_Marque.Text; ControllerBrand.ChangeElement(Marque); this.DialogResult = DialogResult.OK; } this.Close(); } catch (Exception Ex) { Marque = null; MessageBox.Show("Une erreur est survenue lors de " + NameMessage.ToLower() + "avec le message suivant:\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }