DataEntryPanel.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace PRSDesktop
  13. {
  14. /// <summary>
  15. /// Interaction logic for DataEntryPanel.xaml
  16. /// </summary>
  17. /// <remarks>
  18. /// This is a host because it has a singular code popup editor
  19. /// </remarks>
  20. public partial class DataEntryPanel : UserControl, IBasePanel, IDynamicEditorHost
  21. {
  22. //IPopupEditorControl? _popup;
  23. private IDynamicDataGrid? _grid;
  24. private Type? _selectedType;
  25. private Guid _entityID;
  26. private Guid _originalID;
  27. private bool _processenabled;
  28. private Entity? _entity;
  29. private Button? _process;
  30. private bool _isChanged;
  31. private bool IsChanged
  32. {
  33. get => _isChanged;
  34. set
  35. {
  36. if(_isChanged != value)
  37. {
  38. _isChanged = value;
  39. Editor.HideButtons = !value;// (_entity?.IsChanged() != true) && (_originalID == _entityID);
  40. if (_process != null)
  41. _process.IsEnabled = value;// ((_entity?.ID ?? Guid.Empty) != Guid.Empty) || (_entity?.IsChanged() != false);
  42. _documents._dataEntryGrid.IsEnabled = !value;// (_entity?.IsChanged() != true) && (_originalID == _entityID);
  43. }
  44. }
  45. }
  46. public void Select(Type? type, Guid id)
  47. {
  48. ClearEditor();
  49. _selectedType = type;
  50. _entityID = id;
  51. if (_selectedType != null)
  52. {
  53. ClearEditor();
  54. CreateEditor();
  55. PopulateEditor();
  56. //LoadPopup();
  57. }
  58. }
  59. private void ScanPanel_OnSelectScan(string appliesto, Guid entityid, bool processenabled)
  60. {
  61. _processenabled = processenabled;
  62. _originalID = entityid;
  63. Select(
  64. CoreUtils.GetEntityOrNull(appliesto),
  65. entityid
  66. );
  67. }
  68. public DataEntryPanel()
  69. {
  70. InitializeComponent();
  71. }
  72. public void Setup()
  73. {
  74. _documents.Setup();
  75. IsChanged = false;
  76. }
  77. public void Refresh()
  78. {
  79. _documents.Refresh();
  80. }
  81. public bool IsReady { get; set; }
  82. public string SectionName => "Data Entry";
  83. public DataModel DataModel(Selection selection)
  84. {
  85. return new EmptyDataModel();
  86. }
  87. public event DataModelUpdateEvent? OnUpdateDataModel;
  88. public void CreateToolbarButtons(IPanelHost host)
  89. {
  90. if (Security.IsAllowed<CanSetupDataEntryTags>())
  91. {
  92. host.CreateSetupAction(new PanelAction
  93. {
  94. Caption = "Data Entry Tags",
  95. OnExecute = (action) =>
  96. {
  97. var list = new MasterList(typeof(DataEntryTag));
  98. list.ShowDialog();
  99. }
  100. });
  101. }
  102. }
  103. public void Heartbeat(TimeSpan time)
  104. {
  105. }
  106. public Dictionary<string, object[]> Selected()
  107. {
  108. return new Dictionary<string, object[]>();
  109. }
  110. private void CheckSaved(CancelEventArgs cancel)
  111. {
  112. var result = MessageBox.Show("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
  113. if (result == MessageBoxResult.Yes)
  114. {
  115. Editor.SaveItem(cancel);
  116. if (!cancel.Cancel)
  117. MessageBox.Show("Item saved.");
  118. }
  119. else if (result == MessageBoxResult.Cancel)
  120. {
  121. cancel.Cancel = true;
  122. }
  123. }
  124. private bool CheckSaved()
  125. {
  126. var cancel = new CancelEventArgs();
  127. CheckSaved(cancel);
  128. return !cancel.Cancel;
  129. }
  130. public void Shutdown(CancelEventArgs? cancel)
  131. {
  132. if (cancel != null && _isChanged)
  133. CheckSaved(cancel);
  134. if (cancel?.Cancel != true)
  135. _documents.Shutdown(cancel);
  136. }
  137. #region Host
  138. public DynamicGridColumns Columns { get; set; } = new();
  139. IEnumerable<DynamicGridColumn> IDynamicEditorHost.Columns => Columns;
  140. public void LoadColumns(string column, Dictionary<string, string> columns)
  141. {
  142. if (_selectedType is null)
  143. return;
  144. columns.Clear();
  145. foreach (var c in LookupFactory.DefineColumns(_selectedType).ColumnNames().Where(x => x != "ID"))
  146. columns.Add(c, c);
  147. //if (_popup?.EditorDefinition is CodePopupEditorControl codePopup && !columns.ContainsKey(codePopup.CodeColumn))
  148. // columns.Add(codePopup.CodeColumn, codePopup.CodeColumn);
  149. }
  150. public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(type);
  151. public void LoadLookups(ILookupEditorControl sender)
  152. {
  153. var editor = sender.EditorDefinition as ILookupEditor;
  154. var colname = sender.ColumnName;
  155. var values = editor.Values(colname, Editor.Items);
  156. sender.LoadLookups(values);
  157. }
  158. object?[] IDynamicEditorHost.GetItems() => Editor.Items;
  159. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  160. #endregion
  161. private void ClearEditor()
  162. {
  163. DetailBorder.Child = null;
  164. IsChanged = false;
  165. //if (_popup is UIElement element)
  166. // DetailHeader.Children.Remove(element);
  167. }
  168. private void CreateEditor()
  169. {
  170. if (_selectedType == null)
  171. return;
  172. Editor = new EmbeddedDynamicEditorForm();
  173. Editor.SetLayoutType<VerticalDynamicEditorGridLayout>();
  174. Editor.HighlightButtons = true;
  175. Editor.HideButtons = true;
  176. Editor.SetValue(Grid.RowProperty, 1);
  177. Editor.SetValue(Grid.ColumnProperty, 0);
  178. Editor.SetValue(Grid.ColumnSpanProperty, 4);
  179. Editor.OnAfterEditorValueChanged += (sender, args) =>
  180. {
  181. IsChanged = IsChanged || (_entity?.IsChanged() == true || _originalID != _entityID);
  182. return null;
  183. };
  184. Editor.OnOK += () => { DoSave(false); };
  185. Editor.OnCancel += () =>
  186. {
  187. _entityID = _originalID;
  188. Select(_selectedType,_entityID);
  189. };
  190. Editor.OnChanged += (sender, args) => IsChanged = true;
  191. Editor.OnFormCustomiseEditor += (sender, items, column, editor) =>
  192. {
  193. if ((editor is BaseCodeEditor be) && editor.Editable.EditorVisible())
  194. {
  195. be.Buttons = new[]
  196. {
  197. new EditorButton(null, "..", 30, DoLookup, false)
  198. };
  199. }
  200. };
  201. DetailBorder.Child = Editor;
  202. _grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _selectedType) as IDynamicDataGrid;
  203. }
  204. private void DoLookup(object editor, object? item)
  205. {
  206. if (editor is CodeEditorControl ce)
  207. {
  208. Dictionary<string, string>? filter = null;
  209. if (ce.Value != null)
  210. {
  211. filter = new Dictionary<string, string>();
  212. filter[ce.ColumnName] = ce.Value;
  213. }
  214. var popup = new PopupList(_selectedType, _entityID, new String[] { }, filter);
  215. popup.OnDefineFilter += type => LookupFactory.DefineFilter(type);
  216. if (popup.ShowDialog() == true)
  217. {
  218. _entityID = popup.ID;
  219. Select(_selectedType, _entityID);
  220. }
  221. }
  222. }
  223. private void SaveDocument(DataEntryDocument dataEntryDocument)
  224. {
  225. var linktype = CoreUtils.TypeList(x =>
  226. x.GetInterfaces().Contains(typeof(IEntityLink))
  227. && x.BaseType != null
  228. && x.BaseType.IsGenericType
  229. && x.BaseType.GenericTypeArguments.FirstOrDefault() == _selectedType
  230. ).FirstOrDefault();
  231. if (linktype == null)
  232. return;
  233. var doctype = CoreUtils.TypeList(x =>
  234. x.GetInterfaces().Contains(typeof(IEntityDocument))
  235. && x.BaseType != null
  236. && x.BaseType.IsGenericType
  237. && x.BaseType.GenericTypeArguments.FirstOrDefault() == linktype
  238. ).FirstOrDefault();
  239. if (doctype == null)
  240. return;
  241. var doc = (IEntityDocument)Activator.CreateInstance(doctype)!;
  242. CoreUtils.SetPropertyValue(doc,"EntityLink.ID",_entityID);
  243. doc.DocumentLink.ID = dataEntryDocument.Document.ID;
  244. doc.Thumbnail = dataEntryDocument.Thumbnail;
  245. ClientFactory.CreateClient(doctype).Save(doc,"Added from Data Entry Screen");
  246. }
  247. private void DoSave(bool markasprocessed)
  248. {
  249. var cancel = new System.ComponentModel.CancelEventArgs();
  250. if (markasprocessed && (_entity is IDataEntryInstance scannable))
  251. scannable.DataEntered = DateTime.Now;
  252. Editor.SaveItem(cancel);
  253. if (!cancel.Cancel)
  254. {
  255. _originalID = _entityID;
  256. IsChanged = false;
  257. var row = _documents._dataEntryGrid.SelectedRows.FirstOrDefault();
  258. if (row != null)
  259. {
  260. var scan = row?.ToObject<DataEntryDocument>();
  261. scan.EntityID = _entity.ID;
  262. if (markasprocessed)
  263. {
  264. SaveDocument(scan);
  265. scan.Archived = DateTime.Now;
  266. }
  267. if (scan.IsChanged())
  268. {
  269. new Client<DataEntryDocument>().Save(scan, "Updated from Data Entry Screen");
  270. _documents.Refresh();
  271. }
  272. }
  273. }
  274. }
  275. private void PopulateEditor()
  276. {
  277. if (_selectedType == null)
  278. return;
  279. _entity = null;
  280. if (_entityID != Guid.Empty)
  281. {
  282. _entity = Client.Create(_selectedType)
  283. .Query(
  284. Filter.Create<Entity>(_selectedType, x => x.ID).IsEqualTo(_entityID),
  285. _grid.LoadEditorColumns()
  286. )
  287. .ToObjects(_selectedType)
  288. .OfType<Entity>()
  289. .FirstOrDefault();
  290. }
  291. _entity ??= Activator.CreateInstance(_selectedType) as Entity;
  292. if (_entity == null)
  293. return;
  294. _grid?.InitialiseEditorForm(Editor, new object[] { _entity }, null, true);
  295. _process = new Button()
  296. {
  297. Content = "Mark as Processed",
  298. BorderBrush = new SolidColorBrush(Colors.DarkBlue),
  299. Background = new SolidColorBrush(Colors.DodgerBlue),
  300. Margin = new Thickness(5, 5, 0, 5),
  301. Padding = new Thickness(10, 0, 10, 0),
  302. IsEnabled = _processenabled
  303. };
  304. _process.Click += (sender, args) => DoSave(true);
  305. Editor.AddButton(_process);
  306. IsChanged = false;
  307. }
  308. }
  309. }