/// <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; Familles Family = (Familles)(Obj); Familles FamilyFound = Manager.GetFamille(Id: Family.Id); if (FamilyFound != null) { if (Manager.GetFamille(Family.Nom) == null) // Check if the family already exist or not { Count = Manager.UpdateFamilles(Family); if (Count != 1) { throw new Exception("Une erreur liée à la base de données à empêcher la modification de la famille " + Family.Nom); } } else { throw new Exception("La famille " + Family.Nom + " existe déjà dans la base"); } } else { throw new Exception("La famille " + Family.Nom + " n'existe pas dans la base"); } return(Count); }
/// <summary> /// Get one family by name OR id /// </summary> /// <param name="Name"> The name of the family to get </param> /// <param name="Id"> The id of the family to get </param> /// <returns> The family searched </returns> public Familles GetFamille(string Name = "", int Id = -1) { Familles Famille = new Familles(); SQLiteCommand Sql; if (Name.CompareTo("") != 0) { Sql = new SQLiteCommand("SELECT * FROM Familles WHERE Nom = @name", Conn); Sql.Parameters.AddWithValue("@name", Name); } else { Sql = new SQLiteCommand("SELECT * FROM Familles WHERE RefFamille = @idFamille", Conn); Sql.Parameters.AddWithValue("@idFamille", Id); } SQLiteDataReader Reader = Sql.ExecuteReader(); if (Reader.Read()) { Famille.ConvertDataReaderToFamilles(Reader); return(Famille); } else { return(null); } }
/// <summary> /// Check if the family contains a mistake spelling /// </summary> /// <param name="Name"> The name of the family </param> /// <returns> The family fixed or null if no family have been found </returns> protected Familles CheckSpellingFamilles(string Name) { int BestDistance = 255, TempDistance; Familles Famille = null; Dictionary <int, Familles> ListFamille = DbManager.GetAllFamilles(); // Retrieve all family of the database foreach (KeyValuePair <int, Familles> Entry in ListFamille) { TempDistance = DistanceLevenshtein(Name, Entry.Value.Nom); // Compute the distance if (TempDistance < BestDistance) { BestDistance = TempDistance; Famille = Entry.Value; } } if (BestDistance <= 2) // Here we decide the degree of tolerance { return(Famille); } else { return(null); } }
/// <summary> /// If not exist, check if spelling mistake, if not, create new one. /// </summary> private void TreatFamille() { Familles Famille = DbManager.GetFamille(Node.SelectSingleNode("famille").InnerText); // Check if the famille already exist if (Famille == null) // Famille does not exist { Famille = CheckSpellingFamilles(Node.SelectSingleNode("famille").InnerText); if (Famille == null) { NewFamille(); } else { UpdateListView(TypeMessage.Avertissement, SubjectMessage.Erreur_orthographe, "La famille de l'article " + Article.Reference + " est \"" + Node.SelectSingleNode("famille").InnerText + "\". Elle a été remplacé par \"" + Famille.Nom + "\""); Article.IdFamille = Famille.Id; Node.SelectSingleNode("famille").InnerText = Famille.Nom; // Change the text of the XML to correct the spelling mistake } } else { Article.IdFamille = Famille.Id; } }
/// <summary> /// Insert new family to the database /// </summary> protected void NewFamille() { Familles Famille = new Familles(); Famille.Nom = Node.SelectSingleNode("famille").InnerText; Article.IdFamille = DbManager.InsertFamille(Famille); // Insert return the last id of the famille added. UpdateListView(TypeMessage.Succès, SubjectMessage.Ajouter_famille, "La famille " + Famille.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) { Familles Family = (Familles)Obj; Familles FamilyFound = Manager.GetFamille(Family.Nom); if (FamilyFound == null) { Manager.InsertFamille(Family); } else { throw new Exception("La famille " + FamilyFound.Nom + " existe déja dans la base"); } }
/// <summary> /// Get all family /// </summary> /// <returns> A dictionnary of the family </returns> public Dictionary <int, Familles> GetAllFamilles() { Dictionary <int, Familles> ListFamille = new Dictionary <int, Familles>(); SQLiteCommand Sql = new SQLiteCommand("SELECT * FROM Familles", Conn); SQLiteDataReader Reader = Sql.ExecuteReader(); while (Reader.Read()) { Familles Famille = new Familles(); Famille.ConvertDataReaderToFamilles(Reader); ListFamille.Add(Famille.Id, Famille); } return(ListFamille); }
/// <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 family to the database /// </summary> /// <param name="Family"> The family to update </param> /// <returns>The number of line modified </returns> public int UpdateFamilles(Familles Family) { SQLiteCommand Sql = new SQLiteCommand("UPDATE Familles SET Nom = @nom WHERE RefFamille = @reference", Conn); Sql.Parameters.AddWithValue("@nom", Family.Nom); Sql.Parameters.AddWithValue("@reference", Family.Id); try { return(Sql.ExecuteNonQuery()); } catch (Exception Ex) { throw new Exception(Ex.Message); } }
/// <summary> /// Insert family to the database /// </summary> /// <param name="Famille"> The new family to insert </param> /// <returns> Id of the new family added </returns> public int InsertFamille(Familles Famille) { SQLiteCommand Sql = new SQLiteCommand( "INSERT INTO Familles (RefFamille, Nom) VALUES((SELECT ifnull((SELECT RefFamille FROM Familles ORDER BY RefFamille DESC LIMIT 1) + 1, 1)), @nom);", Conn); Sql.Parameters.AddWithValue("@nom", Famille.Nom); try { Sql.ExecuteNonQuery(); return(Convert.ToInt32(Conn.LastInsertRowId)); } catch (Exception Ex) { throw new Exception(Ex.Message); } }