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     'timestamp with time zone' : 'date',
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         
304         if (!e.Type) {
305             print(JSON.stringify(e, null,4));    Seed.quit();
306         }
307         
308         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
309         var row  = { }; 
310         if (type) {
311             e.Type = type[1];
312             e.Size = type[2];
313         }
314         
315         
316         
317         row.name = e.Field;
318         
319         
320         if (typeof(map[e.Type]) == 'undefined') {
321            console.dump(e);
322            throw {
323                 name: "ArgumentError", 
324                 message: "Unknown mapping for type : " + e.Type
325             };
326         }
327         row.type = map[e.Type];
328         
329         if (row.type == 'string' && !firstTxtCol.length) {
330             firstTxtCol = row.name;
331         }
332         
333         if (row.type == 'date') {
334             row.dateFormat = 'Y-m-d';
335         }
336         reader.push(row);
337         
338         if (combofields.length == 1 && row.type == 'string') {
339             combofields.push(row);
340         }
341         
342         
343         var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
344         title  = title[0].toUpperCase() + title.substring(1);
345         
346         colmodel.push({
347             "xtype": "ColumnModel",
348             "header": title,
349             "width":  row.type == 'string' ? 200 : 75,
350             "dataIndex": row.name,
351             "|renderer": row.type != 'date' ? 
352                     "function(v) { return String.format('{0}', v); }" :
353                     "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
354             "|xns": "Roo.grid",
355             "*prop": "colModel[]"
356         });
357         var xtype = 'TextField';
358         
359         
360         if (row.type == 'number') {
361             xtype = 'NumberField';
362         }
363         if (row.type == 'date') {
364             xtype = 'DateField';
365         }
366         if (e.Type == 'text') {
367             xtype = 'TextArea';
368         }
369         if (row.name == 'id') {
370             xtype = 'Hidden';
371         } 
372         // what about booleans.. -> checkboxes..
373         
374         
375         
376         form[row.name] = {
377             fieldLabel : title,
378             name : row.name,
379             width : row.type == 'string' ? 200 : 75,
380             '|xns' : 'Roo.form',
381             xtype : xtype
382         }
383         if (xtype == 'TextArea') {
384             form[row.name].height = 100;
385         }
386         if (xtype == 'Hidden') {
387             delete form[row.name].fieldLabel;
388             delete form[row.name].width;
389         }
390         
391     });
392     
393     var combo = {
394         '|xns' : 'Roo.form',
395         xtype: 'ComboBox',
396         allowBlank : 'false',
397         editable : 'false',
398         emptyText : 'Select ' + table,
399         forceSelection : true,
400         listWidth : 400,
401         loadingText: 'Searching...',
402         minChars : 2,
403         pageSize : 20,
404         qtip: 'Select ' + table,
405         selectOnFocus: true,
406         triggerAction : 'all',
407         typeAhead: true,
408         
409         width: 300,
410         
411         
412         
413         tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
414         queryParam : '',// SET WHEN USED
415         fieldLabel : table,  // SET WHEN USED
416         valueField : 'id',
417         displayField : '', // SET WHEN USED eg. project_id_name
418         hiddenName : '', // SET WHEN USED eg. project_id
419         name : '', // SET WHEN USED eg. project_id_name
420         items : [
421             {
422                     
423                 '*prop' : 'store',
424                 'xtype' : 'Store',
425                 '|xns' : 'Roo.data',
426                 'remoteSort' : true,
427                 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
428                 listeners : {
429                     '|beforeload' : 'function (_self, o)' +
430                     "{\n" +
431                     "    o.params = o.params || {};\n" +
432                     "    // set more here\n" +
433                     "}\n"
434                 },
435                 items : [
436                     {
437                         '*prop' : 'proxy',
438                         'xtype' : 'HttpProxy',
439                         'method' : 'GET',
440                         '|xns' : 'Roo.data',
441                         '|url' : "baseURL + '/Roo/" + table + ".php'",
442                     },
443                     
444                     {
445                         '*prop' : 'reader',
446                         'xtype' : 'JsonReader',
447                         '|xns' : 'Roo.data',
448                         'id' : 'id',
449                         'root' : 'data',
450                         'totalProperty' : 'total',
451                         '|fields' : JSON.stringify(combofields)
452                         
453                     }
454                 ]
455             }
456         ]
457     }
458     
459     
460     
461     
462     //print(JSON.stringify(reader,null,4));
463     readers.push({
464         table : table ,
465         combo : combo,
466         combofields : combofields,
467         reader :  reader,
468         oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
469         colmodel : colmodel,
470         firstTxtCol : firstTxtCol,
471         form : form
472     });
473     
474     //console.dump(schema );
475     
476      
477 });
478
479
480
481 // merge in the linked tables..
482 readers.forEach(function(reader) {
483     if (typeof(ini[reader.table]) == 'undefined') {
484      
485         return;
486     }
487     print("OVERLAY - " + reader.table);
488     // we have a map..
489     for (var col in ini[reader.table]) {
490         var kv = ini[reader.table][col].split(':');
491         
492         
493         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
494         if (!add) {
495             continue;
496         }
497         // merge in data (eg. project_id => project_id_*****
498      
499         add.oreader.forEach(function(or) {
500             reader.reader.push({
501                 name : col + '_' + or.name,
502                 type : or.type
503             });
504         });
505         
506         // col is mapped to something..
507         var combofields = add.combofields;
508         if (add.combofields.length < 2) {
509             continue;
510         }
511         if (typeof(reader.form[col]) == 'undefined') {
512             print (JSON.stringify(reader.form, null,4));
513             print("missing linked column " + col);
514             continue;
515         }
516         
517         var combofields_name = add.combofields[1].name;
518         var old =   reader.form[col];
519         reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
520         reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
521         reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
522         reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
523         reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
524         reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
525         reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
526         
527              
528     };
529     
530     
531 });
532
533 //readers.forEach(function(reader) {
534 //    delete reader.oreader;
535 //});
536
537  
538
539
540
541 //print(JSON.stringify(readers, null, 4));
542
543 readers.forEach(function(reader) {
544     
545
546     var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader'; 
547     if (!File.isDirectory(dir)) {
548         print("mkdir " + dir);
549         File.mkdir(dir);
550     }
551     
552     // READERS
553     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
554     
555                 
556     var jreader = {
557         '|xns' : 'Roo.data',
558         xtype : "JsonReader",
559         totalProperty : "total",
560         root : "data",
561         '*prop' : "reader",
562         id : 'id', // maybe no..
563        
564         '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
565     };
566     
567     File.write(
568         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
569         JSON.stringify(jreader, null, 4)
570     )
571     
572     
573     // GRIDS
574     dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel'; 
575     if (!File.isDirectory(dir)) {
576         print("mkdir " + dir);
577         File.mkdir(dir);
578     }
579     
580
581     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
582     
583     File.write(
584         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
585             
586        
587         JSON.stringify({
588             '|xns' : 'Roo',
589             xtype : "GridPanel",
590             "title": reader.table,
591             "fitToframe": true,
592             "fitContainer": true,
593             "tableName": reader.table,
594             "background": true,
595             "region" : 'center',
596             "listeners": {
597                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
598             },
599             "items": [
600                 {
601                     "*prop": "grid",
602                     "xtype": "Grid",
603                     "autoExpandColumn": reader.firstTxtCol,
604                     "loadMask": true,
605                     "listeners": {
606                         "|render": "function() \n" +
607                             "{\n" +
608                             "    _this.grid = this; \n" +
609                             "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
610                             "    if (_this.panel.active) {\n" +
611                             "       this.footer.onClick('first');\n" +
612                             "    }\n" +
613                             "}",
614                         "|rowdblclick": "function (_self, rowIndex, e)\n" + 
615                             "{\n" + 
616                             "    if (!_this.dialog) return;\n" + 
617                             "    _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" + 
618                             "        _this.grid.footer.onClick('first');\n" + 
619                             "    }); \n" + 
620                             "}\n"
621                     },
622                     "|xns": "Roo.grid",
623
624                     "items": [
625                         {
626                             "*prop": "dataSource",
627                             "xtype": "Store",
628                              remoteSort : true,
629                             '|sortInfo' : "{ field : '" + reader.firstTxtCol  +  "', direction: 'ASC' }", 
630                             "|xns": "Roo.data",
631                             "items": [
632                                 
633                                 {
634                                     "*prop": "proxy",
635                                     "xtype": "HttpProxy",
636                                     "method": "GET",
637                                     "|url": "baseURL + '/Roo/" + reader.table + ".php'",
638                                     "|xns": "Roo.data"
639                                 },
640                                 jreader
641                             ]
642                         },
643                         {
644                             "*prop": "footer",
645                             "xtype": "PagingToolbar",
646                             "pageSize": 25,
647                             "displayInfo": true,
648                             "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
649                             "emptyMsg": "No " + reader.table + " found",
650                             "|xns": "Roo"
651                         },
652                         {
653                             "*prop": "toolbar",
654                             "xtype": "Toolbar",
655                             "|xns": "Roo",
656                             "items": [
657                                 {
658                                     "text": "Add",
659                                     "xtype": "Button",
660                                     "cls": "x-btn-text-icon",
661                                     "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
662                                     "listeners": {
663                                         "|click": "function()\n"+
664                                             "{\n"+
665                                             "    if (!_this.dialog) return;\n" +
666                                             "    _this.dialog.show( { id : 0 } , function() {\n"+
667                                             "        _this.grid.footer.onClick('first');\n"+
668                                             "   }); \n"+
669                                             "}\n"
670                                     },
671                                     "|xns": "Roo.Toolbar"
672                                 },
673                                 {
674                                     "text": "Edit",
675                                     "xtype": "Button",
676                                     "cls": "x-btn-text-icon",
677                                     "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
678                                     "listeners": {
679                                         "|click": "function()\n"+
680                                             "{\n"+
681                                             "    var s = _this.grid.getSelectionModel().getSelections();\n"+
682                                             "    if (!s.length || (s.length > 1))  {\n"+
683                                             "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
684                                             "        return;\n"+
685                                             "    }\n"+
686                                             "    if (!_this.dialog) return;\n" +
687                                             "    _this.dialog.show(s[0].data, function() {\n"+
688                                             "        _this.grid.footer.onClick('first');\n"+
689                                             "    }); \n"+
690                                             "    \n"+
691                                             "}\n" 
692                                         
693                                     },
694                                     "|xns": "Roo.Toolbar"
695                                 },
696                                 {
697                                     "text": "Delete",
698                                     "cls": "x-btn-text-icon",
699                                     "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
700                                     "xtype": "Button",
701                                     "listeners": {
702                                         "|click": "function()\n"+
703                                             "{\n"+
704                                             "     Pman.genericDelete(_this, '" + reader.table + "'); \n"+
705                                             "}\n"+
706                                             "        "
707                                     },
708                                     "|xns": "Roo.Toolbar"
709                                 }
710                             ]
711                         }, // end toolbar
712                     ].concat( reader.colmodel)
713                 }
714             ]
715             
716             
717         }, null, 4)
718     )
719     
720     /// FORMS..
721     
722     dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form'; 
723     if (!File.isDirectory(dir)) {
724         print("mkdir " + dir);
725         File.mkdir(dir);
726     }
727     var formElements = [];
728     var formHeight = 50;
729     for (var k in reader.form) {
730         if (k == 'id') { // should really do primary key testing..
731             continue;
732         }
733         formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
734         
735         formElements.push(reader.form[k]);
736     }
737     if (reader.form['id']) {
738         formElements.push(reader.form['id']);
739     }
740     
741
742     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
743     var frmCfg = 
744     {
745         '|xns' : 'Roo.form',
746         xtype : "Form",
747         listeners : {
748             "|actioncomplete" : "function(_self,action)\n"+
749                 "{\n"+
750                 "    if (action.type == 'setdata') {\n"+
751                 "       //_this.dialog.el.mask(\"Loading\");\n"+
752                 "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
753                 "       return;\n"+
754                 "    }\n"+
755                 "    if (action.type == 'load') {\n"+
756                 "        _this.dialog.el.unmask();\n"+
757                 "        return;\n"+
758                 "    }\n"+
759                 "    if (action.type =='submit') {\n"+
760                 "    \n"+
761                 "        _this.dialog.el.unmask();\n"+
762                 "        _this.dialog.hide();\n"+
763                 "    \n"+
764                 "         if (_this.callback) {\n"+
765                 "            _this.callback.call(_this, _this.form.getValues());\n"+
766                 "         }\n"+
767                 "         _this.form.reset();\n"+
768                 "         return;\n"+
769                 "    }\n"+
770                 "}\n",
771             
772             "|rendered" : "function (form)\n"+
773                 "{\n"+
774                 "    _this.form= form;\n"+
775                 "}\n"
776         },
777         method : "POST",
778         style : "margin:10px;",
779         "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
780         items : formElements
781     };
782     
783     
784     File.write(
785         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
786             
787        
788         JSON.stringify( frmCfg, null, 4)
789     );
790             
791             
792    
793    
794    
795      /// COMBO..
796     
797     dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
798     if (!File.isDirectory(dir)) {
799         print("mkdir " + dir);
800         File.mkdir(dir);
801     }
802    
803     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
804     
805     File.write(
806         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
807             
808        
809         JSON.stringify(reader.combo, null, 4)
810     );
811             
812    
813    
814    
815    
816    
817     // DIALOG.
818    
819    
820     dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog'; 
821     if (!File.isDirectory(dir)) {
822         print("mkdir " + dir);
823         File.mkdir(dir);
824     }
825     var formElements = [];
826     for (var k in reader.form) {
827         if (k == 'id') { // should really do primary key testing..
828             continue;
829         }
830         formElements.push(reader.form[k]);
831     }
832     formElements.push(reader.form['id']);
833
834     print("WRITE: " +  dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
835     
836     File.write(
837         dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
838             
839        
840         JSON.stringify({
841        
842             "closable": false,
843             "collapsible": false,
844             "height": formHeight,
845             "resizable": false,
846             "title": "Edit / Create " + reader.table,
847             "width": 400,
848             "xtype": "LayoutDialog",
849             "|xns": "Roo",
850             "items": [
851                 {
852                     "|xns": "Roo",
853                     "xtype": "LayoutRegion",
854                     "*prop": "center"
855                 },
856                 {
857                     "region": "center",
858                     "xtype": "ContentPanel",
859                     "|xns": "Roo",
860                     "items": [
861                         frmCfg
862                     ]
863                 },
864                 
865                 {
866                     "listeners": {
867                         "click": "function (_self, e)\n{\n    _this.dialog.hide();\n}"
868                     },
869                     "*prop": "buttons[]",
870                     "text": "Cancel",
871                     "xtype": "Button",
872                     "|xns": "Roo"
873                 },
874                 {
875                     "listeners": {
876                         "click": "function (_self, e)\n{\n    // do some checks?\n     \n    \n    _this.dialog.el.mask(\"Saving\");\n    _this.form.doAction(\"submit\");\n\n}"
877                     },
878                     "*prop": "buttons[]",
879                     "text": "Save",
880                     "xtype": "Button",
881                     "|xns": "Roo"
882                 }
883             ]
884         }, null,4)
885     );
886    
887    
888    
889 });
890
891