DigitalFormGrid.cs 27 KB

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