Newer
Older
kaloyane
committed
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
/*!
* OpenBIS API
*
* An API for accessing openBIS. Depends on jQuery.
*/
var jsonRequestData = function(params) {
// KE: generate unique ids ? Hardcoded "id" seems to work too for now
params["id"] = "1"
params["jsonrpc"] = "2.0"
return JSON.stringify(params)
}
var ajaxRequest = function(settings) {
settings.type = "POST";
settings.processData = false;
settings.dataType = "json";
settings.crossDomain = true;
settings.data = jsonRequestData(settings.data);
$.ajax(settings);
}
/*
* Functions for working with cookies.
*
* These are from http://www.quirksmode.org/js/cookies.html
*/
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function openbis(url, dssUrl) {
this.generalInfoServiceUrl = url + "/rmi-general-information-v1.json"
this.queryServiceUrl = url + "/rmi-query-v1.json"
this.dssUrl = dssUrl + "/rmi-dss-api-v1.json"
}
openbis.prototype.login = function(username, password, action) {
openbisObj = this
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "tryToAuthenticateForAllServices",
"params" : [ username, password ]
},
success:
function(data) {
openbisObj.sessionToken = data.result;
openbisObj.rememberSession();
action(data)
},
error: function() {
alert("Login failed")
}
});
}
openbis.prototype.rememberSession = function() {
// Store the result in a cookie, so the user doesn't need to log in every time.
createCookie('openbis', this.sessionToken, 1);
}
openbis.prototype.restoreSession = function() {
this.sessionToken = readCookie('openbis');
}
openbis.prototype.isSessionActive = function(action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "isSessionActive",
"params" : [ this.sessionToken ]
},
success: action
});
}
openbis.prototype.ifRestoredSessionActive = function(action) {
this.restoreSession();
this.isSessionActive(function(data) { if (data.result) action(data) });
}
openbis.prototype.logout = function(action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "logout",
"params" : [ this.sessionToken ]
},
success: action
});
}
openbis.prototype.listProjects = function(action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "listProjects",
"params" : [ this.sessionToken ]
},
success: action
});
}
openbis.prototype.listExperiments = function(projects, experimentType, action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "listExperiments",
"params" : [ this.sessionToken, projects, experimentType ]
},
success: action
});
}
openbis.prototype.searchForSamples = function(searchCriteria, action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "searchForSamples",
"params" : [ this.sessionToken,
searchCriteria ] },
success: action
});
}
openbis.prototype.searchForSamples = function(searchCriteria, action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "searchForSamples",
"params" : [ this.sessionToken,
searchCriteria ] },
success: action
});
}
openbis.prototype.searchForDataSets = function(searchCriteria, action) {
ajaxRequest({
url: this.generalInfoServiceUrl,
data: { "method" : "searchForDataSets",
"params" : [ this.sessionToken,
searchCriteria ] },
success: action
});
}
openbis.prototype.listFilesForDataSet = function(dataSetCode, path, recursive, action) {
ajaxRequest({
url: this.dssUrl,
data: { "method" : "listFilesForDataSet",
"params" : [ this.sessionToken, dataSetCode, path, recursive ]
},
success: action
});
}
openbis.prototype.getDownloadUrlForFileForDataSet = function(dataSetCode, filePath, action) {
ajaxRequest({
url: this.dssUrl,
data: { "method" : "getDownloadUrlForFileForDataSet",
"params" : [ this.sessionToken, dataSetCode, filePath ]
},
success: action
});
}
openbis.prototype.listQueries = function(action) {
ajaxRequest({
url: this.queryServiceUrl,
data: { "method" : "listQueries",
"params" : [ this.sessionToken ] },
success: action
});
}
openbis.prototype.executeQuery = function(queryId, parameterBindings, action) {
ajaxRequest({
url: this.queryServiceUrl,
data: { "method" : "executeQuery",
"params" : [ this.sessionToken, queryId, parameterBindings ] },
success: action
});
}
/**
* A utility class for deferring an action until all of some kind of action has completed
*
* @argument dependencies An array of the keys for the dependencies.
*/
function actionDeferrer(pendingAction, dependencies) {
this.pendingAction = pendingAction;
this.dependencies = {};
var newme = this;
dependencies.forEach(function(key) {
newme.dependencies[key] = false;
});
}
/**
* Note that a dependency completed. Execute the pending action if appropriate.
*/
actionDeferrer.prototype.dependencyCompleted = function(key) {
this.dependencies[key] = true;
var shouldExecute = true;
for (prop in this.dependencies) {
if (false == this.dependencies[prop]) {
shouldExecute = false;
break;
}
}
if (shouldExecute) {
this.pendingAction();
}
}