source: MDService2/branches/MDService_simple3/WebContent/scripts/mdservice_model.js @ 1588

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

squery removing changes

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