-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3qlCode.js
430 lines (326 loc) · 13 KB
/
s3qlCode.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
418
419
420
421
422
423
424
425
426
427
428
429
430
function S3QLquery(s3ql)
{
/*Function S3QLQuery builds the S3QL query for any remote uri
#INPUT: s3ql is an array with at least key and url
#OUTPUT: a string, containing the URI with the information on the input element UID
s3ql = new Array();s3ql['url'] = 'http://ibl.mdanderson.org/TCGA/';s3ql['key'] = 'mudamseostempos';s3ql['where'] = new Array();s3ql['from'] = 'statements';s3ql['where']['rule_id']='191';
*/
var wrap='';
wrap = s3ql['url'] + '/S3QL.php';
if (s3ql['user_id']!=null) {
wrap += '?user_id=' + s3ql['user_id'];
}
if (s3ql['format']!=null) {
( s3ql['user_id']==null)? r = '?': r = '&';
wrap += r + 'format=' + s3ql['format'];
}
( s3ql['user_id']==null && s3ql['format']==null )? r = '?': r = '&';
wrap += r + 'query=<S3QL>';
wrap += '<key>' + s3ql['key'] + '</key>';
//remove the elements already used to build the query, keep the rest
delete s3ql['url']; delete s3ql['key']; delete s3ql['user_id']; delete s3ql['format'];
for (var i in s3ql) {
var field = i;
var val = s3ql[i];
var go = (is_array(val) || is_string(val));
if(go){
if(!is_array(s3ql[field])) //if is not an array, just build the simple xml
wrap += '<' + field + '>' + s3ql[field] + '</' + field + '>';
else //for arrays, build the nested xml
{wrap += '<' + field + '>';
for (var j in val)
{
var subfield = j;
var subvalue = val[subfield];
var go = (is_array(subvalue) || is_string(subvalue));
if (go) {
wrap += '<' + subfield + '>' + subvalue + '</' + subfield + '>';
}
}
wrap += '</' + field + '>';
}
}
}
wrap += '</S3QL>';
return (wrap);
}
function itemStatInserted(stat, value, item, rule, button_id) {
if(stat[0]['statement_id'])
document.getElementById(button_id).innerHTML = value+'<br /><font color="#66CC00">inserted</font>';
else {
document.getElementById(button_id).innerHTML = value+'<br /><font color="#FF0000">NOT inserted: '+stat[0]['message']+'</font>';
}
}
function insertItemStat(item,value, rule, button_id) {
var s3ql = new Array;
s3ql['url']=document.getElementById('url').value;
s3ql['key']=document.getElementById('key').value;
s3ql['insert'] = 'statement';
s3ql.where = new Array;
s3ql['where']['item_id']=item;
s3ql['where']['rule_id'] = rule;
s3ql['where']['value'] = value;
s3ql['format'] = 'json';
var url = S3QLquery(s3ql);
s3db_jsonpp_call(url,"itemStatInserted(ans, '" + value + "','" + item + "', '" + rule + "', '"+button_id+"')");
}
function findItem(collection, notes, button_id,event)
{
if(event && event.keyCode==13 || event=='finditem'){
document.getElementById(button_id).innerHTML = 'processing ...';
if(!notes){
button_id.innerHTML = 'Please input some label';
}
else {
var s3ql = new Array;
s3ql['url']=document.getElementById('url').value;
s3ql['key']=document.getElementById('key').value;
s3ql['select'] = '*';
s3ql['from'] = 'items';
s3ql.where = new Array;
s3ql['where']['collection_id'] = collection;
s3ql['where']['notes'] = notes;
s3ql['format'] = 'json';
var url = S3QLquery(s3ql);
item=s3db_jsonpp_call(url,"itemFound(ans, '" + notes + "','" + collection + "', '" + button_id + "')");
return item
}
}
}
function displayItemButton(item, collection , label, button_id) {
//document.getElementById(button_id).innerHTML = '<input type="button" value="'+label+' (ID#'+item[0].item_id+')" onClick="window.open('+document.getElementById('url').value+'/item.php?key='+document.getElementById('key').value+'&item_id='+item[0].item_id+')"> <font color="#FF0000" item_id="'+item[0].item_id+'">inserted</font>';
}
function itemInserted(ans, label, collection, button_id) {
if(ans[0].item_id)
{
document.getElementById(button_id).item_id = ans[0].item_id;
document.getElementById(button_id).innerHTML = label + '<input type="button" value="'+label+' (ID#'+ans[0].item_id+')" onClick="window.open(\''+document.getElementById('url').value+'/item.php?key='+document.getElementById('key').value+'&item_id='+ans[0].item_id+'\')" item_id="'+ans[0].item_id+'">';
//We may at this point run through the statements pf the item, as it has been found
itemStatementsInsert(ans[0].item_id,label, collection, button_id)
}
else {
document.getElementById(button_id).innerHTML = '<font color="#FF0000">' +label + 'NOT INSERTED</font>';
}
}
function itemStatementsInsert(item,label, collection, button_id) {
//Go through the columns and identify the collection of the item
data = document.getElementById(button_id).data;
line = document.getElementById(button_id).line;
sC = new Array ();
sD = new Array ();
sL = new Array ();
for (col = 0; col < data.length; col++) {
rule= document.getElementById('rule_'+col).rule_id;
value = data[col];
//based on the rule, determine whehter the subject of trhe rule is the same
//separate the data based on the subjects
subjectsData = new Array();
ruleData = document.getElementById('rule_'+col).info;
if(collection==ruleData['subject_id']) {
stat_button_id = 'line_'+line+'_col_'+(col+1);
insertItemStat(item,value, rule, stat_button_id);
}
else {
//Find the item collection, find the data
array_push(sC, ruleData['subject_id']);
array_push(sD, data[col]);
pat = /name|label|identification|ID/i;
if(pat.test(ruleData['object']))
array_push(sL, data[col]);
else
array_push(sL, label);
}
}
//Now go through all teh items that were discovered and create them
//creating a new Item ofthe subject collections and finding the right rule in which to insert it.
sFound = new Array();
createdItems = new Array ();
for (i = 0; i < sD.length; i++) {
if(array_search(sD[i], sFound)==''){
button_item = document.createElement('div');
button_item.id = sD[i];
document.body.appendChild(button_item);
stats[sD[i]] = new Array ();
}
else {
array_push(stats[sD[i]], sD[i]);
}
}
//Item = document.createElement('div');
//Item.id= 'line_'+line+'col_'+col+'_item';
//findItem(ruleData['subject_id'], data[col], Item.id);
}
function returnAns(ans,button_id) {
document.getElementById(button_id).data = ans;
}
function insertItem(collection, label, button_id) {
url=document.getElementById('url').value;
key=document.getElementById('key').value;
s3ql = new Array;
s3ql['url']=document.getElementById('url').value;
s3ql['key']=document.getElementById('key').value;
s3ql['insert']='item';
s3ql['where'] = new Array;
s3ql['where']['collection_id']=collection;
s3ql['where']['notes']=label;
var url = S3QLquery(s3ql);
//s3db_jsonpp_call(url,'displayItemButton(ans, \''+ collection +'\',\''+ label +'\', \''+ button_id +'\')');
s3db_jsonpp_call(url,'itemInserted(ans, \''+label+'\',\''+collection+'\',\''+button_id+'\')');
}
function findAllItemStatements(item, url, key, collection, collectionRules) {
//Build an array to query last inserted item on collceton
s3ql = new Array();
s3ql['url'] = url;
s3ql['key'] = key;
s3ql['from'] = 'statements';
s3ql['where'] = new Array();
s3ql['where']['item_id']=item;
q = S3QLquery(s3ql);
s3db_jsonpp_call(q,'writeAllItemStatementsOnText(ans, \''+ item +'\', \''+ url +'\', \''+ key +'\', \''+collectionRules+'\')');
}
function RuleFound(ans, rule,button_id) {
//create a new cell for this rule in the rule line. This will identify the rule
//ruleLine = document.getElementById('ruleLine');
ruleDiv = document.getElementById(button_id);
if(ans[0].rule_id){
ruleDiv.rule_id=rule;
ruleDiv.found=1;
ruleDiv.innerHTML ='<font color="#00CC33">'+ans[0].subject+'<br>'+ans[0].verb+'<br>'+ans[0].object+'<br> (R#'+rule+' found)</font>';
} else {
ruleDiv.innerHTML ='<font color="#FF0000">(R#'+rule+' NOT found)<!-- <a href="#" onClick="createRule()"><br /><i>Create this rule</i></a> --></font>';
ruleDiv.found=0;
}
//ruleLine.appendChild(ruleTd);
ruleDiv.info=ans[0];
nrRules = explode(',',document.getElementById('items').getAttribute('rules')).length;
if(document.getElementById('rule_col_'+(nrRules-1)))
{
document.getElementById('items').innerHTML='<a href="#" onClick="readTheLines()" style="font-style: italic;color: #3300FF">Import the items</a> <a href="#" style="font-style: italic;color: #3300FF" onClick="window.location=window.location.pathname">Reset</a>';
//document.getElementById('items').innerHTML='<a href="#" style="font-style: italic;color: #3300FF" onClick="window.location=window.location.pathname">Reset</a>';
//readTheLines();
}
}
function findRule(rule, url, key,button_id) {
var s3ql = new Array();
s3ql.url=url;
s3ql.key=key;
s3ql.from='rules';
s3ql.where =new Array();
s3ql['where']['rule_id']=rule+'';
var url = S3QLquery(s3ql);
s3db_jsonpp_call(url,'RuleFound(ans, \''+ rule +'\', \''+button_id+'\')');
}
function findKey(url, username, pass, authority) {
if(!authority){
q = url+'/apilogin.php?&username='+username+'&password='+pass+'&format=json';
}
else {
q = url+'/apilogin.php?authority='+authority+'&username='+username+'&password='+pass+'&format=json';
}
s3db_jsonpp_call(q,'keyFound(ans)');
}
function projectRulesFound(ans, button_id)
{
//at this point, we have enough information to determine whether there is one subject or many subjects. so were can decide which subject must tbe introduced first
allRules = ans;
document.getElementById(button_id).allRules = allRules;
for (r = 0; r < ans.length; r++) {
var rules = document.getElementById(button_id);
if(rules.subjects){
rules.subjects = rules.subjects + ',' + ans[r].subject_id;
}
else {
rules.subjects = ans[r].subject_id;
}
rules.rule_id = ans[r].rule_id;
if(ans[r].object_id)
{
if(rules.objectIsCollection){
rules.objectIsCollection = rules.objectIsCollection + ',' + '1';
}
else {
rules.objectIsCollection = '1';
}
if(rules.objects){
rules.objects = rules.objects + ',' + ans[r].object_id;
}
else {
rules.objects = ans[r].object_id;
}
}
else {
if(rules.objectIsCollection){
rules.objectIsCollection = rules.objectIsCollection + ',' + '0';
}
else {
rules.objectIsCollection = '0';
}
if(rules.objects){
//rules.objects = rules.objects + ',' + ans[r].object;
}
else {
//rules.objects = ans[r].object;
}
}
}
var nodes=explode(',',document.getElementById('rules').subjects+document.getElementById('rules').objects)
nodes = array_unique(nodes);
nodes = nodes.sort();
document.getElementById(button_id).nodes = nodes;
// //M=Matrix.Zero(nodes.length,nodes.length) //make a matrix of zeros the size of the nr of nodes
// for (r = 0; r < allRules.length; r++) {
//
// subInd=array_search(allRules[r].subject_id, nodes);
// if(allRules[r].object_id)
// { objInd=array_search(allRules[r].object_id, nodes);}
// else {
// objInd=array_search(allRules[r].object, nodes);
// }
//
// M.elements[subInd][objInd]=1;//after thi,matrix should contain the rules
//
// }
// document.getElementById(button_id).M = M;
}
function findProjectRules(url,key) {
document.getElementById('collections').innerHTML = 'processing ...';
var project = document.getElementById('project_id').value;
var s3ql = new Array();
s3ql.url=url;
s3ql.key=key;
s3ql.from='rules';
s3ql.where = new Array();
s3ql.where['project_id'] = project;
s3ql.where['object'] = '!=\'UID\'';
var q = S3QLquery(s3ql);
s3db_jsonpp_call(q,'projectRulesFound(ans, \'rules\')');
//To obtain a collection reference, display collections
s3ql.url=url;
s3ql.key=key;
s3ql.from='collection';
s3ql.where = new Array();
s3ql.where['project_id'] = project;
var q = S3QLquery(s3ql);
s3db_jsonpp_call(q,'projectCollectionsFound(ans, \'collections\')');
}
function findProjects(url,key, button_id) {
var s3ql = new Array();
s3ql.url=url;
s3ql.key=key;
s3ql.from='projects';
var q = S3QLquery(s3ql);
s3db_jsonpp_call(q,'projectsFound(ans, \''+button_id+'\')');
}
function findLastInsertedItem(collection, url,key) {
//Build an array to query last inserted item on collceton
s3ql = new Array();
s3ql['url'] = url;
s3ql['key'] = key;
s3ql['from'] = 'items';
s3ql['where'] = new Array();
s3ql['where']['collection_id']=collection;
s3ql['order_by']='created_on desc';
s3ql['limit']='1';
q = S3QLquery(s3ql);
s3db_jsonpp_call(q,'lastIntertedItemFound(ans, \''+ collection +'\', \''+ url +'\', \''+ key +'\')');
}