Hide one column, provide infos in details box
[misc/kostenrechnung] / lib / rico / ricoLiveGridAir.js
1 /**
2   *  (c) 2005-2008 Richard Cowin (http://openrico.org)
3   *  (c) 2005-2008 Matt Brown (http://dowdybrown.com)
4   *
5   *  Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6   *  file except in compliance with the License. You may obtain a copy of the License at
7   *   http://www.apache.org/licenses/LICENSE-2.0
8   **/
9
10 if(typeof Rico=='undefined') throw("LiveGridAir requires the Rico JavaScript framework");
11 if(typeof RicoUtil=='undefined') throw("LiveGridAir requires the RicoUtil object");
12 if(typeof Rico.Buffer=='undefined') throw("LiveGridAir requires the Rico.Buffer object");
13 if(typeof window.runtime=='undefined') throw("LiveGridAir requires the Adobe AIR runtime");
14
15
16 Rico.writeDebugMsg = function(msg) {
17   window.runtime.trace(this.timeStamp()+msg);
18 };
19
20
21 /**
22  * Data source is an Adobe AIR SQLite database
23  */
24 Rico.Buffer.AirSQL = Class.create();
25
26 Rico.Buffer.AirSQL.prototype = {
27
28 initialize: function(dbConn, fromClause, options) {
29   Object.extend(this, new Rico.Buffer.AjaxSQL(null, options));
30   Object.extend(this, new Rico.Buffer.AirSQLMethods());
31   this.dataSource=this.airFetch;
32   this.colnames=[];
33   this.colsql=[];
34   this.fromClause=' from '+fromClause;
35   this.dbConn=dbConn;
36   this.SQLStatement = window.runtime.flash.data.SQLStatement;
37   this.options.sortParmFmt='index';
38 }
39
40 };
41
42 Rico.Buffer.AirSQLMethods = function() {};
43
44 Rico.Buffer.AirSQLMethods.prototype = {
45
46 addColumn: function(sql,name) {
47   this.colsql.push(sql);
48   this.colnames.push(name);
49 },
50
51 allColumnsSql: function() {
52   var s='';
53   for (var i=0; i<this.colnames.length; i++) {
54     if (i>0) s+=',';
55     s+=this.colsql[i];
56     if (this.colnames[i]) s+=" AS '"+this.colnames[i]+"'";
57   }
58   return s;
59 },
60
61 // override
62 formQueryHashSQL: function(startPos,fetchSize) {
63   var queryHash=this.formQueryHashXML(startPos,fetchSize);
64   Object.extend(queryHash,this.sortParm);
65
66   // filters
67   queryHash.filters=[];
68   for (var n=0; n<this.liveGrid.columns.length; n++) {
69     var c=this.liveGrid.columns[n];
70     if (c.filterType == Rico.TableColumn.UNFILTERED) continue;
71     var colnum=c.format.filterUI && c.format.filterUI.length > 1 ? parseInt(c.format.filterUI.substr(1),10) : c.index;
72     var f={};
73     f.columnIndex=colnum;
74     f.op=c.filterOp;
75     f.values=c.filterValues;
76     queryHash.filters.push(f);
77   }
78   return queryHash;
79 },
80
81 addCondition: function(whereClause,colnum,op,value) {
82   var field=this.colsql[colnum];
83   whereClause+=(whereClause ? ' and ' : ' where ');
84   whereClause+='('+field+op+value+')';
85   return whereClause;
86 },
87
88 airFetch: function(options) {
89   Rico.writeDebugMsg("airFetch");
90   var i,j,stmt;
91   var sqlwhere='';
92   var parms=options.parameters;
93   var sqlparms=[];
94   var sqlorder=parms.sort_dir ? ' order by '+(parms.sort_col+1)+' '+parms.sort_dir : '';
95   for (var n=0; n<parms.filters.length; n++) {
96     var f=parms.filters[n];
97     var v0=f.values[0];
98     switch (f.op) {
99       case "EQ":
100         sqlparms.push(v0);
101         sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'=','?');
102         break;
103       case "LE":
104         sqlparms.push(v0);
105         sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'<=','?');
106         break;
107       case "GE":
108         sqlparms.push(v0);
109         sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'>=','?');
110         break;
111       case "NE":
112         var ne="(";
113         for (i=0; i<f.values.length; i++) {
114           if (i>0) ne+=",";
115           ne+='?';
116           sqlparms.push(f.values[i]);
117         }
118         ne+=")";
119         sqlwhere=this.addCondition(sqlwhere,f.columnIndex,' NOT IN ',ne);
120         break;
121       case "LIKE":
122         if (v0.indexOf('*')==-1) v0='*'+v0+'*';
123         sqlparms.push(v0.replace(/\*/g,"%"));
124         sqlwhere=this.addCondition(sqlwhere,f.columnIndex,' LIKE ','?');
125         break;
126     }
127   }
128   if (typeof(this.sqltotalrows)=='undefined' || options.parameters.get_total=='true') {
129     stmt = new this.SQLStatement();
130     stmt.sqlConnection = this.dbConn;
131     stmt.text = "select count(*) as cnt"+this.fromClause+sqlwhere;
132     for (i=0; i<sqlparms.length; i++) stmt.parameters[i]=sqlparms[i];
133     stmt.execute();
134     this.sqltotalrows=stmt.getResult().data[0].cnt;
135   }
136   stmt = new this.SQLStatement();
137   stmt.sqlConnection = this.dbConn;
138   var newRows=[];
139   var offset=options.parameters.offset;
140   var limit=Math.min(this.sqltotalrows-offset,options.parameters.page_size);
141   stmt.text = "select "+this.allColumnsSql()+this.fromClause+sqlwhere+sqlorder+" LIMIT "+limit+" OFFSET "+offset;
142   for (i=0; i<sqlparms.length; i++) stmt.parameters[i]=sqlparms[i];
143   Rico.writeDebugMsg(stmt.text);
144   stmt.execute();
145   var result = stmt.getResult();
146   if( result.data == null ) {
147     Rico.writeDebugMsg('no data');
148   } else {
149     for (i = 0; i < result.data.length; i++) {
150       var dataRow = result.data[i];
151       var newRow=[];
152       for (j=0; j<this.colnames.length; j++)
153         newRow.push(dataRow[this.colnames[j]]);
154       newRows.push(newRow);
155     }
156   }
157   options.onComplete(newRows,false,this.sqltotalrows);
158 }
159
160 };
161
162 Rico.includeLoaded('ricoLiveGridAir.js');