123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for DataEntryPanel.xaml
- /// </summary>
- /// <remarks>
- /// This is a host because it has a singular code popup editor
- /// </remarks>
- public partial class DataEntryPanel : UserControl, IBasePanel, IDynamicEditorHost
- {
- //IPopupEditorControl? _popup;
- private IDynamicDataGrid? _grid;
-
- private Type? _selectedType;
- private Guid _entityID;
- private Guid _originalID;
- private bool _processenabled;
- private Entity? _entity;
- private Button? _process;
- private bool _isChanged;
- private bool IsChanged
- {
- get => _isChanged;
- set
- {
- if(_isChanged != value)
- {
- _isChanged = value;
- Editor.HideButtons = !value;// (_entity?.IsChanged() != true) && (_originalID == _entityID);
- if (_process != null)
- _process.IsEnabled = value;// ((_entity?.ID ?? Guid.Empty) != Guid.Empty) || (_entity?.IsChanged() != false);
- _documents._dataEntryGrid.IsEnabled = !value;// (_entity?.IsChanged() != true) && (_originalID == _entityID);
- }
- }
- }
-
- public void Select(Type? type, Guid id)
- {
- ClearEditor();
-
- _selectedType = type;
- _entityID = id;
- if (_selectedType != null)
- {
- ClearEditor();
- CreateEditor();
- PopulateEditor();
- //LoadPopup();
- }
-
- }
-
- private void ScanPanel_OnSelectScan(string appliesto, Guid entityid, bool processenabled)
- {
- _processenabled = processenabled;
- _originalID = entityid;
- Select(
- CoreUtils.GetEntityOrNull(appliesto),
- entityid
- );
- }
-
- public DataEntryPanel()
- {
- InitializeComponent();
- }
- public void Setup()
- {
- _documents.Setup();
- IsChanged = false;
- }
-
- public void Refresh()
- {
- _documents.Refresh();
- }
- public bool IsReady { get; set; }
- public string SectionName => "Data Entry";
- public DataModel DataModel(Selection selection)
- {
- return new EmptyDataModel();
- }
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- if (Security.IsAllowed<CanSetupDataEntryTags>())
- {
- host.CreateSetupAction(new PanelAction
- {
- Caption = "Data Entry Tags",
- OnExecute = (action) =>
- {
- var list = new MasterList(typeof(DataEntryTag));
- list.ShowDialog();
- }
- });
- }
- }
- public void Heartbeat(TimeSpan time)
- {
- }
-
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]>();
- }
-
- private void CheckSaved(CancelEventArgs cancel)
- {
- var result = MessageBox.Show("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
- if (result == MessageBoxResult.Yes)
- {
- Editor.SaveItem(cancel);
- if (!cancel.Cancel)
- MessageBox.Show("Item saved.");
- }
- else if (result == MessageBoxResult.Cancel)
- {
- cancel.Cancel = true;
- }
- }
- private bool CheckSaved()
- {
- var cancel = new CancelEventArgs();
- CheckSaved(cancel);
- return !cancel.Cancel;
- }
- public void Shutdown(CancelEventArgs? cancel)
- {
- if (cancel != null && _isChanged)
- CheckSaved(cancel);
- if (cancel?.Cancel != true)
- _documents.Shutdown(cancel);
- }
- #region Host
-
- public DynamicGridColumns Columns { get; set; } = new();
- IEnumerable<DynamicGridColumn> IDynamicEditorHost.Columns => Columns;
- public void LoadColumns(string column, Dictionary<string, string> columns)
- {
- if (_selectedType is null)
- return;
- columns.Clear();
- foreach (var c in LookupFactory.DefineColumns(_selectedType).ColumnNames().Where(x => x != "ID"))
- columns.Add(c, c);
-
- //if (_popup?.EditorDefinition is CodePopupEditorControl codePopup && !columns.ContainsKey(codePopup.CodeColumn))
- // columns.Add(codePopup.CodeColumn, codePopup.CodeColumn);
- }
- public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(type);
- public void LoadLookups(ILookupEditorControl sender)
- {
- var editor = sender.EditorDefinition as ILookupEditor;
- var colname = sender.ColumnName;
- var values = editor.Values(colname, Editor.Items);
- sender.LoadLookups(values);
- }
- object?[] IDynamicEditorHost.GetItems() => Editor.Items;
- public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
- #endregion
- private void ClearEditor()
- {
- DetailBorder.Child = null;
- IsChanged = false;
-
- //if (_popup is UIElement element)
- // DetailHeader.Children.Remove(element);
- }
-
- private void CreateEditor()
- {
- if (_selectedType == null)
- return;
-
- Editor = new EmbeddedDynamicEditorForm();
- Editor.SetLayoutType<VerticalDynamicEditorGridLayout>();
- Editor.HighlightButtons = true;
- Editor.HideButtons = true;
- Editor.SetValue(Grid.RowProperty, 1);
- Editor.SetValue(Grid.ColumnProperty, 0);
- Editor.SetValue(Grid.ColumnSpanProperty, 4);
- Editor.OnAfterEditorValueChanged += (sender, args) =>
- {
- IsChanged = IsChanged || (_entity?.IsChanged() == true || _originalID != _entityID);
- return null;
- };
- Editor.OnOK += () => { DoSave(false); };
- Editor.OnCancel += () =>
- {
- _entityID = _originalID;
- Select(_selectedType,_entityID);
- };
- Editor.OnChanged += (sender, args) => IsChanged = true;
- Editor.OnFormCustomiseEditor += (sender, items, column, editor) =>
- {
- if ((editor is BaseCodeEditor be) && editor.Editable.EditorVisible())
- {
- be.Buttons = new[]
- {
- new EditorButton(null, "..", 30, DoLookup, false)
- };
- }
- };
-
- DetailBorder.Child = Editor;
- _grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _selectedType) as IDynamicDataGrid;
-
- }
-
- private void DoLookup(object editor, object? item)
- {
- if (editor is CodeEditorControl ce)
- {
-
- Dictionary<string, string>? filter = null;
- if (ce.Value != null)
- {
- filter = new Dictionary<string, string>();
- filter[ce.ColumnName] = ce.Value;
- }
- var popup = new PopupList(_selectedType, _entityID, new String[] { }, filter);
- popup.OnDefineFilter += type => LookupFactory.DefineFilter(type);
- if (popup.ShowDialog() == true)
- {
- _entityID = popup.ID;
- Select(_selectedType, _entityID);
- }
- }
- }
- private void SaveDocument(DataEntryDocument dataEntryDocument)
- {
- var linktype = CoreUtils.TypeList(x =>
- x.GetInterfaces().Contains(typeof(IEntityLink))
- && x.BaseType != null
- && x.BaseType.IsGenericType
- && x.BaseType.GenericTypeArguments.FirstOrDefault() == _selectedType
- ).FirstOrDefault();
- if (linktype == null)
- return;
-
- var doctype = CoreUtils.TypeList(x =>
- x.GetInterfaces().Contains(typeof(IEntityDocument))
- && x.BaseType != null
- && x.BaseType.IsGenericType
- && x.BaseType.GenericTypeArguments.FirstOrDefault() == linktype
- ).FirstOrDefault();
- if (doctype == null)
- return;
- var doc = (IEntityDocument)Activator.CreateInstance(doctype)!;
- CoreUtils.SetPropertyValue(doc,"EntityLink.ID",_entityID);
- doc.DocumentLink.ID = dataEntryDocument.Document.ID;
- doc.Thumbnail = dataEntryDocument.Thumbnail;
- ClientFactory.CreateClient(doctype).Save(doc,"Added from Data Entry Screen");
- }
- private void DoSave(bool markasprocessed)
- {
- var cancel = new System.ComponentModel.CancelEventArgs();
- if (markasprocessed && (_entity is IDataEntryInstance scannable))
- scannable.DataEntered = DateTime.Now;
- Editor.SaveItem(cancel);
- if (!cancel.Cancel)
- {
- _originalID = _entityID;
- IsChanged = false;
- var row = _documents._dataEntryGrid.SelectedRows.FirstOrDefault();
- if (row != null)
- {
- var scan = row?.ToObject<DataEntryDocument>();
- scan.EntityID = _entity.ID;
- if (markasprocessed)
- {
- SaveDocument(scan);
- scan.Archived = DateTime.Now;
- }
- if (scan.IsChanged())
- {
- new Client<DataEntryDocument>().Save(scan, "Updated from Data Entry Screen");
- _documents.Refresh();
- }
-
- }
- }
- }
- private void PopulateEditor()
- {
- if (_selectedType == null)
- return;
- _entity = null;
- if (_entityID != Guid.Empty)
- {
- _entity = Client.Create(_selectedType)
- .Query(
- Filter.Create<Entity>(_selectedType, x => x.ID).IsEqualTo(_entityID),
- _grid.LoadEditorColumns()
- )
- .ToObjects(_selectedType)
- .OfType<Entity>()
- .FirstOrDefault();
- }
- _entity ??= Activator.CreateInstance(_selectedType) as Entity;
- if (_entity == null)
- return;
- _grid?.InitialiseEditorForm(Editor, new object[] { _entity }, null, true);
-
- _process = new Button()
- {
- Content = "Mark as Processed",
- BorderBrush = new SolidColorBrush(Colors.DarkBlue),
- Background = new SolidColorBrush(Colors.DodgerBlue),
- Margin = new Thickness(5, 5, 0, 5),
- Padding = new Thickness(10, 0, 10, 0),
- IsEnabled = _processenabled
- };
- _process.Click += (sender, args) => DoSave(true);
- Editor.AddButton(_process);
- IsChanged = false;
- }
-
- }
- }
|