Initial commit
This commit is contained in:
153
src/models/entity.ts
Normal file
153
src/models/entity.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Class model for Entity Interface
|
||||
*/
|
||||
|
||||
import type { EntityInterface } from "@/types/entity";
|
||||
import type { IndividualInterface } from "@/types/individual";
|
||||
import type { OrganizationInterface } from "@/types/organization";
|
||||
import type { GroupInterface } from "@/types/group";
|
||||
import { IndividualObject } from "./individual";
|
||||
import { OrganizationObject } from "./organization";
|
||||
import { GroupObject } from "./group";
|
||||
|
||||
export class EntityObject implements EntityInterface {
|
||||
|
||||
_data!: EntityInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'people:entity',
|
||||
version: 1,
|
||||
in: null,
|
||||
id: null,
|
||||
createdOn: null,
|
||||
createdBy: null,
|
||||
modifiedOn: null,
|
||||
modifiedBy: null,
|
||||
signature: null,
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface) : EntityObject {
|
||||
this._data = data;
|
||||
if (data.data) {
|
||||
const type = data.data.type;
|
||||
if (type === 'organization') {
|
||||
this._data.data = new OrganizationObject().fromJson(data.data as OrganizationInterface);
|
||||
} else if (type === 'group') {
|
||||
this._data.data = new GroupObject().fromJson(data.data as GroupInterface);
|
||||
} else {
|
||||
this._data.data = new IndividualObject().fromJson(data.data as IndividualInterface);
|
||||
}
|
||||
} else {
|
||||
this._data.data = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.data instanceof IndividualObject ||
|
||||
this._data.data instanceof OrganizationObject ||
|
||||
this._data.data instanceof GroupObject) {
|
||||
json.data = this._data.data.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject()
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data))
|
||||
return cloned
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get in(): number | string | null {
|
||||
return this._data.in;
|
||||
}
|
||||
|
||||
get id(): number | string | null {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
return this._data.version;
|
||||
}
|
||||
|
||||
get createdOn(): Date | null {
|
||||
return this._data.createdOn;
|
||||
}
|
||||
|
||||
get createdBy(): string | null {
|
||||
return this._data.createdBy;
|
||||
}
|
||||
|
||||
get modifiedOn(): Date | null {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
get modifiedBy(): string | null {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
set createdOn(value: Date | null) {
|
||||
this._data.createdOn = value;
|
||||
}
|
||||
|
||||
set createdBy(value: string | null) {
|
||||
this._data.createdBy = value;
|
||||
}
|
||||
|
||||
set modifiedOn(value: Date | null) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string | null) {
|
||||
this._data.modifiedBy = value;
|
||||
}
|
||||
|
||||
set signature(value: string | null) {
|
||||
this._data.signature = value;
|
||||
}
|
||||
|
||||
get data(): IndividualObject | OrganizationObject | GroupObject | null {
|
||||
if (this._data.data instanceof IndividualObject ||
|
||||
this._data.data instanceof OrganizationObject ||
|
||||
this._data.data instanceof GroupObject) {
|
||||
return this._data.data;
|
||||
}
|
||||
|
||||
if (this._data.data) {
|
||||
const type = this._data.data.type;
|
||||
let hydrated;
|
||||
if (type === 'organization') {
|
||||
hydrated = new OrganizationObject().fromJson(this._data.data as OrganizationInterface);
|
||||
} else if (type === 'group') {
|
||||
hydrated = new GroupObject().fromJson(this._data.data as GroupInterface);
|
||||
} else {
|
||||
hydrated = new IndividualObject().fromJson(this._data.data as IndividualInterface);
|
||||
}
|
||||
this._data.data = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set data(value: IndividualObject | OrganizationObject | GroupObject | null) {
|
||||
this._data.data = value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user