File

projects/wms-framework/src/lib/dataService/link.ts

Description

Defines the information of a linked property

Index

Properties

Constructor

constructor(property: string, sourceEntity: any, deleted: boolean)

Creates an instance of LinkInfo.

Parameters :
Name Type Optional Description
property string No

the name of the property linked

sourceEntity any No

the source entity the link was added to thru the given property

deleted boolean No

indicates wheather link was added (false) or removed (true)

Properties

Public deleted
Type : boolean
indicates wheather link was added (false) or removed (true)
Public property
Type : string
the name of the property linked
Public sourceEntity
Type : any
the source entity the link was added to thru the given property
import { InvalidOperationException } from '../baseframework/Exceptions';
import { EntityObject } from './dataServiceInterfaces';

/**
 * Defines the information of a linked property
 *
 * @export
 * @class LinkInfo
 */
export class LinkInfo {
  /**
   * Creates an instance of LinkInfo.
   * @param {string} property the name of the property linked
   * @param {number} sourceEntity the source entity the link was added to thru the given property
   * @param {boolean} deleted indicates wheather link was added (false) or removed (true)
   * @memberof LinkInfo
   */
  constructor(
    public property: string,
    public sourceEntity: any,
    public deleted: boolean
  ) {}
}

export class Link {
  // true for boolean property indicates deleted, otherway is added
  #links: Map<string, Map<number, { entity; state }>> = new Map<string, any>();

  /**
   * Gets all links kept by this Link object
   */
  *[Symbol.iterator](): Iterator<LinkInfo, any, any> {
    for (const linkProp of this.#links) {
      for (const linkObj of linkProp[1]) {
        yield new LinkInfo(linkProp[0], linkObj[1].entity, linkObj[1].state);
      }
    }
  }

  /**
   * Links the given target entity to a property
   *
   * @param {string} property property to link
   * @param {EntityObject} sourceEntity the source entity to be linked by the given property
   * @memberof Link
   */
  addLink(property: string, sourceEntity: EntityObject) {
    if (!this.#links.has(property)) {
      this.#links.set(property, new Map());
    }
    let linkedObjects = this.#links.get(property);
    let targetKey = sourceEntity.dataServiceId;
    if (linkedObjects.has(targetKey)) {
      throw new InvalidOperationException(
        `Property ${property} already linked to entity:${targetKey}`
      );
    }
    linkedObjects.set(targetKey, { entity: sourceEntity, state: false }); // added as not removed
  }
}

result-matching ""

    No results matching ""