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 : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
56 { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
57 { arg_long : 'PASSWORD' , arg_short : 'p', description : '' , arg_default :'' },
58 { arg_long : 'INI' , arg_short : 'I', description : 'Either base directory which has Pman/***/DataObjects/***.ini or location of ini file.' },
65 var cfg = o.parse(Seed.argv);
66 print(JSON.stringify(cfg, null,4));
68 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DBNAME,
69 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
70 Gda.ConnectionOptions.NONE, null);
77 Gda.DataSelect.prototype.fetchAll = function()
81 for (var i =0;i < this.get_n_columns(); i++) {
82 cols.push(this.get_column_name(i));
84 //print(JSON.stringify(cols, null,4));
85 var iter = this.create_iter();
87 //print(this.get_n_rows());
89 for (var r = 0; r < this.get_n_rows(); r++) {
93 if (cols.length == 1) {
94 res.push(this.get_value_at(0,r).get_string());
99 cols.forEach(function(n,i) {
100 var val = _this.get_value_at(i,r);
101 var type = GObject.type_name(val.g_type) ;
102 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
103 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
104 //print (n + '=' + iter.get_value_at(i).value);
118 'time' : 'string', //bogus
127 'varchar' : 'string',
129 'longtext' : 'string',
130 'tinytext' : 'string',
131 'mediumtext' : 'string',
133 'timestamp' : 'number',
142 print('Read INI : ' + fn);
143 var key_file = new GLib.KeyFile.c_new();
144 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
148 var groups = key_file.get_groups();
149 groups.forEach(function(g) {
152 var keys = key_file.get_keys(g);
153 if (!keys) { return; }
154 keys.forEach(function(k) {
155 ini[g][k] = key_file.get_value(g,k);
160 if (File.isFile(cfg.INI)) {
161 if (cfg.INI.match(/links\.ini$/)) {
164 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
169 if (File.isDirectory(cfg.INI)) {
172 //--- load ini files..
173 // this is very specific.
175 var dirs = File.list( cfg.INI + '/Pman').filter(
177 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
185 dirs.forEach(function(d) {
186 // this currently misses the web.*/Pman/XXXX/DataObjects..
187 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
189 if (!File.isDirectory(path)) {
192 var inis = File.list(path).filter(
193 function(e) { return e.match(/\.links\.ini$/); }
199 inis.forEach(function(i) {
200 readIni(path + '/' + i);
205 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
206 var inis = File.list(cfg.INI).filter(
207 function(e) { return e.match(/\.links\.ini$/); }
210 inis.forEach(function(i) {
211 readIni(path + '/' + i);
217 //print(JSON.stringify(ini, null,4));
223 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
229 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
231 tables.forEach(function(table) {
233 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
236 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
240 var firstTxtCol = '';
242 //print(JSON.stringify(schema, null,4));
244 schema.forEach(function(e) {
245 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
257 if (typeof(map[e.Type]) == 'undefined') {
260 name: "ArgumentError",
261 message: "Unknown mapping for type : " + e.Type
264 row.type = map[e.Type];
266 if (row.type == 'string' && !firstTxtCol.length) {
267 firstTxtCol = row.name;
270 if (row.type == 'date') {
271 row.dateFormat = 'Y-m-d';
275 if (combofields.length == 1 && row.type == 'string') {
276 combofields.push(row);
280 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
281 title = title[0].toUpperCase() + title.substring(1);
284 "xtype": "ColumnModel",
286 "width": row.type == 'string' ? 200 : 75,
287 "dataIndex": row.name,
288 "|renderer": row.type != 'date' ?
289 "function(v) { return String.format('{0}', v); }" :
290 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
292 "*prop": "colModel[]"
294 var xtype = 'TextField';
295 if (row.type == 'number') {
296 xtype = 'NumberField';
298 if (row.type == 'date') {
301 if (e.Type == 'text') {
304 if (row.name == 'id') {
307 // what about booleans.. -> checkboxes..
314 width : row.type == 'string' ? 200 : 75,
318 if (xtype == 'TextArea') {
319 form[row.name].height = 100;
321 if (xtype == 'Hidden') {
322 delete form[row.name].fieldLabel;
323 delete form[row.name].width;
331 allowBlank : 'false',
333 emptyText : 'Select ' + table,
334 forceSelection : true,
336 loadingText: 'Searching...',
339 qtip: 'Select ' + table,
341 triggerAction : 'all',
348 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
349 queryParam : '',// SET WHEN USED
350 fieldLabel : table, // SET WHEN USED
352 displayField : '', // SET WHEN USED eg. project_id_name
353 hiddenName : '', // SET WHEN USED eg. project_id
354 name : '', // SET WHEN USED eg. project_id_name
362 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
364 '|beforeload' : 'function (_self, o)' +
366 " o.params = o.params || {};\n" +
367 " // set more here\n" +
373 'xtype' : 'HttpProxy',
376 '|url' : "baseURL + '/Roo/" + table + ".php'",
381 'xtype' : 'JsonReader',
385 'totalProperty' : 'total',
386 '|fields' : JSON.stringify(combofields)
397 //print(JSON.stringify(reader,null,4));
401 combofields : combofields,
403 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
405 firstTxtCol : firstTxtCol,
409 //console.dump(schema );
416 // merge in the linked tables..
417 readers.forEach(function(reader) {
418 if (typeof(ini[reader.table]) == 'undefined') {
422 print("OVERLAY - " + reader.table);
424 for (var col in ini[reader.table]) {
425 var kv = ini[reader.table][col].split(':');
428 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
432 // merge in data (eg. project_id => project_id_*****
434 add.oreader.forEach(function(or) {
436 name : col + '_' + or.name,
441 // col is mapped to something..
442 var combofields = add.combofields;
443 if (add.combofields.length < 2) {
446 if (typeof(reader.form[col]) == 'undefined') {
447 print (JSON.stringify(reader.form, null,4));
448 print("missing linked column " + col);
452 var combofields_name = add.combofields[1].name;
453 var old = reader.form[col];
454 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
455 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
456 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
457 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
458 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
459 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
460 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
468 //readers.forEach(function(reader) {
469 // delete reader.oreader;
476 //print(JSON.stringify(readers, null, 4));
478 readers.forEach(function(reader) {
481 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
482 if (!File.isDirectory(dir)) {
483 print("mkdir " + dir);
488 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
493 xtype : "JsonReader",
494 totalProperty : "total",
497 id : 'id', // maybe no..
499 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
503 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
504 JSON.stringify(jreader, null, 4)
509 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
510 if (!File.isDirectory(dir)) {
511 print("mkdir " + dir);
516 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
519 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
525 "title": reader.table,
527 "fitContainer": true,
528 "tableName": reader.table,
532 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
538 "autoExpandColumn": reader.firstTxtCol,
541 "|render": "function() \n" +
543 " _this.grid = this; \n" +
544 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
545 " if (_this.panel.active) {\n" +
546 " this.footer.onClick('first');\n" +
549 "|rowdblclick": "function (_self, rowIndex, e)\n" +
551 " if (!_this.dialog) return;\n" +
552 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
553 " _this.grid.footer.onClick('first');\n" +
561 "*prop": "dataSource",
564 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
570 "xtype": "HttpProxy",
572 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
580 "xtype": "PagingToolbar",
583 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
584 "emptyMsg": "No " + reader.table + " found",
595 "cls": "x-btn-text-icon",
596 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
598 "|click": "function()\n"+
600 " if (!_this.dialog) return;\n" +
601 " _this.dialog.show( { id : 0 } , function() {\n"+
602 " _this.grid.footer.onClick('first');\n"+
606 "|xns": "Roo.Toolbar"
611 "cls": "x-btn-text-icon",
612 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
614 "|click": "function()\n"+
616 " var s = _this.grid.getSelectionModel().getSelections();\n"+
617 " if (!s.length || (s.length > 1)) {\n"+
618 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
621 " if (!_this.dialog) return;\n" +
622 " _this.dialog.show(s[0].data, function() {\n"+
623 " _this.grid.footer.onClick('first');\n"+
629 "|xns": "Roo.Toolbar"
633 "cls": "x-btn-text-icon",
634 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
637 "|click": "function()\n"+
639 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
643 "|xns": "Roo.Toolbar"
647 ].concat( reader.colmodel)
657 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
658 if (!File.isDirectory(dir)) {
659 print("mkdir " + dir);
662 var formElements = [];
664 for (var k in reader.form) {
665 if (k == 'id') { // should really do primary key testing..
668 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
670 formElements.push(reader.form[k]);
672 if (reader.form['id']) {
673 formElements.push(reader.form['id']);
677 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
683 "|actioncomplete" : "function(_self,action)\n"+
685 " if (action.type == 'setdata') {\n"+
686 " //_this.dialog.el.mask(\"Loading\");\n"+
687 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
690 " if (action.type == 'load') {\n"+
691 " _this.dialog.el.unmask();\n"+
694 " if (action.type =='submit') {\n"+
696 " _this.dialog.el.unmask();\n"+
697 " _this.dialog.hide();\n"+
699 " if (_this.callback) {\n"+
700 " _this.callback.call(_this, _this.form.getValues());\n"+
702 " _this.form.reset();\n"+
707 "|rendered" : "function (form)\n"+
709 " _this.form= form;\n"+
713 style : "margin:10px;",
714 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
720 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
723 JSON.stringify( frmCfg, null, 4)
732 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
733 if (!File.isDirectory(dir)) {
734 print("mkdir " + dir);
738 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
741 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
744 JSON.stringify(reader.combo, null, 4)
755 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
756 if (!File.isDirectory(dir)) {
757 print("mkdir " + dir);
760 var formElements = [];
761 for (var k in reader.form) {
762 if (k == 'id') { // should really do primary key testing..
765 formElements.push(reader.form[k]);
767 formElements.push(reader.form['id']);
769 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
772 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
778 "collapsible": false,
779 "height": formHeight,
781 "title": "Edit / Create " + reader.table,
783 "xtype": "LayoutDialog",
788 "xtype": "LayoutRegion",
793 "xtype": "ContentPanel",
802 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
804 "*prop": "buttons[]",
811 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
813 "*prop": "buttons[]",