import { BusinessEventType } from "../.."; import { Model } from "../../../model"; import { BusinessObjectType } from '../../businessObjectType'; import { IEventParticipantTableJSON } from "./iEventParticipantTableJSON"; import { IEventParticipantTableAuthorization } from "./iEventParticipantTableAuthorization"; import { IEventParticipantTableModelFormat } from "./iEventParticipantTableModelFormat"; import { IEventParticipantTableAuthorizationModelFormat } from "./iEventParticipantTableAuthorizationModelFormat"; /** * The `EventParticipantTable` defines which [[BusinessEventType]] can be triggered by which type of participant (i.e. a subset of [[BusinessObjectType]]s). * * EventParticipationTables are **mutable** * * @specfield allowed(evt, ptcp): determines whether or not (true/false) event type evt can be triggered by participant type ptcp */ export class EventParticipantTable { //================================ Representation ================================ private ept: Map>; //================================= Constructor ================================== /** * Creates a new EventParticipantTable ept so that: * * ept.allowed(evt, ptcp) = false for every event type and participant type found in `model` * * By default, everything is denied * @param model Domain model */ public constructor(model: Model) { this.ept = new Map>(); const participants: Array = model.getBusinessObjectTypes().filter(item => item.isParticipant()); const events: Array = model.getBusinessEventTypes(); for(const event of events) { this.ept.set(event, new Map()); for(const participant of participants) { this.ept.get(event).set(participant, false); } } } //=================================== Getters ==================================== /** * @willreturn allowed(event, participantType) both event and participantType are already part of this. * @willreturn otherwise, returns false * @param event Event type for which authorization to trigger is checked * @param participantType Participant type for which authorization to trigger event is checked */ public isAllowed(event: BusinessEventType, participantType: BusinessObjectType): boolean { const result = this.ept.get(event).get(participantType); if(!result) return false; return result; } //=================================== Setters ==================================== /** * @throws an error in case participantType.participant = false * @modifies this * @effects otherwise, this_post.allowed(event, participantType) = true * @param event Event for which authorization to trigger must be provided * @param participantType ParticipantType for which authorization must be provided */ public allow(event: BusinessEventType, participantType: BusinessObjectType): void { if(!participantType.isParticipant()) throw new Error(`[EventParticipantTable.allow(...)]: The business object type must be a participant type (not the case for ${participantType.getName()})`); const currentEvent = this.ept.get(event); currentEvent.set(participantType, true); } /** * @throws an error in case participantType.participant = false * @modifies this * @effects otherwise, this_post.allowed(event, participantType) = false * @param event Event for which authorization to trigger must be removed * @param participantType ParticipantType for which authorization must be removed */ public deny(event: BusinessEventType, participantType: BusinessObjectType): void { if(!participantType.isParticipant()) throw new Error(`[EventParticipantTable.deny(...)]: The business object type must be a participant type (not the case for ${participantType.getName()})`); const currentEvent = this.ept.get(event); currentEvent.set(participantType, false); } //=================================== Other ==================================== public toJSON(): IEventParticipantTableJSON { const auth: Array = new Array(); for(const individualAuth of this.ept) { const businessEventType = individualAuth[0]; for(const ptcpt of individualAuth[1]) { const singleAuth: IEventParticipantTableAuthorization = { businessEventType: businessEventType.toJSON(), participantType : ptcpt[0].toJSON(), allowed : ptcpt[1] }; auth.push(singleAuth); } } return { authorizations: auth }; } public toModelFormat(): IEventParticipantTableModelFormat { const auth: Array = new Array(); for(const individualAuth of this.ept) { const businessEventType = individualAuth[0]; for(const ptcpt of individualAuth[1]) { const singleAuth: IEventParticipantTableAuthorizationModelFormat = { businessEventType: businessEventType.getId(), participantType : ptcpt[0].getId(), allowed : ptcpt[1] }; auth.push(singleAuth); } } return { authorizations: auth }; } }