using PX.Data; using PX.Data.BQL; using PX.Data.BQL.Fluent; using PX.Data.Maintenance.GI; using System; using System.Collections.Generic; using System.Linq; namespace GISidePanels { // Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod This extension is required on Generic Inquiry (SM208000). public class GenericInquiryDesigner_Extension : PXGraphExtension { public PXSelect>>, OrderBy>>> SidePanelTargets; public PXSelect>, And>>>, OrderBy>> SidePanelParameters; protected virtual void _(Events.RowInserting eventArgs) { GISidePanelTarget row = eventArgs.Row; if (row == null) { return; } GIDesign design = Base.Designs.Current; row.DesignID = design?.DesignID; row.IsActive = row.IsActive ?? true; row.Title = string.IsNullOrWhiteSpace(row.Title) ? (string.IsNullOrWhiteSpace(design?.SitemapTitle) ? design?.Name : design.SitemapTitle) : row.Title; row.Icon = string.IsNullOrWhiteSpace(row.Icon) ? "apps" : row.Icon; } protected virtual void _(Events.RowPersisting eventArgs) { GISidePanelTarget row = eventArgs.Row; if (row == null || eventArgs.Operation == PXDBOperation.Delete) { return; } if (GISidePanelRuntime.ResolveTargetGraphType(Base, row.TargetScreenID) == null) { throw new PXRowPersistingException( nameof(GISidePanelTarget.TargetScreenID), row.TargetScreenID, GISidePanelMessages.TargetGraphUnavailable, row.TargetScreenID); } } protected virtual void _(Events.RowInserting eventArgs) { GISidePanelParameter row = eventArgs.Row; if (row == null) { return; } GISidePanelTarget target = SidePanelTargets.Current; row.DesignID = row.DesignID ?? target?.DesignID ?? Base.Designs.Current?.DesignID; row.TargetScreenID = string.IsNullOrWhiteSpace(row.TargetScreenID) ? target?.TargetScreenID : row.TargetScreenID; } protected virtual void _(Events.RowSelected eventArgs) { ConfigureParameterLists(eventArgs.Cache, eventArgs.Row); } protected virtual void _(Events.FieldUpdated eventArgs) { if (eventArgs.Row != null) { eventArgs.Cache.SetValue(eventArgs.Row, null); } } protected virtual void _(Events.RowPersisting eventArgs) { GISidePanelParameter row = eventArgs.Row; if (row == null || eventArgs.Operation == PXDBOperation.Delete) { return; } if (string.IsNullOrWhiteSpace(row.ParameterName) && string.IsNullOrWhiteSpace(row.SourceViewName) && string.IsNullOrWhiteSpace(row.SourceFieldName)) { eventArgs.Cancel = true; return; } Guid? designID = row.DesignID ?? Base.Designs.Current?.DesignID; bool parameterExists = SelectActiveParameters(designID) .Any(parameter => string.Equals(parameter.Name, row.ParameterName, StringComparison.OrdinalIgnoreCase)); if (!parameterExists) { throw new PXRowPersistingException( nameof(GISidePanelParameter.ParameterName), row.ParameterName, GISidePanelMessages.GIParameterUnavailable, row.ParameterName); } bool duplicateParameterExists = SidePanelParameters.Select() .RowCast() .Any(parameter => parameter.LineNbr != row.LineNbr && string.Equals(parameter.ParameterName, row.ParameterName, StringComparison.OrdinalIgnoreCase)); if (duplicateParameterExists) { throw new PXRowPersistingException( nameof(GISidePanelParameter.ParameterName), row.ParameterName, GISidePanelMessages.DuplicateGIParameterMapping, row.ParameterName); } Type graphType = GISidePanelRuntime.ResolveTargetGraphType(Base, row.TargetScreenID) ?? throw new PXRowPersistingException( nameof(GISidePanelParameter.TargetScreenID), row.TargetScreenID, GISidePanelMessages.TargetGraphUnavailable, row.TargetScreenID); Type sourceCacheType = string.IsNullOrWhiteSpace(row.SourceViewName) ? null : GISidePanelRuntime.ResolveTargetViewCacheType(graphType, row.SourceViewName); sourceCacheType = sourceCacheType ?? throw new PXRowPersistingException( nameof(GISidePanelParameter.SourceViewName), row.SourceViewName, GISidePanelMessages.SourceViewUnavailable, row.SourceViewName, row.TargetScreenID); PXCache sourceCache = Base.Caches[sourceCacheType]; if (!GISidePanelRuntime.IsMappableSourceField(sourceCache, row.SourceFieldName)) { throw new PXRowPersistingException( nameof(GISidePanelParameter.SourceFieldName), row.SourceFieldName, GISidePanelMessages.SourceFieldUnavailable, row.SourceFieldName, row.SourceViewName, row.TargetScreenID); } } private void ConfigureParameterLists(PXCache cache, GISidePanelParameter row) { if (row == null) { return; } if (Base.Designs.Current?.DesignID == null) { SetEmptyParameterLists(cache, row); return; } string[] parameterNames = SelectActiveParameters(Base.Designs.Current.DesignID) .Select(parameter => parameter.Name) .OrderBy(name => name, StringComparer.OrdinalIgnoreCase) .ToArray(); PXStringListAttribute.SetList( cache, row, parameterNames, parameterNames); if (string.IsNullOrWhiteSpace(row.ParameterName) || string.IsNullOrWhiteSpace(row.TargetScreenID)) { SetEmptySourceLists(cache, row); return; } Type graphType = GISidePanelRuntime.ResolveTargetGraphType(Base, row.TargetScreenID); if (graphType == null) { SetEmptySourceLists(cache, row); return; } string[] viewNames = GISidePanelRuntime.GetTargetViewNames(graphType); PXStringListAttribute.SetList( cache, row, viewNames, viewNames); if (string.IsNullOrWhiteSpace(row.SourceViewName)) { PXStringListAttribute.SetList( cache, row, Array.Empty(), Array.Empty()); return; } Type sourceCacheType = GISidePanelRuntime.ResolveTargetViewCacheType(graphType, row.SourceViewName); if (sourceCacheType == null) { PXStringListAttribute.SetList( cache, row, Array.Empty(), Array.Empty()); return; } PXCache sourceCache = Base.Caches[sourceCacheType]; List> fields = sourceCache.Fields .Where(fieldName => GISidePanelRuntime.IsMappableSourceField(sourceCache, fieldName)) .Select(fieldName => new KeyValuePair( fieldName, GetFieldLabel(sourceCache, fieldName))) .OrderBy(field => field.Value, StringComparer.OrdinalIgnoreCase) .ToList(); PXStringListAttribute.SetList( cache, row, fields.Select(field => field.Key).ToArray(), fields.Select(field => field.Value).ToArray()); } private static string GetFieldLabel(PXCache cache, string fieldName) { string displayName = PXUIFieldAttribute.GetDisplayName(cache, fieldName); return string.IsNullOrWhiteSpace(displayName) || string.Equals(displayName, fieldName, StringComparison.OrdinalIgnoreCase) ? fieldName : displayName + " (" + fieldName + ")"; } private IEnumerable SelectActiveParameters(Guid? designID) { if (designID == null) { return Enumerable.Empty(); } return SelectFrom .Where> .View.ReadOnly.Select(Base, designID) .RowCast() .ToArray() .Where(parameter => parameter.IsActive == true && !string.IsNullOrWhiteSpace(parameter.Name)); } private static void SetEmptySourceLists(PXCache cache, GISidePanelParameter row) { PXStringListAttribute.SetList( cache, row, Array.Empty(), Array.Empty()); PXStringListAttribute.SetList( cache, row, Array.Empty(), Array.Empty()); } private static void SetEmptyParameterLists(PXCache cache, GISidePanelParameter row) { PXStringListAttribute.SetList( cache, row, Array.Empty(), Array.Empty()); SetEmptySourceLists(cache, row); } } }