File

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

Description

Defines a set of entities of the same kind.

Index

Properties
Methods

Constructor

constructor(name: string)

Creates an instance of DSEntitySet.

Parameters :
Name Type Optional Description
name string No

name of the new entity set

Properties

Private addedElements
Default value : new Map<number, any>()

Keeps track of all recently added entities, new entities will be tracked here till a save operation happens.

entitiesWithLink
Type : Map<number | Link>
Default value : new Map<number, Link>()
Private indexedEntities
Default value : new Map<string, any>()

Keeps track of all entity objects of this entity set

Public name
Type : string
name of the new entity set

Methods

add
add(entity: EntityObject)

Adds a new object to this set, once added the state of the given object will be set to DataServiceEntityState.Add. An error is thrown if the entity key is already tracked.

Parameters :
Name Type Optional Description
entity EntityObject No

the {

Returns : void
addLink
addLink(sourceEntity: EntityObject, property: string, targetEntity: EntityObject)

Registers a link for an entity belonging to this entity set

Parameters :
Name Type Optional Description
sourceEntity EntityObject No

the source entity to add the link to

property string No

the property to bind

targetEntity EntityObject No

the target entity to link

Returns : void
attach
attach(entity: EntityObject)

Attaches the given entity object to this set, once atttached changes on the object will be tracked in order to update the services tier. DSEntitySet object.

Parameters :
Name Type Optional Description
entity EntityObject No

the {

Returns : void
detach
detach(entity: EntityObject)

Detaches the given entity from this entity set, once detached the object won't be tracked anymore so no changes or updates will be updated to/from the storage

Parameters :
Name Type Optional Description
entity EntityObject No

the entity to detach from this entity set

Returns : boolean

true if properly detached, false otherwise

existsKey
existsKey(key: string)

Checks if an object identified by the given key is already tracked.

Parameters :
Name Type Optional Description
key string No

the key that uniquely identifies the object

Returns : boolean

true if the object key is already track, otherwise false

get
get(key: string)

Gets the entity object tracked by the given key.

Parameters :
Name Type Optional Description
key string No

the key of the entity to get

Returns : any
getLinks
getLinks(entity: EntityObject)

Gets the links associated to the given entity

Parameters :
Name Type Optional Description
entity EntityObject No

entity to get target links for

Returns : Link

{Link}

removeAdded
removeAdded(entity: EntityObject)

Removes the given entity object from the added ones.

Parameters :
Name Type Optional Description
entity EntityObject No

the {

Returns : void
save
save(entity: EntityObject)

Marks as the object identified by the given key as saved.

Parameters :
Name Type Optional
entity EntityObject No
Returns : void
()

Gets all entities tracked by this {DSEntitySet}

import { InvalidOperationException } from '../baseframework/Exceptions';
import { DataServiceEntityState } from './dataServiceEnums';
import { EntityObject } from './dataServiceInterfaces';
import { Link } from './link';

/**
 * Defines a set of entities of the same kind.
 */
export class DSEntitySet {
  // entities indexed by set and key
  /**
   * Keeps track of all entity objects of this entity set
   *
   * @private
   * @memberof DSEntitySet
   */

  private indexedEntities = new Map<string, any>();
  /**
   * Keeps track of all recently added entities, new entities will be tracked here
   * till a save operation happens.
   *
   * @private
   * @memberof DSEntitySet
   */
  private addedElements = new Map<number, any>();

  /**
   * Tracks the links of every tracked entity object
   *
   * @type {Map<string, Link>}
   * @memberof DSEntitySet
   */
  #entitiesWithLink: Map<number, Link> = new Map<number, Link>();

  /**
   * Creates an instance of DSEntitySet.
   * @param {string} name name of the new entity set
   * @memberof DSEntitySet
   */
  constructor(public name: string) {}
  /**
   * Gets all entities tracked by this {DSEntitySet}
   */
  *[Symbol.iterator](): Iterator<EntityObject, any, any> {
    for (const entityEntry of this.indexedEntities) {
      yield entityEntry[1];
    }
    for (const entityEntry of this.addedElements) {
      yield entityEntry[1];
    }
  }
  /**
   * Adds a new object to this set, once added the state of the given object
   * will be set to {@link DataServiceEntityState.Add}.
   * An error is thrown if the entity key is already tracked.
   * @param entity the {@link EntityObject} to add to this set
   */
  add(entity: EntityObject) {
    this.addedElements.set(entity.dataServiceId, entity);
    entity.dataServiceState = DataServiceEntityState.Added;
  }

  /**
   * Registers a link for an entity belonging to this entity set
   *
   * @param {EntityObject} sourceEntity the source entity to add the link to
   * @param {string} property the property to bind
   * @param {EntityObject} targetEntity the target entity to link
   * @memberof DSEntitySet
   */
  addLink(
    sourceEntity: EntityObject,
    property: string,
    targetEntity: EntityObject
  ) {
    const targetKey = targetEntity.dataServiceId;
    if (!this.#entitiesWithLink.has(targetKey)) {
      this.#entitiesWithLink.set(targetKey, new Link());
    }
    let link = this.#entitiesWithLink.get(targetKey);
    link.addLink(property, sourceEntity);
  }

  /**
   * Gets the links associated to the given entity
   *
   * @param {EntityObject} entity entity to get target links for
   * @return {*}  {Link}
   * @memberof DSEntitySet
   */
  getLinks(entity: EntityObject): Link {
    /* istanbul ignore else */
    if (this.#entitiesWithLink.has(entity.dataServiceId)) {
      return this.#entitiesWithLink.get(entity.dataServiceId);
    }
    return null;
  }

  /**
   * Attaches the given entity object to this set, once atttached changes on the object will
   * be tracked in order to update the services tier.
   * @param entity the {@link EntityObject} object to start tracking as part of this
   * {@link DSEntitySet} object.
   */
  attach(entity: EntityObject) {
    let key: string = entity.getKey();
    if (this.existsKey(key)) {
      throw new InvalidOperationException('Already existing key');
    }
    this.indexedEntities.set(key, entity);
  }

  /**
   * Detaches the given entity from this entity set, once detached the object won't be tracked anymore
   * so no changes or updates will be updated to/from the storage
   * @param entity the entity to detach from this entity set
   * @returns true if properly detached, false otherwise
   */
  detach(entity: EntityObject): boolean {
    let key: string = entity.getKey();
    /* istanbul ignore else */
    if (this.existsKey(key)) {
      this.indexedEntities.delete(key);
      this.addedElements.delete(entity.dataServiceId);
      entity.dataServiceState = DataServiceEntityState.Detached;
      return true;
    }
    // when added element is still does not exist in indexedEntities
    /* istanbul ignore else */
    if (this.addedElements.has(entity.dataServiceId)) {
      this.addedElements.delete(entity.dataServiceId);
      entity.dataServiceState = DataServiceEntityState.Detached;
      return true;
    }
    return false;
  }

  /**
   * Removes the given entity object from the added ones.
   * @param entity the {@link EntityObject} to remove form added ones.
   */
  removeAdded(entity: EntityObject) {
    if (this.addedElements.has(entity.dataServiceId)) {
      this.addedElements.delete(entity.dataServiceId);
    }
  }

  /**
   * Checks if an object identified by the given key is already tracked.
   * @param entitySet the set the object belongs to
   * @param key the key that uniquely identifies the object
   * @returns true if the object key is already track, otherwise false
   */
  existsKey(key: string): boolean {
    return this.indexedEntities.has(key);
  }

  /**
   * Gets the entity object tracked by the given key.
   *
   * @param {string} key the key of the entity to get
   * @return {*}
   * @memberof DSEntitySet
   */
  get(key: string) {
    return this.indexedEntities.get(key);
  }

  /**
   * Marks as the object identified by the given key as saved.
   *
   * @param {string} key the key of the object to save
   * @memberof DSEntitySet
   */
  save(entity: EntityObject) {
    switch (entity.dataServiceState) {
      case DataServiceEntityState.Added:
        this.addedElements.delete(entity.dataServiceId);
        this.indexedEntities.set(entity.getKey(), entity);
        entity.dataServiceState = DataServiceEntityState.Unchanged;
        break;
      case DataServiceEntityState.Deleted:
        this.indexedEntities.delete(entity.getKey());
        this.addedElements.delete(entity.dataServiceId);
        break;
      case DataServiceEntityState.Modified:
        entity.dataServiceState = DataServiceEntityState.Unchanged;
        break;
      default:
        break;
    }
  }
}

result-matching ""

    No results matching ""