using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net.Security; using System.Text; using System.Threading.Tasks; namespace JRCookbookBusiness { public enum RecipeIngredientLinkType { Unlinked, Ingredient, NoLink } public class RecipeIngredient: ICloneable { public Guid? recipeIngredientID = null; public Guid? recipeID = null; public Int32 ingredientIndex = 0; public String ingredientText = String.Empty; private bool _isHeading = false; public RecipeIngredientLinkType linkType = RecipeIngredientLinkType.Unlinked; private static SortedList ingredientSkipWords = null; public bool isHeading { get { return (this is RecipeIngredientHeading); } } internal string linkTypeAsString { get { return Enum.GetName(typeof(RecipeIngredientLinkType), linkType); } } public RecipeIngredientHeading ConvertToHeading() { if (isHeading) { return (RecipeIngredientHeading)this; } else { RecipeIngredientHeading temp = new RecipeIngredientHeading(); temp.recipeIngredientID = this.recipeIngredientID; temp.recipeID = this.recipeID; temp.ingredientIndex = this.ingredientIndex; temp.ingredientText = this.ingredientText; temp.linkType = RecipeIngredientLinkType.NoLink; return temp; } } public RecipeIngredientItem ConvertToItem() { if (!isHeading) { return (RecipeIngredientItem)this; } else { RecipeIngredientItem temp = new RecipeIngredientItem(); temp.recipeIngredientID = this.recipeIngredientID; temp.recipeID = this.recipeID; temp.ingredientIndex = this.ingredientIndex; temp.ingredientText = this.ingredientText; temp.linkType = RecipeIngredientLinkType.Unlinked; temp.linkQuality = 0; return temp; } } //protected RecipeIngredient() //{ //} public void PasteIntoChapter(Guid newRecipeID) { this.recipeIngredientID = null; this.recipeID = newRecipeID; } public void Save() { if (this.recipeIngredientID == null) { this.recipeIngredientID = clsDatabaseLayer.GetDatabaseLayer().GetNewIDRecipeIngredient(); clsDatabaseLayer.GetDatabaseLayer().InsertRecipeIngredient(this); } else { clsDatabaseLayer.GetDatabaseLayer().UpdateRecipeIngredient(this); } } public void Delete() { clsDatabaseLayer.GetDatabaseLayer().DeleteRecipeIngredientByID(recipeIngredientID.Value); } public static void DeleteAllByRecipeID(Guid recipeID) { clsDatabaseLayer.GetDatabaseLayer().DeleteRecipeIngredientsByRecipeID(recipeID); } public static RecipeIngredient GetRecipeIngredientByID(Guid recipeIngredientID) { DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetRecipeIngredientByID(recipeIngredientID); var row = recordSet.Tables[0].Rows[0]; return GetRecipeIngredientByDataRow(row); } internal static RecipeIngredient GetRecipeIngredientByDataRow(DataRow recipeIngredientDataRow) { if (recipeIngredientDataRow["Heading"].ToString() == "Y") { var recipeIngredientHeading = new RecipeIngredientHeading(); recipeIngredientHeading.PopulateFromDataRow(recipeIngredientDataRow); return recipeIngredientHeading; } else { var recipeIngredientItem = new RecipeIngredientItem(); recipeIngredientItem.PopulateFromDataRow(recipeIngredientDataRow); return recipeIngredientItem; } } internal virtual void PopulateFromDataRow(DataRow dataRow) { //initializing in child objects if (dataRow.IsNull("recipeIngredientID")) recipeIngredientID = null; else recipeIngredientID = (Guid)dataRow["recipeIngredientID"]; if (dataRow.IsNull("recipeID")) recipeID = null; else recipeID = (Guid)dataRow["recipeID"]; if (dataRow.IsNull("ingredientindex")) ingredientIndex = 0; else ingredientIndex = (Int32)dataRow["ingredientindex"]; if (dataRow.IsNull("ingredientText")) ingredientText = String.Empty; else ingredientText = (String)dataRow["ingredientText"]; if (dataRow.IsNull("Heading")) _isHeading = false; else _isHeading = (((String)dataRow["Heading"]).ToUpper() == "Y"); if (dataRow.IsNull("linkType")) linkType = RecipeIngredientLinkType.Unlinked; else linkType = (RecipeIngredientLinkType)Enum.Parse(typeof(RecipeIngredientLinkType),(String)dataRow["linkType"]); //Convert back to string //String t = Enum.GetName(typeof(Roles), roleValue); } protected virtual void InitializeAllFields() { recipeIngredientID = null; recipeID = null; ingredientIndex = 0; ingredientText = String.Empty; _isHeading = false; linkType = RecipeIngredientLinkType.Unlinked; } public static String NormalizeRecipeIngredientName(String strInputRecipeIngredientName) { LoadSkipWords(); strInputRecipeIngredientName = clsDiacriticRemover.RemoveDiacritics(strInputRecipeIngredientName).ToLower(); StringBuilder thisWord = new StringBuilder(); SortedList allWords = new SortedList(); foreach (var thisChar in strInputRecipeIngredientName) { if (thisChar >= 'a' && thisChar <= 'z') { thisWord.Append(thisChar); } else { if (thisWord.Length > 0) { String fullWord = thisWord.ToString(); if (!allWords.ContainsKey(fullWord)) { allWords.Add(fullWord,fullWord); } thisWord.Clear(); } } } if (thisWord.Length > 0) { String fullWord = thisWord.ToString(); if (!allWords.ContainsKey(fullWord)) { allWords.Add(fullWord, fullWord); } thisWord.Clear(); } StringBuilder normalizedName = new StringBuilder(); foreach (var wordToAdd in allWords.Values) { if (!ingredientSkipWords.ContainsKey(wordToAdd)) { normalizedName.Append(wordToAdd); normalizedName.Append(' '); } } return normalizedName.ToString().Trim(); } private static void LoadSkipWords() { if (ingredientSkipWords == null) { var ingredientSkipWordsUnsorted = IngredientSkipSearchWord.GetAllIngredientSkipSearchWords(); ingredientSkipWords = new SortedList(); foreach (var loadVal in ingredientSkipWordsUnsorted) { ingredientSkipWords.Add(loadVal.word, loadVal); } } } public static List GetRecipeIngredientAll() { List allIngredients = new List(); DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetRecipeIngredientAll(); foreach (DataRow row in recordSet.Tables[0].Rows) { var recipeIngredient = GetRecipeIngredientByDataRow(row); if (recipeIngredient.isHeading == false) { if (((RecipeIngredientItem)recipeIngredient).ingredientID != null) { allIngredients.Add(recipeIngredient); } } } return allIngredients; } public object Clone() { return this.MemberwiseClone(); } } }