using Microsoft.AspNetCore.Html; using Microsoft.Office.Interop.Word; using System; using System.CodeDom; using System.Collections.Generic; using System.Data; using System.Linq; using System.Security.Policy; using System.Text; using System.Threading.Tasks; namespace JRCookbookBusiness { public class CookbookChapter { public Guid? cookbookChapterID = null; public Guid? parentChapterID = null; public Guid? cookbookID = null; public String name = String.Empty; public String comments = String.Empty; public List cookbookChapters = new List(); public List recipeHighlights = new List(); public event EventHandler RecipeRenderedEvent; public CookbookChapter() { } public CookbookChapter(Guid cookbookID, Guid cookbookChapterID) { PopulateByID(cookbookID, cookbookChapterID); } public void Save() { if (this.cookbookChapterID == null) { this.cookbookChapterID = clsDatabaseLayer.GetDatabaseLayer().GetNewIDCookbookChapter(); clsDatabaseLayer.GetDatabaseLayer().InsertCookbookChapter(this); } else { clsDatabaseLayer.GetDatabaseLayer().UpdateCookbookChapter(this); } } public void Delete() { foreach (var cookbookChapter in cookbookChapters) { cookbookChapter.Delete(); } foreach (var recipe in recipeHighlights) { recipe.Delete(); } clsDatabaseLayer.GetDatabaseLayer().DeleteCookbookChapterByID(cookbookID.Value, cookbookChapterID.Value); } internal String GenerateWebPagesToFolder(String folderToSaveTo, Dictionary recipePageDictionary, SortedList recipesAlpha, SortedSet pagesAlreadyAdded, List trackbackListForThisChapter) { String pageNameToUse = SharedRoutines.RemoveSpecialCharactersFromFileName(this.name); String pageNameToUseWithExtension = pageNameToUse + ".htm"; while (pagesAlreadyAdded.Contains(pageNameToUseWithExtension.ToUpper())) { pageNameToUse += "1"; pageNameToUseWithExtension = pageNameToUse + ".htm"; } pagesAlreadyAdded.Add(pageNameToUseWithExtension.ToUpper()); var builderChapterIndex = new HtmlContentBuilder(); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendFormat("{0}" + Constants.CRLF, this.name); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); if ((trackbackListForThisChapter?.Count() ?? 0) > 0) { builderChapterIndex.AppendHtmlLine(" "); builderChapterIndex.AppendHtmlLine(" "); builderChapterIndex.AppendHtmlLine(" "); } builderChapterIndex.AppendHtml(HTMLAddChapterName()); Trackback thisTrackback = new Trackback(); thisTrackback.TrackbackURL = pageNameToUseWithExtension; thisTrackback.TrackbackText = this.name; List trackbackListForChildren = new List(); trackbackListForChildren.AddRange(trackbackListForThisChapter); trackbackListForChildren.Add(thisTrackback); foreach (var cookbookChapter in cookbookChapters) { var chapterURLAdded = cookbookChapter.GenerateWebPagesToFolder(folderToSaveTo, recipePageDictionary, recipesAlpha, pagesAlreadyAdded, trackbackListForChildren); cookbookChapter.RecipeRenderedEvent += HandleRecipeRendered; builderChapterIndex.AppendHtml(HTMLAddSubChapterToIndex(cookbookChapter, chapterURLAdded)); cookbookChapter.RecipeRenderedEvent -= HandleRecipeRendered; } foreach (var recipeHighlight in recipeHighlights) { var recipe = Recipe.GetRecipeByID(recipeHighlight.recipeID.Value); recipe.GenerateWebPageToFolder(folderToSaveTo, recipePageDictionary, recipesAlpha, pagesAlreadyAdded, trackbackListForChildren); builderChapterIndex.AppendHtml(HTMLAddRecipeToIndex(recipe, recipePageDictionary)); if (RecipeRenderedEvent != null) { RecipeRenderedEvent.Invoke(recipe, new EventArgs()); } } builderChapterIndex.AppendHtmlLine("
"); builderChapterIndex.AppendHtml(" "); builderChapterIndex.AppendHtmlLine("
"); builderChapterIndex.AppendHtmlLine(""); builderChapterIndex.AppendHtmlLine(""); var strChapterPath = folderToSaveTo; if (strChapterPath.EndsWith("\\") == false) { strChapterPath += "\\"; } strChapterPath += pageNameToUseWithExtension; using (var writer = new System.IO.StreamWriter(strChapterPath, false)) { builderChapterIndex.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); } return pageNameToUseWithExtension; } private String HTMLAddChapterName() { var builder = new HtmlContentBuilder(); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); builder.AppendHtml("

"); builder.AppendFormat("{0}", this.name); builder.AppendHtmlLine("

"); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); using (var writer = new System.IO.StringWriter()) { builder.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); return writer.ToString(); } } private String HTMLAddSubChapterToIndex(CookbookChapter chapter, String chapterURL) { var builder = new HtmlContentBuilder(); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); builder.AppendHtml("

"); builder.AppendFormat("", chapterURL); builder.AppendFormat("{0}", chapter.name); builder.AppendHtmlLine(""); builder.AppendHtmlLine("

"); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); using (var writer = new System.IO.StringWriter()) { builder.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); return writer.ToString(); } } private String HTMLAddRecipeToIndex(Recipe recipe, Dictionary recipePageDictionary) { var recipePageURL = recipePageDictionary.GetValueOrDefault(recipe.recipeID.Value, String.Empty); var builder = new HtmlContentBuilder(); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); builder.AppendHtml("

"); builder.AppendFormat("", recipePageURL); builder.AppendFormat("{0}", recipe.recipename); builder.AppendHtmlLine(""); builder.AppendHtmlLine("

"); builder.AppendHtmlLine(" "); builder.AppendHtmlLine(" "); using (var writer = new System.IO.StringWriter()) { builder.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default); return writer.ToString(); } } internal void AddToWordDoc(Microsoft.Office.Interop.Word.Application objWord, Dictionary recipePageDictionary, SortedList recipesAlpha, SortedSet pagesAlreadyAdded, ref bool isFirstPageOfContent) { String pageNameToUse = SharedRoutines.RemoveSpecialCharactersFromFileName(this.name); String pageNameToUseWithExtension = pageNameToUse; while (pagesAlreadyAdded.Contains(pageNameToUseWithExtension.ToUpper())) { pageNameToUse += "1"; pageNameToUseWithExtension = pageNameToUse; } pagesAlreadyAdded.Add(pageNameToUseWithExtension.ToUpper()); clsWord.WordApplication.Selection.InsertBreak(WdBreakType.wdSectionBreakNextPage); clsWord.Insert_HeaderText(this.name); clsWord.Insert_PageNumberFooter(ref isFirstPageOfContent); objWord.Selection.Font.Bold = 0; objWord.Selection.Font.Size = clsWord.WORD_NORMAL_FONT_SIZE; objWord.ActiveDocument.Tables.Add(objWord.Selection.Range, 1, 1, Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior, Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent); objWord.Selection.Cells[1].LeftPadding = 0; //objWord.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone; //objWord.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone; //objWord.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone; objWord.Selection.Tables[1].Borders.OutsideLineStyle = WdLineStyle.wdLineStyleNone; objWord.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle; objWord.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth050pt; //objWord.Selection.Font.Bold = 1; //objWord.Selection.Font.Size = clsWord.WORD_CHAPTER_NAME_FONT_SIZE; //objWord.Selection.TypeText(this.name); //objWord.Selection.Font.Bold = 0; objWord.Selection.set_Style(objWord.ActiveDocument.Styles["Heading 1"]); objWord.Selection.TypeText(this.name); objWord.Selection.Font.Size = clsWord.WORD_NORMAL_FONT_SIZE; objWord.Selection.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdStory); clsWord.AddHalfSpaceLineAtEndOfDocument(); objWord.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle; objWord.Selection.ParagraphFormat.SpaceAfter = 0F; foreach (var cookbookChapter in cookbookChapters) { cookbookChapter.RecipeRenderedEvent += HandleRecipeRendered; cookbookChapter.AddToWordDoc(objWord, recipePageDictionary, recipesAlpha, pagesAlreadyAdded, ref isFirstPageOfContent); cookbookChapter.RecipeRenderedEvent -= HandleRecipeRendered; } foreach (var recipeHighlight in recipeHighlights) { var recipe = Recipe.GetRecipeByID(recipeHighlight.recipeID.Value); recipe.AddToWordDoc(objWord, recipePageDictionary, recipesAlpha, pagesAlreadyAdded); if (RecipeRenderedEvent != null) { RecipeRenderedEvent.Invoke(recipe, new EventArgs()); } //builderChapterIndex.AppendHtml(HTMLAddRecipeToIndex(recipe, recipePageDictionary)); } } private void HandleRecipeRendered(object sender, System.EventArgs e) { if (RecipeRenderedEvent != null) { RecipeRenderedEvent.Invoke(sender, new EventArgs()); } } public static CookbookChapter GetCookbookByID(Guid cookbookID, Guid cookbookChapterID) { return new CookbookChapter(cookbookID, cookbookChapterID); } public static CookbookChapter GetCookbookChapterByDataRow(DataRow row) { var newChapter = new CookbookChapter(); newChapter.PopulateFromDataRow(row); return newChapter; } private void PopulateByID(Guid cookbookID, Guid cookbookChapterID) { DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetCookbookChapterByID(cookbookID, cookbookChapterID); if (recordSet.Tables[0].Rows.Count > 0) { DataRow ldbrwRow; ldbrwRow = recordSet.Tables[0].Rows[0]; PopulateFromDataRow(ldbrwRow); } } private void PopulateFromDataRow(DataRow dataRow) { InitializeAllFields(); if (dataRow.IsNull("cookbookChapterID")) cookbookChapterID = null; else cookbookChapterID = (Guid)dataRow["cookbookChapterID"]; if (dataRow.IsNull("cookbookID")) cookbookID = null; else cookbookID = (Guid)dataRow["cookbookID"]; if (dataRow.IsNull("parentChapterID")) parentChapterID = null; else parentChapterID = (Guid)dataRow["parentChapterID"]; if (dataRow.IsNull("name")) name = String.Empty; else name = (String)dataRow["name"]; if (dataRow.IsNull("comments")) comments = String.Empty; else comments = (String)dataRow["comments"]; LoadChapters(); LoadRecipeHighlights(); } private void InitializeAllFields() { cookbookChapterID = null; parentChapterID = null; cookbookID = null; name = String.Empty; comments = String.Empty; cookbookChapters = new List(); recipeHighlights = new List(); } private void LoadChapters() { //Populate child objects DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetCookbookChaptersByParentChapter(this.cookbookID.Value, this.cookbookChapterID); foreach (DataRow childDataRow in recordSet.Tables[0].Rows) { var newChapter = CookbookChapter.GetCookbookChapterByDataRow(childDataRow); cookbookChapters.Add(newChapter); } } private void LoadRecipeHighlights() { //Populate child objects DataSet recordSet; recordSet = clsDatabaseLayer.GetDatabaseLayer().GetRecipeHighlightsByParentChapter(this.cookbookID.Value, this.cookbookChapterID); foreach (DataRow childDataRow in recordSet.Tables[0].Rows) { var newRecipeHighlights = RecipeHighlights.GetRecipeHighlightsByDataRow(childDataRow); recipeHighlights.Add(newRecipeHighlights); } } public int GetTotalRecipeCount() { int returnValue = 0; foreach (var cookbookChapter in cookbookChapters) { returnValue += cookbookChapter.GetTotalRecipeCount(); } returnValue += recipeHighlights.Count; return returnValue; } } }