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"));
168 if (File.isDirectory(cfg.INI)) {
171 //--- load ini files..
172 // this is very specific.
174 var dirs = File.list( cfg.INI + '/Pman').filter(
176 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
184 dirs.forEach(function(d) {
185 // this currently misses the web.*/Pman/XXXX/DataObjects..
186 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
188 if (!File.isDirectory(path)) {
191 var inis = File.list(path).filter(
192 function(e) { return e.match(/\.links\.ini$/); }
198 inis.forEach(function(i) {
199 readIni(path + '/' + i);
204 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
205 var inis = File.list(cfg.INI).filter(
206 function(e) { return e.match(/\.links\.ini$/); }
209 inis.forEach(function(i) {
210 readIni(path + '/' + i);
216 //print(JSON.stringify(ini, null,4));
222 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
228 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
230 tables.forEach(function(table) {
232 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
235 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
239 var firstTxtCol = '';
241 //print(JSON.stringify(schema, null,4));
243 schema.forEach(function(e) {
244 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
256 if (typeof(map[e.Type]) == 'undefined') {
259 name: "ArgumentError",
260 message: "Unknown mapping for type : " + e.Type
263 row.type = map[e.Type];
265 if (row.type == 'string' && !firstTxtCol.length) {
266 firstTxtCol = row.name;
269 if (row.type == 'date') {
270 row.dateFormat = 'Y-m-d';
274 if (combofields.length == 1 && row.type == 'string') {
275 combofields.push(row);
279 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
280 title = title[0].toUpperCase() + title.substring(1);
283 "xtype": "ColumnModel",
285 "width": row.type == 'string' ? 200 : 75,
286 "dataIndex": row.name,
287 "|renderer": row.type != 'date' ?
288 "function(v) { return String.format('{0}', v); }" :
289 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
291 "*prop": "colModel[]"
293 var xtype = 'TextField';
294 if (row.type == 'number') {
295 xtype = 'NumberField';
297 if (row.type == 'date') {
300 if (e.Type == 'text') {
303 if (row.name == 'id') {
306 // what about booleans.. -> checkboxes..
313 width : row.type == 'string' ? 200 : 75,
317 if (xtype == 'TextArea') {
318 form[row.name].height = 100;
320 if (xtype == 'Hidden') {
321 delete form[row.name].fieldLabel;
322 delete form[row.name].width;
330 allowBlank : 'false',
332 emptyText : 'Select ' + table,
333 forceSelection : true,
335 loadingText: 'Searching...',
338 qtip: 'Select ' + table,
340 triggerAction : 'all',
347 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
348 queryParam : '',// SET WHEN USED
349 fieldLabel : table, // SET WHEN USED
351 displayField : '', // SET WHEN USED eg. project_id_name
352 hiddenName : '', // SET WHEN USED eg. project_id
353 name : '', // SET WHEN USED eg. project_id_name
361 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
363 '|beforeload' : 'function (_self, o)' +
365 " o.params = o.params || {};\n" +
366 " // set more here\n" +
372 'xtype' : 'HttpProxy',
375 '|url' : "baseURL + '/Roo/" + table + ".php'",
380 'xtype' : 'JsonReader',
384 'totalProperty' : 'total',
385 '|fields' : JSON.stringify(combofields)
396 //print(JSON.stringify(reader,null,4));
400 combofields : combofields,
402 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
404 firstTxtCol : firstTxtCol,
408 //console.dump(schema );
415 // merge in the linked tables..
416 readers.forEach(function(reader) {
417 if (typeof(ini[reader.table]) == 'undefined') {
421 print("OVERLAY - " + reader.table);
423 for (var col in ini[reader.table]) {
424 var kv = ini[reader.table][col].split(':');
427 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
431 // merge in data (eg. project_id => project_id_*****
433 add.oreader.forEach(function(or) {
435 name : col + '_' + or.name,
440 // col is mapped to something..
441 var combofields = add.combofields;
442 if (add.combofields.length < 2) {
445 if (typeof(reader.form[col]) == 'undefined') {
446 print (JSON.stringify(reader.form, null,4));
447 print("missing linked column " + col);
451 var combofields_name = add.combofields[1].name;
452 var old = reader.form[col];
453 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
454 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
455 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
456 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
457 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
458 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
459 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
467 //readers.forEach(function(reader) {
468 // delete reader.oreader;
475 //print(JSON.stringify(readers, null, 4));
477 readers.forEach(function(reader) {
480 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
481 if (!File.isDirectory(dir)) {
482 print("mkdir " + dir);
487 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
492 xtype : "JsonReader",
493 totalProperty : "total",
496 id : 'id', // maybe no..
498 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
502 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
503 JSON.stringify(jreader, null, 4)
508 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
509 if (!File.isDirectory(dir)) {
510 print("mkdir " + dir);
515 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
518 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
524 "title": reader.table,
526 "fitContainer": true,
527 "tableName": reader.table,
531 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
537 "autoExpandColumn": reader.firstTxtCol,
540 "|render": "function() \n" +
542 " _this.grid = this; \n" +
543 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
544 " if (_this.panel.active) {\n" +
545 " this.footer.onClick('first');\n" +
548 "|rowdblclick": "function (_self, rowIndex, e)\n" +
550 " if (!_this.dialog) return;\n" +
551 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
552 " _this.grid.footer.onClick('first');\n" +
560 "*prop": "dataSource",
563 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
569 "xtype": "HttpProxy",
571 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
579 "xtype": "PagingToolbar",
582 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
583 "emptyMsg": "No " + reader.table + " found",
594 "cls": "x-btn-text-icon",
595 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
597 "|click": "function()\n"+
599 " if (!_this.dialog) return;\n" +
600 " _this.dialog.show( { id : 0 } , function() {\n"+
601 " _this.grid.footer.onClick('first');\n"+
605 "|xns": "Roo.Toolbar"
610 "cls": "x-btn-text-icon",
611 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
613 "|click": "function()\n"+
615 " var s = _this.grid.getSelectionModel().getSelections();\n"+
616 " if (!s.length || (s.length > 1)) {\n"+
617 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
620 " if (!_this.dialog) return;\n" +
621 " _this.dialog.show(s[0].data, function() {\n"+
622 " _this.grid.footer.onClick('first');\n"+
628 "|xns": "Roo.Toolbar"
632 "cls": "x-btn-text-icon",
633 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
636 "|click": "function()\n"+
638 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
642 "|xns": "Roo.Toolbar"
646 ].concat( reader.colmodel)
656 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
657 if (!File.isDirectory(dir)) {
658 print("mkdir " + dir);
661 var formElements = [];
663 for (var k in reader.form) {
664 if (k == 'id') { // should really do primary key testing..
667 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
669 formElements.push(reader.form[k]);
671 if (reader.form['id']) {
672 formElements.push(reader.form['id']);
676 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
682 "|actioncomplete" : "function(_self,action)\n"+
684 " if (action.type == 'setdata') {\n"+
685 " //_this.dialog.el.mask(\"Loading\");\n"+
686 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
689 " if (action.type == 'load') {\n"+
690 " _this.dialog.el.unmask();\n"+
693 " if (action.type =='submit') {\n"+
695 " _this.dialog.el.unmask();\n"+
696 " _this.dialog.hide();\n"+
698 " if (_this.callback) {\n"+
699 " _this.callback.call(_this, _this.form.getValues());\n"+
701 " _this.form.reset();\n"+
706 "|rendered" : "function (form)\n"+
708 " _this.form= form;\n"+
712 style : "margin:10px;",
713 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
719 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
722 JSON.stringify( frmCfg, null, 4)
731 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
732 if (!File.isDirectory(dir)) {
733 print("mkdir " + dir);
737 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
740 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
743 JSON.stringify(reader.combo, null, 4)
754 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
755 if (!File.isDirectory(dir)) {
756 print("mkdir " + dir);
759 var formElements = [];
760 for (var k in reader.form) {
761 if (k == 'id') { // should really do primary key testing..
764 formElements.push(reader.form[k]);
766 formElements.push(reader.form['id']);
768 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
771 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
777 "collapsible": false,
778 "height": formHeight,
780 "title": "Edit / Create " + reader.table,
782 "xtype": "LayoutDialog",
787 "xtype": "LayoutRegion",
792 "xtype": "ContentPanel",
801 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
803 "*prop": "buttons[]",
810 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
812 "*prop": "buttons[]",