File

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

Index

Properties
Methods

Properties

links
Type : Map<string | Map<number, literal type>>
Default value : new Map<string, any>()

Methods

addLink
addLink(property: string, sourceEntity: EntityObject)

Links the given target entity to a property

Parameters :
Name Type Optional Description
property string No

property to link

sourceEntity EntityObject No

the source entity to be linked by the given property

Returns : void
()

Gets all links kept by this Link object

Returns : Iterator<LinkInfo, any, any>
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 ""