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

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