using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Media; using System.Windows.Documents; using System.Data.SQLite; using System.IO; using Demos.Warden.StudyRelatedEntities; using MigraDom = MigraDoc.DocumentObjectModel; namespace Demos.Warden.TagTextEditing { public class FlowDocumentTagManager { internal const string GeneratedDocumentsDir = "GeneratedDocuments"; private const string GenderTagsDbFilename = "genderInflectedTags.sqlite"; private ConversionTypes _lastChosenDirection = ConversionTypes.ToActualValues; public enum threeColLayoutWidths { AUTO, A4 }; // key is filename private List> _preparedPDFDocuments = new List>(); public void ApplyActionOnTagElement(FlowDocument document, FlowDocumentWalker.ElementAction action) { FlowDocumentWalker walker = new FlowDocumentWalker(); walker.PerformActionOnTagElements(document, action); } public void Convert(FlowDocument document, ConversionTypes direction) { this._lastChosenDirection = direction; FlowDocumentWalker walker = new FlowDocumentWalker(); walker.PerformActionOnTagElements(document, this.elementConvertChange); } private void elementConvertChange(TagBaseElement myElement, Inline inlineOwner) { if (this._lastChosenDirection == ConversionTypes.ToActualValues) { myElement.Text = myElement.getActualValue(); myElement.Background = new SolidColorBrush(Colors.Transparent); } else { myElement.setElementDefaultAppearance(); } } public void ReplaceTagsWithStaticActualValues(FlowDocument document) { throw new NotImplementedException("Not currently supported."); //FlowDocumentWalker walker = new FlowDocumentWalker(); //walker.PerformActionOnTagElements(document, (TagBaseElement element, Inline inlineOwner) => //{ // Paragraph owningPara = inlineOwner.Parent as Paragraph; // if (owningPara != null) // { // inlineOwner.ContentStart.InsertTextInRun(element.getActualValue()); // //owningPara.Inlines.Remove(inlineOwner); // } //}); } public void BindModel(FlowDocument document, T model) { FlowDocumentWalker walker = new FlowDocumentWalker(); walker.PerformActionOnTagElements(document, (TagBaseElement element, Inline inlineOwner) => { if (element is TagElement) { ((TagElement)element).SetModel(model); } }); } public void BindGender(FlowDocument document, StudyRelatedEntities.Sex sex) { FlowDocumentWalker walker = new FlowDocumentWalker(); walker.PerformActionOnTagElements(document, (TagBaseElement element, Inline inlineOwner) => { if (element is SpecificTypeTagElements.GenderInflictedTagElement) { SpecificTypeTagElements.GenderInflictedTagElement genTag = (SpecificTypeTagElements.GenderInflictedTagElement)element; SpecificTypeTagElements.GenderInflictedTagElement.DesiredForm form; if (sex == StudyRelatedEntities.Sex.Female) { form = SpecificTypeTagElements.GenderInflictedTagElement.DesiredForm.Female; } else { form = SpecificTypeTagElements.GenderInflictedTagElement.DesiredForm.Male; } genTag.SelectedForm = form; } }); } public void setWidthOfThreeColLayouts(FlowDocument document, threeColLayoutWidths type) { foreach(Block block in document.Blocks.ToArray()) { if (block is ThreeColumnLayoutTable && type == threeColLayoutWidths.A4) { ThreeColumnLayoutTable.setColumnA4Width((ThreeColumnLayoutTable)block); } else if (block is ThreeColumnLayoutTable && type == threeColLayoutWidths.AUTO) { ThreeColumnLayoutTable.setColumnAutoWidth((ThreeColumnLayoutTable)block); } } } public void saveGenderInflectionTagData(SpecificTypeTagElements.GenderInflictedTagElement[] tags) { if (tags == null || tags.Length == 0) { return; } string dirname = FlowDocumentTagManager.GeneratedDocumentsDir; if (!Directory.Exists(dirname)) { Directory.CreateDirectory(dirname); } string file = dirname + "/" + FlowDocumentTagManager.GenderTagsDbFilename; using (SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + file + ";Version=3;")) { dbConnection.Open(); string query = @" CREATE TABLE IF NOT EXISTS genderTagsData ( id INTEGER PRIMARY KEY, tagTitle VARCHAR(255), maleForm VARCHAR(255), femaleForm VARCHAR(255), description VARCHAR(500) )"; SQLiteCommand cmd = new SQLiteCommand(query, dbConnection); cmd.ExecuteNonQuery(); query = @"DELETE FROM genderTagsData"; cmd.CommandText = query; cmd.ExecuteNonQuery(); StringBuilder sb = new StringBuilder(); foreach (SpecificTypeTagElements.GenderInflictedTagElement tag in tags) { sb.Append(String.Format("INSERT INTO genderTagsData(tagTitle, maleForm, femaleForm, description) VALUES('{0}','{1}','{2}','{3}');", tag.TagTitle, tag.MaleForm, tag.FemaleForm, "")); } cmd.CommandText = sb.ToString(); cmd.ExecuteNonQuery(); dbConnection.Close(); } } public SpecificTypeTagElements.GenderInflictedTagElement[] loadGenderTagsFromDbFile() { List retrievedTags = new List(); string file = FlowDocumentTagManager.GeneratedDocumentsDir + "/" + FlowDocumentTagManager.GenderTagsDbFilename; if (!File.Exists(file)) { return retrievedTags.ToArray(); } using (SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + file + ";Version=3;")) { dbConnection.Open(); string query = @"Select * From genderTagsData Order by lower(tagTitle)"; SQLiteCommand cmd = new SQLiteCommand(query, dbConnection); SQLiteDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.Default); while(reader.Read()) { SpecificTypeTagElements.GenderInflictedTagElement tag = new SpecificTypeTagElements.GenderInflictedTagElement(); tag.InitializeState(reader["tagTitle"].ToString(), reader["maleForm"].ToString(), reader["femaleForm"].ToString()); retrievedTags.Add(tag); } dbConnection.Close(); } return retrievedTags.ToArray(); } public void PrepareSinglePDF(FlowDocument document, string filename, bool ignoreUnprocessableElements = false) { if (!ignoreUnprocessableElements) { bool onlyProcessableElements = this.CheckProcessableElements(document); if (!onlyProcessableElements) { throw new ContainsUnprocessableElements(); } } PdfConverter pdfConv = new PdfConverter(); MigraDom.Document doc = pdfConv.convertToMigraDoc(document); this._preparedPDFDocuments.Add(new KeyValuePair(filename, doc)); } public void SavePreparedPDFDocuments(string dirPath, bool batch = false) { if (!batch) { foreach (KeyValuePair loadedDoc in this._preparedPDFDocuments) { MigraDoc.Rendering.PdfDocumentRenderer renderer = new MigraDoc.Rendering.PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always); renderer.Document = loadedDoc.Value; renderer.RenderDocument(); renderer.Save(String.Format("{0}\\{1}", dirPath, loadedDoc.Key)); } } else { MigraDom.Document finalDoc = new MigraDom.Document(); foreach (KeyValuePair loadedDoc in this._preparedPDFDocuments) { foreach (MigraDom.Section sec in loadedDoc.Value.Sections) { finalDoc.Add(sec.Clone()); } // need to figure out their connection } MigraDoc.Rendering.PdfDocumentRenderer renderer = new MigraDoc.Rendering.PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always); renderer.Document = finalDoc; renderer.RenderDocument(); renderer.Save(String.Format("{0}\\{1}", dirPath, "exportSpolecne.pdf")); } } public void ClearPreparedPDFDocuments() { this._preparedPDFDocuments = new List>(); } public bool CheckProcessableElements(FlowDocument document) { ProcessableElementsChecker checker = new ProcessableElementsChecker(); return checker.Check(document); } } }