Skip to content

Instantly share code, notes, and snippets.

@ste-bel
Last active December 3, 2020 05:33
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 ste-bel/a27c51dd4234d88c7d098b161b3d9386 to your computer and use it in GitHub Desktop.
Save ste-bel/a27c51dd4234d88c7d098b161b3d9386 to your computer and use it in GitHub Desktop.
Slot loader with Yaql
using PX.Data;
using PX.DbServices.QueryObjectModel;
using System;
using System.Collections.Generic;
using CR = PX.Objects.CR;
namespace PB.Objects.B2B.Slot {
public class TypePermissions : IPrefetchable {
private static readonly string SLOT_NAME = typeof(TypePermissions).Name;
private static readonly Type SLOT_TYPE = typeof(B2MessageTypePermission);
private static readonly string ALIAS = SLOT_TYPE.Name;
private IEnumerable<PXDataRecord> GetData() {
var data = PXDatabase.
SelectMulti<B2MessageTypePermission>(
Yaql.join<B2MessageType>(Yaql.eq<B2MessageType.messageType, B2MessageTypePermission.messageType>()),
Yaql.join<CR.BAccount>(Yaql.eq<CR.BAccount.bAccountID, B2MessageTypePermission.bAccountID>()),
new PXDataField<B2MessageTypePermission.messageType>(ALIAS),
new PXDataField<B2MessageTypePermission.bAccountID>(ALIAS),
new PXDataField<B2MessageTypePermission.active>(ALIAS)
);
return data;
}
public class TypePermission {
public List<object> Key { get; }
public bool? Active { get; }
internal TypePermission(PXDataRecord record) {
var index = 0;
var messageType = record.GetString(index++);
var bAccount = record.GetInt32(index++);
Key = new ContentHashList<object>() { messageType, bAccount };
Active = record.GetBoolean(index++);
}
internal TypePermission(string messageType, int? bAccount, bool active) {
Key = new ContentHashList<object>() { messageType, bAccount };
Active = active;
}
}
private readonly IDictionary<List<object>, TypePermission> content = new Dictionary<List<object>, TypePermission>();
public void Prefetch() {
content.Clear();
var data = GetData(); // Watch out, cannot be enumerated multiple times
foreach (var record in data) {
var typePermission = new TypePermission(record);
var key = typePermission.Key;
if (!content.ContainsKey(key)) {
content.Add(key, typePermission);
}
}
}
public IDictionary<List<object>, TypePermission> Content => content;
public static bool TryGetTypePermission(string messageType, int? bAccount, out TypePermission value) {
var slot = PXDatabase.GetSlot<TypePermissions>(SLOT_NAME, SLOT_TYPE);
var key = new ContentHashList<object>() { messageType, bAccount };
var found = slot.Content.TryGetValue(key, out var response);
if (!found) {
MessageTypes.TryGetMessageType(messageType, out var messType);
if (messType == null) {
PXTrace.WriteWarning($"A permission was requested for a Message Type that does no exists : {messageType}");
}
response = new TypePermission(messageType, bAccount, messType?.AllowedForAll == true);
slot.Content.Add(response.Key, response);
}
value = response;
return true;
}
public static TypePermission GetTypePermission(string messageType, int? bAccount) {
var found = TryGetTypePermission(messageType, bAccount, out var response);
if (found) {
return response;
}
throw new PXException(PXLocalizer.LocalizeFormat(Messages.TypeAccountNoPermission, messageType, bAccount));
}
public static void Reset() {
PXDatabase.ResetSlot<TypePermissions>(SLOT_NAME, SLOT_TYPE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment