-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsrapi.js
177 lines (161 loc) · 6.46 KB
/
wsrapi.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
/*global jQuery,jQuery.Deferred*/
/*
This will eventually become a streamlined replacement for any and all OOI-WSR operations.
- Manuel Warum (AIT), Oct 28, 2013
*/
/**
* @param {string} apiUri the OOI-WSR's API URI
* @author Manuel Warum (AIT)
* @version 0.6.1
* @constructor
*/
function WorldStateRepository(apiUri) {
/**
* @type {string} the OOI-WSR's API URI
* @private
*/
this.apiUri = apiUri;
}
/**
* Creates a fully qualified URL for the specified resource path. If a resource ID is provided,
* the path will point at the resource URL; otherwise it will point to the resource collection.
* @param {string} path the path (usually the resource's name) to fetch
* @param {number?} entityId the unique identifier of the resource to fetch
* @returns {string} a fully qualified URL pointing to the specified resource
*/
WorldStateRepository.prototype.createUrl = function(path, entityId) {
var uri = this.apiUri + '/' + path;
if (entityId) uri += '/' + entityId;
return this.proxify(uri);
};
/**
* Creates an URL pointing to the specified URL which is routed through Wirecloud's internal proxy iff
* the MashupPlatform API is detected. Otherwise, the specified URL itself is returned without any changes.
* The proxy address can be used to access data from external sources that would otherwise be inaccessible due to
* the same-origin policy of browsers. Note that this method does not check if the specified URL is actually on a
* foreign origin; it will simply assume it is and defer further processing to the buildProxyURL method of the
* MashupPlatform API.
* @param {string} url the URL you want to access
* @returns {string} an URL pointing to the specified address
*/
WorldStateRepository.prototype.proxify = function(url) {
return typeof (MashupPlatform) === 'undefined' ? url : MashupPlatform.http.buildProxyURL(url);
};
/**
* Fetches all entities from a resource collection.
* @param {string} path the resource's name
* @param {*?} data any data that should be sent to the server
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.fetch = function(path, data) {
return $.get(this.createUrl(path), data);
};
/**
* Fetches a single resource identified by its unique identifier.
* @param {string} path the resource's name
* @param {number} entityId the resource's unique identifier
* @param {*?} data any data that should be sent to the server
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.fetchById = function(path, entityId, data) {
return $.get(this.createUrl(path, entityId), data);
};
/**
* Inserts a single (new) resource instance.
* @param {string} path the resource's name
* @param {object} instance the instance to insert
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.singleInsert = function(path, instance) {
return $.post(this.createUrl(path), instance);
};
/**
* Updates/replaces a single resource instance.
* @param {string} path the resource's name
* @param {number} entityId the resource's unique identifier
* @param {object} instance the instance to replace
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.singleReplace = function(path, entityId, instance) {
return $.ajax({
data: instance,
type: 'PUT',
url: this.createUrl(path, entityId)
});
};
/**
* Deletes a single resource instance identified by its unique identifier.
* @param {string} path the resource's name
* @param {number} entityId the resource's unique identifier
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.singleDelete = function(path, entityId) {
return $.ajax({
type: 'DELETE',
url: this.createUrl(path, entityId)
});
};
/**
* Performs a mass insert of resource instances.
* @param {string} path the resource's name
* @param {object[]} data an array of resource instances that will be inserted on the server
* @returns {jQuery.Deferred} a jQuery deferred object instance for this request
*/
WorldStateRepository.prototype.massInsert = function(path, data) {
return $.ajax({
contentType: 'application/json',
data: JSON.stringify(data),
processData: false,
type: 'POST',
url: this.createUrl(path)
});
};
/*
* the following function essentially creates all programmatically generated instance methods for the
* WorldStateRepository class.
*/
(function () {
/**
* Resources for which default operations will be generated (list, get, insert, update, delete).
* @const
* @type {string[]}
*/
var pathsWithDefaultBehaviour = [
'Entity',
'EntityTypeProperty',
'Event',
'WorldState',
'EntityType',
'EntityGeometry',
'Simulation',
'EntityProperty'
];
/**
* Resources for which mass operations will be generated (insert).
* @const
* @type {string[]}
*/
var pathsWithCollectionBehaviour = [
'EntityProperties',
'EntityGeometries'
];
var pluralize = function(string) {
return string.match(/y$/) ? string.replace(/y$/, 'ies') : string + 's';
};
function wireDefault(path) {
WorldStateRepository.prototype['list' + pluralize(path)] = function () { return this.fetch(path); };
WorldStateRepository.prototype['get' + path] = function (id) { return this.fetchById(path, id); };
WorldStateRepository.prototype['insert' + path] = function (data) { return this.singleInsert(path, data); };
WorldStateRepository.prototype['update' + path] = function (id, data) { return this.singleReplace(path, id, data); };
WorldStateRepository.prototype['delete' + path] = function (id) { return this.singleDelete(path, id); };
}
function wireCollection(path) {
WorldStateRepository.prototype['insert' + path] = function (data) { return this.massInsert(path, data); };
}
var i;
for (i = 0; i < pathsWithDefaultBehaviour.length; i++)
wireDefault(pathsWithDefaultBehaviour[i]);
for (i = 0; i < pathsWithCollectionBehaviour.length; i++)
wireCollection(pathsWithCollectionBehaviour[i]);
})();