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