123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using Syncfusion.Data.Extensions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PRSDesktop.Panels.Jobs;
- public class JobProductMappingsGrid : DynamicDataGrid<JobProductMapping>, IJobControl, IDataModelSource
- {
- public Job Job { get; set; }
- JobPanelSettings IJobControl.Settings { get; set; }
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x => x.JobDocumentSet.ID);
- HiddenColumns.Add(x => x.Code);
- HiddenColumns.Add(x => x.Description);
- ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
- }
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- options.Add(DynamicGridOption.SelectColumns);
- }
- private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
- {
- if (row is not null)
- {
- var docSet = row.Get<JobProductMapping, Guid>(x => x.JobDocumentSet.ID);
- if(docSet != Guid.Empty)
- {
- var caption = Security.CanEdit<JobDocumentSet>() ? "Edit Document Set" : "View Document Set";
- column.AddItem("Edit Document Set", PRSDesktop.Resources.design, (_) =>
- {
- ViewDocumentSet(docSet);
- });
- }
- else
- {
- if (Security.CanEdit<JobDocumentSet>())
- {
- column.AddItem("Create Document Set", PRSDesktop.Resources.design, (_) =>
- {
- CreateDocumentSet(row);
- });
- }
- }
- }
- }
- private static DynamicDataGrid<JobDocumentSet> GetDocumentSetGrid()
- {
- var gridType = DynamicGridUtils.FindDynamicGrid(typeof(DynamicDataGrid<>), typeof(JobDocumentSet))
- ?? typeof(DynamicDataGrid<JobDocumentSet>);
- return (Activator.CreateInstance(gridType) as DynamicDataGrid<JobDocumentSet>)!;
- }
- private void ViewDocumentSet(Guid docSetID)
- {
- var docSet = Client.Query(
- new Filter<JobDocumentSet>(x => x.ID).IsEqualTo(docSetID),
- DynamicGridUtils.LoadEditorColumns<JobDocumentSet>(new Columns<JobDocumentSet>(x => x.ID)))
- .ToObjects<JobDocumentSet>()
- .FirstOrDefault();
- if(docSet is null)
- {
- MessageWindow.ShowMessage("Document set does not exist.", "Error");
- Logger.Send(LogType.Information, "", "Document set does not exist.");
- return;
- }
- GetDocumentSetGrid().EditItems(new JobDocumentSet[] { docSet });
- }
- private void CreateDocumentSet(CoreRow row)
- {
- var folder = new MultiSelectDialog<JobDocumentSetFolder>(
- new Filter<JobDocumentSetFolder>(x => x.Job.ID).IsEqualTo(Job.ID),
- new Columns<JobDocumentSetFolder>(x => x.ID),
- multiselect: false);
- if (!folder.ShowDialog())
- {
- MessageWindow.ShowMessage("You need to select a folder to add this document set to.", "Folder required");
- return;
- }
- var code = row.Get<JobProductMapping, string>(x => x.Code);
- var docSet = new JobDocumentSet
- {
- Code = code,
- Description = row.Get<JobProductMapping, string>(x => x.Description) + $"\nAttached to JobProductMapping '{code}'"
- };
- docSet.Job.ID = Job.ID;
- docSet.Folder.ID = folder.IDs().First();
- if(GetDocumentSetGrid().EditItems(new JobDocumentSet[] { docSet }))
- {
- var productMapping = row.ToObject<JobProductMapping>();
- productMapping.JobDocumentSet.ID = docSet.ID;
- Client.Save(productMapping, "Attached to new JobDocumentSet");
- Refresh(false, true);
- }
- }
- protected override bool CanCreateItems()
- {
- return Job.ID != Guid.Empty;
- }
- protected override JobProductMapping CreateItem()
- {
- var result = base.CreateItem();
- result.Job.ID = Job.ID;
- result.Job.Synchronise(Job);
- return result;
- }
- protected override void Reload(Filters<JobProductMapping> criteria, Columns<JobProductMapping> columns, ref SortOrder<JobProductMapping>? sort, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(new Filter<JobProductMapping>(x => x.Job.ID).IsEqualTo(Job.ID));
- base.Reload(criteria, columns, ref sort, action);
- }
- #region IPanel
- public string SectionName => "Job Product Mappings";
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public DataModel DataModel(Selection selection)
- {
- var ids = ExtractValues(x => x.ID, selection).ToArray();
- return new BaseDataModel<JobProductMapping>(new Filter<JobProductMapping>(x => x.ID).InList(ids));
- }
- #endregion
- }
|