DataEntryPanel.xaml.cs 13 KB

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