From 0a1757320a716d478d38c20917622966a473d384 Mon Sep 17 00:00:00 2001 From: Jonas Rapp Date: Mon, 2 Sep 2024 20:01:07 +0200 Subject: [PATCH] Preparing/trying to save/open file with a view #1011 --- FetchXmlBuilder/Controls/attributeControl.cs | 2 +- FetchXmlBuilder/DockControls/ResultGrid.cs | 4 ++-- .../DockControls/TreeBuilderControl.cs | 23 +++++++++++++++---- FetchXmlBuilder/FXBGui.cs | 22 +++++++++++++++--- FetchXmlBuilder/FetchXmlBuilder.cs | 6 ++--- FetchXmlBuilder/Views/LayoutXML.cs | 2 +- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/FetchXmlBuilder/Controls/attributeControl.cs b/FetchXmlBuilder/Controls/attributeControl.cs index 7f937aed..07c4cb32 100644 --- a/FetchXmlBuilder/Controls/attributeControl.cs +++ b/FetchXmlBuilder/Controls/attributeControl.cs @@ -160,7 +160,7 @@ private void UpdateCellFromUI() cell.Name = Node.GetAttributeLayoutName(); cell.Width = chkLayoutVisible.Checked ? trkLayoutWidth.Value : 0; cell.IsHidden = !chkLayoutVisible.Checked; - fxb.dockControlLayoutXml?.UpdateXML(cell.Parent?.ToXML()); + fxb.dockControlLayoutXml?.UpdateXML(cell.Parent?.ToXMLString()); } grpLayout.Visible = cell != null; trkLayoutWidth.Enabled = chkLayoutVisible.Checked; diff --git a/FetchXmlBuilder/DockControls/ResultGrid.cs b/FetchXmlBuilder/DockControls/ResultGrid.cs index d53f4801..7a4547a1 100644 --- a/FetchXmlBuilder/DockControls/ResultGrid.cs +++ b/FetchXmlBuilder/DockControls/ResultGrid.cs @@ -157,7 +157,7 @@ internal void SetLayoutToGrid() } var tmpreloaded = reloaded; reloaded = true; - crmGridView1.LayoutXML = form.dockControlBuilder?.LayoutXML?.ToXML(); + crmGridView1.LayoutXML = form.dockControlBuilder?.LayoutXML?.ToXMLString(); crmGridView1.Refresh(); reloaded = tmpreloaded; } @@ -455,7 +455,7 @@ private void crmGridView1_LayoutChanged(object sender, DataGridViewColumnEventAr private void mnuResetLayout_Click(object sender, EventArgs e) { form.dockControlBuilder.ResetLayout(); - crmGridView1.LayoutXML = form.dockControlBuilder.LayoutXML?.ToXML(); + crmGridView1.LayoutXML = form.dockControlBuilder.LayoutXML?.ToXMLString(); RefreshData(); } diff --git a/FetchXmlBuilder/DockControls/TreeBuilderControl.cs b/FetchXmlBuilder/DockControls/TreeBuilderControl.cs index 070b57ec..7cac0a7d 100644 --- a/FetchXmlBuilder/DockControls/TreeBuilderControl.cs +++ b/FetchXmlBuilder/DockControls/TreeBuilderControl.cs @@ -1,6 +1,7 @@ using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Metadata; using Microsoft.Xrm.Sdk.Query; +using Rappen.XRM.Helpers.Extensions; using Rappen.XRM.Helpers.FetchXML; using Rappen.XRM.Helpers.Interfaces; using Rappen.XTB.FetchXmlBuilder.Builder; @@ -417,7 +418,7 @@ internal void UpdateLayoutXML() { return; } - fxb.dockControlLayoutXml?.UpdateXML(LayoutXML?.ToXML()); + fxb.dockControlLayoutXml?.UpdateXML(LayoutXML?.ToXMLString()); if (GetCurrentControl() is attributeControl attrcontrol) { attrcontrol.UpdateUIFromCell(); @@ -516,16 +517,28 @@ private void DisplayDefinition(XmlDocument fetchDoc) ManageMenuDisplay(); } - private XmlDocument GetFetchDocument() + private XmlDocument GetFetchDocument(bool includelayout = false) { var doc = new XmlDocument(); if (tvFetch.Nodes.Count > 0) { XmlNode rootNode = doc.CreateElement("root"); doc.AppendChild(rootNode); - TreeNodeHelper.AddXmlNode(tvFetch.Nodes[0], rootNode); - var xmlbody = doc.SelectSingleNode("root/fetch").OuterXml; - doc.LoadXml(xmlbody); + if (includelayout && LayoutXML != null) + { + //rootNode.AppendChild(new XmlNode("view")); + //view.AppendChild(LayoutXML.ToXMLString().ToXmlNode()); + //view.AppendChild(GetFetchDocument(false)); + TreeNodeHelper.AddXmlNode(tvFetch.Nodes[0], rootNode); + var xmlbody = doc.SelectSingleNode("root/view").OuterXml; + doc.LoadXml(xmlbody); + } + else + { + TreeNodeHelper.AddXmlNode(tvFetch.Nodes[0], rootNode); + var xmlbody = doc.SelectSingleNode("root/fetch").OuterXml; + doc.LoadXml(xmlbody); + } } return doc; } diff --git a/FetchXmlBuilder/FXBGui.cs b/FetchXmlBuilder/FXBGui.cs index 756bdbf4..62f127e8 100644 --- a/FetchXmlBuilder/FXBGui.cs +++ b/FetchXmlBuilder/FXBGui.cs @@ -141,7 +141,7 @@ internal void UpdateLiveXML(bool preventxmlupdate = false) { if (settings.Results.WorkWithLayout) { - dockControlLayoutXml.UpdateXML(dockControlBuilder.LayoutXML?.ToXML()); + dockControlLayoutXml.UpdateXML(dockControlBuilder.LayoutXML?.ToXMLString()); } else { @@ -340,7 +340,23 @@ private void OpenFile() FileName = ofd.FileName; var fetchDoc = new XmlDocument(); fetchDoc.Load(ofd.FileName); - dockControlBuilder.Init(fetchDoc.OuterXml, null, false, "open file", true); + switch (fetchDoc.FirstChild?.Name) + { + case "fetch": + dockControlBuilder.Init(fetchDoc.OuterXml, null, false, "open file", true); + break; + + case "view": + var fetch = fetchDoc.SelectSingleNode("//fetch")?.InnerText; + var layout = fetchDoc.SelectSingleNode("//grid")?.InnerText; + dockControlBuilder.Init(fetch, layout, true, "open file", true); + break; + + default: + MessageBox.Show("The selected file does not contain FetchXML.\nPlease select another one.", "Open File", + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + break; + } LogUse("OpenFile"); } EnableControls(true); @@ -682,7 +698,7 @@ private void SaveView(bool saveas) } } var fetch = dockControlBuilder.GetFetchString(false, false); - var layout = includelayout ? dockControlBuilder.LayoutXML.ToXML() : View?["layoutxml"].ToString(); + var layout = includelayout ? dockControlBuilder.LayoutXML.ToXMLString() : View?["layoutxml"].ToString(); var newView = new Entity(viewtype); newView["fetchxml"] = fetch; newView["returnedtypecode"] = entityname; diff --git a/FetchXmlBuilder/FetchXmlBuilder.cs b/FetchXmlBuilder/FetchXmlBuilder.cs index bfb07285..1f951638 100644 --- a/FetchXmlBuilder/FetchXmlBuilder.cs +++ b/FetchXmlBuilder/FetchXmlBuilder.cs @@ -482,7 +482,7 @@ private void SaveSetting() connectionsettings = new FXBConnectionSettings(); } connectionsettings.FetchXML = dockControlBuilder.GetFetchString(false, false); - connectionsettings.LayoutXML = dockControlBuilder.LayoutXML?.ToXML(); + connectionsettings.LayoutXML = dockControlBuilder.LayoutXML?.ToXMLString(); SettingsManager.Instance.Save(typeof(FetchXmlBuilder), connectionsettings, ConnectionDetail?.ConnectionName); } @@ -789,7 +789,7 @@ private void tsmiRepoSave_Click(object sender, EventArgs e) return; } query.Fetch = dockControlBuilder.GetFetchString(true, false); - query.Layout = dockControlBuilder.LayoutXML?.ToXML(); + query.Layout = dockControlBuilder.LayoutXML?.ToXMLString(); SaveRepository(); MessageBox.Show($"Query {query.Name} updated in repository", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } @@ -814,7 +814,7 @@ private void tsmiRepoSaveAs_Click(object sender, EventArgs e) repository.Queries.Remove(repository.Queries.FirstOrDefault(q => q.Name == queryname)); } var fetch = dockControlBuilder.GetFetchString(true, false); - var layout = dockControlBuilder.LayoutXML?.ToXML(); + var layout = dockControlBuilder.LayoutXML?.ToXMLString(); var query = new QueryDefinition { Name = queryname, Fetch = fetch, Layout = layout }; repository.Queries.Add(query); repository.SortQueries(); diff --git a/FetchXmlBuilder/Views/LayoutXML.cs b/FetchXmlBuilder/Views/LayoutXML.cs index 1fd3691a..c118e81e 100644 --- a/FetchXmlBuilder/Views/LayoutXML.cs +++ b/FetchXmlBuilder/Views/LayoutXML.cs @@ -127,7 +127,7 @@ internal void MakeSureAllCellsExistForColumns(Dictionary namewidths } } - public string ToXML() + public string ToXMLString() { var result = $@"