DigitalFormGrid.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media.Imaging;
  11. using FastReport;
  12. using InABox.Clients;
  13. using InABox.Core;
  14. using InABox.Core.Reports;
  15. using InABox.Scripting;
  16. using InABox.Wpf;
  17. using InABox.WPF;
  18. using Microsoft.Win32;
  19. using NPOI.HPSF;
  20. namespace InABox.DynamicGrid
  21. {
  22. public class DigitalFormExportData
  23. {
  24. public string Code { get; set; }
  25. public string Description { get; set; }
  26. public string AppliesTo { get; set; }
  27. public DigitalFormExportData() { }
  28. public DigitalFormExportData(DigitalForm form)
  29. {
  30. Code = form.Code;
  31. Description = form.Description;
  32. AppliesTo = form.AppliesTo;
  33. }
  34. public List<LayoutData> Layouts { get; set; }
  35. public List<VariableData> Variables { get; set; }
  36. public List<DocumentData> Documents { get; set; }
  37. public DigitalForm ToForm() => new DigitalForm
  38. {
  39. Code = Code,
  40. Description = Description,
  41. AppliesTo = AppliesTo
  42. };
  43. public class LayoutData
  44. {
  45. public string Description { get; set; }
  46. public DFLayoutType Type { get; set; }
  47. public string Layout { get; set; }
  48. public LayoutData() { }
  49. public LayoutData(DigitalFormLayout layout)
  50. {
  51. Description = layout.Description;
  52. Type = layout.Type;
  53. Layout = layout.Layout;
  54. }
  55. public DigitalFormLayout ToLayout() => new DigitalFormLayout
  56. {
  57. Description = Description,
  58. Type = Type,
  59. Layout = Layout
  60. };
  61. }
  62. public class VariableData
  63. {
  64. public string Code { get; set; }
  65. public string Description { get; set; }
  66. public string VariableType { get; set; }
  67. public string Parameters { get; set; }
  68. public bool Required { get; set; }
  69. public bool Secure { get; set; }
  70. public bool Retain { get; set; }
  71. public bool Hidden { get; set; }
  72. public long Sequence { get; set; }
  73. public VariableData() { }
  74. public VariableData(DigitalFormVariable variable)
  75. {
  76. Code = variable.Code;
  77. Description = variable.Description;
  78. VariableType = variable.VariableType;
  79. Parameters = variable.Parameters;
  80. Required = variable.Required;
  81. Secure = variable.Secure;
  82. Retain = variable.Retain;
  83. Hidden = variable.Hidden;
  84. Sequence = variable.Sequence;
  85. }
  86. public DigitalFormVariable ToVariable() => new DigitalFormVariable
  87. {
  88. Code = Code,
  89. Description = Description,
  90. VariableType = VariableType,
  91. Parameters = Parameters,
  92. Required = Required,
  93. Secure = Secure,
  94. Retain = Retain,
  95. Hidden = Hidden,
  96. Sequence = Sequence
  97. };
  98. }
  99. public class DocumentData
  100. {
  101. public string FileName { get; set; }
  102. public byte[] Data { get; set; }
  103. public Guid ID { get; set; }
  104. public DocumentData() { }
  105. public DocumentData(Document document)
  106. {
  107. FileName = document.FileName;
  108. Data = document.Data;
  109. ID = document.ID;
  110. }
  111. public Document ToDocument()
  112. {
  113. var doc = new Document()
  114. {
  115. ID = ID
  116. };
  117. doc.CommitChanges();
  118. doc.FileName = FileName;
  119. doc.Data = Data;
  120. doc.TimeStamp = DateTime.Now;
  121. return doc;
  122. }
  123. }
  124. }
  125. public class DigitalFormGrid : DynamicDataGrid<DigitalForm>
  126. {
  127. private bool _showall;
  128. private Button CopyForm = null!; // Late-initialised
  129. protected override void Init()
  130. {
  131. base.Init();
  132. AddButton("Show All", null, ShowAllClick);
  133. // TODO: Add back in
  134. //ActionColumns.Add(new DynamicImageColumn(ReportImage, ReportClick));
  135. if (Security.CanView<DigitalFormGroup>())
  136. {
  137. AddButton("Groups", null, EditGroupsClick);
  138. }
  139. CopyForm = AddButton("Duplicate", InABox.Wpf.Resources.copy.AsBitmapImage(), CopyForm_Click);
  140. CopyForm.IsEnabled = false;
  141. if (!Security.CanEdit<DigitalForm>())
  142. {
  143. CopyForm.Visibility = Visibility.Collapsed;
  144. }
  145. OnCustomiseEditor += DigitalFormGrid_OnCustomiseEditor;
  146. }
  147. protected override void DoReconfigure(DynamicGridOptions options)
  148. {
  149. base.DoReconfigure(options);
  150. options.ImportData = true;
  151. options.ExportData = true;
  152. options.FilterRows = true;
  153. options.SelectColumns = true;
  154. }
  155. protected override void SelectItems(CoreRow[]? rows)
  156. {
  157. base.SelectItems(rows);
  158. CopyForm.IsEnabled = rows is not null && rows.Length == 1;
  159. }
  160. private bool CopyForm_Click(Button btn, CoreRow[] rows)
  161. {
  162. if(rows.Length != 1)
  163. {
  164. MessageWindow.ShowMessage("Please select one form to copy.", "Invalid selection.");
  165. return false;
  166. }
  167. var form = rows[0].ToObject<DigitalForm>();
  168. Client.EnsureColumns(form,
  169. Columns.None<DigitalForm>().Add(x => x.Description)
  170. .Add(x => x.AppliesTo)
  171. .Add(x => x.Secure)
  172. .Add(x => x.Final)
  173. .Add(x => x.Group.ID));
  174. var newForm = new DigitalForm();
  175. newForm.Description = form.Description;
  176. newForm.AppliesTo = form.AppliesTo;
  177. newForm.Secure = form.Secure;
  178. newForm.Final = form.Final;
  179. newForm.Group.ID = form.Group.ID;
  180. // Note that we don't load the ID for any of these entities.
  181. var children = Client.QueryMultiple(
  182. new KeyedQueryDef<DigitalFormLayout>(
  183. Filter<DigitalFormLayout>.Where(x => x.Form.ID).IsEqualTo(form.ID),
  184. Columns.None<DigitalFormLayout>()
  185. .Add(x => x.Description)
  186. .Add(x => x.Type)
  187. .Add(x => x.Layout)
  188. .Add(x => x.Active)),
  189. new KeyedQueryDef<DigitalFormVariable>(
  190. Filter<DigitalFormVariable>.Where(x => x.Form.ID).IsEqualTo(form.ID),
  191. Columns.None<DigitalFormVariable>().Add(x => x.Code)
  192. .Add(x => x.Description)
  193. .Add(x => x.VariableType)
  194. .Add(x => x.Group)
  195. .Add(x => x.Parameters)
  196. .Add(x => x.Required)
  197. .Add(x => x.Secure)
  198. .Add(x => x.Retain)
  199. .Add(x => x.Hidden)
  200. .Add(x => x.Sequence)),
  201. new KeyedQueryDef<ReportTemplate>(
  202. Filter<ReportTemplate>.Where(x => x.Section).IsEqualTo(form.ID.ToString()),
  203. Columns.None<ReportTemplate>().Add(x => x.Section)
  204. .Add(x=>x.Name)
  205. .Add(x=>x.Script)
  206. .Add(x=>x.Visible)
  207. .Add(x=>x.AllRecords)
  208. .Add(x=>x.DataModel)
  209. .Add(x=>x.PrinterName)
  210. .Add(x=>x.SelectedRecords)
  211. .Add(x=>x.RDL)
  212. .Add(x=>x.Section)),
  213. new KeyedQueryDef<DigitalFormDocument>(
  214. Filter<DigitalFormDocument>.Where(x => x.EntityLink.ID).IsEqualTo(form.ID),
  215. Columns.None<DigitalFormDocument>().Add(x => x.Type)
  216. .Add(x => x.DocumentLink.ID)
  217. .Add(x => x.Superceded)
  218. .Add(x => x.Thumbnail)
  219. .Add(x => x.Notes)));
  220. if (EditItems([newForm], type =>
  221. {
  222. return children.GetOrDefault(type.Name);
  223. }))
  224. {
  225. return true;
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. }
  232. private bool EditGroupsClick(Button arg1, CoreRow[] arg2)
  233. {
  234. new MasterList(typeof(DigitalFormGroup)).ShowDialog();
  235. return false;
  236. }
  237. private BitmapImage? ReportImage(CoreRow arg)
  238. {
  239. return arg != null ? Wpf.Resources.printer.AsBitmapImage() : null;
  240. }
  241. /*private bool ReportClick(CoreRow arg)
  242. {
  243. if (arg == null)
  244. return false;
  245. var typename = arg.Get<DigitalForm, string>(c => c.AppliesTo);
  246. var formid = arg.Get<DigitalForm, Guid>(c => c.ID);
  247. // Get Applies To
  248. /*Type type = CoreUtils.GetEntity("Comal.Classes."+typename);
  249. CoreTable entity = new CoreTable();
  250. entity.LoadColumns(type);
  251. // Get Form Details
  252. CoreTable form = new CoreTable();
  253. form.Columns.Add(new CoreColumn() { ColumnName = "ID", DataType = typeof(Guid) });
  254. form.Columns.Add(new CoreColumn() { ColumnName = "Parent.ID", DataType = typeof(Guid) });
  255. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Form.Description, "."), DataType = typeof(String) });
  256. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.FormCompletedBy.UserID, "."), DataType = typeof(String) });
  257. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, DateTime>(x => x.FormCompleted, "."), DataType = typeof(String) });
  258. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Location.Address, "."), DataType = typeof(String) });
  259. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Latitude, "."), DataType = typeof(String) });
  260. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Longitude, "."), DataType = typeof(String) });
  261. var variables = new Client<DigitalFormVariable>().Query(Filter<DigitalFormVariable>.Where(x => x.Form.ID).IsEqualTo(formid));
  262. foreach (var row in variables.Rows)
  263. {
  264. form.Columns.Add(
  265. new CoreColumn()
  266. {
  267. ColumnName = row.Get<DigitalFormVariable, String>(c => c.Code),
  268. DataType = DigitalFormVariable.DataType(row.Get<DigitalFormVariable, DigitalFormVariableType>(c => c.VariableType))
  269. }
  270. );
  271. }*
  272. // Create DataModel
  273. //DigitalFormReportDataModel model = new DigitalFormReportDataModel(form,entity,typename);
  274. AutoDataModel<DigitalForm> model = new AutoDataModel<DigitalForm>(Filter<DigitalForm>.Where(x => x.ID).IsEqualTo(formid));
  275. // Load Template
  276. var template = new Client<ReportTemplate>()
  277. .Load(Filter<ReportTemplate>.Where(x => x.Section).IsEqualTo("DigitalForms").And(x => x.Name).IsEqualTo(formid.ToString()))
  278. .FirstOrDefault();
  279. if (template == null)
  280. {
  281. template = new ReportTemplate
  282. {
  283. Section = "DigitalForms",
  284. Name = formid.ToString(),
  285. Visible = false
  286. };
  287. new Client<ReportTemplate>().Save(template, "");
  288. }
  289. ReportUtils.DesignReport(template, model);
  290. // Preview Report
  291. return false;
  292. }*/
  293. private bool ShowAllClick(Button arg1, CoreRow[] arg2)
  294. {
  295. _showall = !_showall;
  296. UpdateButton(arg1, null, _showall ? "Hide Inactive" : "Show All");
  297. return true;
  298. }
  299. protected override void Reload(
  300. Filters<DigitalForm> criteria, Columns<DigitalForm> columns, ref SortOrder<DigitalForm>? sort,
  301. CancellationToken token, Action<CoreTable?, Exception?> action)
  302. {
  303. if (!_showall)
  304. criteria.Add(Filter<DigitalForm>.Where(x => x.Active).IsEqualTo(true));
  305. base.Reload(criteria, columns, ref sort, token, action);
  306. }
  307. public override DynamicEditorPages LoadEditorPages(DigitalForm item)
  308. {
  309. var pages = base.LoadEditorPages(item);
  310. pages.Add(new DigitalFormReportGrid());
  311. return pages;
  312. }
  313. private DynamicVariableGrid? GetVariableGrid(IDynamicEditorForm sender)
  314. => sender.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  315. as DynamicVariableGrid;
  316. private List<DigitalFormVariable> GetVariables(IDynamicEditorForm sender)
  317. => GetVariableGrid(sender)?.Items.ToList() ?? new List<DigitalFormVariable>();
  318. // Using the event because it also has the editor form 'sender'.
  319. private void DigitalFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, DigitalForm[]? items, DynamicGridColumn column, BaseEditor editor)
  320. {
  321. if(editor is ExpressionEditor exp && ( new Column<DigitalForm>(x => x.DescriptionExpression).IsEqualTo(column.ColumnName) || new Column<DigitalForm>(x => x.ExportExpression).IsEqualTo(column.ColumnName)))
  322. {
  323. exp.OnGetVariables += () =>
  324. {
  325. var variables = new List<string>();
  326. foreach (var variable in GetVariables(sender))
  327. {
  328. foreach (var col in variable.GetVariableColumns())
  329. variables.Add($"Data.{col.ColumnName}");
  330. }
  331. var appliesTo = items?.Select(x => x.AppliesTo).Distinct().SingleOrDefault();
  332. if (!appliesTo.IsNullOrWhiteSpace() && DFUtils.GetFormInstanceType(appliesTo) is Type instanceType)
  333. {
  334. foreach(var property in DatabaseSchema.Properties(instanceType).Where(x => !x.Name.StartsWith("Parent.")))
  335. variables.Add($"Form.{property.Name}");
  336. }
  337. if (!appliesTo.IsNullOrWhiteSpace() && DFUtils.FormEntityType(appliesTo) is Type entityType)
  338. {
  339. foreach(var property in DatabaseSchema.Properties(entityType))
  340. variables.Add($"{entityType.EntityName().Split('.').Last()}.{property.Name}");
  341. }
  342. variables.Sort();
  343. return variables;
  344. };
  345. }
  346. }
  347. public override bool EditItems(DigitalForm[] items, Func<Type, CoreTable?>? PageDataHandler = null, bool PreloadPages = false)
  348. {
  349. // Need to do this to make sure that the variables are available to the layouts (and vice versa?)
  350. return base.EditItems(items, PageDataHandler, true);
  351. }
  352. private const string ExportFileFilter = "Excel Files (*.xls, *xlsx)|*.xls;*.xlsx|Digital Forms (*.prs-form)|*.prs-form";
  353. protected override void DoImport()
  354. {
  355. var dialog = new OpenFileDialog
  356. {
  357. Filter = ExportFileFilter
  358. };
  359. if (dialog.ShowDialog() != true)
  360. return;
  361. String extension = Path.GetExtension(dialog.FileName) ?? "";
  362. if (extension.ToLower().Equals(".xls") || extension.ToLower().Equals(".xlsx"))
  363. {
  364. String appliesto = "";
  365. String code = "";
  366. var lookups = new DigitalFormCategoryLookups(null).Lookups();
  367. if (!DictionaryComboEdit.Execute<String>(lookups, ref appliesto, "Applies To", "Select Form Type") || String.IsNullOrWhiteSpace(appliesto))
  368. return;
  369. Progress.ShowModal("Creating Form", (progress) =>
  370. {
  371. var codes = new Client<DigitalForm>().Query(
  372. null,
  373. Columns.None<DigitalForm>().Add(x => x.Code),
  374. null
  375. ).Rows.Select(r => r.Get<DigitalForm, String>(c => c.Code)).ToArray();
  376. int i = 1;
  377. code = Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper();
  378. while (codes.Contains(code))
  379. code = $"{Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper()} ({i++})";
  380. var form = new DigitalForm
  381. {
  382. Code = code,
  383. Description = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetFileNameWithoutExtension(dialog.FileName)),
  384. AppliesTo = appliesto
  385. };
  386. Client.Save(form, $"Imported from {dialog.FileName}");
  387. progress.Report("Importing Data");
  388. DFLayout data;
  389. var variables = new List<DigitalFormVariable>();
  390. var layout = new DigitalFormLayout();
  391. layout.Form.ID = form.ID;
  392. layout.Description = form.Description;
  393. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  394. {
  395. var spreadsheet = new Spreadsheet(fs);
  396. data = DigitalFormUtils.LoadLayout(spreadsheet);
  397. layout.Layout = data.SaveLayout();
  398. }
  399. Client.Save(layout, $"Imported from {dialog.FileName}");
  400. progress.Report("Setting Up Variables");
  401. String group = "";
  402. foreach (var element in data.Elements)
  403. {
  404. if (element is DFLayoutHeader header)
  405. {
  406. group = header.Header;
  407. }
  408. else if (element is DFLayoutField field)
  409. {
  410. var variable = new DigitalFormVariable();
  411. variable.Form.ID = form.ID;
  412. variable.SetFieldType(field.GetType());
  413. variable.SaveProperties(field.GetProperties());
  414. variable.Group = group;
  415. variable.Code = field.Name;
  416. variable.Description = field.Name;
  417. variables.Add(variable);
  418. }
  419. }
  420. Client.Save(variables, $"Imported from {dialog.FileName}");
  421. progress.Report("Creating Report");
  422. var model = DigitalFormUtils.GetDataModel(appliesto, variables);
  423. var template = DigitalFormUtils.GenerateReport(layout, model);
  424. var report = new ReportTemplate();
  425. report.Section = form.ID.ToString();
  426. report.DataModel = model.Name;
  427. report.Name = form.Description;
  428. report.RDL = template?.SaveToString();
  429. Client.Save(report, $"Imported from {dialog.FileName}");
  430. });
  431. MessageBox.Show($"[{code}] imported successully!");
  432. Refresh(false, true);
  433. return;
  434. }
  435. DigitalFormExportData? data;
  436. using(var stream = dialog.OpenFile())
  437. {
  438. data = Serialization.Deserialize<DigitalFormExportData>(stream);
  439. if (data is null)
  440. {
  441. MessageBox.Show("File corrupt");
  442. return;
  443. }
  444. }
  445. try
  446. {
  447. var form = data.ToForm();
  448. var layouts = data.Layouts.Select(x => x.ToLayout()).ToList();
  449. var variables = data.Variables.Select(x => x.ToVariable()).ToList();
  450. var documents = data.Documents.Select(x => x.ToDocument()).ToList();
  451. var formDocuments = new List<DigitalFormDocument>();
  452. foreach (var document in documents)
  453. {
  454. Client.Save(document, "");
  455. var digitalFormDocument = new DigitalFormDocument();
  456. digitalFormDocument.DocumentLink.ID = document.ID;
  457. digitalFormDocument.DocumentLink.Synchronise(document);
  458. formDocuments.Add(digitalFormDocument);
  459. }
  460. if (EditItems([form], (type) =>
  461. {
  462. var table = new CoreTable();
  463. table.LoadColumns(type);
  464. if(type == typeof(DigitalFormLayout))
  465. {
  466. table.LoadRows(layouts);
  467. }
  468. else if(type == typeof(DigitalFormVariable))
  469. {
  470. table.LoadRows(variables);
  471. }
  472. else if(type == typeof(DigitalFormDocument))
  473. {
  474. table.LoadRows(formDocuments);
  475. }
  476. return table;
  477. }))
  478. {
  479. Refresh(false, true);
  480. }
  481. }
  482. catch(Exception e)
  483. {
  484. MessageBox.Show(e.Message);
  485. }
  486. }
  487. protected override void DoExport()
  488. {
  489. var rows = SelectedRows;
  490. if(rows.Length == 0)
  491. {
  492. MessageBox.Show("Please select a form to export.");
  493. return;
  494. }
  495. else if(rows.Length > 1)
  496. {
  497. MessageBox.Show("Please select only one form to export.");
  498. return;
  499. }
  500. var formID = rows[0].Get<DigitalForm, Guid>(x => x.ID);
  501. var results = Client.QueryMultiple(
  502. new KeyedQueryDef<DigitalForm>(
  503. Filter<DigitalForm>.Where(x => x.ID).IsEqualTo(formID),
  504. Columns.None<DigitalForm>().Add(x => x.Code)
  505. .Add(x => x.Description)
  506. .Add(x => x.AppliesTo)),
  507. new KeyedQueryDef<DigitalFormLayout>(
  508. Filter<DigitalFormLayout>.Where(x => x.Form.ID).IsEqualTo(formID),
  509. Columns.None<DigitalFormLayout>()
  510. .Add(x => x.Description)
  511. .Add(x => x.Type)
  512. .Add(x => x.Layout)),
  513. new KeyedQueryDef<DigitalFormVariable>(
  514. Filter<DigitalFormVariable>.Where(x => x.Form.ID).IsEqualTo(formID),
  515. Columns.None<DigitalFormVariable>().Add(x => x.Code)
  516. .Add(x => x.Description)
  517. .Add(x => x.VariableType)
  518. .Add(x => x.Parameters)
  519. .Add(x => x.Required)
  520. .Add(x => x.Secure)
  521. .Add(x => x.Retain)
  522. .Add(x => x.Hidden)
  523. .Add(x => x.Sequence)),
  524. new KeyedQueryDef<Document>(
  525. Filter<Document>.Where(x => x.ID).InQuery(
  526. Filter<DigitalFormDocument>.Where(x => x.EntityLink.ID).IsEqualTo(formID),
  527. x => x.DocumentLink.ID),
  528. Columns.None<Document>().Add(x => x.FileName)
  529. .Add(x => x.Data)));
  530. var data = new DigitalFormExportData(results.Get<DigitalForm>().Rows.First().ToObject<DigitalForm>())
  531. {
  532. Layouts = results.Get<DigitalFormLayout>().ToList<DigitalFormLayout>().ToList(x => new DigitalFormExportData.LayoutData(x)),
  533. Variables = results.Get<DigitalFormVariable>().ToList<DigitalFormVariable>().ToList(x => new DigitalFormExportData.VariableData(x)),
  534. Documents = results.Get<Document>().ToList<Document>().ToList(x => new DigitalFormExportData.DocumentData(x))
  535. };
  536. var filename = rows[0].Get<DigitalForm, string>(x => x.Description);
  537. if (string.IsNullOrWhiteSpace(filename))
  538. filename = rows[0].Get<DigitalForm, string>(x => x.Code);
  539. if (string.IsNullOrWhiteSpace(filename))
  540. filename = "form";
  541. var dialog = new SaveFileDialog
  542. {
  543. Filter = ExportFileFilter,
  544. FileName = $"{filename}.prs-form"
  545. };
  546. if(dialog.ShowDialog() == true)
  547. {
  548. using var stream = dialog.OpenFile();
  549. Serialization.Serialize(data, stream);
  550. }
  551. }
  552. protected override void DoValidate(DigitalForm[] items, List<string> errors)
  553. {
  554. base.DoValidate(items, errors);
  555. if (items.Any(x => string.IsNullOrWhiteSpace(x.AppliesTo)))
  556. errors.Add("[Applies To] must not be blank!");
  557. }
  558. }
  559. }