using System; using System.Collections.Generic; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using AutoProperties; using InABox.Clients; using InABox.WPF; namespace PRSDesktop.Integrations.Common; public class IntegrationGridCreateEntityArgs(TEntity entity, TType mapping) { public TEntity Entity { get; private set; } = entity; public TType Mapping { get; private set; } = mapping; public bool Cancel { get; set; } } public abstract class BaseIntegrationGrid : DynamicItemsListGrid where TType : BaseIntegrationSource, new() where TEntity : Entity, IRemotable, IPersistent, new() where TLink : EntityLink { private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register( nameof(SourceType), typeof(IntegrationSourceType), typeof(BaseIntegrationGrid) ); public IntegrationSourceType SourceType { get => (IntegrationSourceType)GetValue(SourceTypeProperty); set => SetValue(SourceTypeProperty, value); } private static readonly DependencyProperty CreateEntityProperty = DependencyProperty.Register( nameof(CreateEntity), typeof(ICommand), typeof(BaseIntegrationGrid) ); public ICommand? CreateEntity { get => GetValue(CreateEntityProperty) as ICommand; set => SetValue(CreateEntityProperty, value); } protected override void Init() { base.Init(); var required = Columns.Required(); foreach (var req in required) HiddenColumns.Add(req.Property); AddButton("Create All", PRSDesktop.Resources.plus.AsBitmapImage(), CreateAll); } private bool CreateAll(Button button, CoreRow[] rows) { foreach (var item in Items.Where(x => !string.IsNullOrWhiteSpace(x.Code) && x.Entity.ID == Guid.Empty)) { var entity = new TEntity(); var args = new IntegrationGridCreateEntityArgs(entity,item); CreateEntity?.Execute(args); if (!args.Cancel) { item.Source = SourceType; new Client().Save(entity, "Created by Integration Window"); item.Entity.CopyFrom(entity); } } return true; } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.AddRows = false; options.EditRows = false; options.DeleteRows = false; options.FilterRows = true; options.HideDatabaseFilters = true; options.DirectEdit = true; options.HideDirectEditButton = true; options.SelectColumns = true; options.MultiSelect = true; } protected override DynamicGridColumns LoadColumns() { var result = new DynamicGridColumns(); result.Add(x => x.Code, 200, "Code"); result.Add(x => x.Description, 0, "Description"); result.Add(x => x.Entity.ID, 200, typeof(TEntity).GetCaption()); return result; } protected override BaseEditor CustomiseEditor(DynamicGridColumn column, BaseEditor editor) { editor = base.CustomiseEditor(column, editor); if (String.Equals(CoreUtils.GetFullPropertyName(x => x.Code, "."), column.ColumnName)) editor.Editable = Editable.Disabled; else if (String.Equals(CoreUtils.GetFullPropertyName(x => x.Description, "."), column.ColumnName)) editor.Editable = Editable.Disabled; else if (String.Equals(CoreUtils.GetFullPropertyName(x => x.Entity.ID, "."), column.ColumnName)) { var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit() }; var required = Columns.Required(); foreach (var req in required) popup.OtherColumns[$"Entity.{req.Property}"] = req.Property; editor = popup; } // else if(new Column(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup) // popup.CanAdd = Security.CanEdit(); return editor; } public override void SaveItem(TType item) { base.SaveItem(item); Client.Save(item,"Updated by Integration Window "); } }