DigitalFormGrid.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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("Duplicate", 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(DynamicGridOptions options)
  142. {
  143. base.DoReconfigure(options);
  144. options.ImportData = true;
  145. options.ExportData = true;
  146. options.FilterRows = true;
  147. options.SelectColumns = true;
  148. }
  149. protected override void SelectItems(CoreRow[]? rows)
  150. {
  151. base.SelectItems(rows);
  152. CopyForm.IsEnabled = rows is not null && rows.Length == 1;
  153. }
  154. private bool CopyForm_Click(Button btn, CoreRow[] rows)
  155. {
  156. if(rows.Length != 1)
  157. {
  158. MessageWindow.ShowMessage("Please select one form to copy.", "Invalid selection.");
  159. return false;
  160. }
  161. var form = rows[0].ToObject<DigitalForm>();
  162. Client.EnsureColumns(form,
  163. Columns.None<DigitalForm>().Add(x => x.Description)
  164. .Add(x => x.AppliesTo)
  165. .Add(x => x.Secure)
  166. .Add(x => x.Final)
  167. .Add(x => x.Group.ID));
  168. var newForm = new DigitalForm();
  169. newForm.Description = form.Description;
  170. newForm.AppliesTo = form.AppliesTo;
  171. newForm.Secure = form.Secure;
  172. newForm.Final = form.Final;
  173. newForm.Group.ID = form.Group.ID;
  174. var children = Client.QueryMultiple(
  175. new KeyedQueryDef<DigitalFormLayout>(
  176. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.ID),
  177. Columns.None<DigitalFormLayout>().Add(x => x.Code)
  178. .Add(x => x.Description)
  179. .Add(x => x.Type)
  180. .Add(x => x.Layout)
  181. .Add(x => x.Active)),
  182. new KeyedQueryDef<DigitalFormVariable>(
  183. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(form.ID),
  184. Columns.None<DigitalFormVariable>().Add(x => x.Code)
  185. .Add(x => x.Description)
  186. .Add(x => x.VariableType)
  187. .Add(x => x.Group)
  188. .Add(x => x.Parameters)
  189. .Add(x => x.Required)
  190. .Add(x => x.Secure)
  191. .Add(x => x.Retain)
  192. .Add(x => x.Hidden)
  193. .Add(x => x.Sequence)),
  194. new KeyedQueryDef<ReportTemplate>(
  195. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(form.ID.ToString()),
  196. Columns.None<ReportTemplate>().Add(x => x.Section)
  197. .Add(x=>x.Name)
  198. .Add(x=>x.Script)
  199. .Add(x=>x.Visible)
  200. .Add(x=>x.AllRecords)
  201. .Add(x=>x.DataModel)
  202. .Add(x=>x.PrinterName)
  203. .Add(x=>x.SelectedRecords)
  204. .Add(x=>x.RDL)
  205. .Add(x=>x.Section)),
  206. new KeyedQueryDef<DigitalFormDocument>(
  207. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(form.ID),
  208. Columns.None<DigitalFormDocument>().Add(x => x.Type)
  209. .Add(x => x.DocumentLink.ID)
  210. .Add(x => x.Superceded)
  211. .Add(x => x.Thumbnail)
  212. .Add(x => x.Notes)));
  213. if (EditItems(new[] { newForm }, type =>
  214. {
  215. return children.GetOrDefault(type.Name);
  216. }))
  217. {
  218. return true;
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. private bool EditGroupsClick(Button arg1, CoreRow[] arg2)
  226. {
  227. new MasterList(typeof(DigitalFormGroup)).ShowDialog();
  228. return false;
  229. }
  230. private BitmapImage? ReportImage(CoreRow arg)
  231. {
  232. return arg != null ? Wpf.Resources.printer.AsBitmapImage() : null;
  233. }
  234. /*private bool ReportClick(CoreRow arg)
  235. {
  236. if (arg == null)
  237. return false;
  238. var typename = arg.Get<DigitalForm, string>(c => c.AppliesTo);
  239. var formid = arg.Get<DigitalForm, Guid>(c => c.ID);
  240. // Get Applies To
  241. /*Type type = CoreUtils.GetEntity("Comal.Classes."+typename);
  242. CoreTable entity = new CoreTable();
  243. entity.LoadColumns(type);
  244. // Get Form Details
  245. CoreTable form = new CoreTable();
  246. form.Columns.Add(new CoreColumn() { ColumnName = "ID", DataType = typeof(Guid) });
  247. form.Columns.Add(new CoreColumn() { ColumnName = "Parent.ID", DataType = typeof(Guid) });
  248. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Form.Description, "."), DataType = typeof(String) });
  249. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.FormCompletedBy.UserID, "."), DataType = typeof(String) });
  250. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, DateTime>(x => x.FormCompleted, "."), DataType = typeof(String) });
  251. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Location.Address, "."), DataType = typeof(String) });
  252. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Latitude, "."), DataType = typeof(String) });
  253. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Longitude, "."), DataType = typeof(String) });
  254. var variables = new Client<DigitalFormVariable>().Query(new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formid));
  255. foreach (var row in variables.Rows)
  256. {
  257. form.Columns.Add(
  258. new CoreColumn()
  259. {
  260. ColumnName = row.Get<DigitalFormVariable, String>(c => c.Code),
  261. DataType = DigitalFormVariable.DataType(row.Get<DigitalFormVariable, DigitalFormVariableType>(c => c.VariableType))
  262. }
  263. );
  264. }*
  265. // Create DataModel
  266. //DigitalFormReportDataModel model = new DigitalFormReportDataModel(form,entity,typename);
  267. AutoDataModel<DigitalForm> model = new AutoDataModel<DigitalForm>(new Filter<DigitalForm>(x => x.ID).IsEqualTo(formid));
  268. // Load Template
  269. var template = new Client<ReportTemplate>()
  270. .Load(new Filter<ReportTemplate>(x => x.Section).IsEqualTo("DigitalForms").And(x => x.Name).IsEqualTo(formid.ToString()))
  271. .FirstOrDefault();
  272. if (template == null)
  273. {
  274. template = new ReportTemplate
  275. {
  276. Section = "DigitalForms",
  277. Name = formid.ToString(),
  278. Visible = false
  279. };
  280. new Client<ReportTemplate>().Save(template, "");
  281. }
  282. ReportUtils.DesignReport(template, model);
  283. // Preview Report
  284. return false;
  285. }*/
  286. private bool ShowAllClick(Button arg1, CoreRow[] arg2)
  287. {
  288. _showall = !_showall;
  289. UpdateButton(arg1, null, _showall ? "Hide Inactive" : "Show All");
  290. return true;
  291. }
  292. protected override void Reload(Filters<DigitalForm> criteria, Columns<DigitalForm> columns, ref SortOrder<DigitalForm>? sort,
  293. Action<CoreTable?, Exception?> action)
  294. {
  295. if (!_showall)
  296. criteria.Add(new Filter<DigitalForm>(x => x.Active).IsEqualTo(true));
  297. base.Reload(criteria, columns, ref sort, action);
  298. }
  299. public override DynamicEditorPages LoadEditorPages(DigitalForm item)
  300. {
  301. var pages = base.LoadEditorPages(item);
  302. pages.Add(new DigitalFormReportGrid());
  303. return pages;
  304. }
  305. private DynamicVariableGrid? GetVariableGrid(IDynamicEditorForm sender)
  306. => sender.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  307. as DynamicVariableGrid;
  308. private List<DigitalFormVariable> GetVariables(IDynamicEditorForm sender)
  309. => GetVariableGrid(sender)?.Items.ToList() ?? new List<DigitalFormVariable>();
  310. // Using the event because it also has the editor form 'sender'.
  311. private void DigitalFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, DigitalForm[]? items, DynamicGridColumn column, BaseEditor editor)
  312. {
  313. if(editor is ExpressionEditor exp && ( new Column<DigitalForm>(x => x.DescriptionExpression).IsEqualTo(column.ColumnName) || new Column<DigitalForm>(x => x.ExportExpression).IsEqualTo(column.ColumnName)))
  314. {
  315. exp.OnGetVariables += () =>
  316. {
  317. var variables = new List<string>();
  318. foreach (var variable in GetVariables(sender))
  319. {
  320. foreach (var col in variable.GetVariableColumns())
  321. variables.Add($"Data.{col.ColumnName}");
  322. }
  323. var appliesTo = items?.Select(x => x.AppliesTo).Distinct().SingleOrDefault();
  324. if (!appliesTo.IsNullOrWhiteSpace() && DFUtils.GetFormInstanceType(appliesTo) is Type instanceType)
  325. {
  326. foreach(var property in DatabaseSchema.Properties(instanceType).Where(x => !x.Name.StartsWith("Parent")))
  327. variables.Add($"Form.{property.Name}");
  328. }
  329. if (!appliesTo.IsNullOrWhiteSpace() && DFUtils.FormEntityType(appliesTo) is Type entityType)
  330. {
  331. foreach(var property in DatabaseSchema.Properties(entityType))
  332. variables.Add($"{entityType.EntityName().Split('.').Last()}.{property.Name}");
  333. }
  334. variables.Sort();
  335. return variables;
  336. };
  337. }
  338. }
  339. public override bool EditItems(DigitalForm[] items, Func<Type, CoreTable?>? PageDataHandler = null, bool PreloadPages = false)
  340. {
  341. // Need to do this to make sure that the variables are available to the layouts (and vice versa?)
  342. return base.EditItems(items, PageDataHandler, true);
  343. }
  344. private const string ExportFileFilter = "Excel Files (*.xls, *xlsx)|*.xls;*.xlsx|Digital Forms (*.prs-form)|*.prs-form";
  345. protected override void DoImport()
  346. {
  347. var dialog = new OpenFileDialog
  348. {
  349. Filter = ExportFileFilter
  350. };
  351. if (dialog.ShowDialog() != true)
  352. return;
  353. String extension = Path.GetExtension(dialog.FileName) ?? "";
  354. if (extension.ToLower().Equals(".xls") || extension.ToLower().Equals(".xlsx"))
  355. {
  356. String appliesto = "";
  357. String code = "";
  358. var lookups = new DigitalFormCategoryLookups(null).Lookups();
  359. if (!DictionaryEdit.Execute<String>(lookups, ref appliesto, "Applies To", "Select Form Type") || String.IsNullOrWhiteSpace(appliesto))
  360. return;
  361. Progress.ShowModal("Creating Form", (progress) =>
  362. {
  363. var codes = new Client<DigitalForm>().Query(
  364. null,
  365. Columns.None<DigitalForm>().Add(x => x.Code),
  366. null
  367. ).Rows.Select(r => r.Get<DigitalForm, String>(c => c.Code)).ToArray();
  368. int i = 1;
  369. code = Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper();
  370. while (codes.Contains(code))
  371. code = $"{Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper()} ({i++})";
  372. DigitalForm form = new DigitalForm();
  373. form.Code = code;
  374. form.Description = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetFileNameWithoutExtension(dialog.FileName));
  375. form.AppliesTo = appliesto;
  376. new Client<DigitalForm>().Save(form, $"Imported from {dialog.FileName}");
  377. progress.Report("Importing Data");
  378. DFLayout data;
  379. var variables = new List<DigitalFormVariable>();
  380. var layout = new DigitalFormLayout();
  381. layout.Form.ID = form.ID;
  382. layout.Code = form.Code;
  383. layout.Description = form.Description;
  384. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  385. {
  386. var spreadsheet = new Spreadsheet(fs);
  387. data = DigitalFormUtils.LoadLayout(spreadsheet);
  388. layout.Layout = data.SaveLayout();
  389. }
  390. new Client<DigitalFormLayout>().Save(layout, $"Imported from {dialog.FileName}");
  391. progress.Report("Setting Up Variables");
  392. String group = "";
  393. foreach (var element in data.Elements)
  394. {
  395. if (element is DFLayoutHeader header)
  396. {
  397. group = header.Header;
  398. }
  399. else if (element is DFLayoutField field)
  400. {
  401. var variable = new DigitalFormVariable();
  402. variable.Form.ID = form.ID;
  403. variable.SetFieldType(field.GetType());
  404. variable.SaveProperties(field.GetProperties());
  405. variable.Group = group;
  406. variable.Code = field.Name;
  407. variable.Description = field.Name;
  408. variables.Add(variable);
  409. }
  410. }
  411. if (variables.Any())
  412. new Client<DigitalFormVariable>().Save(variables, $"Imported from {dialog.FileName}");
  413. progress.Report("Creating Report");
  414. var model = DigitalFormUtils.GetDataModel(appliesto, variables);
  415. var template = DigitalFormUtils.GenerateReport(layout, model);
  416. var report = new ReportTemplate();
  417. report.Section = form.ID.ToString();
  418. report.DataModel = model.Name;
  419. report.Name = form.Description;
  420. report.RDL = template?.SaveToString();
  421. new Client<ReportTemplate>().Save(report, $"Imported from {dialog.FileName}");
  422. });
  423. MessageBox.Show($"[{code}] imported successully!");
  424. Refresh(false, true);
  425. return;
  426. }
  427. DigitalFormExportData? data;
  428. using(var stream = dialog.OpenFile())
  429. {
  430. data = Serialization.Deserialize<DigitalFormExportData>(stream);
  431. if (data is null)
  432. {
  433. MessageBox.Show("File corrupt");
  434. return;
  435. }
  436. }
  437. try
  438. {
  439. var form = data.ToForm();
  440. var layouts = data.Layouts.Select(x => x.ToLayout()).ToList();
  441. var variables = data.Variables.Select(x => x.ToVariable()).ToList();
  442. var documents = data.Documents.Select(x => x.ToDocument()).ToList();
  443. var formDocuments = new List<DigitalFormDocument>();
  444. foreach (var document in documents)
  445. {
  446. new Client<Document>().Save(document, "");
  447. var digitalFormDocument = new DigitalFormDocument();
  448. digitalFormDocument.DocumentLink.ID = document.ID;
  449. digitalFormDocument.DocumentLink.Synchronise(document);
  450. formDocuments.Add(digitalFormDocument);
  451. }
  452. if (EditItems(new DigitalForm[] { form }, (type) =>
  453. {
  454. var table = new CoreTable();
  455. table.LoadColumns(type);
  456. if(type == typeof(DigitalFormLayout))
  457. {
  458. table.LoadRows(layouts);
  459. }
  460. else if(type == typeof(DigitalFormVariable))
  461. {
  462. table.LoadRows(variables);
  463. }
  464. else if(type == typeof(DigitalFormDocument))
  465. {
  466. table.LoadRows(formDocuments);
  467. }
  468. return table;
  469. }))
  470. {
  471. Refresh(false, true);
  472. }
  473. /*new Client<DigitalForm>().Save(form, "Imported by user");
  474. foreach (var layout in layouts)
  475. {
  476. layout.Form.ID = form.ID;
  477. new Client<DigitalFormLayout>().Save(layout, "");
  478. }
  479. foreach (var variable in variables)
  480. {
  481. variable.Form.ID = form.ID;
  482. new Client<DigitalFormVariable>().Save(variable, "");
  483. }
  484. foreach (var document in documents)
  485. {
  486. new Client<Document>().Save(document, "");
  487. var digitalFormDocument = new DigitalFormDocument();
  488. digitalFormDocument.EntityLink.ID = form.ID;
  489. digitalFormDocument.DocumentLink.ID = document.ID;
  490. new Client<DigitalFormDocument>().Save(digitalFormDocument, "");
  491. }*/
  492. }
  493. catch(Exception e)
  494. {
  495. MessageBox.Show(e.Message);
  496. }
  497. }
  498. protected override void DoExport()
  499. {
  500. var rows = SelectedRows;
  501. if(rows.Length == 0)
  502. {
  503. MessageBox.Show("Please select a form to export.");
  504. return;
  505. }
  506. else if(rows.Length > 1)
  507. {
  508. MessageBox.Show("Please select only one form to export.");
  509. return;
  510. }
  511. var formID = rows[0].Get<DigitalForm, Guid>(x => x.ID);
  512. var results = Client.QueryMultiple(
  513. new KeyedQueryDef<DigitalForm>(
  514. new Filter<DigitalForm>(x => x.ID).IsEqualTo(formID),
  515. Columns.None<DigitalForm>().Add(x => x.Code)
  516. .Add(x => x.Description)
  517. .Add(x => x.AppliesTo)),
  518. new KeyedQueryDef<DigitalFormLayout>(
  519. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(formID),
  520. Columns.None<DigitalFormLayout>().Add(x => x.Code)
  521. .Add(x => x.Description)
  522. .Add(x => x.Type)
  523. .Add(x => x.Layout)),
  524. new KeyedQueryDef<DigitalFormVariable>(
  525. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formID),
  526. Columns.None<DigitalFormVariable>().Add(x => x.Code)
  527. .Add(x => x.Description)
  528. .Add(x => x.VariableType)
  529. .Add(x => x.Parameters)
  530. .Add(x => x.Required)
  531. .Add(x => x.Secure)
  532. .Add(x => x.Retain)
  533. .Add(x => x.Hidden)
  534. .Add(x => x.Sequence)),
  535. new KeyedQueryDef<Document>(
  536. new Filter<Document>(x => x.ID).InQuery(
  537. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(formID),
  538. x => x.DocumentLink.ID),
  539. Columns.None<Document>().Add(x => x.FileName)
  540. .Add(x => x.Data)));
  541. var data = new DigitalFormExportData(results.Get<DigitalForm>().Rows.First().ToObject<DigitalForm>())
  542. {
  543. Layouts = results.Get<DigitalFormLayout>().ToObjects<DigitalFormLayout>().Select(x => new DigitalFormExportData.LayoutData(x)).ToList(),
  544. Variables = results.Get<DigitalFormVariable>().ToObjects<DigitalFormVariable>().Select(x => new DigitalFormExportData.VariableData(x)).ToList(),
  545. Documents = results.Get<Document>().ToObjects<Document>().Select(x => new DigitalFormExportData.DocumentData(x)).ToList()
  546. };
  547. var filename = rows[0].Get<DigitalForm, string>(x => x.Description);
  548. if (string.IsNullOrWhiteSpace(filename))
  549. filename = rows[0].Get<DigitalForm, string>(x => x.Code);
  550. if (string.IsNullOrWhiteSpace(filename))
  551. filename = "form";
  552. var dialog = new SaveFileDialog
  553. {
  554. Filter = ExportFileFilter,
  555. FileName = $"{filename}.prs-form"
  556. };
  557. if(dialog.ShowDialog() == true)
  558. {
  559. using var stream = dialog.OpenFile();
  560. Serialization.Serialize(data, stream);
  561. }
  562. }
  563. protected override void DoValidate(DigitalForm[] items, List<string> errors)
  564. {
  565. base.DoValidate(items, errors);
  566. if (items.Any(x => string.IsNullOrWhiteSpace(x.AppliesTo)))
  567. errors.Add("[Applies To] must not be blank!");
  568. }
  569. }
  570. }