projects/wms-framework/src/lib/domainservicesupport/EntitiesSupport.ts
EntityContainer class
Properties |
Methods |
HasChanges |
Type : boolean
|
Default value : false
|
PropertyChanged |
Default value : new SubscriptionEvent<
(
sender: any,
e: {
PropertyName: string;
}
) => void
>()
|
CreateEntitySet | ||||||
CreateEntitySet(arg0: EntitySetOperations)
|
||||||
Type parameters :
|
||||||
Creates an entity set
Parameters :
Returns :
any
{*} |
LoadEntities | ||||||
LoadEntities(entities: any[])
|
||||||
Loads entities
Parameters :
Returns :
void
|
import { ICollection } from '../baseframework';
import { MetadataAttribute } from '../baseframework/MetadataAttribute';
import { ReflectionHelper } from '../baseframework/ReflectionSupport';
import { SimpleDictionary } from '../baseframework/SimpleDictionary';
import { Debugger } from '../diagnostics/Debugger';
import { ValidationResult } from '../models';
import { SubscriptionEvent } from '../utils/SubscriptionEvent';
import { IClassConstructor } from './DomainContext';
import { EntitySetOperations } from './OperationsSupport';
/**
* KeyAttribute class
*
* @export
* @class KeyAttribute
* @extends {MetadataAttribute}
* @wType System.ComponentModel.DataAnnotations.KeyAttribute
*/
export class KeyAttribute extends MetadataAttribute {}
/**
* RequiredAttribute class
*
* @export
* @class RequiredAttribute
* @extends {MetadataAttribute}
* @wType System.ComponentModel.DataAnnotations.RequiredAttribute
*/
export class RequiredAttribute extends MetadataAttribute {}
/**
* EntityContainer class
*
* @export
* @class EntityContainer
* @wType System.ServiceModel.DomainServices.Client.EntityContainer
*/
export class EntityContainer {
/**
* Loads entities
*
* @param {any[]} entities
* @memberof EntityContainer
* @wNoMap
*/
LoadEntities(entities: any[]) {
Debugger.Throw('Method not implemented.');
}
HasChanges: boolean = false;
PropertyChanged = new SubscriptionEvent<
(
sender: any,
e: {
PropertyName: string;
}
) => void
>();
/**
* Creates an entity set
*
* @template T
* @param {EntitySetOperations} arg0
* @return {*} {*}
* @memberof EntityContainer
* @wNoMap
*/
CreateEntitySet<T>(arg0: EntitySetOperations): any {}
}
export interface IPendingChange {
keys: any[];
keyFields: string[];
changedProperties: string[];
entityNamespace: string;
entity: any;
originalData: any;
}
export class EntityContainerHelper extends EntityContainer {
public static entities = <any>{};
public static pendingChanges = new Map<string, Map<string, IPendingChange>>();
public static addedElements = new Map<string, any[]>();
public static cachedConstructors: { [x: string]: any } = {};
public getEntitySet<T extends Entity>(name?: string): EntitySet<T> {
if (EntityContainerHelper.entities[name] != undefined) {
return new EntitySet(name, EntityContainerHelper.entities[name]);
} else {
return new EntitySet(name, []);
}
}
static getEntities<T>(name?: string): T[] {
if (EntityContainerHelper.entities[name] != undefined) {
return EntityContainerHelper.entities[name];
} else {
return [];
}
}
static getEntitiesWithFilter(
entityName: string,
filter: (entity: any) => boolean
): any {
let dataset = [];
if (EntityContainerHelper.entities[entityName] != undefined) {
dataset = EntityContainerHelper.entities[entityName];
}
return dataset.filter(filter);
}
static registerChange(
entityName: string,
entityNamespace: string,
keyValues: any[],
keyFields: string[],
memberName: string,
entity: Entity
): any {
if (
EntityContainerHelper.changeTrackingEnabled &&
entity.EntityState != EntityState.Detached
) {
entity.EntityState = EntityState.Modified;
let changes: Map<string, IPendingChange> = null;
if (!EntityContainerHelper.pendingChanges.has(entityName)) {
changes = new Map<string, IPendingChange>();
EntityContainerHelper.pendingChanges.set(entityName, changes);
}
if (keyFields.length <= 0) {
throw 'Fatal: key fields not specified';
}
let changedObjectKeyValue = entity[keyFields[0].toString()];
if (!changes.has(changedObjectKeyValue)) {
changes.set(changedObjectKeyValue, {
keys: keyValues,
keyFields: keyFields,
changedProperties: [memberName],
entityNamespace: entityNamespace,
entity: entity,
originalData: entity.copyEntityObjectDataInto({}),
});
} else {
changes.get(changedObjectKeyValue).changedProperties.push(memberName);
}
}
}
public static changeTrackingEnabled = true;
}
/**
* EntitySet class
*
* @export
* @class EntitySet
* @template T
* @wType System.ServiceModel.DomainServices.Client.EntitySet`1
*/
export class EntitySet<T extends Entity> {
private addedEntities: T[] = [];
constructor(public entityName: string, public data: T[]) {}
Add(newElement: T): void {
this.addedEntities.push(newElement);
let addedCollection: any[] = null;
if (EntityContainerHelper.addedElements.has(this.entityName)) {
addedCollection = EntityContainerHelper.addedElements.get(
this.entityName
);
} else {
addedCollection = [];
EntityContainerHelper.addedElements.set(this.entityName, addedCollection);
}
addedCollection.push(newElement);
newElement.EntityState = EntityState.New;
}
}
/**
* EntityQuery class
*
* @export
* @class EntityQuery
* @template T
* @wType System.ServiceModel.DomainServices.Client.EntityQuery`1
*/
export class EntityQuery<T> {
parameters: { [x: string]: any };
Constructor: IClassConstructor<T>;
filter: string = null;
sort: string = null;
public whereExpr: string = null;
constructor(public methodName: String) {}
public getUrlFragment() {
if (this.parameters) {
let result = this.methodName + '?';
let i = 0;
if (this.parameters instanceof SimpleDictionary) {
for (let pair of this.parameters) {
let newLocal = pair[1];
if (newLocal instanceof Date) {
newLocal =
newLocal.getFullYear().toString() +
'-' +
(newLocal.getMonth() + 1).toString() +
'-' +
newLocal.getDate().toString();
}
result = result + pair[0] + '=' + newLocal.toString();
i++;
if (i < this.parameters.count) {
result = result + '&';
}
}
} else {
for (let parameter of Object.keys(this.parameters)) {
result =
result + parameter + '=' + this.parameters[parameter].toString();
i++;
if (i < Object.keys(this.parameters).length) {
result = result + ';';
}
}
}
if (typeof this.filter != 'undefined' && this.filter !== '') {
result = result + this.filter;
}
if (typeof this.sort != 'undefined' && this.sort !== '') {
result = result + this.sort;
}
if (typeof this.whereExpr != 'undefined') {
result = result + '&$where=' + this.whereExpr;
}
return result;
} else {
let result = this.methodName;
if (typeof this.filter != 'undefined' && this.filter != '') {
result = result + '?' + this.filter;
if (this.sort !== '') {
result = result + '&' + this.sort;
}
} else {
if (typeof this.sort != 'undefined' && this.sort != '') {
result = result + '?' + this.sort;
}
}
if (typeof this.whereExpr != 'undefined') {
result = result + '&$where=' + this.whereExpr;
}
return result;
}
}
setFilterCriterias(filter: string, sort: string) {
this.filter = filter;
this.sort = sort;
}
}
/**
* Entity class
*
* @export
* @class Entity
* @wType System.ServiceModel.DomainServices.Client.Entity
*/
export class Entity {
public EntityState: EntityState;
/**
* Collection of ValidationResults
*
* @memberof Entity
*/
public ValidationErrors: ICollection<ValidationResult>;
/**
* Updates the action state
*
* @param {string} arg0
* @param {string} arg1
* @param {string} arg2
* @return {*} {*}
* @memberof Entity
* @wNoMap
*/
UpdateActionState(arg0: string, arg1: string, arg2: string): any {
Debugger.Throw('Method not implemented.');
}
/**
* Invokes the action
*
* @param {string} arg0
* @return {*} {*}
* @memberof Entity
* @wNoMap
*/
InvokeAction(arg0: string): any {
Debugger.Throw('Method not implemented.');
}
/**
* CanInvokeAction method
*
* @param {string} arg0
* @return {*} {boolean}
* @memberof Entity
* @wNoMap
*/
CanInvokeAction(arg0: string): boolean {
return false;
}
/**
* IsActionInvoked method
*
* @param {string} arg0
* @return {*} {boolean}
* @memberof Entity
* @wNoMap
*/
IsActionInvoked(arg0: string): boolean {
return false;
}
protected entityNamespace = '';
constructor() {
this.EntityState = EntityState.Detached;
}
protected getDataMembers(): string[] {
let type = ReflectionHelper.getTypeInfo(this.constructor);
let properties = type.getProperties();
return properties.map((prop) => prop.Name);
}
protected getRequiredFields(): string[] {
let type = ReflectionHelper.getTypeInfo(this.constructor);
let properties = type.getProperties();
let result = [];
for (let prop of properties) {
for (let attribute of prop.getMetadataAttributes()) {
if (attribute instanceof RequiredAttribute) {
result.push(prop.Name);
}
}
}
return result;
}
protected getKeyFields(): string[] {
let type = ReflectionHelper.getTypeInfo(this.constructor);
let properties = type.getProperties();
let result = [];
for (let prop of properties) {
for (let attribute of prop.getMetadataAttributes()) {
if (attribute instanceof KeyAttribute) {
result.push(prop.Name);
}
}
}
return result;
}
copyEntityObjectDataInto(result: any): any {
let typeInfo = ReflectionHelper.getTypeInfo(this);
for (let field of this.getDataMembers()) {
if (field === 'rowguid' && this['rowguid'] === null) {
continue;
}
let propertyInfo = typeInfo.getProperty(field);
if (this[field] instanceof Date) {
result[field] = this.serializeDate(this[field]);
} else {
if (
!(
propertyInfo?.propertyType?.innerType === Date &&
this[field] === null
)
) {
result[field] = this[field];
}
}
}
return result;
}
/**
* RaiseDataMemberChanging method
*
* @protected
* @param {string} memberName
* @memberof Entity
* @wNoMap
*/
protected RaiseDataMemberChanging(memberName: string) {}
/**
* ValidateProperty method
*
* @protected
* @param {string} memberName
* @param {*} value
* @memberof Entity
* @wNoMap
*/
protected ValidateProperty(memberName: string, value: any) {}
protected RaiseDataMemberChanged(memberName: string) {
if (EntityContainerHelper.changeTrackingEnabled) {
var entityName = this.constructor.name;
var keyValues = this.getKeyFields().map((key) => this[key]);
EntityContainerHelper.registerChange(
entityName,
this.entityNamespace,
keyValues,
this.getKeyFields(),
memberName,
this
);
}
}
/**
* RaisePropertyChanged method
*
* @protected
* @param {string} memberName
* @memberof Entity
* @wNoMap
*/
protected RaisePropertyChanged(memberName: string) {}
protected deserializeDate(strDate: any) {
if (typeof strDate == 'string') {
return new Date(parseInt(strDate.replace(/^\/Date\((.*)-.*$/, '$1')));
} else {
return strDate;
}
}
protected serializeDate(date: Date) {
return `/Date(${date.valueOf()})/`;
}
/**
* OnPropertyChanged method
*
* @param {{ PropertyName: string }} e
* @memberof Entity
* @wNoMap
*/
public OnPropertyChanged(e: { PropertyName: string }): void {}
}
/**
* EntityCollection class
*
* @export
* @class EntityCollection
* @template T
* @wType System.ServiceModel.DomainServices.Client.EntityCollection`1
*/
export class EntityCollection<T> {
/**
* Add method
*
* @param {*} arg0
* @return {*} {*}
* @memberof EntityCollection
* @wNoMap
*/
Add(arg0: any): any {
Debugger.Throw('Method not implemented.');
}
/**
* Remove method
*
* @param {*} arg0
* @return {*} {*}
* @memberof EntityCollection
* @wNoMap
*/
Remove(arg0: any): any {
Debugger.Throw('Method not implemented.');
}
constructor(
entity: Entity,
propertyName: string,
filter: (e: T) => boolean,
attach: (e: Entity) => void,
detach: (e: Entity) => void
) {}
}
/**
* EntityRef class
*
* @export
* @class EntityRef
* @template T
* @wType System.ServiceModel.DomainServices.Client.EntityRef`1
*/
export class EntityRef<T> {
constructor(
private refEntity: any,
private targetEntityName: string,
private entityPredicate: (x: T) => boolean
) {}
/**
* Gets the entity
*
* @type {T}
* @memberof EntityRef
* @wNoMap
*/
public get Entity(): T {
return EntityContainerHelper.getEntitiesWithFilter(
this.targetEntityName,
this.entityPredicate
)[0];
}
/**
* Sets the entity
*
* @memberof EntityRef
*/
public set Entity(value: T) {}
}
/**
* EntityState enum
*
* @export
* @enum {number}
* @wEnum System.ServiceModel.DomainServices.Client.EntityState
*/
export enum EntityState {
New,
Modified,
Unmodified,
Detached,
}
/**
* EntityKey class
*
* @export
* @class EntityKey
* @wType System.ServiceModel.DomainServices.Client.EntityKey
*/
export class EntityKey {
/**
* Create method
*
* @static
* @param {string} v1
* @param {string} v2
* @param {*} [v3]
* @param {*} [v4]
* @return {*} {*}
* @memberof EntityKey
* @wNoMap
*/
static Create(v1: string, v2: string, v3?: any, v4?: any): any {
Debugger.Throw('Method not implemented.');
}
}