-
Notifications
You must be signed in to change notification settings - Fork 44
/
breeze.labs.dataservice.sharepoint.js
404 lines (354 loc) · 14.5 KB
/
breeze.labs.dataservice.sharepoint.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
/*
* Breeze Labs SharePoint 2013 & 2010 OData DataServiceAdapter
*
* v.0.10.1
*
* SharePoint 2010 DataServiceAdapter name = SharePointOData2010
* SharePoint 2013+ DataServiceAdapter name = SharePointOData
*
* Registers a SharePoint 2013 & 2010 OData DataServiceAdapter with Breeze
*
* REQUIRES breeze.labs.dataservice.abstractrest.js v.0.6.3+
*
* This adapter cannot get metadata from the server and in general one should
* not do so because such metadata cover much more of than you want and are huge (>1MB)
* Better to define the metadata "by hand" on the client.
*
* W/o need to get metadata, can use AJAX adapter instead of Data.JS.
*
* Typical usage in Angular
* // configure breeze to use SharePoint OData service
* var dsAdapter = breeze.config.initializeAdapterInstance('dataService', 'SharePointOData', true);
*
* // provide method returning value for the SP OData 'X-RequestDigest' header
* dsAdapter.getRequestDigest = function(){return securityService.requestDigest}
* // note that the digest is NOT required if you are authenticating using OAuth tokens
* // as the digest is only needed to protect against XSFR that involves cookies...
* // when using OAuth tokens for authentication, cookies aren't used and thus XSFR is moot
*
* This adapter has its own JsonResultsAdapter which you could replace.
*
* The dataservice adapter looks for clientTypeNameToServer and serverTypeNameToClient methods
* on the JsonResultsAdapter during transformation of entity data to/from the server
* so it can convert server type names to client EntityType names.
* These are initialized to the default versions defined here.
* You can create and attach alternative type name conversion methods to the JsonResultsAdapter
*
* This adapter memorizes the type names it encounters
* by adding a 'typeMap' object to the JsonResultsAdapter.
*
* 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 2015 IdeaBlade, Inc. All Rights Reserved.
* Licensed under the MIT License
* http://opensource.org/licenses/mit-license.php
* Authors: Ward Bell, Andrew Connell
*/
(function (definition) {
if (typeof breeze === "object") {
definition(breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze-client');
definition(b);
} else if (typeof define === "function" && define["amd"]) {
// Requirejs / AMD
define(['breeze-client'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
"use strict";
// register SharePoint 2013+ data service adapter
var ctor = function () {
this.name = "SharePointOData";
this.dataServiceVersion = '3.0';
};
// Delay setting the prototype until we're sure AbstractRestDataServiceAdapter is loaded
var proto = breeze.AbstractRestDataServiceAdapter.prototype;
proto = breeze.core.extend(ctor.prototype, proto);
proto.executeQuery = executeQuery;
proto._addToSaveContext = _addToSaveContext;
proto._clientTypeNameToServerDefault = clientTypeNameToServerDefault;
proto._createChangeRequest = _createChangeRequest;
proto._createErrorFromResponse = _createErrorFromResponse;
proto._createJsonResultsAdapter = _createJsonResultsAdapter;
proto._getResponseData = _getResponseData;
proto._processSavedEntity = _processSavedEntity;
proto._serverTypeNameToClientDefault = serverTypeNameToClientDefault;
breeze.config.registerAdapter("dataService", ctor);
// </> register SharePoint 2013+ data service adapter
// register SharePoint 2010 data service adapter
var ctor2010 = function Ctor2010() {
this.name = "SharePointOData2010";
this.dataServiceVersion = '2.0';
};
var proto2010 = breeze.core.extend(ctor2010.prototype, proto);
proto2010._clientTypeNameToServerDefault = clientTypeNameToServer2010Default;
proto2010._serverTypeNameToClientDefault = serverTypeNameToClient2010Default
breeze.config.registerAdapter("dataService", ctor2010);
// </> register SharePoint 2010 data service adapter
///////// implementation functions //////////////////////////////////
function _addToSaveContext(saveContext) {
saveContext.requestDigest = this.getRequestDigest ? this.getRequestDigest() : null;
}
function applyDefaultSelect(mappingContext) {
var query = mappingContext.query;
var entityType = mappingContext.entityType;
// get the default select if query lacks a select and
// the result type is known and it has a defaultSelect
var defaultSelect = !query.selectClause && entityType &&
entityType.custom && entityType.custom.defaultSelect;
if (defaultSelect) {
// revise query with a new query that has the default select
mappingContext.query = query.select(defaultSelect);
}
}
function clientTypeNameToServerDefault(clientTypeName) {
return 'SP.Data.' + clientTypeName + 'ListItem';
}
function clientTypeNameToServer2010Default(clientTypeName) {
return 'Microsoft.SharePoint.DataService.' + clientTypeName + 'Item';
}
function _createChangeRequest(saveContext, entity, index) {
var adapter = saveContext.adapter;
var data, rawEntity, request;
var entityManager = saveContext.entityManager;
var helper = entityManager.helper;
var tempKeys = saveContext.tempKeys;
var aspect = entity.entityAspect;
var state = aspect.entityState;
var type = entity.entityType;
var headers = {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose',
'DataServiceVersion': adapter.dataServiceVersion
};
// conditionally include the digest if provided
if (saveContext.requestDigest) {
headers['X-RequestDigest'] = saveContext.requestDigest;
}
if (state.isAdded()) {
var rn = type.defaultResourceName;
if (!rn) {
throw new Error("Missing defaultResourceName for type " + type.name);
}
if (type.autoGeneratedKeyType !== breeze.AutoGeneratedKeyType.None) {
tempKeys[index] = aspect.getKey(); // DO NOT PUSH. Gaps expected!
}
rawEntity = helper.unwrapInstance(entity, adapter._transformSaveValue);
rawEntity.__metadata = {
'type': adapter._clientTypeNameToServer(type.shortName)
};
data = adapter._serializeToJson(rawEntity);
request = {
requestUri: entityManager.dataService.serviceName + rn,
method: "POST",
data: data
};
} else if (state.isDeleted()) {
request = {
method: "DELETE",
data: null
};
adjustUpdateDeleteRequest();
} else if (state.isModified()) {
request = {
method: "POST"
};
adjustUpdateDeleteRequest();
headers['X-HTTP-Method'] = 'MERGE';
rawEntity = helper.unwrapChangedValues(
entity, entityManager.metadataStore, adapter._transformSaveValue);
rawEntity.__metadata = { 'type': aspect.extraMetadata.type };
request.data = adapter._serializeToJson(rawEntity);
} else {
throw new Error("Cannot save an entity whose EntityState is " + state.name);
}
request.headers = headers;
return request;
function adjustUpdateDeleteRequest() {
var extraMetadata = aspect.extraMetadata;
if (!extraMetadata) {
throw new Error("Missing the extra metadata for an update/delete entity");
}
var uri = extraMetadata.uri || extraMetadata.id;
request.requestUri = uri;
if (extraMetadata.etag) {
headers["If-Match"] = extraMetadata.etag;
}
return request;
}
}
// Create error object for both query and save responses.
// A method on the adapter (`this`)
// 'context' can help differentiate query and save
// 'errorEntity' only defined for save response
function _createErrorFromResponse(response, url, context, errorEntity) {
// OData errors can have the message buried very deeply - and nonobviously
// this code is tricky so be careful changing the response.body parsing.
var err = new Error();
err.response = response;
if (url) { err.url = url; }
err.message = response.message || response.error || response.statusText;
err.statusText = response.statusText;
err.status = response.status;
setSPODataErrorMessage(err);
this._catchNoConnectionError(err);
return err;
}
// TODO: relocate where re-usable?
function setSPODataErrorMessage(err) {
// OData errors can have the message buried very deeply - and nonobviously
// Normal MS OData responses have a response.body
// SharePoint OData responses have a response.data instead
// this code is tricky so be careful changing the response.data parsing.
var data = err.data = err.response.data,
m,
msg = [],
nextErr;
if (data) {
try {
if (typeof data === "string") {
data = err.data = JSON.parse(data);
}
do {
nextErr = data.error || data.innererror;
if (!nextErr) {
m = data.message || "";
msg.push((typeof m === "string") ? m : m.value);
}
nextErr = nextErr || data.internalexception;
data = nextErr;
} while (nextErr);
if (msg.length > 0) {
err.message = msg.join('; ') + '.';
}
} catch (e) { /* carry on */ }
}
}
function _createJsonResultsAdapter() {
var dataServiceAdapter = this;
var jsonResultsAdapter = new breeze.JsonResultsAdapter({
name: dataServiceAdapter.name + "_default",
visitNode: visitNode
});
jsonResultsAdapter.clientTypeNameToServer = dataServiceAdapter._clientTypeNameToServerDefault;
jsonResultsAdapter.serverTypeNameToClient = dataServiceAdapter._serverTypeNameToClientDefault;
return jsonResultsAdapter;
function visitNode(node, mappingContext, nodeContext) {
var result = { ignore: true };
if (!node) { return result; }
var propertyName = nodeContext.propertyName;
var ignore = node.__deferred != null || propertyName === "__metadata" ||
// EntityKey properties can be produced by EDMX models
(propertyName === "EntityKey" && node.$type && core.stringStartsWith(node.$type, "System.Data"));
if (!ignore) {
result = {};
updateEntityNode(node, mappingContext, result);
// OData v3 - projection arrays will be enclosed in a results array
if (node.results) {
result.node = node.results;
}
}
return result;
}
// Determine if this is an Entity node and update the node appropriately if so
function updateEntityNode(node, mappingContext, result) {
var metadata = node.__metadata;
if (!metadata) { return; } // every SharePoint entity node has __metadata
var entityType = node.$entityType;
if (entityType) {
// save result node
result.entityType = entityType;
result.extraMetadata = metadata;
return;
}
// query node
var typeName = dataServiceAdapter._serverTypeNameToClient(mappingContext, metadata.type);
entityType = dataServiceAdapter._getNodeEntityType(mappingContext, typeName);
if (entityType) {
// ASSUME if #-of-properties on node is >= #-of-props for the type
// that this is the full entity and not a partial projection.
// Therefore we declare that we've received an entity
if (entityType._mappedPropertiesCount <= Object.keys(node).length - 1) {
result.entityType = entityType;
result.extraMetadata = metadata;
// Delete node properties that look like nested navigation paths
// Breeze gets confused into thinking such properties contain actual entities.
// Todo: rethink this if/when can include related entities through expand
var navPropNames = entityType.navigationProperties.map(function (p) { return p.name; });
navPropNames.forEach(function (n) { if (node[n]) { delete node[n]; } });
}
}
}
}
function executeQuery(mappingContext) {
var adapter = mappingContext.adapter = this;
mappingContext.entityType = adapter._getEntityTypeFromMappingContext(mappingContext);
applyDefaultSelect(mappingContext);
var deferred = adapter.Q.defer();
var url = mappingContext.getUrl();
var headers = {
'Accept': 'application/json;odata=verbose',
'DataServiceVersion': adapter.dataServiceVersion
};
adapter._ajaxImpl.ajax({
type: "GET",
url: url,
headers: headers,
params: mappingContext.query.parameters,
success: querySuccess,
error: function (response) {
deferred.reject(adapter._createErrorFromResponse(response, url, mappingContext));
}
});
return deferred.promise;
function querySuccess(response) {
try {
var data = response.data;
var inlineCount = data.__count ? parseInt(data.__count, 10) : undefined;
var rData = {
results: adapter._getResponseData(response),
inlineCount: inlineCount,
httpResponse: response
};
deferred.resolve(rData);
} catch (e) {
// if here, the adapter is broken, not bad data
var err = new Error("Query failed while parsing successful query response")
err.name = "Program Error";
err.response = response;
err.originalError = e;
deferred.reject(err);
}
}
}
function _getResponseData(response) {
var data = response.data && response.data.d;
return data.results === undefined ? data : data.results;
}
function _processSavedEntity(savedEntity, response /*, saveContext, index*/) {
var etag = savedEntity && savedEntity.entityAspect && response.getHeaders('ETag');
if (etag) {
savedEntity.entityAspect.extraMetadata.etag = etag;
}
}
function serverTypeNameToClientDefault(serverTypeName) {
// strip off leading 'SP.Data.' and trailing 'ListItem'
var re = /^(SP\.Data.)(.*)(ListItem)$/;
var typeName = serverTypeName.replace(re, '$2');
return breeze.MetadataStore.normalizeTypeName(typeName);
}
function serverTypeNameToClient2010Default(serverTypeName) {
// strip off leading 'Microsoft.SharePoint.DataService.' and trailing 'Item'
var regex = /^(Microsoft\.SharePoint\.DataService\.)(.*)(Item)$/;
var typeName = serverTypeName.replace(regex, '$2');
return breeze.MetadataStore.normalizeTypeName(typeName);
}
}));