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

Last change on this file since 961 was 961, checked in by gaba, 14 years ago

query-names

File size: 16.4 KB
Line 
1// client-side oportunistic modelling of "business-objects":
2// Query, Queryset, Component?
3var page_record_count = 10;
4var formatquerystring_len = 25;
5
6function Collection(index, name){
7        this.index = index;
8        this.name = name;
9};
10var collections  = [];
11
12
13function Query(collection, columns, query , squery) {
14        this.collection = collection;   // []
15        this.columns = columns;         // string
16        this.query = query;              //sctext
17        this.squery = squery;
18        this.listid = "";
19        this.container = {};
20        // page
21        this.startRecord = 1;
22        this.maximumRecords = parseInt(this.startRecord) + page_record_count - 1;
23       
24        // repository
25        this.repository = 1;
26       
27        this.columns_widget = null;
28};
29
30Query.prototype.load  = function(json) {
31        //var collection_text = Query.collectiontext(json);
32       
33        var columns_text = "";
34        if (json.columns != "null"){
35                $.each(json.columns,function(i,item){
36                        columns_text = columns_text + "," + item;
37                });
38                if (columns_text.length > 1) {
39                        columns_text = columns_text.substring(1);       
40                }
41        }
42        this.columns = columns_text;
43       
44        var collection = [];
45        if (json.collections != "null"){
46                $.each(json.collections,function(i,item){
47                        var coll = new Collection(item.index, item.name);
48                        collection[i] = coll;
49                });
50        }
51        this.collection = collection;
52        if (json["querystring"] == "[NULL]"){
53                this.query = null;
54        } else {
55                this.query = json["querystring"];       
56        }
57        if (json["squerystring"] == "[NULL]"){
58                this.squery = null;
59        } else {
60                this.squery = json["squerystring"];     
61        }
62        this.columns = columns_text;
63};
64
65Query.prototype.getcolumnstext = function (){
66       
67        if (this.columns_widget == null ) {
68                return this.columns;
69        }
70        return this.columns_widget.getColumnsListText();
71};
72
73Query.prototype.query_uri = function () {
74        var params = "?";
75        if (this.query != null) {
76                params = params + "q=" + escape(this.query) + "&";
77        }
78        if (this.squery != null) {
79                params = params + "squery=" + escape(this.squery) + "&";
80        }
81        params = params + "collection=" + this.getcollectiontext("index") + "&columns=" + this.getcolumnstext() + "&startRecord=" + this.startRecord + "&maximumRecords=" + this.maximumRecords + "&repository=" + this.repository;
82        return params;
83        //return $.param(this.query);
84};
85
86Query.prototype.query_json = function () {     
87        var jsonq = {"squerystring":this.squery, 
88                                 "querystring":this.query,                               
89                                 "collections" : this.getcollectiontext("index"), 
90                                 "columns" : this.columns,
91                                 "startRecord" : this.startRecord,
92                                 "maximumRecords" : this.maximumRecords};
93        return JSON.stringify(jsonq);
94};
95/*
96Query.prototype.getcolumnstext = function(what) {
97        var columns_text = "";
98        for (var i = 0; i < this.columns.length; i++) {
99                if (what == "index") {
100                        columns_text = columns_text + "," + this.columns[i];
101                } else {
102                        columns_text = columns_text + "," + this.columns[i];   
103                }
104        }
105        if (columns_text.length > 1) {
106                columns_text = columns_text.substring(1);       
107        }
108        return columns_text;
109};
110*/
111
112Query.prototype.getcollectiontext = function(what) {
113        var collection_text = "";
114        for (var i = 0; i < this.collection.length; i++) {
115                if (what == "index") {
116                        collection_text = collection_text + "," + this.collection[i].index;
117                } else {
118                        collection_text = collection_text + "," + this.collection[i].name;     
119                }
120        }
121        if (collection_text.length > 1) {
122                collection_text = collection_text.substring(1); 
123        }
124        return collection_text;
125};
126Query.prototype.getcollectionindextext = function() {
127        var collection_text = "";
128        for (var i = 0; i < this.collection.length; i++) {
129                collection_text = collection_text + "," + this.collection[i].index;
130        }
131        if (collection_text.length > 1) {
132                collection_text = collection_text.substring(1); 
133        }
134        return collection_text;
135};
136
137Query.collectiontext = function(json) {
138        var collection_text = "";
139        if (json.collections != "null"){
140                $.each(json.collections,function(i,item){
141                        collection_text = collection_text + "," + item.name;
142                });
143                if (collection_text.length > 1) {
144                        collection_text = collection_text.substring(1); 
145                }
146        }
147        return collection_text;
148};
149
150Query.fullformatstring = function (json) {
151        var qs = json.querystring;
152        var sqs = json.squerystring;
153       
154        if (qs == "[NULL]") {
155                qs = null;
156        }
157        if (sqs == "[NULL]") {
158                sqs = null;
159        }
160        var collection_text = Query.collectiontext(json);
161       
162        var full_str = "";
163        var len = formatquerystring_len;
164       
165        if (sqs != null) {
166                if (sqs.length > len){
167                        full_str = sqs.substring(0,len) + "..| ";
168                } else {
169                        sqs = sqs + "                                     ";
170                        full_str = sqs.substring(0,len) + "  | ";
171                }
172        }
173        if (qs != null) {
174                qs = Query.simplequerystring(qs);
175                if (qs.length > len){
176                        full_str = full_str + qs.substring(0,len) + "..| ";
177                } else {
178                        qs = qs + "                                     ";
179                        full_str = full_str + qs.substring(0,len) + "  | ";
180                }
181        }
182        if (collection_text.length > (len - 6)) {
183                full_str = full_str + collection_text.substring(0,len);
184        } else {
185                full_str = full_str + collection_text;
186        }
187       
188        return full_str;
189};
190
191Query.queryliststring = function(squery, query) {
192        var s_squery = "";
193        var s_query = "";
194        var str = "";
195       
196        if (squery != null){
197                s_squery = squery;
198        }
199        if (query != null){
200                s_query = query;
201        }
202        if (s_squery.length != 0 && s_query.length != 0){
203                str = squery + " & (" + Query.simplequerystring(query) +  ")";
204        } else if (s_squery.length != 0) {
205                str = squery;
206        } else {
207                str =  Query.simplequerystring(query);
208        }
209        return str;
210};
211
212Query.simplequerystring = function (querystring) {
213
214        if (querystring == null){
215                return "";
216        }
217        var arr_and = querystring.split(" and ");
218        var simple_form = "";
219        var simple_form_all = "";
220        var rel = "";
221       
222        for( var i=0;i<arr_and.length;i++){
223                arr_and[i] = $.trim(arr_and[i]);
224                var arr_or = arr_and[i].split(" or ");
225                simple_form = "";
226                for( var j=0;j<arr_or.length;j++){
227                        arr_or[j] = $.trim(arr_or[j]);
228                        while (arr_or[j].substring(0,1) == "(" ) {
229                                arr_or[j] = arr_or[j].substring(1,arr_or[j].length);
230                                arr_or[j] = $.trim(arr_or[j]);
231                        }
232                        while ( arr_or[j].substring(arr_or[j].length-1) == ")"){
233                                arr_or[j] = arr_or[j].substring(0,arr_or[j].length-1);
234                                arr_or[j] = $.trim(arr_or[j]);
235                        }
236                        if (j > 0) { 
237                                rel = " or ";
238                        } else {
239                                rel = "";
240                        }
241                        simple_form = simple_form + rel + arr_or[j];
242                }
243                if (arr_or.length > 1){
244                        simple_form = "(" + simple_form + ") ";
245                }
246                if (i > 0) { 
247                        rel = " and  ";
248                } else {
249                        rel = "";
250                }
251                simple_form_all = simple_form_all + rel + simple_form;
252               
253        }
254       
255       
256        //notifyUser("querystring:" + querystring, 'debug');
257        //notifyUser("simplequerystring:" + simple_form_all, 'debug');
258        return simple_form_all;
259};
260
261Query.prototype.render = function () {
262        var x = "<div id='" + this.listid + "' class='query_wrapper ui-widget' ><div class='query_header ui-widget-header ui-state-default ui-corner-top'>" +
263        "<span class='cmd cmd_get'></span><span class='cmd cmd_save'></span><span class='cmd cmd_del'> </span>" +
264        "<span class='query_id'>" +     this.listid + "</span>: <span class='query'>" +
265        this.getcollectiontext("name") + ":" + Query.queryliststring(this.squery, this.query) + "</span>" +
266                        "<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>" +
267                //      <span class='cmd cmd-xml'></span><span class='cmd cmd-page'></span><span class='cmd cmd-link'></span></div>" +         
268                        "<div class='columns-wrapper'><div class='query-columns'></div><span class='cmd cmd_del'></span></div><div class='result ui-widget-content ui-corner-bottom'></div>";
269        addToQuerylist(x);     
270       
271        this.container = $('#' + this.listid );
272       
273        this.columns_widget = new ListWidget($(this.container).find('.query-columns'));
274        //$(this.container).find('.query-columns').append(this.columns_widget);
275       
276        this.columns_widget.load(this.columns.split(','));
277        this.columns_widget.initAutocomplete(element_autocomplete);
278       
279        $(this.container).children('.query_header').find('.cmd_del').live('click',  function(event) {
280                queryset.removequery($(this).closest('.query_wrapper').attr('id'));
281        });
282
283        /*
284        $(this.container).children('.columns-wrapper').children('.cmd_del').live('click',  function(event) {
285                $(this).parent().hide();
286        });
287        */
288       
289        $(this.container).find('.columns-wrapper').hide();
290               
291 };
292
293 Query.prototype.open = function (type) {
294         if (type != null)
295            window.open(this.link(type));
296         else
297                window.open(this.link());
298};
299 
300 Query.prototype.link = function (type) {
301         var uri="";
302         if (type=="fullpage")  {
303                //var jsonq = {"squerystring":this.squery, "querystring":this.query, "searchclauses":"null" , "collections" : this.json_coll, "columns" : this.json_cols};
304                //uri = "?query=" + JSON.stringify(jsonq) + "&startRecord=" + this.startRecord + "&maximumRecords=" + this.maximumRecords;
305                 //uri = this.query_json();
306                 uri = link('base',this.query_uri());
307         } else {
308                 uri = link('recordset',type, this.query_uri());               
309         }
310        return uri;
311 };
312 
313 Query.prototype.link_obsoleted = function () {
314         
315                // JSON conversion
316         /*
317                if (this.query.length == 0){
318                        xqstring = "[NULL]";
319                } else {
320                        xqstring = this.query;
321                }
322                if (this.squery.length == 0){
323                        xsquery = "[NULL]";
324                } else {
325                        xsquery = this.squery;
326                }
327         */
328                var jsonq = {"squerystring":this.squery, "querystring":this.query, "searchclauses":"null" , "collections" : this.json_coll, "columns" : this.json_cols};
329
330                var uri = "?query=" + JSON.stringify(jsonq) + "&startRecord=" + this.startRecord + "&maximumRecords=" + this.maximumRecords;
331                return uri;
332};
333
334 Query.prototype.submit = function () {
335               
336       
337                var uri = link('recordset','htmltable', this.query_uri());
338               
339                notifyUser("submitting query:" +  uri);
340                this.container.find('.result').load( uri, function() {
341                                        notifyUser("result-loaded",'debug');
342                                       
343                                        var get = $(this).parent().find('.cmd_get');                           
344
345                                        get.removeClass('cmd_get');
346                                        get.addClass('cmd_up');
347                                        // get.show();
348                                       
349                                        var result_header = $(this).children('.result-header');
350                                       
351                                        var q_header;
352                                        q_header = $(this).parent().find('.query_header');                                     
353                                        q_header.append(result_header);                                 
354                                        q_header.find('.cmd_reload').click(function() {
355                                                var qid = $(this).closest('.query_wrapper').attr("id");
356                                                queryset.resubmit(qid);
357                                        });
358                                        q_header.find('.cmd_prev').click(function() {
359                                                var qid = $(this).closest('.query_wrapper').attr("id");
360                                                queryset.next(qid,-1);
361                                        });
362                                        q_header.find('.cmd_next').click(function() {
363                                                        var qid = $(this).closest('.query_wrapper').attr("id");
364                                                        queryset.next(qid,1);
365                                        });
366                                       
367                                        $(this).find('.edit-columns').live('click',  function(event) {
368                                                $(this).closest('.query_wrapper').find('.columns-wrapper').show();
369                                        });
370                                       
371                                });
372
373        };
374       
375Query.prototype.resubmit = function () {
376       
377       
378        var uri = link('recordset','htmltable', this.query_uri());
379        var qid = this.listid;
380        var q_uri = this.query_uri();
381       
382        notifyUser("resubmitting query:" +  uri);
383       
384        var get = $('#' + qid ).find('.cmd_up');
385        if (get.length == 0) {
386                get = $('#' + qid ).find('.cmd_down');
387        }
388        get.addClass('cmd_get');
389        get.removeClass('cmd_up');
390        get.removeClass('cmd_down');
391        // get.show(); 
392
393        $('#' + qid ).children('.result').children().remove();
394        $('#' + qid ).find('.result').load( uri, function() {
395                                // update link-hrefs
396                                var qid = $(this).closest('.query_wrapper').attr("id");
397                                notifyUser("result-loaded ID:" + qid,'debug');
398                               
399                                var q = queryset.getQuery(qid);
400                                $('#' + qid ).find(".cmd_link").attr("href", q.link("fullpage"));
401                               
402                                var get = $(this).parent().find('.cmd_get');                           
403                                get.removeClass('cmd_get');
404                                get.addClass('cmd_up');
405                                // get.show(); 
406                               
407                                var result_header = $(this).children('.result-header');
408                               
409                                var q_header;
410                                q_header = $(this).parent().find('.query_header');
411                                q_header.children('.result-header').remove();
412                               
413                                q_header.append(result_header);                                 
414                                q_header.find('.cmd_reload').click(function() {
415                                        var qid = $(this).closest('.query_wrapper').attr("id");
416                                        queryset.resubmit(qid);
417                                });
418                                q_header.find('.cmd_prev').click(function() {
419                                        var qid = $(this).closest('.query_wrapper').attr("id");
420                                        queryset.next(qid,-1);
421                                });
422                                q_header.find('.cmd_next').click(function() {
423                                                var qid = $(this).closest('.query_wrapper').attr("id");
424                                                queryset.next(qid,1);
425                                });
426                                $(this).find('.edit-columns').live('click',  function(event) {
427                                        $(this).closest('.query_wrapper').find('.columns-wrapper').show();
428                                });     });
429
430};
431
432
433
434var queryset_container = $("#querylist"); 
435
436var queryset = { queries: [],
437        container: '#querylist',
438               
439        addquery: function (query){
440                       
441                this.queries[this.queries.length] = query;
442                query.listid = "q" + this.queries.length;
443                query.render();                         
444                query.submit();         
445                //$('#querylist').html(this.render());
446        },
447
448        removequery: function (qid) {
449                notifyUser("removing query:"  + qid);
450               
451                 for (var i = 0; i < this.queries.length; i++) {
452                        if (this.queries[i].listid == qid) {
453                                this.queries.splice(i, 1);
454                        } 
455                }       
456                $('#' + qid).remove();
457                notifyUser("query removed, new queries.length:"  + this.queries.length);
458               
459        },
460
461        getsquery: function(qid) {
462                var qstring = "";
463               
464                for (var i = 0; i < this.queries.length; i++) {
465                        if (this.queries[i].listid == qid) {
466                                qstring = this.queries[i].squery;
467                        } 
468                }
469                if (qstring == null){
470                        qstring = "";
471                }
472                return qstring;
473        },
474        getquerystring: function(qid) {
475                var qstring = "";
476               
477                for (var i = 0; i < this.queries.length; i++) {
478                        if (this.queries[i].listid == qid) {
479                                qstring = this.queries[i].query;
480                        } 
481                }
482                if (qstring == null){
483                        qstring = "";
484                }
485
486                return qstring;
487        },
488        getcollections: function(qid) {
489                var coll = "";
490                var json_coll, json_temp;
491               
492                for (var i = 0; i < this.queries.length; i++) {
493                        if (this.queries[i].listid == qid) {
494                                coll = this.queries[i].collection;
495                        } 
496                }               
497                if (coll.length == 0) {
498                        json_coll = {};
499                        json_coll = "null";
500                } else { 
501                        json_coll = [{}];
502                        for (var i = 0; i < coll.length; i++) {
503                                json_coll[i] = {"index" : coll[i].index, "name" :coll[i].name};
504                        }
505                }
506                return json_coll;
507        },
508       
509        getcolumns: function(qid) {
510                var cols = "";
511                var json_cols, json_temp;
512               
513                for (var i = 0; i < this.queries.length; i++) {
514                        if (this.queries[i].listid == qid) {
515                                cols = this.queries[i].columns;
516                        } 
517                }
518               
519                if (cols == "") {
520                        json_cols = {};
521                        json_cols = "null";
522                } else { 
523                        var pos = cols.indexOf(",", 0);
524                        var i = 0;
525                       
526                        json_cols = [{}];
527                        while (pos > -1){
528                                if (pos > -1){
529                                        json_cols[i] = cols.substring(0,pos);
530                                } else {
531                                        json_cols[i] = cols;
532                                }
533                                cols = cols.substring(pos+1);
534                                pos = cols.indexOf(",", 0);
535                                i = i+1;
536                        }
537                       
538                        json_cols[i] = cols;
539
540                       
541                }
542                return json_cols;
543        },
544       
545        resubmit: function(qid){
546                var query = queryset.queries[qid.substring(1)-1];
547               
548                query.repository = parseInt(getSelectedRepository());
549                query.startRecord = $('#' + qid ).find('.start_record').val();
550                query.maximumRecords = $('#' + qid ).find('.maximum_records').val();
551                query.resubmit();
552
553        },
554       
555        getQuery: function(qid){
556                var query = queryset.queries[qid.substring(1)-1];
557                return query;
558        },
559       
560        next: function(qid,pages){
561                var query = queryset.queries[qid.substring(1)-1];
562                var start = 0;
563                var num = 0;
564                var max_value = $('#' + qid ).find('.result-header').attr("max_value");
565               
566                if (parseInt(query.startRecord) + pages * page_record_count >= 1){
567                        start = parseInt(query.startRecord) + pages * page_record_count ;
568                } else if (parseInt(query.startRecord) + pages * page_record_count + page_record_count - 1 >= 1){
569                        start = 1 ;
570                }
571       
572                if (start > 0){
573                        if (start + page_record_count - 1 <= max_value) {
574                                num = page_record_count;
575                        } else if (start <= max_value){
576                                num = max_value - start;
577                        }
578                       
579                        if (num > 0){
580                                query.startRecord =     start;
581                                query.maximumRecords = num;
582                                query.resubmit();
583                        }
584                }
585               
586                //$('#' + qid ).find('.start_record').val(query.startRecord);
587                //$('#' + qid ).find('.maximum_records').val(query.maximumRecords);
588                //var max_results =
589               
590        }
591       
592       
593};
594
595$('#querylist .columns-edit').live('click',function(){
596        $(this).closest('query_wrapper').find('query-columns').show();
597});
598
599
600$('.cmd_del').live('click',function(){
601        if ($(this).parent().attr('class') == 'columns-wrapper'){
602                $(this).parent().hide();
603        }
604});
Note: See TracBrowser for help on using the repository browser.