DigitalFormGrid.cs 26 KB

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