1 //<script type="text/javascript">
4 * This is a hacky generator to generate element definitions for the Roo version from the database
6 * Let's see if libgda can be used to generate our Readers for roo...
8 * Concept - conect to database..
16 * usage: seed generate.js
21 *Hack needed to latest GLib-2.0.gir
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*"/>
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*
38 GObject = imports.gi.GObject;
40 GLib = imports.gi.GLib;
42 console = imports.console;
43 File = imports.File.File;
44 Options = imports.Options.Options;
48 var prov = Gda.Config.list_providers ();
49 //print(prov.dump_as_string());
52 help_description : 'Element builder for App Builder based on database schema',
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 : 'Either base directory which has Pman/***/DataObjects/***.ini or location of ini file.' },
66 var cfg = o.parse(Seed.argv);
67 print(JSON.stringify(cfg, null,4));
69 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DBNAME,
70 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
71 Gda.ConnectionOptions.NONE, null);
78 Gda.DataSelect.prototype.fetchAll = function()
82 for (var i =0;i < this.get_n_columns(); i++) {
83 cols.push(this.get_column_name(i));
85 //print(JSON.stringify(cols, null,4));
86 var iter = this.create_iter();
88 //print(this.get_n_rows());
90 for (var r = 0; r < this.get_n_rows(); r++) {
94 if (cols.length == 1) {
95 res.push(this.get_value_at(0,r).get_string());
100 cols.forEach(function(n,i) {
101 var val = _this.get_value_at(i,r);
102 var type = GObject.type_name(val.g_type) ;
103 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
104 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
105 //print (n + '=' + iter.get_value_at(i).value);
119 'time' : 'string', //bogus
128 'varchar' : 'string',
130 'longtext' : 'string',
131 'tinytext' : 'string',
132 'mediumtext' : 'string',
134 'timestamp' : 'number',
143 print('Read INI : ' + fn);
144 var key_file = new GLib.KeyFile.c_new();
145 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
149 var groups = key_file.get_groups();
150 groups.forEach(function(g) {
153 var keys = key_file.get_keys(g);
154 if (!keys) { return; }
155 keys.forEach(function(k) {
156 ini[g][k] = key_file.get_value(g,k);
161 if (File.isFile(cfg.INI)) {
162 if (cfg.INI.match(/links\.ini$/)) {
165 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
170 if (File.isDirectory(cfg.INI)) {
173 //--- load ini files..
174 // this is very specific.
176 var dirs = File.list( cfg.INI + '/Pman').filter(
178 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
186 dirs.forEach(function(d) {
187 // this currently misses the web.*/Pman/XXXX/DataObjects..
188 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
190 if (!File.isDirectory(path)) {
193 var inis = File.list(path).filter(
194 function(e) { return e.match(/\.links\.ini$/); }
200 inis.forEach(function(i) {
201 readIni(path + '/' + i);
206 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
207 var inis = File.list(cfg.INI).filter(
208 function(e) { return e.match(/\.links\.ini$/); }
211 inis.forEach(function(i) {
212 readIni(path + '/' + i);
218 //print(JSON.stringify(ini, null,4));
224 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
230 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
232 tables.forEach(function(table) {
234 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
237 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
241 var firstTxtCol = '';
243 //print(JSON.stringify(schema, null,4));
245 schema.forEach(function(e) {
246 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
258 if (typeof(map[e.Type]) == 'undefined') {
261 name: "ArgumentError",
262 message: "Unknown mapping for type : " + e.Type
265 row.type = map[e.Type];
267 if (row.type == 'string' && !firstTxtCol.length) {
268 firstTxtCol = row.name;
271 if (row.type == 'date') {
272 row.dateFormat = 'Y-m-d';
276 if (combofields.length == 1 && row.type == 'string') {
277 combofields.push(row);
281 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
282 title = title[0].toUpperCase() + title.substring(1);
285 "xtype": "ColumnModel",
287 "width": row.type == 'string' ? 200 : 75,
288 "dataIndex": row.name,
289 "|renderer": row.type != 'date' ?
290 "function(v) { return String.format('{0}', v); }" :
291 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
293 "*prop": "colModel[]"
295 var xtype = 'TextField';
296 if (row.type == 'number') {
297 xtype = 'NumberField';
299 if (row.type == 'date') {
302 if (e.Type == 'text') {
305 if (row.name == 'id') {
308 // what about booleans.. -> checkboxes..
315 width : row.type == 'string' ? 200 : 75,
319 if (xtype == 'TextArea') {
320 form[row.name].height = 100;
322 if (xtype == 'Hidden') {
323 delete form[row.name].fieldLabel;
324 delete form[row.name].width;
332 allowBlank : 'false',
334 emptyText : 'Select ' + table,
335 forceSelection : true,
337 loadingText: 'Searching...',
340 qtip: 'Select ' + table,
342 triggerAction : 'all',
349 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
350 queryParam : '',// SET WHEN USED
351 fieldLabel : table, // SET WHEN USED
353 displayField : '', // SET WHEN USED eg. project_id_name
354 hiddenName : '', // SET WHEN USED eg. project_id
355 name : '', // SET WHEN USED eg. project_id_name
363 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
365 '|beforeload' : 'function (_self, o)' +
367 " o.params = o.params || {};\n" +
368 " // set more here\n" +
374 'xtype' : 'HttpProxy',
377 '|url' : "baseURL + '/Roo/" + table + ".php'",
382 'xtype' : 'JsonReader',
386 'totalProperty' : 'total',
387 '|fields' : JSON.stringify(combofields)
398 //print(JSON.stringify(reader,null,4));
402 combofields : combofields,
404 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
406 firstTxtCol : firstTxtCol,
410 //console.dump(schema );
417 // merge in the linked tables..
418 readers.forEach(function(reader) {
419 if (typeof(ini[reader.table]) == 'undefined') {
423 print("OVERLAY - " + reader.table);
425 for (var col in ini[reader.table]) {
426 var kv = ini[reader.table][col].split(':');
429 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
433 // merge in data (eg. project_id => project_id_*****
435 add.oreader.forEach(function(or) {
437 name : col + '_' + or.name,
442 // col is mapped to something..
443 var combofields = add.combofields;
444 if (add.combofields.length < 2) {
447 if (typeof(reader.form[col]) == 'undefined') {
448 print (JSON.stringify(reader.form, null,4));
449 print("missing linked column " + col);
453 var combofields_name = add.combofields[1].name;
454 var old = reader.form[col];
455 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
456 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
457 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
458 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
459 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
460 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
461 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
469 //readers.forEach(function(reader) {
470 // delete reader.oreader;
477 //print(JSON.stringify(readers, null, 4));
479 readers.forEach(function(reader) {
482 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
483 if (!File.isDirectory(dir)) {
484 print("mkdir " + dir);
489 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
494 xtype : "JsonReader",
495 totalProperty : "total",
498 id : 'id', // maybe no..
500 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
504 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
505 JSON.stringify(jreader, null, 4)
510 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
511 if (!File.isDirectory(dir)) {
512 print("mkdir " + dir);
517 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
520 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
526 "title": reader.table,
528 "fitContainer": true,
529 "tableName": reader.table,
533 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
539 "autoExpandColumn": reader.firstTxtCol,
542 "|render": "function() \n" +
544 " _this.grid = this; \n" +
545 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
546 " if (_this.panel.active) {\n" +
547 " this.footer.onClick('first');\n" +
550 "|rowdblclick": "function (_self, rowIndex, e)\n" +
552 " if (!_this.dialog) return;\n" +
553 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
554 " _this.grid.footer.onClick('first');\n" +
562 "*prop": "dataSource",
565 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
571 "xtype": "HttpProxy",
573 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
581 "xtype": "PagingToolbar",
584 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
585 "emptyMsg": "No " + reader.table + " found",
596 "cls": "x-btn-text-icon",
597 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
599 "|click": "function()\n"+
601 " if (!_this.dialog) return;\n" +
602 " _this.dialog.show( { id : 0 } , function() {\n"+
603 " _this.grid.footer.onClick('first');\n"+
607 "|xns": "Roo.Toolbar"
612 "cls": "x-btn-text-icon",
613 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
615 "|click": "function()\n"+
617 " var s = _this.grid.getSelectionModel().getSelections();\n"+
618 " if (!s.length || (s.length > 1)) {\n"+
619 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
622 " if (!_this.dialog) return;\n" +
623 " _this.dialog.show(s[0].data, function() {\n"+
624 " _this.grid.footer.onClick('first');\n"+
630 "|xns": "Roo.Toolbar"
634 "cls": "x-btn-text-icon",
635 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
638 "|click": "function()\n"+
640 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
644 "|xns": "Roo.Toolbar"
648 ].concat( reader.colmodel)
658 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
659 if (!File.isDirectory(dir)) {
660 print("mkdir " + dir);
663 var formElements = [];
665 for (var k in reader.form) {
666 if (k == 'id') { // should really do primary key testing..
669 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
671 formElements.push(reader.form[k]);
673 if (reader.form['id']) {
674 formElements.push(reader.form['id']);
678 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
684 "|actioncomplete" : "function(_self,action)\n"+
686 " if (action.type == 'setdata') {\n"+
687 " //_this.dialog.el.mask(\"Loading\");\n"+
688 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
691 " if (action.type == 'load') {\n"+
692 " _this.dialog.el.unmask();\n"+
695 " if (action.type =='submit') {\n"+
697 " _this.dialog.el.unmask();\n"+
698 " _this.dialog.hide();\n"+
700 " if (_this.callback) {\n"+
701 " _this.callback.call(_this, _this.form.getValues());\n"+
703 " _this.form.reset();\n"+
708 "|rendered" : "function (form)\n"+
710 " _this.form= form;\n"+
714 style : "margin:10px;",
715 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
721 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
724 JSON.stringify( frmCfg, null, 4)
733 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
734 if (!File.isDirectory(dir)) {
735 print("mkdir " + dir);
739 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
742 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
745 JSON.stringify(reader.combo, null, 4)
756 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
757 if (!File.isDirectory(dir)) {
758 print("mkdir " + dir);
761 var formElements = [];
762 for (var k in reader.form) {
763 if (k == 'id') { // should really do primary key testing..
766 formElements.push(reader.form[k]);
768 formElements.push(reader.form['id']);
770 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
773 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
779 "collapsible": false,
780 "height": formHeight,
782 "title": "Edit / Create " + reader.table,
784 "xtype": "LayoutDialog",
789 "xtype": "LayoutRegion",
794 "xtype": "ContentPanel",
803 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
805 "*prop": "buttons[]",
812 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
814 "*prop": "buttons[]",