DigitalFormGrid.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 Microsoft.Win32;
  17. namespace InABox.DynamicGrid
  18. {
  19. public class DigitalFormExportData
  20. {
  21. public string Code { get; set; }
  22. public string Description { get; set; }
  23. public string AppliesTo { get; set; }
  24. public DigitalFormExportData() { }
  25. public DigitalFormExportData(DigitalForm form)
  26. {
  27. Code = form.Code;
  28. Description = form.Description;
  29. AppliesTo = form.AppliesTo;
  30. }
  31. public List<LayoutData> Layouts { get; set; }
  32. public List<VariableData> Variables { get; set; }
  33. public List<DocumentData> Documents { get; set; }
  34. public DigitalForm ToForm() => new DigitalForm
  35. {
  36. Code = Code,
  37. Description = Description,
  38. AppliesTo = AppliesTo
  39. };
  40. public class LayoutData
  41. {
  42. public string Code { get; set; }
  43. public string Description { get; set; }
  44. public DFLayoutType Type { get; set; }
  45. public string Layout { get; set; }
  46. public LayoutData() { }
  47. public LayoutData(DigitalFormLayout layout)
  48. {
  49. Code = layout.Code;
  50. Description = layout.Description;
  51. Type = layout.Type;
  52. Layout = layout.Layout;
  53. }
  54. public DigitalFormLayout ToLayout() => new DigitalFormLayout
  55. {
  56. Code = Code,
  57. Description = Description,
  58. Type = Type,
  59. Layout = Layout
  60. };
  61. }
  62. public class VariableData
  63. {
  64. public string Code { get; set; }
  65. public string Description { get; set; }
  66. public string VariableType { get; set; }
  67. public string Parameters { get; set; }
  68. public bool Required { get; set; }
  69. public bool Secure { get; set; }
  70. public bool Retain { get; set; }
  71. public bool Hidden { get; set; }
  72. public long Sequence { get; set; }
  73. public VariableData() { }
  74. public VariableData(DigitalFormVariable variable)
  75. {
  76. Code = variable.Code;
  77. Description = variable.Description;
  78. VariableType = variable.VariableType;
  79. Parameters = variable.Parameters;
  80. Required = variable.Required;
  81. Secure = variable.Secure;
  82. Retain = variable.Retain;
  83. Hidden = variable.Hidden;
  84. Sequence = variable.Sequence;
  85. }
  86. public DigitalFormVariable ToVariable() => new DigitalFormVariable
  87. {
  88. Code = Code,
  89. Description = Description,
  90. VariableType = VariableType,
  91. Parameters = Parameters,
  92. Required = Required,
  93. Secure = Secure,
  94. Retain = Retain,
  95. Hidden = Hidden,
  96. Sequence = Sequence
  97. };
  98. }
  99. public class DocumentData
  100. {
  101. public string FileName { get; set; }
  102. public byte[] Data { get; set; }
  103. public Guid ID { get; set; }
  104. public DocumentData() { }
  105. public DocumentData(Document document)
  106. {
  107. FileName = document.FileName;
  108. Data = document.Data;
  109. ID = document.ID;
  110. }
  111. public Document ToDocument() => new Document
  112. {
  113. FileName = FileName,
  114. Data = Data,
  115. TimeStamp = DateTime.Now,
  116. ID = ID
  117. };
  118. }
  119. }
  120. public class DigitalFormGrid : DynamicDataGrid<DigitalForm>
  121. {
  122. private bool _showall;
  123. public DigitalFormGrid()
  124. {
  125. Options.AddRange(DynamicGridOption.ImportData, DynamicGridOption.ExportData);
  126. AddButton("Show All", null, ShowAllClick);
  127. // TODO: Add back in
  128. //ActionColumns.Add(new DynamicImageColumn(ReportImage, ReportClick));
  129. AddButton("Groups", null, EditGroupsClick);
  130. }
  131. private bool EditGroupsClick(Button arg1, CoreRow[] arg2)
  132. {
  133. new MasterList(typeof(DigitalFormGroup)).ShowDialog();
  134. return false;
  135. }
  136. private BitmapImage ReportImage(CoreRow arg)
  137. {
  138. return arg != null ? Wpf.Resources.printer.AsBitmapImage() : null;
  139. }
  140. /*private bool ReportClick(CoreRow arg)
  141. {
  142. if (arg == null)
  143. return false;
  144. var typename = arg.Get<DigitalForm, string>(c => c.AppliesTo);
  145. var formid = arg.Get<DigitalForm, Guid>(c => c.ID);
  146. // Get Applies To
  147. /*Type type = CoreUtils.GetEntity("Comal.Classes."+typename);
  148. CoreTable entity = new CoreTable();
  149. entity.LoadColumns(type);
  150. // Get Form Details
  151. CoreTable form = new CoreTable();
  152. form.Columns.Add(new CoreColumn() { ColumnName = "ID", DataType = typeof(Guid) });
  153. form.Columns.Add(new CoreColumn() { ColumnName = "Parent.ID", DataType = typeof(Guid) });
  154. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Form.Description, "."), DataType = typeof(String) });
  155. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.FormCompletedBy.UserID, "."), DataType = typeof(String) });
  156. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, DateTime>(x => x.FormCompleted, "."), DataType = typeof(String) });
  157. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Location.Address, "."), DataType = typeof(String) });
  158. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Latitude, "."), DataType = typeof(String) });
  159. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Longitude, "."), DataType = typeof(String) });
  160. var variables = new Client<DigitalFormVariable>().Query(new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formid));
  161. foreach (var row in variables.Rows)
  162. {
  163. form.Columns.Add(
  164. new CoreColumn()
  165. {
  166. ColumnName = row.Get<DigitalFormVariable, String>(c => c.Code),
  167. DataType = DigitalFormVariable.DataType(row.Get<DigitalFormVariable, DigitalFormVariableType>(c => c.VariableType))
  168. }
  169. );
  170. }*
  171. // Create DataModel
  172. //DigitalFormReportDataModel model = new DigitalFormReportDataModel(form,entity,typename);
  173. AutoDataModel<DigitalForm> model = new AutoDataModel<DigitalForm>(new Filter<DigitalForm>(x => x.ID).IsEqualTo(formid));
  174. // Load Template
  175. var template = new Client<ReportTemplate>()
  176. .Load(new Filter<ReportTemplate>(x => x.Section).IsEqualTo("DigitalForms").And(x => x.Name).IsEqualTo(formid.ToString()))
  177. .FirstOrDefault();
  178. if (template == null)
  179. {
  180. template = new ReportTemplate
  181. {
  182. Section = "DigitalForms",
  183. Name = formid.ToString(),
  184. Visible = false
  185. };
  186. new Client<ReportTemplate>().Save(template, "");
  187. }
  188. ReportUtils.DesignReport(template, model);
  189. // Preview Report
  190. return false;
  191. }*/
  192. private bool ShowAllClick(Button arg1, CoreRow[] arg2)
  193. {
  194. _showall = !_showall;
  195. UpdateButton(arg1, null, _showall ? "Hide Inactive" : "Show All");
  196. return true;
  197. }
  198. protected override void Reload(Filters<DigitalForm> criteria, Columns<DigitalForm> columns, ref SortOrder<DigitalForm>? sort,
  199. Action<CoreTable?, Exception?> action)
  200. {
  201. if (!_showall)
  202. criteria.Add(new Filter<DigitalForm>(x => x.Active).IsEqualTo(true));
  203. base.Reload(criteria, columns, ref sort, action);
  204. }
  205. public override DynamicEditorPages LoadEditorPages(DigitalForm item)
  206. {
  207. var pages = base.LoadEditorPages(item);
  208. pages.Add(new DigitalFormReportGrid());
  209. return pages;
  210. }
  211. public override bool EditItems(DigitalForm[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
  212. {
  213. // Need to do this to make sure that the variables are available to the layouts (and vice versa?)
  214. return base.EditItems(items, PageDataHandler, true);
  215. }
  216. private const string ExportFileFilter = "Excel Files (*.xls, *xlsx)|*.xls;*.xlsx|Digital Forms (*.prs-form)|*.prs-form";
  217. protected override void DoImport()
  218. {
  219. var dialog = new OpenFileDialog
  220. {
  221. Filter = ExportFileFilter
  222. };
  223. if (dialog.ShowDialog() != true)
  224. return;
  225. String extension = Path.GetExtension(dialog.FileName) ?? "";
  226. if (extension.ToLower().Equals(".xls") || extension.ToLower().Equals(".xlsx"))
  227. {
  228. String appliesto = "";
  229. String code = "";
  230. var lookups = new DigitalFormCategoryLookups(null).Lookups();
  231. if (!DictionaryEdit.Execute<String>(lookups, ref appliesto, "Applies To", "Select Form Type") || String.IsNullOrWhiteSpace(appliesto))
  232. return;
  233. Progress.ShowModal("Creating Form", (progress) =>
  234. {
  235. var codes = new Client<DigitalForm>().Query(
  236. null,
  237. new Columns<DigitalForm>(x => x.Code),
  238. null
  239. ).Rows.Select(r => r.Get<DigitalForm, String>(c => c.Code)).ToArray();
  240. int i = 1;
  241. code = Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper();
  242. while (codes.Contains(code))
  243. code = $"{Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper()} ({i++})";
  244. DigitalForm form = new DigitalForm();
  245. form.Code = code;
  246. form.Description = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetFileNameWithoutExtension(dialog.FileName));
  247. form.AppliesTo = appliesto;
  248. new Client<DigitalForm>().Save(form, $"Imported from {dialog.FileName}");
  249. progress.Report("Importing Data");
  250. DFLayout data = null;
  251. var variables = new List<DigitalFormVariable>();
  252. var layout = new DigitalFormLayout();
  253. layout.Form.ID = form.ID;
  254. layout.Code = form.Code;
  255. layout.Description = form.Description;
  256. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  257. {
  258. var spreadsheet = new Spreadsheet(fs);
  259. data = DigitalFormUtils.LoadLayout(spreadsheet);
  260. layout.Layout = data.SaveLayout();
  261. }
  262. new Client<DigitalFormLayout>().Save(layout, $"Imported from {dialog.FileName}");
  263. progress.Report("Setting Up Variables");
  264. String group = "";
  265. foreach (var element in data.Elements)
  266. {
  267. if (element is DFLayoutHeader header)
  268. {
  269. group = header.Header;
  270. }
  271. else if (element is DFLayoutField field)
  272. {
  273. var variable = new DigitalFormVariable();
  274. variable.Form.ID = form.ID;
  275. variable.SetFieldType(field.GetType());
  276. variable.SaveProperties(field.GetProperties());
  277. variable.Group = group;
  278. variable.Code = field.Name;
  279. variable.Description = field.Name;
  280. variables.Add(variable);
  281. }
  282. }
  283. if (variables.Any())
  284. new Client<DigitalFormVariable>().Save(variables, $"Imported from {dialog.FileName}");
  285. progress.Report("Creating Report");
  286. var model = DigitalFormUtils.GetDataModel(appliesto, variables);
  287. var template = DigitalFormUtils.GenerateReport(layout, model);
  288. var report = new ReportTemplate();
  289. report.Section = form.ID.ToString();
  290. report.DataModel = model.Name;
  291. report.Name = form.Description;
  292. report.RDL = template?.SaveToString();
  293. new Client<ReportTemplate>().Save(report, $"Imported from {dialog.FileName}");
  294. });
  295. MessageBox.Show($"[{code}] imported successully!");
  296. Refresh(false, true);
  297. return;
  298. }
  299. DigitalFormExportData? data;
  300. using(var stream = dialog.OpenFile())
  301. {
  302. data = Serialization.Deserialize<DigitalFormExportData>(stream);
  303. if (data is null)
  304. {
  305. MessageBox.Show("File corrupt");
  306. return;
  307. }
  308. }
  309. try
  310. {
  311. var form = data.ToForm();
  312. var layouts = data.Layouts.Select(x => x.ToLayout()).ToList();
  313. var variables = data.Variables.Select(x => x.ToVariable()).ToList();
  314. var documents = data.Documents.Select(x => x.ToDocument()).ToList();
  315. var formDocuments = new List<DigitalFormDocument>();
  316. foreach (var document in documents)
  317. {
  318. new Client<Document>().Save(document, "");
  319. var digitalFormDocument = new DigitalFormDocument();
  320. digitalFormDocument.DocumentLink.ID = document.ID;
  321. digitalFormDocument.DocumentLink.Synchronise(document);
  322. formDocuments.Add(digitalFormDocument);
  323. }
  324. if (EditItems(new DigitalForm[] { form }, (type) =>
  325. {
  326. var table = new CoreTable();
  327. table.LoadColumns(type);
  328. if(type == typeof(DigitalFormLayout))
  329. {
  330. table.LoadRows(layouts);
  331. }
  332. else if(type == typeof(DigitalFormVariable))
  333. {
  334. table.LoadRows(variables);
  335. }
  336. else if(type == typeof(DigitalFormDocument))
  337. {
  338. table.LoadRows(formDocuments);
  339. }
  340. return table;
  341. }))
  342. {
  343. Refresh(false, true);
  344. }
  345. /*new Client<DigitalForm>().Save(form, "Imported by user");
  346. foreach (var layout in layouts)
  347. {
  348. layout.Form.ID = form.ID;
  349. new Client<DigitalFormLayout>().Save(layout, "");
  350. }
  351. foreach (var variable in variables)
  352. {
  353. variable.Form.ID = form.ID;
  354. new Client<DigitalFormVariable>().Save(variable, "");
  355. }
  356. foreach (var document in documents)
  357. {
  358. new Client<Document>().Save(document, "");
  359. var digitalFormDocument = new DigitalFormDocument();
  360. digitalFormDocument.EntityLink.ID = form.ID;
  361. digitalFormDocument.DocumentLink.ID = document.ID;
  362. new Client<DigitalFormDocument>().Save(digitalFormDocument, "");
  363. }*/
  364. }
  365. catch(Exception e)
  366. {
  367. MessageBox.Show(e.Message);
  368. }
  369. }
  370. protected override void DoExport()
  371. {
  372. var rows = SelectedRows;
  373. if(rows.Length == 0)
  374. {
  375. MessageBox.Show("Please select a form to export.");
  376. return;
  377. }
  378. else if(rows.Length > 1)
  379. {
  380. MessageBox.Show("Please select only one form to export.");
  381. return;
  382. }
  383. var formID = rows[0].Get<DigitalForm, Guid>(x => x.ID);
  384. var results = Client.QueryMultiple(
  385. new KeyedQueryDef<DigitalForm>(
  386. new Filter<DigitalForm>(x => x.ID).IsEqualTo(formID),
  387. new Columns<DigitalForm>(x => x.Code)
  388. .Add(x => x.Description)
  389. .Add(x => x.AppliesTo)),
  390. new KeyedQueryDef<DigitalFormLayout>(
  391. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(formID),
  392. new Columns<DigitalFormLayout>(x => x.Code)
  393. .Add(x => x.Description)
  394. .Add(x => x.Type)
  395. .Add(x => x.Layout)),
  396. new KeyedQueryDef<DigitalFormVariable>(
  397. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formID),
  398. new Columns<DigitalFormVariable>(x => x.Code)
  399. .Add(x => x.Description)
  400. .Add(x => x.VariableType)
  401. .Add(x => x.Parameters)
  402. .Add(x => x.Required)
  403. .Add(x => x.Secure)
  404. .Add(x => x.Retain)
  405. .Add(x => x.Hidden)
  406. .Add(x => x.Sequence)),
  407. new KeyedQueryDef<Document>(
  408. new Filter<Document>(x => x.ID).InQuery(
  409. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(formID),
  410. x => x.DocumentLink.ID),
  411. new Columns<Document>(x => x.FileName)
  412. .Add(x => x.Data)));
  413. var data = new DigitalFormExportData(results.Get<DigitalForm>().Rows.First().ToObject<DigitalForm>())
  414. {
  415. Layouts = results.Get<DigitalFormLayout>().ToObjects<DigitalFormLayout>().Select(x => new DigitalFormExportData.LayoutData(x)).ToList(),
  416. Variables = results.Get<DigitalFormVariable>().ToObjects<DigitalFormVariable>().Select(x => new DigitalFormExportData.VariableData(x)).ToList(),
  417. Documents = results.Get<Document>().ToObjects<Document>().Select(x => new DigitalFormExportData.DocumentData(x)).ToList()
  418. };
  419. var filename = rows[0].Get<DigitalForm, string>(x => x.Description);
  420. if (string.IsNullOrWhiteSpace(filename))
  421. filename = rows[0].Get<DigitalForm, string>(x => x.Code);
  422. if (string.IsNullOrWhiteSpace(filename))
  423. filename = "form";
  424. var dialog = new SaveFileDialog
  425. {
  426. Filter = ExportFileFilter,
  427. FileName = $"{filename}.prs-form"
  428. };
  429. if(dialog.ShowDialog() == true)
  430. {
  431. using var stream = dialog.OpenFile();
  432. Serialization.Serialize(data, stream);
  433. }
  434. }
  435. protected override void DoValidate(DigitalForm[] items, List<string> errors)
  436. {
  437. base.DoValidate(items, errors);
  438. if (items.Any(x => string.IsNullOrWhiteSpace(x.AppliesTo)))
  439. errors.Add("[Applies To] must not be blank!");
  440. }
  441. }
  442. }