source: MDService2/trunk/MDService2/WebContent/scripts/mdservice_model.js @ 1467

Last change on this file since 1467 was 1467, checked in by gaba, 13 years ago

SRU queries added

File size: 37.2 KB
Line 
1 /**
2 * @fileOverview  This file provides basic client-side model of the Query-object. <code>Query</code> construction,
3 * manipulating and <code>queryset</code> model functionality.
4 *
5 * @author
6 * @version
7 */
8
9var page_record_count = 10;
10var formatquerystring_len = 25;
11//var workspace;
12
13// json for user and server data
14function Workspace(){
15        user_json = undefined;
16        server_json = undefined;
17};
18
19Workspace.prototype.getTypedElement = function(type, elementtype, id){
20        var json; 
21        if (type == 'server'){
22                json = this.server_json;
23        } else {
24                json = this.user_json;
25        }
26        switch(elementtype){
27        case 'queryset':
28                return json["WorkspaceProfile"].Querysets;
29        case 'bookmarkset':
30                return json["WorkspaceProfile"].Bookmarksets;
31        case 'query':
32                return  json["WorkspaceProfile"]["Querysets"][id]["Queries"];
33        case 'bookmark':
34                return json["WorkspaceProfile"]["Bookmarksets"][id]["Bookmarks"];
35        case 'customtermset':
36                return json["WorkspaceProfile"].CustomTermsets;
37        }
38
39};
40
41Workspace.prototype.setTypedElement = function(type, id,data){
42        switch(type){
43        case 'queryset':
44                if (data == "[{}]"){
45                        this.user_json["WorkspaceProfile"].Querysets = [{}];
46                } else {
47                        this.user_json["WorkspaceProfile"].Querysets = data;
48                }
49                break;
50        case 'bookmarkset':
51                if (data == "[{}]"){
52                        this.user_json["WorkspaceProfile"].Bookmarksets = [{}];
53                } else {
54                        this.user_json["WorkspaceProfile"].Bookmarksets = data;
55                }
56                break;
57        case 'query':
58                if (data == "[{}]"){
59                        workspace.user_json["WorkspaceProfile"]["Querysets"][id]["Queries"] = [{}];
60                } else {
61                        workspace.user_json["WorkspaceProfile"]["Querysets"][id]["Queries"] = data;
62                }
63                break;
64        case 'bookmark':
65                if (data == "[{}]"){
66                        workspace.user_json["WorkspaceProfile"]["Bookmarksets"][id]["Bookmarks"] = [{}];
67                } else {
68                        workspace.user_json["WorkspaceProfile"]["Bookmarksets"][id]["Bookmarks"] = data;
69                }
70                break;
71        case 'customtermset':
72                if (data == "[{}]"){
73                        this.user_json["WorkspaceProfile"].CustomTermsets = [{}];
74                } else {
75                        this.user_json["WorkspaceProfile"].CustomTermsets = data;
76                }
77                break;
78        }
79
80};
81
82Workspace.prototype.addelement = function(type, json, id){
83        var elems = this.getTypedElement('user', type, id);
84        var iq = 0;
85       
86        if (elems == "null"){
87                this.setTypedElement(type,id, "[{}]");
88                elems = this.getTypedElement('user', type, id);
89        } else {
90                iq = elems.length;
91        }
92        if (json == undefined){
93                var str_time = dateFormat(new Date());//dt.toString("yyyy-MM-dd HH:mm:ss");
94                var name;
95                if (type == "bookmarkset"){
96                        name = "new_bookmarkset";
97                        json  = {"name":name, "id":0, "vcrid":0, "created":str_time , "Bookmarks" : "null"};
98                } 
99                if (type == "queryset"){
100                        name = "new_queryset";
101                        json  = {"name":name, "id":0, "vcrid":0, "created":str_time , "Queries" : "null"};
102                }
103        }
104        elems[iq] = json;
105
106        return json;
107        //this.user_json["WorkspaceProfile"]["Querysets"][id] = {"name":name, "id":0, "created":str_time , "Queries" : "null"};
108       
109};
110Workspace.prototype.getRepositoryType = function(repository){
111        var reptype = "";
112        $.each(workspace.server_json["WorkspaceProfile"]["Repositories"], function(i,item){
113                if (item.name==repository){
114                        reptype=item.type;
115                }
116        });
117        return reptype;
118};
119/*
120Workspace.prototype.addquery = function(jsonquery, qsid){
121        var qsjson = this.user_json["WorkspaceProfile"]["Querysets"][qsid];
122        var queries = qsjson["Queries"]; //this.user_json["WorkspaceProfile"]["Querysets"][qsid]["Queries"];
123        var iq = 0;
124       
125        if (queries == "null"){
126                //this.user_json["WorkspaceProfile"]["Querysets"][qsid]["Queries"] = [{}];
127                qsjson["Queries"] = [{}];
128        } else {
129                iq = queries.length;
130        }
131        //this.user_json["WorkspaceProfile"]["Querysets"][qsid]["Queries"][iq] = jsonq;
132        qsjson["Queries"][iq] = jsonquery;
133};
134Workspace.prototype.addqueryset = function(name){
135
136        var id = $('#qts_select option').size();
137        var dt = new Date();
138        var str_time = dateFormat(dt);//dt.toString("yyyy-MM-dd HH:mm:ss");
139       
140        if (id == 0){
141                this.user_json["WorkspaceProfile"]["Querysets"] = [{}];
142        }
143
144        this.user_json["WorkspaceProfile"]["Querysets"][id] = {"name":name, "id":0, "created":str_time , "Queries" : "null"};
145       
146};
147
148Workspace.prototype.addbookmark = function(jsonquery, qsid){
149        var qsjson = this.user_json["WorkspaceProfile"]["Bookmarksets"][qsid];
150        var queries = qsjson["Bookmarks"];
151        var iq = 0;
152       
153        if (queries == "null"){
154                qsjson["Bookmarks"] = [{}];
155        } else {
156                iq = queries.length;
157        }
158        qsjson["Bookmarks"][iq] = jsonquery;
159};
160Workspace.prototype.addbookmarkset = function(name){
161
162        var id = $('#bts_select option').size();
163        var dt = new Date();
164        var str_time = dateFormat(dt);
165       
166        if (id == 0){
167                this.user_json["WorkspaceProfile"]["Bookmarksets"] = [{}];
168        }
169
170        this.user_json["WorkspaceProfile"]["Bookmarksets"][id] = {"name":name, "id":0, "created":str_time , "Bookmarks" : "null"};
171       
172};
173*/
174Workspace.prototype.removeelement = function(type, id1, id2){
175        var set = this.getTypedElement('user', type,id2);
176        var size = set.length;
177        for (var i=id1;i<size-1;i++){
178                set[i] = set[i+1];
179        }
180        delete set[size-1];
181        set.length = size-1;
182        // set null string , if 0 querysets
183        if (size == 1) {
184                this.setTypedElement(type,id2,"null");
185        }
186};
187/*
188Workspace.prototype.removebookmarkset = function(qsid){
189       
190        var size = this.user_json["WorkspaceProfile"].Bookmarksets.length;
191       
192        for (var i=qsid;i<size-1;i++){
193                this.user_json["WorkspaceProfile"].Bookmarksets[i] = this.user_json["WorkspaceProfile"].Bookmarksets[i+1];
194        }
195
196        delete this.user_json["WorkspaceProfile"].Bookmarksets[size-1];
197        this.user_json["WorkspaceProfile"]["Bookmarksets"].length = size-1;
198        // set null string , if 0 querysets
199        if (size == 1) {
200                this.user_json["WorkspaceProfile"]["Bookmarksets"] = "null";
201        }
202};
203Workspace.prototype.getQuerysets = function(type){
204        var json;
205        if (type == "user"){
206                return  this.user_json["WorkspaceProfile"]["Querysets"];
207        }
208        return  this.server_json["WorkspaceProfile"]["Querysets"];
209};
210
211Workspace.prototype.getBookmarksets = function(){
212       
213        return  this.user_json["WorkspaceProfile"]["Bookmarksets"];
214       
215};
216*/
217Workspace.prototype.getCustomTermsets = function(){
218        var json;
219        json = this.user_json["WorkspaceProfile"]["CustomTermsets"];
220        return json;
221};
222Workspace.prototype.save = function(type){
223        var JSONText;
224
225        JSONText = JSON.stringify(this.user_json["WorkspaceProfile"]);
226        $.post("/MDService2/workspaceprofilesave/" + type + "/",{"data":JSONText},  function(data) {
227        });
228
229};
230Workspace.prototype.savequery = function(json, qsid, query){
231        var JSONText;
232        var type = "user";
233        var qdata = "/";// + querysetid;
234        JSONText = JSON.stringify(json);
235        if (query.bookmark == 0){
236                qdata = qdata + this.getTypedElement(type, 'queryset')[qsid]["id"];
237        } else {
238                qdata = qdata + this.getTypedElement(type, 'bookmarkset')[qsid]["id"];
239        }
240
241        $.post("/MDService2/workspaceprofilesave/" + type + qdata,{"data":JSONText},  function(data) {
242                if (json.id == 0){
243                        json.id = $(data).text();
244                        query.id = $(data).text();
245                        if (query.bookmark == 0){
246                                workspace.addelement("query", json, qsid);
247                                appendQueryUI(json,qsid, $('#userqueries'));
248                        } else {
249                                workspace.addelement("bookmark", json, qsid);
250                                appendBookmarkUI(json,qsid, $('#bookmarks'));
251                        }
252                } 
253        });
254};
255Workspace.prototype.savequeryset = function(json, bookmark){
256        var JSONText;
257        var type = "user";
258        JSONText = JSON.stringify(json);
259       
260        $.post("/MDService2/workspaceprofilesave/" + type,{"data":JSONText},  function(data) {
261                if (json.id == 0){
262                        json.id = $(data).text();
263                        // todo appendQueryset
264                        if (bookmark == 0){
265                                loadQuerysets(type);
266                        } else {
267                                loadBookmarksets();
268                        }
269                } 
270        });
271};
272/*Workspace.save = function(type, query, id, iq){
273        var JSONText;
274        var qdata = "/";
275       
276        JSONText = JSON.stringify(jsonw["WorkspaceProfile"]);
277        if (query != undefined){
278                qdata = "/" + query.id;
279        }
280        $.post("/MDService2/workspaceprofilesave/" + type + qdata,{"data":JSONText},  function(data) {
281                if (query != undefined){
282                        if (query.id == 0){
283                                query.id = $(data).text();
284                                jsonw["WorkspaceProfile"]["Querysets"][id]["Queries"][iq]["id"] = query.id;
285                                appendQueryUI(jsonw["WorkspaceProfile"]["Querysets"][id]["Queries"][iq],iq, $('#userqueries'));
286                        }
287                }
288        });
289
290};
291*/
292function Collection(index, name){
293        this.index = index;
294        this.name = name;
295};
296//var collections  = [];
297
298/**
299Creates a new Query
300@class Represents a Query.
301*/ 
302function Query(collection, columns, query , squery) {
303        this.id = 0;
304        this.collection = collection;   // []
305        this.columns = columns;         // string
306        this.query = query;              //sctext
307        this.squery = squery;
308        this.listid = "";
309        this.container = {};
310        this.bookmark = 0;
311        // page
312        this.startItem = 1;
313        this.maximumItems = parseInt(this.startItem) + page_record_count - 1;
314       
315        // repository
316        this.repository = 1;
317        //this.reptype =
318        this.options = null;
319        this.time_created = null;
320       
321        this.format = 'htmltable';
322        this.columns_widget = null;
323       
324        //vcr
325        this.vcrid = 0;
326};
327
328Query.prototype.load  = function(json) {
329       
330        var columns_text = "";
331        if (json.columns != "null"){
332                $.each(json.columns,function(i,item){
333                        columns_text = columns_text + "," + item;
334                });
335                if (columns_text.length > 1) {
336                        columns_text = columns_text.substring(1);       
337                }
338        }
339        this.columns = columns_text;
340       
341        var collection = [];
342        if (json.collections != "null"){
343                $.each(json.collections,function(i,item){
344                        var coll = new Collection(item.index, item.name);
345                        collection[i] = coll;
346                });
347        }
348        this.collection = collection;
349        if (json["querystring"] == "[NULL]"){
350                this.query = null;
351        } else {
352                this.query = json["querystring"];       
353        }
354        if (json["squerystring"] == "[NULL]"){
355                this.squery = null;
356        } else {
357                this.squery = json["squerystring"];     
358        }
359        this.columns = columns_text;
360       
361        var options_text = "";
362        if (json.options == undefined) {
363                this.options = null;
364        } else {
365                if (json.options != "null"){
366                        var opts = json.options.split(',');
367                        $.each(opts,function(i,item){
368                                options_text = options_text + "," + item;
369                        });
370                        if (options_text.length > 1) {
371                                options_text = options_text.substring(1);       
372                        }
373                        this.options = options_text;
374                } else {
375                        this.options = null;
376                }
377        }
378       
379        if (json.time == undefined){
380                this.time_created = null;
381        } else {
382                this.time_created = json.time;
383        }
384        this.id = json.id;
385        this.vcrid = json.vcrid;
386};
387Query.prototype.save = function(){
388        var qsid;
389        var jsonq = this.toJSON();
390
391        if (this.bookmark == 0){
392                qsid = parseInt($("#qts_select option:selected").val());
393        } else {
394                qsid = parseInt($("#bts_select option:selected").val());
395        }
396        workspace.savequery(jsonq, qsid, this);
397       
398        /*
399        if (this.bookmark == 0){
400                qsid = parseInt($("#qts_select option:selected").val());
401                //qsjson = workspace.getQuerysets("user")[qsid];
402                if (this.id == 0){
403                        workspace.addelement("query",jsonq, qsid);//qsjson);
404                        workspace.savequery(jsonq, qsid, this);
405                } else {
406                        workspace.savequery(jsonq, qsid, this);
407                }
408        } else {
409                qsid = parseInt($("#bts_select option:selected").val());
410                //qsjson = workspace.getBookmarksets()[qsid];
411                if (this.id == 0){
412                        workspace.addelement("bookmark", jsonq, qsid);//qsjson);
413                        workspace.savequery(jsonq, qsid,this);
414                } else {
415                        workspace.savequery(jsonq, qsid, this);
416                }
417        }
418       
419        */
420       
421};
422/*
423Query.prototype.save = function(){
424        //queryset id
425        var qsid = parseInt($("#qts_select option:selected").val());
426        var iq = 0;
427        var queries = jsonw["WorkspaceProfile"]["Querysets"][qsid]["Queries"];
428       
429        //var iq = $('#userqueries').children().size();
430        var jsonq = this.toJSON();
431        if (this.id == 0){
432                if (queries == "null"){
433                        jsonw["WorkspaceProfile"]["Querysets"][qsid]["Queries"] = [{}];
434                } else {
435                        iq = queries.length;
436                }
437                jsonw["WorkspaceProfile"]["Querysets"][qsid]["Queries"][iq] = jsonq;
438                Workspace.save("USER", this, qsid, iq);
439        } else {
440                $.each(jsonw["WorkspaceProfile"]["Querysets"][qsid]["Queries"], function(index,value){
441                        if (value.id == jsonq.id){
442                                iq = index;
443                        }
444                });
445                jsonw["WorkspaceProfile"]["Querysets"][qsid]["Queries"][iq] = jsonq;
446                Workspace.save("USER", this, qsid, iq);
447        }
448       
449       
450        //appendQueryUI(jsonw["WorkspaceProfile"]["Querysets"][id]["Queries"][iq],iq, $('#userqueries'));
451       
452};
453*/
454
455Query.prototype.getcolumnstext = function (){
456       
457        if (this.columns_widget == null ) {
458                return this.columns;
459        }
460        return this.columns_widget.getListText();
461};
462
463Query.prototype.updatecolumnstext = function (list) {
464        var columns_text = "";
465       
466        $(list).each(function(index){
467                if (index > 0) {
468                        columns_text = columns_text + ",";
469                }
470                columns_text = columns_text + $(this).text();
471        });
472        this.columns = columns_text;
473};
474
475/** special handling for special characters: double escaping (escape the %-sign)
476 * to survive the %-encoding through the request (and parsing) down to the transformation in XCQL2XPath.xsl
477 * it's: whitespace, and single and double-quotes (unified to double quotes %22)
478*/ 
479
480Query.prototype.query_uri = function () {
481        var params = "?";
482        if (this.query != null) {
483                params = params + "q=" + escape(this.query) + "&";
484        }
485        if (this.squery != null) {
486                escaped_sq =escape(this.squery).replace(/%20/g,"%2520").replace(/\%2[27]/g,"%2522") ;
487                escaped_sq = escaped_sq.replace(/%7C/g,"%257C").replace(/\+/g,"%2B"); 
488                params = params + "squery=" + escaped_sq + "&";
489        }
490        params = params + "collection=" + this.getcollectiontext("index") + "&columns=" + this.getcolumnstext() + "&startItem=" + this.startItem + "&maximumItems=" + this.maximumItems + "&repository=" + this.repository;
491        if  (this.options != null) {
492                params = params + "&options=withSummary";
493        }
494       
495        return params;
496
497        //return $.param(this.query);
498};
499
500Query.prototype.sruquery_uri = function () {
501        var params = "?operation=searchRetrieve&version=1.2&";
502        var q = "";
503        if (this.query != null) {
504                q = "( "  + escape(this.query) + " )";
505        }
506        if (this.squery != null) {
507                escaped_sq =escape(this.squery).replace(/%20/g,"%2520").replace(/\%2[27]/g,"%2522") ;
508                escaped_sq = escaped_sq.replace(/%7C/g,"%257C").replace(/\+/g,"%2B"); 
509                if (q.length > 0){
510                        q = q + " and ";
511                }
512                q = q + escaped_sq;
513        }
514        params = params + "query=" + q + "&x-cmd-collections=" + this.getcollectiontext("index") + 
515        "&startRecord=" + this.startItem + "&maximumRecords=" + this.maximumItems + "&x-cmd-repository=" + this.repository;
516       
517        return params;
518};
519
520Query.prototype.toString = function(){
521        var str = "";
522        var coll = (this.getcollectiontext("name") != "" ) ? " in " + this.getcollectiontext("name") : "";
523        str = Query.queryliststring(this.squery, this.query) + coll + " @" + this.repository.toString();
524        return str;
525};
526
527Query.prototype.publish = function() {
528        //$.data("query",this);
529        var q = this;
530        //if (q.vcrid == 0){
531                $.get("/MDService2/virtualcollection/USER/" + this.id,"", function(data){
532                        if (q.vcrid == 0){
533                                var id = parseInt($(data).children().children('virtualcollectionid').text());
534                                q.vcrid = id;
535                                q.save();
536                        }
537                });
538        //}
539};
540
541Query.prototype.toJSON = function () {
542//Query.prototype.query_json = function () {   
543        var dt = new Date();
544        var str_time = dateFormat(dt);//dt.toString("dd/mm/yyyy HH:mm:ss");
545        //var jsonq = { "options" : json_options, "bookmark" : bookmark, "time" : str_time};
546        var json_options = this.options; 
547        if (this.options != null) {
548                json_opts = [{}];
549                json_opts[0] = this.options;
550        }
551
552        // collections
553        var json_coll;
554               
555        if (this.collection.length == 0) {
556                json_coll = {};
557                json_coll = "null";
558        } else { 
559                json_coll = [{}];
560                for (var i = 0; i < this.collection.length; i++) {
561                        json_coll[i] = {"index" : this.collection[i].index, "name" :this.collection[i].name};
562                }
563        }
564        //columns
565        var json_cols;
566        var cols = this.columns;
567        if (cols == "") {
568                //json_cols = {};
569                json_cols = "null";
570        } else { 
571                var pos = this.columns.indexOf(",", 0);
572                var i = 0;
573               
574                json_cols = [{}];
575                while (pos > -1){
576                        if (pos > -1){
577                                json_cols[i] = cols.substring(0,pos);
578                        } else {
579                                json_cols[i] = cols;
580                        }
581                        cols = cols.substring(pos+1);
582                        pos = cols.indexOf(",", 0);
583                        i = i+1;
584                }
585               
586                json_cols[i] = cols;
587        }
588
589        var jsonq = {"id":this.id,
590                                 "name":"",
591                             "squerystring":this.squery, 
592                                 "querystring":this.query,     
593                                 "searchclauses":"null" ,
594                                 "collections" : json_coll, 
595                                 "columns" : json_cols,
596                                 "startItem" : this.startItem,
597                                 "maximumItems" : this.maximumItems,
598                                 "options" : json_options, 
599                                 "bookmark" : this.bookmark,
600                                 "time" : str_time,
601                                 "vcrid": this.vcrid};
602       
603        if (this.bookmark == "1") {
604                qstring = Url.decode(qstring);
605                var s = qstring.split(':');
606                jsonq.name = s[s.length - 2] + ":" + s[s.length - 1];
607        } else {
608                jsonq.name = Query.fullformatstring(jsonq);
609        }
610       
611        return jsonq;//JSON.stringify(jsonq);
612};
613
614Query.prototype.getcollectiontext = function(what) {
615        var collection_text = "";
616        for (var i = 0; i < this.collection.length; i++) {
617                if (what == "index") {
618                        collection_text = collection_text + "," + this.collection[i].index;
619                } else {
620                        collection_text = collection_text + "," + this.collection[i].name;     
621                }
622        }
623        if (collection_text.length > 1) {
624                collection_text = collection_text.substring(1); 
625        }
626        return collection_text;
627};
628Query.prototype.getcollectionindextext = function() {
629        var collection_text = "";
630        for (var i = 0; i < this.collection.length; i++) {
631                collection_text = collection_text + "," + this.collection[i].index;
632        }
633        if (collection_text.length > 1) {
634                collection_text = collection_text.substring(1); 
635        }
636        return collection_text;
637};
638
639Query.collectiontext = function(json) {
640        var collection_text = "";
641        if (json.collections != "null"){
642                $.each(json.collections,function(i,item){
643                        collection_text = collection_text + "," + item.name;
644                });
645                if (collection_text.length > 1) {
646                        collection_text = collection_text.substring(1); 
647                }
648        }
649        return collection_text;
650};
651
652Query.columnstext = function(json) {
653        var columns_text = "";
654        if (json.columns != "null"){
655                $.each(json.columns,function(i,item){
656                        columns_text = columns_text + "," + item;
657                });
658                if (columns_text.length > 1) {
659                        columns_text = columns_text.substring(1);       
660                }
661        }
662        return columns_text;
663};
664
665Query.optionstext = function(json) {
666        var options_text = "";
667        if (json.options == undefined){
668                return "";
669        }
670        if (json.options != "null"){
671                $.each(json.options,function(i,item){
672                        options_text = options_text + "," + item;
673                });
674                if (options_text.length > 1) {
675                        options_text = options_text.substring(1);       
676                }
677        }
678        return options_text;
679};
680
681Query.fullformatstring = function (json) {
682        var qs = json.querystring;
683        var sqs = json.squerystring;
684       
685        if (qs == "[NULL]") {
686                qs = null;
687        }
688        if (sqs == "[NULL]") {
689                sqs = null;
690        }
691        var collection_text = Query.collectiontext(json);
692       
693        var full_str = "";
694        var len = formatquerystring_len;
695       
696        if (sqs != null) {
697                if (sqs.length > len){
698                        full_str = sqs.substring(0,len) + "..| ";
699                } else {
700                        sqs = sqs + "                                     ";
701                        full_str = sqs.substring(0,len) + "  | ";
702                }
703        }
704        if (qs != null) {
705                qs = Query.simplequerystring(qs);
706                if (qs.length > len){
707                        full_str = full_str + qs.substring(0,len) + "..| ";
708                } else {
709                        qs = qs + "                                     ";
710                        full_str = full_str + qs.substring(0,len) + "  | ";
711                }
712        }
713        if (collection_text.length > (len - 6)) {
714                full_str = full_str + collection_text.substring(0,len);
715        } else {
716                full_str = full_str + collection_text;
717        }
718       
719        return full_str;
720};
721
722Query.queryliststring = function(squery, query) {
723        var s_squery = "";
724        var s_query = "";
725        var str = "";
726       
727        if (squery != null){
728                s_squery = squery;
729        }
730        if (query != null){
731                s_query = query;
732        }
733        if (s_squery.length != 0 && s_query.length != 0){
734                str = squery + " & (" + Query.simplequerystring(query) +  ")";
735        } else if (s_squery.length != 0) {
736                str = squery;
737        } else {
738                str =  Query.simplequerystring(query);
739        }
740        return str;
741};
742
743Query.simplequerystring = function (querystring) {
744
745        if (querystring == null){
746                return "";
747        }
748        querystring = Url.decode(querystring);
749       
750        var arr_and = querystring.split(" and ");
751        var simple_form = "";
752        var simple_form_all = "";
753        var rel = "";
754       
755        for( var i=0;i<arr_and.length;i++){
756                arr_and[i] = $.trim(arr_and[i]);
757                var arr_or = arr_and[i].split(" or ");
758                simple_form = "";
759                for( var j=0;j<arr_or.length;j++){
760                        arr_or[j] = $.trim(arr_or[j]);
761                        while (arr_or[j].substring(0,1) == "(" ) {
762                                arr_or[j] = arr_or[j].substring(1,arr_or[j].length);
763                                arr_or[j] = $.trim(arr_or[j]);
764                        }
765                        while ( arr_or[j].substring(arr_or[j].length-1) == ")"){
766                                arr_or[j] = arr_or[j].substring(0,arr_or[j].length-1);
767                                arr_or[j] = $.trim(arr_or[j]);
768                        }
769                        if (j > 0) { 
770                                rel = " or ";
771                        } else {
772                                rel = "";
773                        }
774                        simple_form = simple_form + rel + arr_or[j];
775                }
776                if (arr_or.length > 1){
777                        simple_form = "(" + simple_form + ") ";
778                }
779                if (i > 0) { 
780                        rel = " and  ";
781                } else {
782                        rel = "";
783                }
784                simple_form_all = simple_form_all + rel + simple_form;
785               
786        }
787       
788       
789        //notifyUser("querystring:" + querystring, 'debug');
790        //notifyUser("simplequerystring:" + simple_form_all, 'debug');
791        return simple_form_all;
792};
793
794Query.prototype.render = function () {
795/*
796        var x = "<div id='" + this.listid + "' class='query_wrapper ui-widget' ><div class='query_header ui-widget-header ui-state-default ui-corner-top'>" +
797        "<span class='cmd cmd_get'></span><span class='cmd cmd_save'></span><span class='cmd cmd_del'> </span>" +
798        "<span class='query_id'>" +     this.listid + "</span>: <span class='query'>" +
799        this.getcollectiontext("name") + ":" + Query.queryliststring(this.squery, this.query) + ", repository:" + this.repository.toString() + "</span>" +
800                        "<a class='cmd cmd_xml' target='_blank' href='" + this.link("xml") + "'>xml</a> <a class='cmd cmd_link' target='_blank' href='" + this.link("fullpage") + "' >link</a></div>" +
801                //      <span class='cmd cmd-xml'></span><span class='cmd cmd-page'></span><span class='cmd cmd-link'></span></div>" +         
802                        //"<div class='ui-context-dialog columns-wrapper'><div class='query-columns'></div><span class='cmd cmd_del'></span></div><div class='result ui-widget-content ui-corner-bottom'></div>";
803                        "<div class='result ui-widget-content ui-corner-bottom'></div>";
804                        */
805        /*
806        var x = $('#querylistelement').html().toString();
807        x.replace('querylistelement', this.listid);
808        $(x).find(".query-id").text(this.listid);
809        $(x).children().children(".query").text(this.getcollectiontext("name") + ":" + Query.queryliststring(this.squery, this.query) + ", repository:" + this.repository.toString());
810        */
811        // FIXME: this is not nice, there should be a function providing the formatted string of the query.
812        //var coll = (this.getcollectiontext("name") != "" ) ? " in " + this.getcollectiontext("name") : ""; 
813        var x = "<div id='" + this.listid + "' class='query_wrapper ui-widget' name='query' ><div class='query_header ui-widget-header ui-state-default ui-corner-top'>" +
814        "<span class='cmd cmd_get'></span><span class='cmd cmd_del'> </span>" +
815        "<span class='query_id'>" + this.listid + "</span>: <span class='query'>" +
816        this.toString() + 
817        //Query.queryliststring(this.squery, this.query) + coll + " @" + this.repository.toString() +
818        "</span>" + 
819                        "</div>" +
820                        "<div class='result ui-widget-content ui-corner-bottom'></div>";
821        addToQuerylist(x);     
822       
823        this.container = $('#' + this.listid );
824        $(this.container).data('query',this);
825       
826        notifyUser("DEBUG: setting up removing query:" + $(this).closest('.query_wrapper').attr('id'));
827        $(this.container).children('.query_header').find('.cmd_del').click(function(event) {
828                notifyUser("DEBUG: removing query:" + $(this).closest('.query_wrapper').attr('id'));
829                queryset.removequery($(this).closest('.query_wrapper').attr('id'));
830        });
831       
832        createTooltip(this.container);
833 };
834 
835 Query.prototype.open = function (type) {
836         if (type != null)
837            window.open(this.link(type));
838         else
839                window.open(this.link());
840};
841 
842 Query.prototype.link = function (type) {
843         var uri="";
844         if (type=="fullpage")  {
845                 uri = link('base',this.query_uri());
846         } else {
847                 if (type == "xml"){
848                         uri = link('sru',this.sruquery_uri());
849                 } else {
850                         uri = link('recordset',type, this.query_uri());
851                 }             
852         }
853        return uri;
854 };
855 
856 Query.prototype.link_obsoleted = function () {
857         
858                // JSON conversion
859         /*
860                if (this.query.length == 0){
861                        xqstring = "[NULL]";
862                } else {
863                        xqstring = this.query;
864                }
865                if (this.squery.length == 0){
866                        xsquery = "[NULL]";
867                } else {
868                        xsquery = this.squery;
869                }
870         */
871                var jsonq = {"squerystring":this.squery, "querystring":this.query, "searchclauses":"null" , "collections" : this.json_coll, "columns" : this.json_cols};
872
873                var uri = "?query=" + JSON.stringify(jsonq) + "&startItem=" + this.startItem + "&maximumItems=" + this.maximumItems;
874                return uri;
875};
876 Query.prototype.submit = function () {
877               
878                var uri;
879               
880                var reptype = workspace.getRepositoryType(this.repository);
881                if (reptype=="pazpar"){
882                        //pazpar handling this
883                        pazparsubmit(this);
884                        return;
885                }
886                if (reptype=="md" ){
887                        uri = link('recordset',this.format, this.query_uri());
888                } else {
889                        if (reptype=="sru"){
890                                uri = link('sru', this.format, this.sruquery_uri());
891                        } else {
892                                return;
893                        }
894                }
895
896               
897                var query = this;
898                notifyUser("submitting query:" +  uri);
899                this.container.find('.result').load( uri, function() {
900                                        notifyUser("result-loaded",'debug');
901                                       
902                                        var get = $(this).parent().find('.cmd_get');                           
903
904                                        get.removeClass('cmd_get');
905                                        get.addClass('cmd_up');
906                                        // get.show();
907                                       
908                                        var result_header = $(this).children('.result-header');
909                                       
910                                        var q_header;
911                                        q_header = $(this).parent().find('.query_header');                                     
912                                        q_header.append(result_header);
913                                       
914                                        addDetailFunctionality(q_header, query);
915                                       
916                                        createTooltip($(this));
917                                });
918
919        };
920       
921Query.prototype.resubmit = function () {
922       
923        var uri;// = link('recordset',this.format, this.query_uri());
924        var reptype = workspace.getRepositoryType(this.repository);
925        if (reptype=="pazpar"){
926                //pazpar handling this
927                return;
928        }
929        if (reptype=="md" ){
930                uri = link('recordset',this.format, this.query_uri());
931        } else {
932                if (reptype=="sru"){
933                        uri = link('sru', this.format, this.sruquery_uri());
934                } else {
935                        return;
936                }
937        }
938        var qid = this.listid;
939        //var q_uri = this.query_uri();
940        var query = this;
941       
942        notifyUser("resubmitting query:" +  uri);
943       
944        var get = $('#' + qid ).find('.cmd_up');
945        if (get.length == 0) {
946                get = $('#' + qid ).find('.cmd_down');
947        }
948        get.addClass('cmd_get');
949        get.removeClass('cmd_up');
950        get.removeClass('cmd_down');
951        // get.show(); 
952
953        $('#' + qid ).children('.result').children().remove();
954        $('#' + qid ).find('.result').load( uri, function() {
955                                // update link-hrefs
956                                var qid = $(this).closest('.query_wrapper').attr("id");
957                                notifyUser("result-loaded ID:" + qid,'debug');
958                               
959                                var q = queryset.getQuery(qid);
960                                $('#' + qid ).find(".cmd_link").attr("href", q.link("fullpage"));
961                               
962                                var get = $(this).parent().find('.cmd_get');                           
963                                get.removeClass('cmd_get');
964                                get.addClass('cmd_up');
965                                // get.show(); 
966                               
967                                var result_header = $(this).children('.result-header');
968                               
969                                var q_header;
970                                q_header = $(this).parent().find('.query_header');
971                                q_header.children('.result-header').remove();
972                               
973                                q_header.append(result_header);                         
974                                addDetailFunctionality(q_header, query);
975                        });
976
977};
978
979Query.prototype.summaryinfo = function () {
980       
981        var uri = link('recordset',this.format, this.query_uri());
982        //columns-wrapper
983        var temp = $('<div />');
984        $(temp).data('qcontainer',$(this.container));
985        $(temp).load(uri, function(response, status, xhr) { 
986                //detailcaller.getdetail("detail_query").close();
987                /*
988                var profiles = $(this).find('.used-profiles');
989                var parent = $(this).data('qcontainer').find('.used-profiles').parent();
990                $(parent).remove('.used-profiles').append(profiles);
991               
992                var summary = $(this).find('.result-summary');
993                parent = $(this).data('qcontainer').find('.result-summary').parent();
994                $(parent).remove('.result-summary').append(summary);
995                */
996                //detailcaller.calldetail($(this).data('qcontainer').find('.result-header').find('.cmd_detail'));
997
998                // direct replace detail data
999                var detailcontent = detailcaller.getdetail("detail_query").content;
1000               
1001                var profiles = $(this).find('.used-profiles');
1002                var parent = detailcontent.find('.used-profiles').parent();
1003                $(parent).find('.used-profiles').remove();
1004                $(parent).append(profiles);
1005               
1006                var summary = $(this).find('.result-summary');
1007                $(summary).find('.terms-tree').treeTable({initialState:"collapsed"});
1008                var parent = detailcontent.find('.result-summary').parent();
1009                $(parent).find('.result-summary').remove();
1010                $(parent).append(summary);
1011                $(parent).find('.cmd_columns').click(function(){
1012                        query_wrapper_add_column($(this));
1013                });
1014        }); 
1015
1016
1017};
1018
1019Query.prototype.updateColumns = function(selectionlist){
1020        if (selectionlist != undefined){
1021                if (selectionlist.autoSelected()){
1022                        this.columns = "";
1023                } else
1024                {
1025                        this.columns = selectionlist.listwidget.getListText();
1026                }
1027        }
1028};
1029
1030Query.prototype.next = function(pages){
1031        var start = 0;
1032        var num = 0;
1033        var max_value = $('#' + this.listid ).find('.result-header').attr("max_value");
1034       
1035        if (parseInt(this.startItem) + pages * page_record_count >= 1){
1036                start = parseInt(this.startItem) + pages * page_record_count ;
1037        } else if (parseInt(this.startItem) + pages * page_record_count + page_record_count - 1 >= 1){
1038                start = 1 ;
1039        }
1040        if (start > 0){
1041                if (start + page_record_count - 1 <= max_value) {
1042                        num = page_record_count;
1043                } else if (start <= max_value){
1044                        num = max_value - start;
1045                }
1046       
1047                if (num > 0){
1048                        this.startItem = start;
1049                        this.maximumItems = num;
1050                        this.resubmit();
1051                        updateQueryDetailPane(this);
1052                }
1053        }
1054};
1055
1056var queryset_container = $("#querylist"); 
1057
1058/**
1059 * A singleton-object holding all queries.
1060 * @constructor
1061*/ 
1062var queryset = { queries: [],
1063        container: '#querylist',
1064        recordrowselected: undefined,
1065       
1066        addquery: function (query){
1067                       
1068                this.queries[this.queries.length] = query;
1069                query.listid = "q" + this.queries.length;
1070                query.render();                         
1071                query.submit();         
1072        },
1073
1074        removequery: function (qid) {
1075                notifyUser("removing query:"  + qid);
1076               
1077                 for (var i = 0; i < this.queries.length; i++) {
1078                        if (this.queries[i].listid == qid) {
1079                                this.queries.splice(i, 1);
1080                        } 
1081                }       
1082                $('#' + qid).remove();
1083                notifyUser("query removed, new queries.length:"  + this.queries.length);
1084               
1085        },
1086
1087        getsquery: function(qid) {
1088                var qstring = "";
1089               
1090                for (var i = 0; i < this.queries.length; i++) {
1091                        if (this.queries[i].listid == qid) {
1092                                qstring = this.queries[i].squery;
1093                        } 
1094                }
1095                if (qstring == null){
1096                        qstring = "";
1097                }
1098                return qstring;
1099        },
1100        getquerystring: function(qid) {
1101                var qstring = "";
1102               
1103                for (var i = 0; i < this.queries.length; i++) {
1104                        if (this.queries[i].listid == qid) {
1105                                qstring = this.queries[i].query;
1106                        } 
1107                }
1108                if (qstring == null){
1109                        qstring = "";
1110                }
1111
1112                return qstring;
1113        },
1114        getcollections: function(qid) {
1115                var coll = "";
1116                var json_coll, json_temp;
1117               
1118                for (var i = 0; i < this.queries.length; i++) {
1119                        if (this.queries[i].listid == qid) {
1120                                coll = this.queries[i].collection;
1121                        } 
1122                }               
1123                if (coll.length == 0) {
1124                        json_coll = {};
1125                        json_coll = "null";
1126                } else { 
1127                        json_coll = [{}];
1128                        for (var i = 0; i < coll.length; i++) {
1129                                json_coll[i] = {"index" : coll[i].index, "name" :coll[i].name};
1130                        }
1131                }
1132                return json_coll;
1133        },
1134       
1135        getcolumns: function(qid) {
1136                var cols = "";
1137                var json_cols, json_temp;
1138               
1139                for (var i = 0; i < this.queries.length; i++) {
1140                        if (this.queries[i].listid == qid) {
1141                                cols = this.queries[i].columns;
1142                        } 
1143                }
1144               
1145                if (cols == "") {
1146                        json_cols = {};
1147                        json_cols = "null";
1148                } else { 
1149                        var pos = cols.indexOf(",", 0);
1150                        var i = 0;
1151                       
1152                        json_cols = [{}];
1153                        while (pos > -1){
1154                                if (pos > -1){
1155                                        json_cols[i] = cols.substring(0,pos);
1156                                } else {
1157                                        json_cols[i] = cols;
1158                                }
1159                                cols = cols.substring(pos+1);
1160                                pos = cols.indexOf(",", 0);
1161                                i = i+1;
1162                        }
1163                       
1164                        json_cols[i] = cols;
1165
1166                       
1167                }
1168                return json_cols;
1169        },
1170        getoptions: function(qid) {
1171                var opts = null;
1172                var json_opts;
1173               
1174                for (var i = 0; i < this.queries.length; i++) {
1175                        if (this.queries[i].listid == qid) {
1176                                opts = this.queries[i].options;
1177                        } 
1178                }
1179
1180                if (opts != null) {
1181                        json_opts = [{}];
1182                        json_opts[0] = opts;
1183                }
1184                return json_opts;
1185        },
1186        resubmit: function(qid){
1187                var query = queryset.queries[qid.substring(1)-1];
1188               
1189                query.repository = getSelectedRepository();
1190                query.startItem = $('#' + qid ).find('.start_record').val();
1191                query.maximumItems = $('#' + qid ).find('.maximum_records').val();
1192                query.resubmit();
1193
1194        },
1195       
1196        getQuery: function(qid){
1197                var query = queryset.queries[qid.substring(1)-1];
1198                return query;
1199        },
1200        /*
1201        next: function(qid,pages){
1202                var query = queryset.queries[qid.substring(1)-1];
1203                var start = 0;
1204                var num = 0;
1205                var max_value = $('#' + qid ).find('.result-header').attr("max_value");
1206               
1207                if (parseInt(query.startItem) + pages * page_record_count >= 1){
1208                        start = parseInt(query.startItem) + pages * page_record_count ;
1209                } else if (parseInt(query.startItem) + pages * page_record_count + page_record_count - 1 >= 1){
1210                        start = 1 ;
1211                }
1212                if (start > 0){
1213                        if (start + page_record_count - 1 <= max_value) {
1214                                num = page_record_count;
1215                        } else if (start <= max_value){
1216                                num = max_value - start;
1217                        }
1218               
1219                        if (num > 0){
1220                                query.startItem =       start;
1221                                query.maximumItems = num;
1222                                query.resubmit();
1223                        }
1224                }
1225               
1226        },
1227        */
1228        recorddetailselection: function(recordrow){
1229                if (this.recordrowselected != undefined){
1230                        $(this.recordrowselected).removeClass('detailselection');
1231                }
1232                this.recordrowselected = recordrow;
1233                $(this.recordrowselected).addClass('detailselection');
1234        }
1235       
1236       
1237};
1238
1239$('#querylist .cmd_columns').live('click',function(){
1240        notifyUser("DEBUG: #querylist.cmd_columns");
1241        $(this).closest('.query_wrapper').find('.columns-wrapper').toggle();
1242});
1243
1244function query_wrapper_add_column(elem){
1245        var slid = $(elem).closest('.query-columns').find('.widget-wrapper').attr('id');
1246        var sl = selectionlistset.getselectionlist(slid);
1247        if (sl.autoSelected()){
1248                sl.select(0);
1249        }
1250        sl.listwidget.add(new ListItem($(elem).closest('.treecol').children('.column-elem').text()));
1251       
1252        //$(elem).data('query').updateColumns(slid);
1253
1254};
1255
1256function addDetailFunctionality(q_header, query){
1257       
1258        $(q_header).data('query',query);
1259        q_header.find('.cmd_reload').data('query',query);
1260        q_header.find('.cmds .cmd_save').data('qid',query.listid);
1261        q_header.find('.cmds .cmd_savenew').data('qid',query.listid);
1262        q_header.find('.cmd_reload').click(function() {
1263                /*
1264                var qid;
1265                if ($(this).parent().attr('class') == 'result-header'){
1266                        qid = $(this).closest('.query_wrapper').attr("id");
1267                } else {
1268                        var did = $(this).closest('.detail-wrapper').attr("id");
1269                        var slid = $(this).closest('.detail-wrapper').find('.selectionlist-widget').attr("id");
1270                        var detail = detailcaller.getdetail(did);
1271                        qid = detail.temp_id;
1272                       
1273                        var q = queryset.getQuery(qid);
1274                        if (selectionlistset.getselectionlist(slid).autoSelected()){
1275                                q.columns = "";
1276                        } else
1277                        {
1278                                q.columns = selectionlistset.getselectionlist(slid).listwidget.getListText();
1279                        }
1280                }
1281                //queryset.resubmit(qid);
1282                */
1283                // update columns if  reloaded from detail pane
1284                /*
1285                if ($(this).parent().attr('class') != 'result-header'){
1286                        var slid = $(this).closest('.detail-wrapper').find('.selectionlist-widget').attr("id");
1287                        if (selectionlistset.getselectionlist(slid).autoSelected()){
1288                                $(this).data('query').columns = "";
1289                        } else
1290                        {
1291                                $(this).data('query').columns = selectionlistset.getselectionlist(slid).listwidget.getListText();
1292                        }
1293                }
1294                */
1295                /*
1296                var slid = $(this).closest('.detail-wrapper').find('.selectionlist-widget').attr("id");
1297                if (slid != undefined){
1298                        $(this).data('query').updateColumns($(this).data('detail'));
1299                }
1300                */
1301                $(this).data('query').updateColumns($(this).data('selectionlist'));
1302                $(this).data('query').resubmit();
1303        });
1304        q_header.find('.cmd_prev').click(function() {
1305                /*
1306                var qid = $(this).closest('.query_wrapper').attr("id");
1307                queryset.next(qid,-1);
1308                */
1309                $(this).closest('.query_header').data('query').next(-1);
1310        });
1311        q_header.find('.cmd_next').click(function() {
1312/*
1313                var qid = $(this).closest('.query_wrapper').attr("id");
1314                queryset.next(qid,1);
1315                */
1316                $(this).closest('.query_header').data('query').next(1);
1317        });
1318       
1319        q_header.find('.value-format').data('query',query);
1320        q_header.find('.value-format').change(function(){
1321                /*
1322                var did = $(this).closest('.detail-wrapper').attr("id");
1323                var qid = detailcaller.getdetail(did).temp_id;
1324                var q = queryset.getQuery(qid);         
1325                queryset.resubmit(qid);
1326                */
1327                $(this).data('query').format = $(this).find('option:selected').val();
1328                $(this).data('query').resubmit();
1329        });
1330        //q_header.find('.cmd_add').click(function(){
1331        //q_header.find('.cmd_columns').data('query', query);
1332        q_header.find('.cmd_columns').click( function(){
1333                query_wrapper_add_column($(this));
1334                //$(this).data('query').resubmit();
1335        });
1336        q_header.find('.columns-wrapper').hide();
1337        q_header.find('.terms-tree').treeTable({initialState:"collapsed"});
1338
1339
1340};
Note: See TracBrowser for help on using the repository browser.