DigitalFormGrid.cs 27 KB

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