Skip to content

Instantly share code, notes, and snippets.

@BrianMRO
Last active September 28, 2021 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianMRO/6e442e41e0dcaa748f6efceb8c67aeed to your computer and use it in GitHub Desktop.
Save BrianMRO/6e442e41e0dcaa748f6efceb8c67aeed to your computer and use it in GitHub Desktop.
Related Attributes - MyCustomAttributeList
#region MyCustomAttributeList
public class MyCustomAttributeList<TEntity> : CRAttributeList<TEntity>
{
#region SelectDelegate - Redefined
// This is the actual customization that is enabling the Attributes tab to populate for InventoryItem Attributes instead of MyDAC
protected new virtual IEnumerable SelectDelegate()
{
var currentObject = (MyDAC)_Graph.Caches<MyDAC>()?.Current;
InventoryItem item = SelectFrom<InventoryItem>.Where<InventoryItem.inventoryID.IsEqual<@P.AsInt>>
.View.Select(_Graph, currentObject.InventoryID);
return SelectInternal(item);
}
#endregion
#region Copied from CRAttributeList to make this work
private readonly EntityHelper _helper;
private const string CbApiValueFieldName = "Value$value";
private const string CbApiAttributeIDFieldName = "AttributeID$value";
public PlanItemAttributeList(PXGraph graph) : base(graph)
{
_Graph = graph;
_helper = new EntityHelper(graph);
View = graph.IsExport
? GetExportOptimizedView()
: new PXView(graph, false,
new Select3<CSAnswers, OrderBy<Asc<CSAnswers.order>>>(),
new PXSelectDelegate(SelectDelegate));
PX.Api.PXDependToCacheAttribute.AddDependencies(View, new[] { typeof(TEntity) });
_Graph.EnsureCachePersistence(typeof(CSAnswers));
PXDBAttributeAttribute.Activate(_Graph.Caches[typeof(TEntity)]);
_Graph.FieldUpdating.AddHandler<CSAnswers.value>(FieldUpdatingHandler);
_Graph.FieldSelecting.AddHandler<CSAnswers.value>(FieldSelectingHandler);
_Graph.FieldSelecting.AddHandler<CSAnswers.isRequired>(IsRequiredSelectingHandler);
_Graph.FieldSelecting.AddHandler<CSAnswers.attributeCategory>(AttributeCategorySelectingHandler);
_Graph.FieldSelecting.AddHandler<CSAnswers.attributeID>(AttrFieldSelectingHandler);
_Graph.RowPersisting.AddHandler<CSAnswers>(RowPersistingHandler);
_Graph.RowPersisting.AddHandler<TEntity>(ReferenceRowPersistingHandler);
_Graph.RowUpdating.AddHandler<TEntity>(ReferenceRowUpdatingHandler);
_Graph.RowDeleted.AddHandler<TEntity>(ReferenceRowDeletedHandler);
_Graph.RowInserted.AddHandler<TEntity>(RowInsertedHandler);
_Graph.Caches<CSAnswers>().Fields.Add(CbApiValueFieldName);
_Graph.Caches<CSAnswers>().Fields.Add(CbApiAttributeIDFieldName);
_Graph.FieldSelecting.AddHandler(typeof(CSAnswers), CbApiValueFieldName, CbApiValueFieldSelectingHandler);
_Graph.FieldSelecting.AddHandler(typeof(CSAnswers), CbApiAttributeIDFieldName, CbApiAttributeIdFieldSelectingHandler);
}
private PXView GetExportOptimizedView()
{
var instance = _Graph.Caches[typeof(TEntity)].CreateInstance();
var classIdField = GetClassIdField(instance);
var noteIdField = typeof(TEntity).GetNestedType(nameof(CSAttribute.noteID));
var command = BqlTemplate.OfCommand<
Select2<CSAnswers,
InnerJoin<CSAttribute,
On<CSAnswers.attributeID, Equal<CSAttribute.attributeID>>,
InnerJoin<CSAttributeGroup,
On<CSAnswers.attributeID, Equal<CSAttributeGroup.attributeID>>>>,
Where<CSAttributeGroup.isActive, Equal<True>,
And<CSAttributeGroup.entityType, Equal<TypeNameConst>,
And<CSAttributeGroup.entityClassID, Equal<Current<BqlPlaceholder.A>>,
And<CSAnswers.refNoteID, Equal<Current<BqlPlaceholder.B>>>>>>>>
.Replace<BqlPlaceholder.A>(classIdField)
.Replace<BqlPlaceholder.B>(noteIdField)
.ToCommand();
return new PXView(_Graph, true, command);
}
internal virtual void CbApiValueFieldSelectingHandler(PXCache sender, PXFieldSelectingEventArgs e)
{
if (e.Row is CSAnswers answer)
{
// set in FieldUpdating
if (sender.GetAttributes<CSAnswers.value>(answer).OfType<PXUIFieldAttribute>().FirstOrDefault() is IPXInterfaceField uiField)
{
if (uiField.ErrorText != null)
{
e.ReturnState = PXFieldState.CreateInstance(uiField.ErrorValue, typeof(String),
errorLevel: PXErrorLevel.Error,
error: uiField.ErrorText);
}
}
e.ReturnValue = answer.Value;
}
}
internal virtual void CbApiAttributeIdFieldSelectingHandler(PXCache sender, PXFieldSelectingEventArgs e)
{
if (e.Row is CSAnswers answer)
e.ReturnValue = answer.AttributeID;
}
#endregion
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment