import { BusinessObjectType } from "../../businessObjectType"; import { AttributeType } from '../../attribute'; import { Model } from "../../../model"; import { IAttributeReadingTableJSON } from "./iAttributeReadingTableJSON"; import { IAttributeReadingAuthorizationJSON, AttributeReadingAuthorization, IAttributeReadingAuthorizationModelFormat } from "./attributeReadingAuthorization"; import { IAttributeReadingTableModelFormat } from "./iAttributeReadingTableModelFormat"; export class AttributeReadingTable { private authorizations: Map>; public constructor(model: Model) { this.authorizations = new Map>(); const ptcpts = model.getBusinessObjectTypes().filter(item => item.isParticipant()); const botAttributes: Array = new Array(); for(const bot of model.getBusinessObjectTypes()) { for(const attr of bot.getAttributes()) { botAttributes.push(attr); } } for(const attr of botAttributes) { for(const ptcpt of ptcpts) { this.authorizations.set(attr, new Map()); this.authorizations.get(attr).set(ptcpt, false); } } } public allowed(attr: AttributeType, ptcp: BusinessObjectType): boolean { const result = this.authorizations.get(attr).get(ptcp); if(!result) return false; return result; } public allow(attr: AttributeType, participantType: BusinessObjectType): void { if(!participantType.isParticipant()) throw new Error(`[AttributeReadingTable.allow(...)]: ParticipantType must be a participant type`); this.authorizations.get(attr).set(participantType, true); } public deny(attr: AttributeType, participantType: BusinessObjectType): void { if(!participantType.isParticipant()) throw new Error(`[AttributeReadingTable.deny(...)]: ParticipantType must be a participant type`); this.authorizations.get(attr).set(participantType, false); } public toJSON(): IAttributeReadingTableJSON { const auths: Array = new Array(); for(const auth of this.authorizations) { for(const ptcp of auth[1]) { const singleAuth = new AttributeReadingAuthorization(auth[0], ptcp[0], ptcp[1]); auths.push(singleAuth.toJSON()); } } return { authorizations: auths }; } public toModelFormat(): IAttributeReadingTableModelFormat { const auths: Array = new Array(); for(const auth of this.authorizations) { for(const ptcp of auth[1]) { const singleAuth = new AttributeReadingAuthorization(auth[0], ptcp[0], ptcp[1]); auths.push(singleAuth.toModelFormat()); } } return { authorizations: auths }; } }