import { v4 as uuid } from "uuid" ; import { EntitySchema } from "../schema/entity-schema" ; import { DatabaseField } from "../database-field.decorator" ; import { getWarningLevelColor , WarningLevel } from "./warning-level" ; /**
 * This represents a static class of type <T>.
 * It can be used for passing a class from which new objects should be created.
 * It can also be used to check the ENTITY_TYPE of a class
 * For example usage check the  EntityMapperService .
  export type EntityConstructor < T extends Entity = Entity > = ( new ( id ? ) => T ) & typeof Entity ; export const ENTITY_CONFIG_PREFIX = "entity:" ; /**
 * "Entity" is a base class for all domain model classes.
 * It implements the basic general properties and methods that are required for all Entity types
 * e.g. supporting the Entity Schema system or basic database logic.
 *
 * Entity classes do not deal with database actions, use  EntityMapperService with its find/save/delete functions.
 *
 * Do not use the Entity class directly. Instead implement your own Entity types, writing classes that extend "Entity".
 * A How-To Guide on how to implement your own types is available:
 * - [How to Create a new Entity Type] {@link /additional-documentation/how-to-guides/create-a-new-entity-type.html} export class Entity { /**
     * The entity's type.
     * In classes extending Entity this is usually overridden by the class annotation `@DatabaseEntity('NewEntity')`.
     * The type needs to be used as routing path in lower case. The routing path can be defined in the configuration file.
     */ static ENTITY_TYPE = "Entity" ; /**
     * EntitySchema defining property transformations from/to the database.
     * This is auto-generated from the property annotations `@DatabaseField()`.
     *
     * see  {@link /additional-documentation/how-to-guides/create-a-new-entity-type.html} static schema : EntitySchema ; param id static extractTypeFromId ( id ) { const split = id . indexOf ( ":" ) ; return id . substring ( 0 , split ) ; } param id static extractEntityIdFromId ( id ) { const split = id . indexOf ( ":" ) ; return id . substring ( split + 1 ) ; } param type param id static createPrefixedId ( type , id ) { id = String ( id ) ; const prefix = type + ":" ; if ( ! id . startsWith ( prefix ) ) { return prefix + id ; } else { return id ; } } static getBlockComponent ( ) { return ; } example @ DatabaseField ( ) _id : string ; /** internal database doc revision, used to detect conflicts by PouchDB/CouchDB */ @ DatabaseField ( ) _rev : string ; /** actual id without prefix */ get entityId ( ) : string { return Entity . extractEntityIdFromId ( this . _id ) ; } param newEntityId set entityId ( newEntityId ) { this . _id = Entity . createPrefixedId ( this . getType ( ) , newEntityId ) ; } /**
     * Returns an array of strings by which the entity can be searched.
     *
     * By default the parts of the "name" property (split at spaces) is used if it is present.
     *
     * <b>Overwrite this method in subtypes if you want an entity type to be searchable by other properties.</b>
     */ @ DatabaseField ( ) get searchIndices ( ) : string [ ] { let indices = [ ] ; if ( this . hasOwnProperty ( "name" ) ) { indices = this [ "name" ] . split ( " " ) ; } return indices ; } set searchIndices ( value ) { } param id constructor ( id = uuid ( ) ) { this . entityId = id ; } /**
     * Get the class (Entity or the actual subclass of the instance) to call static methods on the correct class considering inheritance
     */ getConstructor ( ) { return < typeof Entity > this . constructor ; } /**
     * Get the entity schema of this class
     */ getSchema ( ) { return this . getConstructor ( ) . schema ; } returns { string } public getId ( ) { return this . entityId ; } returns { string } public getType ( ) { return this . getConstructor ( ) . ENTITY_TYPE ; } returns { string } public toString ( ) { return this . getId ( ) ; } /**
     * Used by some generic UI components to set the color for the entity instance.
     * Override this method as needed.
     */ public getColor ( ) { return getWarningLevelColor ( this . getWarningLevel ( ) ) ; } /**
     * Override getWarningLevel() to define when the entity is in a critical condition and should be color-coded
     * and highlighted in generic components of the UI.
     */ public getWarningLevel ( ) { return WarningLevel . NONE ; } /**
     * Shallow copy of the entity.
     * The resulting entity will be of the same type as this
     * (taking into account subclassing)
     */ public copy ( ) { const other = new ( this . getConstructor ( ) ) ( this . _id ) ; Object . assign ( other , this ) ; return other ; } /**
     * Checks if the entity is valid and if the check fails, throws an error explaining the failed check.
     */ assertValid ( ) { return ; } }