forked from Breeze/breeze.js.labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breeze.labs.dataservice.abstractrest.js
417 lines (361 loc) · 15.4 KB
/
breeze.labs.dataservice.abstractrest.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Breeze Labs Abstract REST DataServiceAdapter
*
* v.0.2.3
*
* Extends Breeze with a REST DataService Adapter abstract type
*
* N.B.: This adapter CANNOT be used directly!
*
* It's a base type for concrete REST adapters such as the SharePoint OData DataService Adapter
*
* A concrete REST adapter
*
* - MUST replace the _createSaveRequest with a concrete implementation to enable save
*
* - SHOULD replace the "noop" JsonResultsAdapter.
*
* - WILL LIKELY replace the executeQuery method.
*
* - COULD replace the fetchMetadata method and MUST do so if getting metadata from the server.
*
* - MAY replace any of the protected members prefixed by '_'.
*
* FOR EXAMPLE IMPLEMENTATION, SEE breeze.labs.dataservice.sharepoint.js
*
* By default this adapter permits multiple entities to be saved at a time,
* each in a separate request that this adapter fires off in parallel.
* and waits for all to complete.
*
* If 'saveOnlyOne' == true, the adapter throws an exception
* when asked to save more than one entity at a time.
*
* Copyright 2014 IdeaBlade, Inc. All Rights Reserved.
* Licensed under the MIT License
* http://opensource.org/licenses/mit-license.php
* Authors: Ward Bell
*/
(function (definition, window) {
if (window.breeze) {
definition(window.breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze');
definition(b);
} else if (typeof define === "function" && define["amd"] && !window.breeze) {
// Requirejs / AMD
define(['breeze'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
"use strict";
var ctor = function () { };
breeze.AbstractRestDataServiceAdapter = ctor;
ctor.prototype = {
// Breeze DataService API
executeQuery: executeQuery,
fetchMetadata: fetchMetadata,
initialize: initialize,
saveChanges: saveChanges,
// Configuration API
checkForRecomposition: checkForRecomposition,
saveOnlyOne: false, // true if may only save one entity at a time.
// "protected" members available to derived concrete dataservice adapter types
_addToSaveContext: _addToSaveContext,
_ajaxImpl: undefined, // see initialize()
_createErrorFromResponse: _createErrorFromResponse,
_createJsonResultsAdapter: _createJsonResultsAdapter,
_createSaveRequest: _createSaveRequest,
_clientTypeNameToServer: _clientTypeNameToServer,
_getEntityTypeFromMappingContext: _getEntityTypeFromMappingContext,
_getNodeEntityType: _getNodeEntityType,
_getResponseData: _getResponseData,
_processSavedEntity: _processSavedEntity,
_serializeToJson: _serializeToJson, // serialize raw entity data to JSON for save
_serverTypeNameToClient: _serverTypeNameToClient,
_transformSaveValue: _transformSaveValue
};
/*** Breeze DataService API ***/
function initialize() {
var adapter = this;
var ajaxImpl = adapter._ajaxImpl = breeze.config.getAdapterInstance("ajax");
if (!ajaxImpl) {
throw new Error("Unable to initialize ajax for " + adapter.name);
}
var ajax = ajaxImpl.ajax;
if (!ajax) {
throw new Error("Breeze was unable to find an 'ajax' adapter for " + adapter.name);
}
// Todo: hacking for Q right now; use promise adapter after Breeze makes it available
// if no breeze.Q, assume Q is in global window namespace (e.g., Q.js)
adapter.Q = breeze.Q ? breeze.Q : window.Q;
if (!adapter.jsonResultsAdapter) {
adapter.jsonResultsAdapter = adapter._createJsonResultsAdapter();
}
};
function checkForRecomposition(interfaceInitializedArgs) {
if (interfaceInitializedArgs.interfaceName === "ajax" && interfaceInitializedArgs.isDefault) {
this.initialize();
}
};
function executeQuery(mappingContext) {
var adapter = this;
var deferred = adapter.Q.defer();
var url = mappingContext.getUrl();
var headers = {
'Accept': 'application/json',
};
adapter._ajaxImpl.ajax({
type: "GET",
url: url,
headers: headers,
params: mappingContext.query.parameters,
success: querySuccess,
error: function (response) {
deferred.reject(adapter._createErrorFromResponse(response, url));
}
});
return deferred.promise;
function querySuccess(response) {
try {
var rData = {
results: adapter._getResponseData(response).results,
httpResponse: response
};
deferred.resolve(rData);
} catch (e) {
// program error means adapter it broken, not SP or the user
deferred.reject(new Error("Program error: failed while parsing successful query response"));
}
}
}
function fetchMetadata() {
throw new Error("Cannot process server metadata; create your own and use that instead");
};
function saveChanges(saveContext, saveBundle) {
var adapter = this;
var Q = adapter.Q;
try {
if (adapter.saveOnlyOne && saveBundle.entities.length > 1) {
throw new Error("Only one entity may be saved at a time.");
}
saveContext.adapter = adapter;
adapter._addToSaveContext(saveContext);
var requests = createSaveRequests(saveContext, saveBundle);
var promises = sendSaveRequests(saveContext, requests);
var comboPromise = Q.all(promises);
return comboPromise
.then(reviewSaveResult)
.then(null, saveFailed);
} catch (err) {
return Q.reject(err);
}
function reviewSaveResult(/* promiseValues */) {
var saveResult = saveContext.saveResult;
var entitiesWithErrors = saveResult.entitiesWithErrors;
var errorCount = entitiesWithErrors.length;
if (!errorCount) { return saveResult; } // all good
// at least one request failed; process those that succeeded
saveContext.processSavedEntities(saveResult);
var error;
// Compose error; promote the first error when one or all fail
if (requests.length === 1 || requests.length === errorCount) {
// When all fail, good chance the first error is the same reason for all
error = entitiesWithErrors[0].error;
} else {
error = new Error("\n The save failed although some entities were saved.");
}
error.message = (error.message || "Save failed") +
" \n See 'error.saveResult' for more details.\n";
error.saveResult = saveResult;
return Q.reject(error);
}
function saveFailed(error) {
return Q.reject(error);
}
};
/*** Members a derived Type might use or replace ***/
function _addToSaveContext(/* saveContext */) { }
function _clientTypeNameToServer(typeName) {
var jrAdapter = this.jsonResultsAdapter;
return jrAdapter.clientTypeNameToServer ?
jrAdapter.clientTypeNameToServer(typeName) : typeName;
}
function _createErrorFromResponse(response, url) {
var result = new Error();
result.response = response;
if (url) { result.url = url; }
result.message = response.message || response.error || response.statusText;
result.statusText = response.statusText;
result.status = response.status;
}
function _createJsonResultsAdapter(/*dataServiceAdapter*/) {
var jsonResultsAdapter = new breeze.JsonResultsAdapter({
name: "noop",
visitNode: function (/*node, mappingContext, nodeContext*/) {
return {};
}
});
return jsonResultsAdapter;
}
function _createSaveRequest(/* saveContext, entity, index */) {
throw new Error("Need a concrete implementation of _createSaveRequest");
}
function _getEntityTypeFromMappingContext(mappingContext) {
var query = mappingContext.query; // 'this' must be the mappingContext
var entityType = query.entityType || query.resultEntityType;
if (!entityType) { // try to figure it out from the query.resourceName
var metadataStore = mappingContext.metadataStore;
var etName = metadataStore.getEntityTypeNameForResourceName(query.resourceName);
if (etName) {
entityType = metadataStore.getEntityType(etName);
}
}
return entityType;
}
function _getNodeEntityType(mappingContext, typeName) {
// Get the EntityType corresponding to the typeName
// A utility for implementation of jsonResultsAdapter.visitNode
// typeName: a string on the node that identifies the type of the raw data
//
// This method memoizes the type names it encounters
// by adding a 'typeMap' object to the JsonResultsAdapter.
if (!typeName) { return undefined; }
var jsonResultsAdapter = mappingContext.jsonResultsAdapter;
var typeMap = jsonResultsAdapter.typeMap;
if (!typeMap) { // if missing, make one with a fallback mapping
typeMap = { "": { _mappedPropertiesCount: NaN } };
jsonResultsAdapter.typeMap = typeMap;
}
var entityType = typeMap[typeName]; // EntityType for a node with this metadata.type
if (!entityType) {
// Haven't see this typeName before; add it to the typeMap
// Figure out what EntityType this is and remember it
entityType = mappingContext.metadataStore.getEntityType(typeName, true);
typeMap[typeName] = entityType || typeMap[""];
}
return entityType;
}
function _getResponseData(response) {
return response.data;
}
function _processSavedEntity(/*savedEntity, saveContext, response, index*/) { }
function _serializeToJson(rawEntityData) {
// Serialize raw entity data to JSON during save
// You could override this default version
// Note that DataJS has an amazingly complex set of tricks for this,
// all of them depending on metadata attached to the property values
// which breeze entity data never have.
return JSON.stringify(rawEntityData);
}
function _serverTypeNameToClient(mappingContext, typeName) {
var jrAdapter = mappingContext.jsonResultsAdapter;
return jrAdapter.serverTypeNameToClient ?
jrAdapter.serverTypeNameToClient(typeName) : typeName;
}
function _transformSaveValue(prop, val) {
// prepare a property value for save by transforming it
if (prop.isUnmapped) { return undefined; }
if (prop.dataType === breeze.DataType.DateTimeOffset) {
// The datajs lib tries to treat client dateTimes that are defined as DateTimeOffset on the server differently
// from other dateTimes. This fix compensates before the save.
// TODO: If not using datajs (and this adapter doesn't) is this necessary?
val = val && new Date(val.getTime() - (val.getTimezoneOffset() * 60000));
} else if (prop.dataType.quoteJsonOData) {
val = val != null ? val.toString() : val;
}
return val;
}
/*** private members ***/
function createSaveRequests(saveContext, saveBundle) {
var adapter = saveContext.adapter;
var originalEntities = saveContext.originalEntities = saveBundle.entities;
saveContext.tempKeys = [];
var requests = originalEntities.map(function (entity, index) {
return adapter._createSaveRequest(saveContext, entity, index);
});
return requests;
}
function getRealKey(entityType, rawEntity) {
return entityType.getEntityKeyFromRawEntity(rawEntity,
breeze.DataProperty.getRawValueFromServer);
}
function sendSaveRequests(saveContext, requests) {
// Sends each prepared save request and processes the promised results
// returns a single "comboPromise" that waits for the individual promises to complete
// Todo: What happens when there are a gazillion async requests?
var saveResult = {
entities: [],
entitiesWithErrors: [],
keyMappings: []
};
saveContext.saveResult = saveResult;
return requests.map(function (request, index) {
return sendSaveRequest(saveContext, request, index);
});
}
function sendSaveRequest(saveContext, request, index) {
var adapter = saveContext.adapter;
var deferred = adapter.Q.defer();
var url = request.requestUri;
adapter._ajaxImpl.ajax({
url: url,
type: request.method,
headers: request.headers,
data: request.data,
success: tryRequestSucceeded,
error: tryRequestFailed
});
return deferred.promise;
function tryRequestSucceeded(response) {
try {
var statusCode = response.status;
if ((!statusCode) || statusCode >= 400) {
tryRequestFailed(response);
} else {
var savedEntity = saveRequestSucceeded(saveContext, response, index);
adapter._processSavedEntity(savedEntity, saveContext, response, index);
deferred.resolve(true);
}
} catch (e) {
// program error means adapter is broken, not remote server or the user
deferred.reject("Program error: failed while processing successful save response");
}
}
function tryRequestFailed(response) {
try {
// Do NOT fail saveChanges at the request level
saveContext.saveResult.entitiesWithErrors.push({
entity: saveContext.originalEntities[index],
error: adapter._createErrorFromResponse(response, url)
});
deferred.resolve(false);
} catch (e) {
// program error means adapter is broken, not remote server or the user
deferred.reject("Program error: failed while processing save error");
}
}
}
function saveRequestSucceeded(saveContext, response, index) {
var saved = saveContext.adapter._getResponseData(response);
if (saved) {
var tempKey = saveContext.tempKeys[index];
if (tempKey) {
var entityType = tempKey.entityType;
var tempValue = tempKey.values[0];
var realKey = getRealKey(entityType, saved);
var keyMapping = {
entityTypeName: entityType.name,
tempValue: tempValue,
realValue: realKey.values[0]
};
saveContext.saveResult.keyMappings.push(keyMapping);
}
} else {
saved = saveContext.originalEntities[index];
}
saveContext.saveResult.entities.push(saved);
return saved;
}
}, this));