DigitalFormGrid.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.Reports;
  11. using InABox.Reports.Common;
  12. using InABox.WPF;
  13. using Microsoft.Win32;
  14. namespace InABox.DynamicGrid
  15. {
  16. public class DigitalFormExportData
  17. {
  18. public string Code { get; set; }
  19. public string Description { get; set; }
  20. public string AppliesTo { get; set; }
  21. public DigitalFormExportData() { }
  22. public DigitalFormExportData(DigitalForm form)
  23. {
  24. Code = form.Code;
  25. Description = form.Description;
  26. AppliesTo = form.AppliesTo;
  27. }
  28. public List<LayoutData> Layouts { get; set; }
  29. public List<VariableData> Variables { get; set; }
  30. public List<DocumentData> Documents { get; set; }
  31. public DigitalForm ToForm() => new DigitalForm
  32. {
  33. Code = Code,
  34. Description = Description,
  35. AppliesTo = AppliesTo
  36. };
  37. public class LayoutData
  38. {
  39. public string Code { get; set; }
  40. public string Description { get; set; }
  41. public DFLayoutType Type { get; set; }
  42. public string Layout { get; set; }
  43. public LayoutData() { }
  44. public LayoutData(DigitalFormLayout layout)
  45. {
  46. Code = layout.Code;
  47. Description = layout.Description;
  48. Type = layout.Type;
  49. Layout = layout.Layout;
  50. }
  51. public DigitalFormLayout ToLayout() => new DigitalFormLayout
  52. {
  53. Code = Code,
  54. Description = Description,
  55. Type = Type,
  56. Layout = Layout
  57. };
  58. }
  59. public class VariableData
  60. {
  61. public string Code { get; set; }
  62. public string Description { get; set; }
  63. public string VariableType { get; set; }
  64. public string Parameters { get; set; }
  65. public bool Required { get; set; }
  66. public bool Secure { get; set; }
  67. public bool Retain { get; set; }
  68. public bool Hidden { get; set; }
  69. public long Sequence { get; set; }
  70. public VariableData() { }
  71. public VariableData(DigitalFormVariable variable)
  72. {
  73. Code = variable.Code;
  74. Description = variable.Description;
  75. VariableType = variable.VariableType;
  76. Parameters = variable.Parameters;
  77. Required = variable.Required;
  78. Secure = variable.Secure;
  79. Retain = variable.Retain;
  80. Hidden = variable.Hidden;
  81. Sequence = variable.Sequence;
  82. }
  83. public DigitalFormVariable ToVariable() => new DigitalFormVariable
  84. {
  85. Code = Code,
  86. Description = Description,
  87. VariableType = VariableType,
  88. Parameters = Parameters,
  89. Required = Required,
  90. Secure = Secure,
  91. Retain = Retain,
  92. Hidden = Hidden,
  93. Sequence = Sequence
  94. };
  95. }
  96. public class DocumentData
  97. {
  98. public string FileName { get; set; }
  99. public byte[] Data { get; set; }
  100. public Guid ID { get; set; }
  101. public DocumentData() { }
  102. public DocumentData(Document document)
  103. {
  104. FileName = document.FileName;
  105. Data = document.Data;
  106. ID = document.ID;
  107. }
  108. public Document ToDocument() => new Document
  109. {
  110. FileName = FileName,
  111. Data = Data,
  112. TimeStamp = DateTime.Now,
  113. ID = ID
  114. };
  115. }
  116. }
  117. public class DigitalFormGrid : DynamicDataGrid<DigitalForm>
  118. {
  119. private bool _showall;
  120. public DigitalFormGrid()
  121. {
  122. Options.AddRange(DynamicGridOption.ImportData, DynamicGridOption.ExportData);
  123. AddButton("Show All", null, ShowAllClick);
  124. // TODO: Add back in
  125. //ActionColumns.Add(new DynamicImageColumn(ReportImage, ReportClick));
  126. AddButton("Groups", null, EditGroupsClick);
  127. }
  128. private bool EditGroupsClick(Button arg1, CoreRow[] arg2)
  129. {
  130. new MasterList(typeof(DigitalFormGroup)).ShowDialog();
  131. return false;
  132. }
  133. private BitmapImage ReportImage(CoreRow arg)
  134. {
  135. return arg != null ? Properties.Resources.printer.AsBitmapImage() : null;
  136. }
  137. /*private bool ReportClick(CoreRow arg)
  138. {
  139. if (arg == null)
  140. return false;
  141. var typename = arg.Get<DigitalForm, string>(c => c.AppliesTo);
  142. var formid = arg.Get<DigitalForm, Guid>(c => c.ID);
  143. // Get Applies To
  144. /*Type type = CoreUtils.GetEntity("Comal.Classes."+typename);
  145. CoreTable entity = new CoreTable();
  146. entity.LoadColumns(type);
  147. // Get Form Details
  148. CoreTable form = new CoreTable();
  149. form.Columns.Add(new CoreColumn() { ColumnName = "ID", DataType = typeof(Guid) });
  150. form.Columns.Add(new CoreColumn() { ColumnName = "Parent.ID", DataType = typeof(Guid) });
  151. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Form.Description, "."), DataType = typeof(String) });
  152. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.FormCompletedBy.UserID, "."), DataType = typeof(String) });
  153. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, DateTime>(x => x.FormCompleted, "."), DataType = typeof(String) });
  154. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, String>(x => x.Location.Address, "."), DataType = typeof(String) });
  155. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Latitude, "."), DataType = typeof(String) });
  156. form.Columns.Add(new CoreColumn() { ColumnName = CoreUtils.GetFullPropertyName<IDigitalFormInstance, double>(x => x.Location.Longitude, "."), DataType = typeof(String) });
  157. var variables = new Client<DigitalFormVariable>().Query(new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formid));
  158. foreach (var row in variables.Rows)
  159. {
  160. form.Columns.Add(
  161. new CoreColumn()
  162. {
  163. ColumnName = row.Get<DigitalFormVariable, String>(c => c.Code),
  164. DataType = DigitalFormVariable.DataType(row.Get<DigitalFormVariable, DigitalFormVariableType>(c => c.VariableType))
  165. }
  166. );
  167. }*
  168. // Create DataModel
  169. //DigitalFormReportDataModel model = new DigitalFormReportDataModel(form,entity,typename);
  170. AutoDataModel<DigitalForm> model = new AutoDataModel<DigitalForm>(new Filter<DigitalForm>(x => x.ID).IsEqualTo(formid));
  171. // Load Template
  172. var template = new Client<ReportTemplate>()
  173. .Load(new Filter<ReportTemplate>(x => x.Section).IsEqualTo("DigitalForms").And(x => x.Name).IsEqualTo(formid.ToString()))
  174. .FirstOrDefault();
  175. if (template == null)
  176. {
  177. template = new ReportTemplate
  178. {
  179. Section = "DigitalForms",
  180. Name = formid.ToString(),
  181. Visible = false
  182. };
  183. new Client<ReportTemplate>().Save(template, "");
  184. }
  185. ReportUtils.DesignReport(template, model);
  186. // Preview Report
  187. return false;
  188. }*/
  189. private bool ShowAllClick(Button arg1, CoreRow[] arg2)
  190. {
  191. _showall = !_showall;
  192. UpdateButton(arg1, null, _showall ? "Hide Inactive" : "Show All");
  193. return true;
  194. }
  195. protected override void Reload(Filters<DigitalForm> criteria, Columns<DigitalForm> columns, ref SortOrder<DigitalForm>? sort,
  196. Action<CoreTable?, Exception?> action)
  197. {
  198. if (!_showall)
  199. criteria.Add(new Filter<DigitalForm>(x => x.Active).IsEqualTo(true));
  200. base.Reload(criteria, columns, ref sort, action);
  201. }
  202. public override bool EditItems(DigitalForm[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
  203. {
  204. // Need to do this to make sure that the variables are available to the layouts (and vice versa?)
  205. return base.EditItems(items, PageDataHandler, true);
  206. }
  207. private const string ExportFileFilter = "Digital Forms (*.prs-form)|*.prs-form";
  208. protected override void DoImport()
  209. {
  210. var dialog = new OpenFileDialog
  211. {
  212. Filter = ExportFileFilter
  213. };
  214. if (dialog.ShowDialog() != true)
  215. return;
  216. DigitalFormExportData? data;
  217. using(var stream = dialog.OpenFile())
  218. {
  219. data = Serialization.Deserialize<DigitalFormExportData>(stream);
  220. if (data is null)
  221. {
  222. MessageBox.Show("File corrupt");
  223. return;
  224. }
  225. }
  226. try
  227. {
  228. var form = data.ToForm();
  229. var layouts = data.Layouts.Select(x => x.ToLayout()).ToList();
  230. var variables = data.Variables.Select(x => x.ToVariable()).ToList();
  231. var documents = data.Documents.Select(x => x.ToDocument()).ToList();
  232. var formDocuments = new List<DigitalFormDocument>();
  233. foreach (var document in documents)
  234. {
  235. new Client<Document>().Save(document, "");
  236. var digitalFormDocument = new DigitalFormDocument();
  237. digitalFormDocument.DocumentLink.ID = document.ID;
  238. digitalFormDocument.DocumentLink.Synchronise(document);
  239. formDocuments.Add(digitalFormDocument);
  240. }
  241. if (EditItems(new DigitalForm[] { form }, (type) =>
  242. {
  243. var table = new CoreTable();
  244. table.LoadColumns(type);
  245. if(type == typeof(DigitalFormLayout))
  246. {
  247. table.LoadRows(layouts);
  248. }
  249. else if(type == typeof(DigitalFormVariable))
  250. {
  251. table.LoadRows(variables);
  252. }
  253. else if(type == typeof(DigitalFormDocument))
  254. {
  255. table.LoadRows(formDocuments);
  256. }
  257. return table;
  258. }))
  259. {
  260. Refresh(false, true);
  261. }
  262. /*new Client<DigitalForm>().Save(form, "Imported by user");
  263. foreach (var layout in layouts)
  264. {
  265. layout.Form.ID = form.ID;
  266. new Client<DigitalFormLayout>().Save(layout, "");
  267. }
  268. foreach (var variable in variables)
  269. {
  270. variable.Form.ID = form.ID;
  271. new Client<DigitalFormVariable>().Save(variable, "");
  272. }
  273. foreach (var document in documents)
  274. {
  275. new Client<Document>().Save(document, "");
  276. var digitalFormDocument = new DigitalFormDocument();
  277. digitalFormDocument.EntityLink.ID = form.ID;
  278. digitalFormDocument.DocumentLink.ID = document.ID;
  279. new Client<DigitalFormDocument>().Save(digitalFormDocument, "");
  280. }*/
  281. }
  282. catch(Exception e)
  283. {
  284. MessageBox.Show(e.Message);
  285. }
  286. }
  287. protected override void DoExport()
  288. {
  289. var rows = SelectedRows;
  290. if(rows.Length == 0)
  291. {
  292. MessageBox.Show("Please select a form to export.");
  293. return;
  294. }
  295. else if(rows.Length > 1)
  296. {
  297. MessageBox.Show("Please select only one form to export.");
  298. return;
  299. }
  300. var formID = rows[0].Get<DigitalForm, Guid>(x => x.ID);
  301. var results = Client.QueryMultiple(
  302. new KeyedQueryDef<DigitalForm>(
  303. new Filter<DigitalForm>(x => x.ID).IsEqualTo(formID),
  304. new Columns<DigitalForm>(x => x.Code)
  305. .Add(x => x.Description)
  306. .Add(x => x.AppliesTo)),
  307. new KeyedQueryDef<DigitalFormLayout>(
  308. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(formID),
  309. new Columns<DigitalFormLayout>(x => x.Code)
  310. .Add(x => x.Description)
  311. .Add(x => x.Type)
  312. .Add(x => x.Layout)),
  313. new KeyedQueryDef<DigitalFormVariable>(
  314. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(formID),
  315. new Columns<DigitalFormVariable>(x => x.Code)
  316. .Add(x => x.Description)
  317. .Add(x => x.VariableType)
  318. .Add(x => x.Parameters)
  319. .Add(x => x.Required)
  320. .Add(x => x.Secure)
  321. .Add(x => x.Retain)
  322. .Add(x => x.Hidden)
  323. .Add(x => x.Sequence)),
  324. new KeyedQueryDef<Document>(
  325. new Filter<Document>(x => x.ID).InQuery(
  326. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(formID),
  327. x => x.DocumentLink.ID),
  328. new Columns<Document>(x => x.FileName)
  329. .Add(x => x.Data)));
  330. var data = new DigitalFormExportData(results.Get<DigitalForm>().Rows.First().ToObject<DigitalForm>())
  331. {
  332. Layouts = results.Get<DigitalFormLayout>().ToObjects<DigitalFormLayout>().Select(x => new DigitalFormExportData.LayoutData(x)).ToList(),
  333. Variables = results.Get<DigitalFormVariable>().ToObjects<DigitalFormVariable>().Select(x => new DigitalFormExportData.VariableData(x)).ToList(),
  334. Documents = results.Get<Document>().ToObjects<Document>().Select(x => new DigitalFormExportData.DocumentData(x)).ToList()
  335. };
  336. var filename = rows[0].Get<DigitalForm, string>(x => x.Description);
  337. if (string.IsNullOrWhiteSpace(filename))
  338. filename = rows[0].Get<DigitalForm, string>(x => x.Code);
  339. if (string.IsNullOrWhiteSpace(filename))
  340. filename = "form";
  341. var dialog = new SaveFileDialog
  342. {
  343. Filter = ExportFileFilter,
  344. FileName = $"{filename}.prs-form"
  345. };
  346. if(dialog.ShowDialog() == true)
  347. {
  348. using var stream = dialog.OpenFile();
  349. Serialization.Serialize(data, stream);
  350. }
  351. }
  352. protected override void DoValidate(DigitalForm[] items, List<string> errors)
  353. {
  354. base.DoValidate(items, errors);
  355. if (items.Any(x => string.IsNullOrWhiteSpace(x.AppliesTo)))
  356. errors.Add("[Applies To] must not be blank!");
  357. }
  358. }
  359. }