Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SearchResultView.xaml.cs
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Data;
5using System.Text;
6using System.Xml;
7using System.Xml.Schema;
8using System.Xml.Xsl;
9using System.Windows;
10using System.Windows.Controls;
11using System.Windows.Data;
12using Microsoft.Win32;
15using Waher.Events;
20
22{
26 public partial class SearchResultView : UserControl, ITabView
27 {
28 private Field[] headers;
29 private Dictionary<string, string>[] records;
30 private DataTable table;
31
32 public SearchResultView()
33 {
34 this.headers = null;
35 this.records = null;
36 this.table = null;
37 }
38
39 public SearchResultView(Field[] Headers, Dictionary<string, string>[] Records)
40 {
41 this.Init(Headers, Records);
42 }
43
44 private void Init(Field[] Headers, Dictionary<string, string>[] Records)
45 {
46 this.headers = Headers;
47 this.records = Records;
48
50
51 if (!(this.table is null))
52 {
53 this.table.Dispose();
54 this.table = null;
55 }
56
57 this.table = new DataTable("SearchResult");
58
59 foreach (Field Header in Headers)
60 this.table.Columns.Add(Header.Var);
61
62 foreach (Dictionary<string, string> Record in Records)
63 {
64 DataRow Row = this.table.NewRow();
65
66 foreach (KeyValuePair<string, string> P in Record)
67 Row[P.Key] = P.Value;
68
69 this.table.Rows.Add(Row);
70 }
71
72 this.table.AcceptChanges();
73
74 this.SearchResultListView.ItemsSource = this.table.DefaultView;
75
76 if (this.SearchResultListView.View is GridView GridView)
77 {
78 GridView.Columns.Clear();
79
80 foreach (Field Header in this.headers)
81 {
82 GridView.Columns.Add(new GridViewColumn()
83 {
84 Header = Header.Label,
85 DisplayMemberBinding = new Binding(Header.Var)
86 });
87 }
88 }
89 }
90
91 public void Dispose()
92 {
93 if (!(this.table is null))
94 {
95 this.table.Dispose();
96 this.table = null;
97 }
98 }
99
100 public void NewButton_Click(object Sender, RoutedEventArgs e)
101 {
102 this.table.Clear();
103 }
104
105 public void SaveButton_Click(object Sender, RoutedEventArgs e)
106 {
107 this.SaveAsButton_Click(Sender, e);
108 }
109
110 public void SaveAsButton_Click(object Sender, RoutedEventArgs e)
111 {
112 SaveFileDialog Dialog = new SaveFileDialog()
113 {
114 AddExtension = true,
115 CheckPathExists = true,
116 CreatePrompt = false,
117 DefaultExt = "html",
118 Filter = "XML Files (*.xml)|*.xml|HTML Files (*.html,*.htm)|*.html,*.htm|All Files (*.*)|*.*",
119 Title = "Save Search Result"
120 };
121
122 bool? Result = Dialog.ShowDialog(MainWindow.FindWindow(this));
123
124 if (Result.HasValue && Result.Value)
125 {
126 try
127 {
128 if (Dialog.FilterIndex == 2)
129 {
130 StringBuilder Xml = new StringBuilder();
131 using (XmlWriter w = XmlWriter.Create(Xml, XML.WriterSettings(true, true)))
132 {
133 this.SaveAsXml(w);
134 }
135
136 string Html = XSL.Transform(Xml.ToString(), searchResultToHtml);
137
138 File.WriteAllText(Dialog.FileName, Html, System.Text.Encoding.UTF8);
139 }
140 else
141 {
142 using (FileStream f = File.Create(Dialog.FileName))
143 {
144 using (XmlWriter w = XmlWriter.Create(f, XML.WriterSettings(true, false)))
145 {
146 this.SaveAsXml(w);
147 }
148 }
149 }
150 }
151 catch (Exception ex)
152 {
153 MessageBox.Show(MainWindow.FindWindow(this), ex.Message, "Unable to save file.", MessageBoxButton.OK, MessageBoxImage.Error);
154 }
155 }
156 }
157
158 private static readonly XslCompiledTransform searchResultToHtml = XSL.LoadTransform("Waher.Client.WPF.Transforms.SearchResultToHTML.xslt");
159 private static readonly XmlSchema schema = XSL.LoadSchema("Waher.Client.WPF.Schema.SearchResult.xsd");
160 private const string searchResultNamespace = "http://waher.se/Schema/SearchResult.xsd";
161 private const string searchResultRoot = "SearchResult";
162
163 private void SaveAsXml(XmlWriter w)
164 {
165 w.WriteStartElement(searchResultRoot, searchResultNamespace);
166 w.WriteStartElement("Headers");
167
168 foreach (Field Header in this.headers)
169 {
170 w.WriteStartElement("Header");
171 w.WriteAttributeString("var", Header.Var);
172 w.WriteAttributeString("label", Header.Label);
173 w.WriteEndElement();
174 }
175
176 w.WriteEndElement();
177 w.WriteStartElement("Records");
178
179 foreach (Dictionary<string, string> Record in this.records)
180 {
181 w.WriteStartElement("Record");
182
183 foreach (KeyValuePair<string, string> Field in Record)
184 {
185 w.WriteStartElement("Field");
186 w.WriteAttributeString("var", Field.Key);
187 w.WriteAttributeString("value", Field.Value);
188 w.WriteEndElement();
189 }
190
191 w.WriteEndElement();
192 }
193
194 w.WriteEndElement();
195 w.WriteEndElement();
196 w.Flush();
197 }
198
199 public void OpenButton_Click(object Sender, RoutedEventArgs e)
200 {
201 try
202 {
203 OpenFileDialog Dialog = new OpenFileDialog()
204 {
205 AddExtension = true,
206 CheckFileExists = true,
207 CheckPathExists = true,
208 DefaultExt = "xml",
209 Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
210 Multiselect = false,
211 ShowReadOnly = true,
212 Title = "Open Search Result"
213 };
214
215 bool? Result = Dialog.ShowDialog(MainWindow.FindWindow(this));
216
217 if (Result.HasValue && Result.Value)
218 {
219 XmlDocument Xml = new XmlDocument()
220 {
221 PreserveWhitespace = true
222 };
223 Xml.Load(Dialog.FileName);
224
225 this.Load(Xml, Dialog.FileName);
226 }
227 }
228 catch (Exception ex)
229 {
230 ex = Log.UnnestException(ex);
231 MessageBox.Show(ex.Message, "Unable to load file.", MessageBoxButton.OK, MessageBoxImage.Error);
232 }
233 }
234
235 public void Load(XmlDocument Xml, string FileName)
236 {
237 XSL.Validate(FileName, Xml, searchResultRoot, searchResultNamespace, schema);
238
239 List<Field> Headers = new List<Field>();
240 List<Dictionary<string, string>> Records = new List<Dictionary<string, string>>();
241
242 foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
243 {
244 if (N is XmlElement E)
245 {
246 switch (E.LocalName)
247 {
248 case "Headers":
249 foreach (XmlNode N2 in E.ChildNodes)
250 {
251 if (N2 is XmlElement E2 && E2.LocalName == "Header")
252 {
253 string Var = XML.Attribute(E2, "var");
254 string Label = XML.Attribute(E2, "label");
255
256 Headers.Add(new TextSingleField(null, Var, Label, false, null, null, string.Empty,
257 new StringDataType(), new BasicValidation(), string.Empty, false, false, false));
258 }
259 }
260 break;
261
262 case "Records":
263 foreach (XmlNode N2 in E.ChildNodes)
264 {
265 if (N2 is XmlElement E2 && E2.LocalName == "Record")
266 {
267 Dictionary<string, string> Record = new Dictionary<string, string>();
268
269 foreach (XmlNode N3 in E2.ChildNodes)
270 {
271 if (N3 is XmlElement E3 && E3.LocalName == "Field")
272 {
273 string Var = XML.Attribute(E3, "var");
274 string Value = XML.Attribute(E3, "value");
275
276 Record[Var] = Value;
277 }
278 }
279
280 Records.Add(Record);
281 }
282 }
283 break;
284 }
285 }
286 }
287
288 this.Init(Headers.ToArray(), Records.ToArray());
289 }
290 }
291}
Interaction logic for SensorDataView.xaml
Interaction logic for xaml
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
Definition: XML.cs:1177
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:15
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:23
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:70
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
Definition: XSL.cs:162
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:118
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
Base class for form fields
Definition: Field.cs:16
string Var
Variable name
Definition: Field.cs:81
Interface for tab view user controls in the client.
Definition: ITabView.cs:10