forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNUEntity.js
108 lines (97 loc) · 3.36 KB
/
NUEntity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import NUAttribute from './NUAttribute';
import NUAbstractModel from './NUAbstractModel';
import NUException from './NUException';
/*
This class models the base Entity
*/
export default class NUEntity extends NUAbstractModel {
static attributeDescriptors = {
ID: new NUAttribute({
localName: 'ID',
attributeType: NUAttribute.ATTR_TYPE_STRING,
canSearch: true,
isIdentifier: true }),
parentID: new NUAttribute({
localName: 'parentID',
attributeType: NUAttribute.ATTR_TYPE_STRING,
isEditable: false }),
parentType: new NUAttribute({
localName: 'parentType',
attributeType: NUAttribute.ATTR_TYPE_STRING,
isEditable: false }),
associatedEntities: new NUAttribute({
localName: 'associatedEntities',
attributeType: NUAttribute.ATTR_TYPE_LIST,
isEditable: true,
isInternal: true }),
associatedEntitiesResourceName: new NUAttribute({
localName: 'associatedEntitiesResourceName',
attributeType: NUAttribute.ATTR_TYPE_STRING,
isEditable: true,
isInternal: true }),
};
static getSearchableAttributes() {
if (!this.searchableAttributes) {
this.searchableAttributes = Object
.entries(this.attributeDescriptors)
.reduce((acc, attr) => {
const [key, value] = attr;
if (value.canSearch) return { ...acc, [key]: value };
return acc;
}, {});
}
return this.searchableAttributes;
}
static hasMandatoryAttributesSet(JSONObject) {
for (const [attr, descriptor] of Object.entries(this.attributeDescriptors)) {
if (descriptor.isRequired && !descriptor.isValueSet(JSONObject[attr])) {
return false;
}
}
return true;
}
constructor() {
super();
this.defineProperties({
ID: null,
parentID: undefined,
parentObject: undefined,
parentType: undefined,
associatedEntities: [],
associatedEntitiesResourceName: undefined,
});
}
/*
To be implemented by specific entity classes
*/
get RESTName() {
throw new NUException('Not implemented');
}
get validationErrors() {
return this._validationErrors;
}
toObject(props) {
const attributeDescriptors = this.constructor.attributeDescriptors;
const assocEntities = this[attributeDescriptors.associatedEntities.name];
if (assocEntities && assocEntities.length > 0) {
return assocEntities.map(item => item.ID);
} else {
return super.toObject(props);
}
}
getDefaults() {
return Object.entries(this).reduce((acc, [key, value]) => {
return (value !== undefined && value !== null && key !== '_validationErrors' && key !== '_validators' && key !== '_associatedEntities')
? { ...acc, [key.substring(1)]: value } : acc;
}, {});
}
isFromTemplate() {
return this.hasOwnProperty('templateID') && this.templateID;
}
isScopeGlobal() {
return this.entityScope === 'GLOBAL';
}
isOwnedByUser(user) {
return user && this.owner === user.ID;
}
}