dbgenerate.js
[app.Builder.js] / dbgenerate.js
1 //<script type="text/javascript">
2
3 /**
4  * This is a hacky generator to generate element definitions for the Roo version from the database
5  * 
6  * Let's see if libgda can be used to generate our Readers for roo...
7  * 
8  * Concept - conect to database..
9  * 
10  * list tables
11  * 
12  * extra schemas..
13  * 
14  * write readers..
15  * 
16  * usage: seed generate.js
17  *
18  *
19  *
20  *
21  *Hack needed to latest GLib-2.0.gir 
22  *
23  * <record name="KeyFile" c:type="GKeyFile" disguised="1">
24         <constructor name="new" c:identifier="g_key_file_new">
25         <return-value transfer-ownership="full">
26           <type name="KeyFile" c:type="GKeyFile*"/>
27         </return-value>
28       </constructor>
29  *
30  *
31  * remove introspectable =0 from g_key_file_get_groups
32  *   and add transfer-owneership = none to return value
33  * remove introspectable =0 from g_key_file_get_keys
34  *   and add transfer-owneership = none to return value* 
35  * 
36  */
37 Gda  = imports.gi.Gda;
38 GObject = imports.gi.GObject;
39
40 GLib = imports.gi.GLib;
41
42 console = imports.console;
43 File = imports.File.File;
44 Options = imports.Options.Options;
45
46 //Gda.init();
47
48 var prov = Gda.Config.list_providers ();
49 //print(prov.dump_as_string());
50
51 var o = new Options({
52     help_description : 'Element builder for App Builder based on database schema',
53     
54     options:  [
55         { arg_long : 'DBTYPE' , arg_short : 't', description : 'Database Type (eg. MySQL or PostgreSQL ' },
56         { arg_long : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
57         { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
58         { arg_long : 'PASSWORD' , arg_short : 'p', description : '' , arg_default :'' },
59         { arg_long : 'INI' , arg_short : 'I', description :
60                     'Either base directory which has Pman/***/DataObjects/***.links.ini or location of ini file.' },
61     ]
62            
63 })
64
65
66
67 var cfg = o.parse(Seed.argv);
68 print(JSON.stringify(cfg, null,4));
69
70 var   cnc = Gda.Connection.open_from_string (cfg.DBTYPE,
71          "DB_NAME=" + cfg.DBNAME, 
72         "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
73         Gda.ConnectionOptions.NONE, null);
74
75
76
77                                               
78
79  
80 Gda.DataSelect.prototype.fetchAll = function()
81 {
82     var cols = [];
83     
84     for (var i =0;i < this.get_n_columns(); i++) {
85         cols.push(this.get_column_name(i));
86     }
87     //print(JSON.stringify(cols, null,4));
88     var iter = this.create_iter();
89     var res = [];
90     //print(this.get_n_rows());
91     var _this = this;
92     for (var r = 0; r < this.get_n_rows(); r++) {
93         
94         // single clo..
95         //print("GOT ROW");
96         if (cols.length == 1) {
97             res.push(this.get_value_at(0,r).get_string());
98             continue;
99         }
100         var add = { };
101         
102         cols.forEach(function(n,i) {
103             var val = _this.get_value_at(i,r);
104             var type = GObject.type_name(val.g_type) ;
105             var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
106             //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
107             //print (n + '=' + iter.get_value_at(i).value);
108             add[n] = vs;
109         });
110         
111         res.push(add);
112         
113     }
114     return res;
115
116 }
117
118 var map = {
119     'date' : 'date',
120     'datetime' : 'date',
121     'datetime' : 'timestamp with time zone',
122     'time' : 'string', //bogus
123     'int' : 'int',
124     'integer' : 'int',
125     'bigint' : 'int',
126     'double' : 'float',
127     'tinyint' : 'int',
128     'smallint' : 'int',
129     'decimal' : 'float',
130     'float' : 'float',
131     'char' : 'string',
132     'varchar' : 'string',
133     'text' : 'string',
134     'longtext' : 'string',
135     'tinytext' : 'string',
136     'mediumtext' : 'string',
137     'enum' : 'string',
138     'timestamp' : 'number',
139     'blob' : 'text'
140     
141 }
142
143 var ini = { }
144
145 function readIni(fn)
146 {
147     print('Read INI : ' + fn);
148     var key_file = new GLib.KeyFile.c_new();
149     if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
150         return;
151     }
152    
153     var groups = key_file.get_groups();
154     groups.forEach(function(g) {
155         ini[g] = {}
156            print("KEY:"+g);
157         var keys = key_file.get_keys(g);
158         if (!keys) { return; }
159          keys.forEach(function(k) {
160             ini[g][k] = key_file.get_value(g,k);
161         })
162     })
163     
164 }
165 if (File.isFile(cfg.INI)) {
166     if (cfg.INI.match(/links\.ini$/)) {
167         readIni(cfg.INI);
168     } else {
169         readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
170     }
171 }
172
173
174 if (File.isDirectory(cfg.INI)) {
175         
176
177     //--- load ini files..
178     // this is very specific.
179     
180     var dirs = File.list( cfg.INI + '/Pman').filter( 
181         function(e) { 
182             if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
183                 return false;
184             }
185             return true;
186         }
187     );
188     
189      
190     dirs.forEach(function(d) {
191         // this currently misses the web.*/Pman/XXXX/DataObjects..
192         var path = cfg.INI + '/Pman/' + d + '/DataObjects';
193          
194         if (!File.isDirectory(path)) {
195             return; //skip
196         }
197         var inis = File.list(path).filter(
198             function(e) { return e.match(/\.links\.ini$/); }
199         );
200         if (!inis.length) {
201             return;
202         }
203         
204         inis.forEach(function(i) {
205             readIni(path + '/' + i); 
206             
207         })
208  
209     });
210     // look at web.XXXX/Pman/XXX/DataObjects/*.ini
211     var inis = File.list(cfg.INI).filter(
212         function(e) { return e.match(/\.links\.ini$/); }
213     )
214     
215      inis.forEach(function(i) {
216         readIni(path + '/' + i); 
217         
218     })
219     
220     
221 }
222 //print(JSON.stringify(ini, null,4));
223  //console.dump(ini);
224
225
226  //Seed.quit();
227
228 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
229
230
231 switch(cfg.DBTYPE) {
232     case "MySQL":
233         query_tables = "SHOW TABLES";
234         query_describe_table = "DESCRIBE `%s`";
235         break;
236     
237     case 'PostgreSQL':
238         query_tables = "select c.relname FROM pg_catalog.pg_class c " + 
239             "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " + 
240             "WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')" +
241             "AND pg_catalog.pg_table_is_visible(c.oid) ";
242          query_describe_table =  
243                 "SELECT " +
244                 "f.attnum AS number, " +
245                 "f.attname AS Field, " +
246                 "f.attnum, " +
247                 "CASE WHEN f.attnotnull = 't' THEN 'NO' ELSE 'YES' END AS isNull, " + 
248                 "pg_catalog.format_type(f.atttypid,f.atttypmod) AS Type, " +
249                 "CASE WHEN p.contype = 'p' THEN 't' ELSE 'f' END AS primarykey, " +
250                 "CASE WHEN p.contype = 'u' THEN 't' ELSE 'f' END AS uniquekey, " +
251                 "CASE WHEN p.contype = 'f' THEN g.relname END AS foreignkey, " +
252                 "CASE WHEN p.contype = 'f' THEN p.confkey END AS foreignkey_fieldnum, " +
253                 "CASE WHEN p.contype = 'f' THEN g.relname END AS foreignkey, " +
254                 "CASE WHEN p.contype = 'f' THEN p.conkey END AS foreignkey_connnum, " +
255                 "CASE WHEN f.atthasdef = 't' THEN d.adsrc END AS default " +
256                 "FROM pg_attribute f JOIN pg_class c ON c.oid = f.attrelid " +
257                 "        JOIN pg_type t ON t.oid = f.atttypid " +
258                 "        LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum " +
259                 "        LEFT JOIN pg_namespace n ON n.oid = c.relnamespace " +
260                 "        LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY ( p.conkey ) " +
261                 "        LEFT JOIN pg_class AS g ON p.confrelid = g.oid " +
262                 "WHERE c.relkind = 'r'::char AND n.nspname = '%n' " +
263                 "AND c.relname = '%s' AND f.attnum > 0 ORDER BY number";
264                 
265                 
266                 
267         break;
268 /*
269            "Field": "province",
270         "Type": "varchar(255)",
271         "Null": "NO", << or is null
272         "Key": null,
273         "Default": null,
274         "Extra": 
275 */  
276 }
277
278
279  
280
281 var tables = Gda.execute_select_command(cnc, query_tables).fetchAll();
282 print(JSON.stringify(tables));
283
284 var readers = [];
285 tables.forEach(function(table) {
286     //print(table);
287     var schema = Gda.execute_select_command(cnc,
288             query_describe_table.replace(/%s/, table).replace(/%n/,'public')
289             ).fetchAll();
290     
291     
292     var reader = []; 
293     var colmodel = []; 
294     var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
295          
296     var form = {}
297        
298     var firstTxtCol = '';
299     
300     //print(JSON.stringify(schema, null,4));    Seed.quit();
301     
302     schema.forEach(function(e)  {
303         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
304         var row  = { }; 
305         if (type) {
306             e.Type = type[1];
307             e.Size = type[2];
308         }
309         
310         
311         
312         row.name = e.Field;
313         
314         
315         if (typeof(map[e.Type]) == 'undefined') {
316            console.dump(e);
317            throw {
318                 name: "ArgumentError", 
319                 message: "Unknown mapping for type : " + e.Type
320             };
321         }
322         row.type = map[e.Type];
323         
324         if (row.type == 'string' && !firstTxtCol.length) {
325             firstTxtCol = row.name;
326         }
327         
328         if (row.type == 'date') {
329             row.dateFormat = 'Y-m-d';
330         }
331         reader.push(row);
332         
333         if (combofields.length == 1 && row.type == 'string') {
334             combofields.push(row);
335         }
336         
337         
338         var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
339         title  = title[0].toUpperCase() + title.substring(1);
340         
341         colmodel.push({
342             "xtype": "ColumnModel",
343             "header": title,
344             "width":  row.type == 'string' ? 200 : 75,
345             "dataIndex": row.name,
346             "|renderer": row.type != 'date' ? 
347                     "function(v) { return String.format('{0}', v); }" :
348                     "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
349             "|xns": "Roo.grid",
350             "*prop": "colModel[]"
351         });
352         var xtype = 'TextField';
353         
354         
355         if (row.type == 'number') {
356             xtype = 'NumberField';
357         }
358         if (row.type == 'date') {
359             xtype = 'DateField';
360         }
361         if (e.Type == 'text') {
362             xtype = 'TextArea';
363         }
364         if (row.name == 'id') {
365             xtype = 'Hidden';
366         } 
367         // what about booleans.. -> checkboxes..
368         
369         
370         
371         form[row.name] = {
372             fieldLabel : title,
373             name : row.name,
374             width : row.type == 'string' ? 200 : 75,
375             '|xns' : 'Roo.form',
376             xtype : xtype
377         }
378         if (xtype == 'TextArea') {
379             form[row.name].height = 100;
380         }
381         if (xtype == 'Hidden') {
382             delete form[row.name].fieldLabel;
383             delete form[row.name].width;
384         }
385         
386     });
387     
388     var combo = {
389         '|xns' : 'Roo.form',
390         xtype: 'ComboBox',
391         allowBlank : 'false',
392         editable : 'false',
393         emptyText : 'Select ' + table,
394         forceSelection : true,
395         listWidth : 400,
396         loadingText: 'Searching...',
397         minChars : 2,
398         pageSize : 20,
399         qtip: 'Select ' + table,
400         selectOnFocus: true,
401         triggerAction : 'all',
402         typeAhead: true,
403         
404         width: 300,
405         
406         
407         
408         tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
409         queryParam : '',// SET WHEN USED
410         fieldLabel : table,  // SET WHEN USED
411         valueField : 'id',
412         displayField : '', // SET WHEN USED eg. project_id_name
413         hiddenName : '', // SET WHEN USED eg. project_id
414         name : '', // SET WHEN USED eg. project_id_name
415         items : [
416             {
417                     
418                 '*prop' : 'store',
419                 'xtype' : 'Store',
420                 '|xns' : 'Roo.data',
421                 'remoteSort' : true,
422                 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
423                 listeners : {
424                     '|beforeload' : 'function (_self, o)' +
425                     "{\n" +
426                     "    o.params = o.params || {};\n" +
427                     "    // set more here\n" +
428                     "}\n"
429                 },
430                 items : [
431                     {
432                         '*prop' : 'proxy',
433                         'xtype' : 'HttpProxy',
434                         'method' : 'GET',
435                         '|xns' : 'Roo.data',
436                         '|url' : "baseURL + '/Roo/" + table + ".php'",
437                     },
438                     
439                     {
440                         '*prop' : 'reader',
441                         'xtype' : 'JsonReader',
442                         '|xns' : 'Roo.data',
443                         'id' : 'id',
444                         'root' : 'data',
445                         'totalProperty' : 'total',
446                         '|fields' : JSON.stringify(combofields)
447                         
448                     }
449                 ]
450             }
451         ]
452     }
453     
454     
455     
456     
457     //print(JSON.stringify(reader,null,4));
458     readers.push({
459         table : table ,
460         combo : combo,
461         combofields : combofields,
462         reader :  reader,
463         oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
464         colmodel : colmodel,
465         firstTxtCol : firstTxtCol,
466         form : form
467     });
468     
469     //console.dump(schema );
470     
471      
472 });
473
474
475
476 // merge in the linked tables..
477 readers.forEach(function(reader) {
478     if (typeof(ini[reader.table]) == 'undefined') {
479      
480         return;
481     }
482     print("OVERLAY - " + reader.table);
483     // we have a map..
484     for (var col in ini[reader.table]) {
485         var kv = ini[reader.table][col].split(':');
486         
487         
488         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
489         if (!add) {
490             continue;
491         }
492         // merge in data (eg. project_id => project_id_*****
493      
494         add.oreader.forEach(function(or) {
495             reader.reader.push({
496                 name : col + '_' + or.name,
497                 type : or.type
498             });
499         });
500         
501         // col is mapped to something..
502         var combofields = add.combofields;
503         if (add.combofields.length < 2) {
504             continue;
505         }
506         if (typeof(reader.form[col]) == 'undefined') {
507             print (JSON.stringify(reader.form, null,4));
508             print("missing linked column " + col);
509             continue;
510         }
511         
512         var combofields_name = add.combofields[1].name;
513         var old =   reader.form[col];
514         reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
515         reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
516         reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
517         reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
518         reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
519         reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
520         reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
521         
522              
523     };
524     
525     
526 });
527
528 //readers.forEach(function(reader) {
529 //    delete reader.oreader;
530 //});
531
532  
533
534
535
536 //print(JSON.stringify(readers, null, 4));
537
538 readers.forEach(function(reader) {
539     
540
541     var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader'; 
542     if (!File.isDirectory(dir)) {
543         print("mkdir " + dir);
544         File.mkdir(dir);
545     }
546     
547     // READERS
548     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
549     
550                 
551     var jreader = {
552         '|xns' : 'Roo.data',
553         xtype : "JsonReader",
554         totalProperty : "total",
555         root : "data",
556         '*prop' : "reader",
557         id : 'id', // maybe no..
558        
559         '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
560     };
561     
562     File.write(
563         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
564         JSON.stringify(jreader, null, 4)
565     )
566     
567     
568     // GRIDS
569     dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel'; 
570     if (!File.isDirectory(dir)) {
571         print("mkdir " + dir);
572         File.mkdir(dir);
573     }
574     
575
576     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
577     
578     File.write(
579         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
580             
581        
582         JSON.stringify({
583             '|xns' : 'Roo',
584             xtype : "GridPanel",
585             "title": reader.table,
586             "fitToframe": true,
587             "fitContainer": true,
588             "tableName": reader.table,
589             "background": true,
590             "region" : 'center',
591             "listeners": {
592                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
593             },
594             "items": [
595                 {
596                     "*prop": "grid",
597                     "xtype": "Grid",
598                     "autoExpandColumn": reader.firstTxtCol,
599                     "loadMask": true,
600                     "listeners": {
601                         "|render": "function() \n" +
602                             "{\n" +
603                             "    _this.grid = this; \n" +
604                             "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
605                             "    if (_this.panel.active) {\n" +
606                             "       this.footer.onClick('first');\n" +
607                             "    }\n" +
608                             "}",
609                         "|rowdblclick": "function (_self, rowIndex, e)\n" + 
610                             "{\n" + 
611                             "    if (!_this.dialog) return;\n" + 
612                             "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
613                             "        _this.grid.footer.onClick('first');\n" + 
614                             "    }); \n" + 
615                             "}\n"
616                     },
617                     "|xns": "Roo.grid",
618
619                     "items": [
620                         {
621                             "*prop": "dataSource",
622                             "xtype": "Store",
623                              remoteSort : true,
624                             '|sortInfo' : "{ field : '" + reader.firstTxtCol  +  "', direction: 'ASC' }", 
625                             "|xns": "Roo.data",
626                             "items": [
627                                 
628                                 {
629                                     "*prop": "proxy",
630                                     "xtype": "HttpProxy",
631                                     "method": "GET",
632                                     "|url": "baseURL + '/Roo/" + reader.table + ".php'",
633                                     "|xns": "Roo.data"
634                                 },
635                                 jreader
636                             ]
637                         },
638                         {
639                             "*prop": "footer",
640                             "xtype": "PagingToolbar",
641                             "pageSize": 25,
642                             "displayInfo": true,
643                             "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
644                             "emptyMsg": "No " + reader.table + " found",
645                             "|xns": "Roo"
646                         },
647                         {
648                             "*prop": "toolbar",
649                             "xtype": "Toolbar",
650                             "|xns": "Roo",
651                             "items": [
652                                 {
653                                     "text": "Add",
654                                     "xtype": "Button",
655                                     "cls": "x-btn-text-icon",
656                                     "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
657                                     "listeners": {
658                                         "|click": "function()\n"+
659                                             "{\n"+
660                                             "    if (!_this.dialog) return;\n" +
661                                             "    _this.dialog.show( { id : 0 } , function() {\n"+
662                                             "        _this.grid.footer.onClick('first');\n"+
663                                             "   }); \n"+
664                                             "}\n"
665                                     },
666                                     "|xns": "Roo.Toolbar"
667                                 },
668                                 {
669                                     "text": "Edit",
670                                     "xtype": "Button",
671                                     "cls": "x-btn-text-icon",
672                                     "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
673                                     "listeners": {
674                                         "|click": "function()\n"+
675                                             "{\n"+
676                                             "    var s = _this.grid.getSelectionModel().getSelections();\n"+
677                                             "    if (!s.length || (s.length > 1))  {\n"+
678                                             "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
679                                             "        return;\n"+
680                                             "    }\n"+
681                                             "    if (!_this.dialog) return;\n" +
682                                             "    _this.dialog.show(s[0].data, function() {\n"+
683                                             "        _this.grid.footer.onClick('first');\n"+
684                                             "    }); \n"+
685                                             "    \n"+
686                                             "}\n" 
687                                         
688                                     },
689                                     "|xns": "Roo.Toolbar"
690                                 },
691                                 {
692                                     "text": "Delete",
693                                     "cls": "x-btn-text-icon",
694                                     "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
695                                     "xtype": "Button",
696                                     "listeners": {
697                                         "|click": "function()\n"+
698                                             "{\n"+
699                                             "     Pman.genericDelete(_this, '" + reader.table + "'); \n"+
700                                             "}\n"+
701                                             "        "
702                                     },
703                                     "|xns": "Roo.Toolbar"
704                                 }
705                             ]
706                         }, // end toolbar
707                     ].concat( reader.colmodel)
708                 }
709             ]
710             
711             
712         }, null, 4)
713     )
714     
715     /// FORMS..
716     
717     dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form'; 
718     if (!File.isDirectory(dir)) {
719         print("mkdir " + dir);
720         File.mkdir(dir);
721     }
722     var formElements = [];
723     var formHeight = 50;
724     for (var k in reader.form) {
725         if (k == 'id') { // should really do primary key testing..
726             continue;
727         }
728         formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
729         
730         formElements.push(reader.form[k]);
731     }
732     if (reader.form['id']) {
733         formElements.push(reader.form['id']);
734     }
735     
736
737     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
738     var frmCfg = 
739     {
740         '|xns' : 'Roo.form',
741         xtype : "Form",
742         listeners : {
743             "|actioncomplete" : "function(_self,action)\n"+
744                 "{\n"+
745                 "    if (action.type == 'setdata') {\n"+
746                 "       //_this.dialog.el.mask(\"Loading\");\n"+
747                 "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
748                 "       return;\n"+
749                 "    }\n"+
750                 "    if (action.type == 'load') {\n"+
751                 "        _this.dialog.el.unmask();\n"+
752                 "        return;\n"+
753                 "    }\n"+
754                 "    if (action.type =='submit') {\n"+
755                 "    \n"+
756                 "        _this.dialog.el.unmask();\n"+
757                 "        _this.dialog.hide();\n"+
758                 "    \n"+
759                 "         if (_this.callback) {\n"+
760                 "            _this.callback.call(_this, _this.form.getValues());\n"+
761                 "         }\n"+
762                 "         _this.form.reset();\n"+
763                 "         return;\n"+
764                 "    }\n"+
765                 "}\n",
766             
767             "|rendered" : "function (form)\n"+
768                 "{\n"+
769                 "    _this.form= form;\n"+
770                 "}\n"
771         },
772         method : "POST",
773         style : "margin:10px;",
774         "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
775         items : formElements
776     };
777     
778     
779     File.write(
780         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
781             
782        
783         JSON.stringify( frmCfg, null, 4)
784     );
785             
786             
787    
788    
789    
790      /// COMBO..
791     
792     dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
793     if (!File.isDirectory(dir)) {
794         print("mkdir " + dir);
795         File.mkdir(dir);
796     }
797    
798     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
799     
800     File.write(
801         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
802             
803        
804         JSON.stringify(reader.combo, null, 4)
805     );
806             
807    
808    
809    
810    
811    
812     // DIALOG.
813    
814    
815     dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog'; 
816     if (!File.isDirectory(dir)) {
817         print("mkdir " + dir);
818         File.mkdir(dir);
819     }
820     var formElements = [];
821     for (var k in reader.form) {
822         if (k == 'id') { // should really do primary key testing..
823             continue;
824         }
825         formElements.push(reader.form[k]);
826     }
827     formElements.push(reader.form['id']);
828
829     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
830     
831     File.write(
832         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
833             
834        
835         JSON.stringify({
836        
837             "closable": false,
838             "collapsible": false,
839             "height": formHeight,
840             "resizable": false,
841             "title": "Edit / Create " + reader.table,
842             "width": 400,
843             "xtype": "LayoutDialog",
844             "|xns": "Roo",
845             "items": [
846                 {
847                     "|xns": "Roo",
848                     "xtype": "LayoutRegion",
849                     "*prop": "center"
850                 },
851                 {
852                     "region": "center",
853                     "xtype": "ContentPanel",
854                     "|xns": "Roo",
855                     "items": [
856                         frmCfg
857                     ]
858                 },
859                 
860                 {
861                     "listeners": {
862                         "click": "function (_self, e)\n{\n    _this.dialog.hide();\n}"
863                     },
864                     "*prop": "buttons[]",
865                     "text": "Cancel",
866                     "xtype": "Button",
867                     "|xns": "Roo"
868                 },
869                 {
870                     "listeners": {
871                         "click": "function (_self, e)\n{\n    // do some checks?\n     \n    \n    _this.dialog.el.mask(\"Saving\");\n    _this.form.doAction(\"submit\");\n\n}"
872                     },
873                     "*prop": "buttons[]",
874                     "text": "Save",
875                     "xtype": "Button",
876                     "|xns": "Roo"
877                 }
878             ]
879         }, null,4)
880     );
881    
882    
883    
884 });
885
886