1 | "use strict";
|
2 |
|
3 | var __assign = (this && this.__assign) || function () {
|
4 | __assign = Object.assign || function(t) {
|
5 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
6 | s = arguments[i];
|
7 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
8 | t[p] = s[p];
|
9 | }
|
10 | return t;
|
11 | };
|
12 | return __assign.apply(this, arguments);
|
13 | };
|
14 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
15 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
16 | if (ar || !(i in from)) {
|
17 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
18 | ar[i] = from[i];
|
19 | }
|
20 | }
|
21 | return to.concat(ar || Array.prototype.slice.call(from));
|
22 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.EntityBase = exports.isExpandedProperty = exports.isExistentProperty = exports.isSelectedProperty = exports.Entity = void 0;
|
25 | var util_1 = require("@sap-cloud-sdk/util");
|
26 | var entity_builder_1 = require("./entity-builder");
|
27 | var properties_util_1 = require("./properties-util");
|
28 |
|
29 |
|
30 |
|
31 | var Entity = (function () {
|
32 | function Entity() {
|
33 | (0, properties_util_1.nonEnumerable)(this, '_oDataVersion');
|
34 | (0, properties_util_1.nonEnumerable)(this, '_customFields');
|
35 | this._customFields = {};
|
36 | }
|
37 | Entity.entityBuilder = function (entityConstructor) {
|
38 | var builder = new entity_builder_1.EntityBuilder(entityConstructor);
|
39 | entityConstructor._allFields.forEach(function (field) {
|
40 | var fieldName = "".concat((0, util_1.camelCase)(field._fieldName));
|
41 | builder[fieldName] = function (value) {
|
42 | this.entity[fieldName] = value;
|
43 | return this;
|
44 | };
|
45 | });
|
46 | return builder;
|
47 | };
|
48 | Object.defineProperty(Entity.prototype, "versionIdentifier", {
|
49 | |
50 |
|
51 |
|
52 |
|
53 | get: function () {
|
54 | return this._versionIdentifier;
|
55 | },
|
56 | enumerable: false,
|
57 | configurable: true
|
58 | });
|
59 | |
60 |
|
61 |
|
62 |
|
63 | Entity.prototype.getCustomFields = function () {
|
64 | return this._customFields;
|
65 | };
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 | Entity.prototype.getCustomField = function (fieldName) {
|
72 | return this._customFields[fieldName];
|
73 | };
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Entity.prototype.setCustomField = function (fieldName, value) {
|
82 | if (this.isConflictingCustomField(fieldName)) {
|
83 | throw new Error("The field name \"".concat(fieldName, "\" is already defined in the entity and cannot be set as custom field."));
|
84 | }
|
85 | this._customFields[fieldName] = value;
|
86 | return this;
|
87 | };
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | Entity.prototype.hasCustomField = function (fieldName) {
|
94 | return this._customFields[fieldName] !== undefined;
|
95 | };
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | Entity.prototype.setCustomFields = function (customFields) {
|
102 | var _this = this;
|
103 | Object.entries(customFields).forEach(function (_a) {
|
104 | var key = _a[0], value = _a[1];
|
105 | _this.setCustomField(key, value);
|
106 | });
|
107 | return this;
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | Entity.prototype.initializeCustomFields = function (customFields) {
|
116 | return this.setCustomFields(customFields);
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | Entity.prototype.setVersionIdentifier = function (etag) {
|
124 | if (etag && typeof etag === 'string') {
|
125 | (0, properties_util_1.nonEnumerable)(this, '_versionIdentifier');
|
126 | this._versionIdentifier = etag;
|
127 | }
|
128 | return this;
|
129 | };
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | Entity.prototype.setOrInitializeRemoteState = function (state) {
|
139 | var _this = this;
|
140 | if (!this.remoteState) {
|
141 | (0, properties_util_1.nonEnumerable)(this, 'remoteState');
|
142 | }
|
143 | state = state || this.asObject();
|
144 | this.remoteState = Object.entries(state).reduce(function (stateObject, _a) {
|
145 | var _b;
|
146 | var fieldName = _a[0], value = _a[1];
|
147 | var propertyName = _this[(0, util_1.camelCase)(fieldName)]
|
148 | ? (0, util_1.camelCase)(fieldName)
|
149 | : fieldName;
|
150 | return __assign(__assign({}, stateObject), (_b = {}, _b[propertyName] = value, _b));
|
151 | }, {});
|
152 | return this;
|
153 | };
|
154 | |
155 |
|
156 |
|
157 |
|
158 | Entity.prototype.getUpdatedCustomFields = function () {
|
159 | var _this = this;
|
160 | if (!this.remoteState) {
|
161 | return this._customFields;
|
162 | }
|
163 | return Object.entries(this.getCustomFields())
|
164 | .filter(function (_a) {
|
165 | var fieldName = _a[0], value = _a[1];
|
166 | return _this.remoteState[fieldName] !== value;
|
167 | })
|
168 | .reduce(function (updatedCustomFields, _a) {
|
169 | var _b;
|
170 | var fieldName = _a[0], value = _a[1];
|
171 | return (__assign(__assign({}, updatedCustomFields), (_b = {}, _b[fieldName] = value, _b)));
|
172 | }, {});
|
173 | };
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | Entity.prototype.getUpdatedProperties = function () {
|
181 | var current = this.asObject();
|
182 | return this.getUpdatedPropertyNames().reduce(function (patch, key) {
|
183 | var _a;
|
184 | return (__assign(__assign({}, patch), (_a = {}, _a[key] = current[key], _a)));
|
185 | }, {});
|
186 | };
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | Entity.prototype.getUpdatedPropertyNames = function () {
|
194 | var _this = this;
|
195 | var currentState = this.asObject();
|
196 | var names = Object.keys(currentState).filter(function (key) { return _this.propertyIsEnumerable(key) && !_this.hasCustomField(key); });
|
197 | return !this.remoteState
|
198 | ? names
|
199 | : names.filter(function (key) { return !(0, util_1.equal)(_this.remoteState[key], currentState[key]); });
|
200 | };
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | Entity.prototype.getCurrentMapKeys = function (visitedEntities) {
|
208 | if (visitedEntities === void 0) { visitedEntities = []; }
|
209 | return this.asObject(visitedEntities);
|
210 | };
|
211 | Entity.prototype.isVisitedEntity = function (entity, visitedEntities) {
|
212 | if (visitedEntities === void 0) { visitedEntities = []; }
|
213 | return Array.isArray(entity)
|
214 | ? entity.some(function (multiLinkChild) { return visitedEntities.includes(multiLinkChild); })
|
215 | : visitedEntities.includes(entity);
|
216 | };
|
217 | Entity.prototype.getCurrentStateForKey = function (key, visitedEntities) {
|
218 | if (visitedEntities === void 0) { visitedEntities = []; }
|
219 | if ((0, properties_util_1.isNavigationProperty)(key, this.constructor)) {
|
220 | if ((0, util_1.isNullish)(this[key])) {
|
221 | return this[key];
|
222 | }
|
223 | return Array.isArray(this[key])
|
224 | ? this[key].map(function (linkedEntity) {
|
225 | return linkedEntity.getCurrentMapKeys(visitedEntities);
|
226 | })
|
227 | : this[key].getCurrentMapKeys(visitedEntities);
|
228 | }
|
229 | return Array.isArray(this[key]) ? __spreadArray([], this[key], true) : this[key];
|
230 | };
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 | Entity.prototype.isConflictingCustomField = function (customFieldName) {
|
237 | return this.constructor._allFields
|
238 | .map(function (f) { return f._fieldName; })
|
239 | .includes(customFieldName);
|
240 | };
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 | Entity.prototype.asObject = function (visitedEntities) {
|
247 | var _this = this;
|
248 | if (visitedEntities === void 0) { visitedEntities = []; }
|
249 | visitedEntities.push(this);
|
250 | return Object.keys(this)
|
251 | .filter(function (key) {
|
252 | return _this.propertyIsEnumerable(key) &&
|
253 | (!(0, properties_util_1.isNavigationProperty)(key, _this.constructor) ||
|
254 | !_this.isVisitedEntity(_this[key], visitedEntities));
|
255 | })
|
256 | .reduce(function (accumulatedMap, key) {
|
257 | var _a;
|
258 | return (__assign(__assign({}, accumulatedMap), (_a = {}, _a[key] = _this.getCurrentStateForKey(key, visitedEntities), _a)));
|
259 | }, this.getCustomFields());
|
260 | };
|
261 | return Entity;
|
262 | }());
|
263 | exports.Entity = Entity;
|
264 | exports.EntityBase = Entity;
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | function isSelectedProperty(json, field) {
|
270 | return json.hasOwnProperty(field._fieldName);
|
271 | }
|
272 | exports.isSelectedProperty = isSelectedProperty;
|
273 |
|
274 |
|
275 |
|
276 | function isExistentProperty(json, link) {
|
277 | return isSelectedProperty(json, link) && json[link._fieldName] !== null;
|
278 | }
|
279 | exports.isExistentProperty = isExistentProperty;
|
280 |
|
281 |
|
282 |
|
283 | function isExpandedProperty(json, link) {
|
284 | return (isExistentProperty(json, link) &&
|
285 | !json[link._fieldName].hasOwnProperty('__deferred'));
|
286 | }
|
287 | exports.isExpandedProperty = isExpandedProperty;
|
288 |
|
\ | No newline at end of file |