| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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, TType>(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<TType, TEntity,TLink> : DynamicItemsListGrid<TType>
- where TType : BaseIntegrationSource<TEntity,TLink>, new()
- where TEntity : Entity, IRemotable, IPersistent, new()
- where TLink : EntityLink<TEntity>
- {
-
- private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register(
- nameof(SourceType),
- typeof(IntegrationSourceType),
- typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
- );
- public IntegrationSourceType SourceType
- {
- get => (IntegrationSourceType)GetValue(SourceTypeProperty);
- set => SetValue(SourceTypeProperty, value);
- }
-
- private static readonly DependencyProperty CreateEntityProperty = DependencyProperty.Register(
- nameof(CreateEntity),
- typeof(ICommand),
- typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
- );
- public ICommand? CreateEntity
- {
- get => GetValue(CreateEntityProperty) as ICommand;
- set => SetValue(CreateEntityProperty, value);
- }
-
- protected override void Init()
- {
- base.Init();
-
- var required = Columns.Required<TType>();
- 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<TEntity,TType>(entity,item);
- CreateEntity?.Execute(args);
- if (!args.Cancel)
- {
- item.Source = SourceType;
- new Client<TEntity>().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<TType>(x => x.Code, 200, "Code");
- result.Add<TType>(x => x.Description, 0, "Description");
- result.Add<TType>(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<TType, string>(x => x.Code, "."), column.ColumnName))
- editor.Editable = Editable.Disabled;
- else if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Description, "."), column.ColumnName))
- editor.Editable = Editable.Disabled;
- else if (String.Equals(CoreUtils.GetFullPropertyName<TType, Guid>(x => x.Entity.ID, "."), column.ColumnName))
- {
- var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit<TEntity>() };
- var required = Columns.Required<TEntity>();
- foreach (var req in required)
- popup.OtherColumns[$"Entity.{req.Property}"] = req.Property;
- editor = popup;
- }
- // else if(new Column<TType>(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
- // popup.CanAdd = Security.CanEdit<TEntity>();
- return editor;
- }
- public override void SaveItem(TType item)
- {
- base.SaveItem(item);
- Client.Save(item,"Updated by Integration Window ");
- }
-
- }
|