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
20 GObject = imports.gi.GObject;
22 GLib = imports.gi.GLib;
24 console = imports.console;
25 File = imports.File.File;
26 Options = imports.Options.Options;
30 var prov = Gda.Config.list_providers ();
31 //print(prov.dump_as_string());
34 help_description : 'Element builder for App Builder based on database schema',
37 { arg_long : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
38 { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
39 { arg_long : 'PASSWORD' , arg_short : 'p', description : '' , arg_default :'' },
40 { arg_long : 'INI' , arg_short : 'I', description : 'Either base directory which has Pman/***/DataObjects/***.ini or location of ini file.' },
47 var cfg = o.parse(Seed.argv);
48 print(JSON.stringify(cfg, null,4));
50 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DBNAME,
51 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
52 Gda.ConnectionOptions.NONE, null);
59 Gda.DataSelect.prototype.fetchAll = function()
63 for (var i =0;i < this.get_n_columns(); i++) {
64 cols.push(this.get_column_name(i));
66 //print(JSON.stringify(cols, null,4));
67 var iter = this.create_iter();
69 //print(this.get_n_rows());
71 for (var r = 0; r < this.get_n_rows(); r++) {
75 if (cols.length == 1) {
76 res.push(this.get_value_at(0,r).get_string());
81 cols.forEach(function(n,i) {
82 var val = _this.get_value_at(i,r);
83 var type = GObject.type_name(val.g_type) ;
84 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
85 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
86 //print (n + '=' + iter.get_value_at(i).value);
100 'time' : 'string', //bogus
108 'varchar' : 'string',
110 'longtext' : 'string',
111 'tinytext' : 'string',
112 'mediumtext' : 'string',
114 'timestamp' : 'number',
123 print('Read INI : ' + fn);
124 var key_file = new GLib.KeyFile.c_new();
125 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
129 var groups = key_file.get_groups();
130 groups.forEach(function(g) {
133 var keys = key_file.get_keys(g);
134 keys.forEach(function(k) {
135 ini[g][k] = key_file.get_value(g,k);
140 if (File.isFile(cfg.INI)) {
141 if (cfg.INI.match(/links\.ini$/)) {
144 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
148 if (File.isDirectory(cfg.INI)) {
151 //--- load ini files..
152 // this is very specific.
154 var dirs = File.list( cfg.INI + '/Pman').filter(
156 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
164 dirs.forEach(function(d) {
165 // this currently misses the web.*/Pman/XXXX/DataObjects..
166 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
168 if (!File.isDirectory(path)) {
171 var inis = File.list(path).filter(
172 function(e) { return e.match(/\.links\.ini$/); }
178 inis.forEach(function(i) {
179 readIni(path + '/' + i);
184 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
185 var inis = File.list(cfg.INI).filter(
186 function(e) { return e.match(/\.links\.ini$/); }
189 inis.forEach(function(i) {
190 readIni(path + '/' + i);
196 //print(JSON.stringify(ini, null,4));
202 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
208 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
210 tables.forEach(function(table) {
212 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
215 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
219 var firstTxtCol = '';
221 //print(JSON.stringify(schema, null,4));
223 schema.forEach(function(e) {
224 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
236 if (typeof(map[e.Type]) == 'undefined') {
239 name: "ArgumentError",
240 message: "Unknown mapping for type : " + e.Type
243 row.type = map[e.Type];
245 if (row.type == 'string' && !firstTxtCol.length) {
246 firstTxtCol = row.name;
249 if (row.type == 'date') {
250 row.dateFormat = 'Y-m-d';
254 if (combofields.length == 1 && row.type == 'string') {
255 combofields.push(row);
259 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
260 title = title[0].toUpperCase() + title.substring(1);
263 "xtype": "ColumnModel",
265 "width": row.type == 'string' ? 200 : 75,
266 "dataIndex": row.name,
267 "|renderer": row.type != 'date' ?
268 "function(v) { return String.format('{0}', v); }" :
269 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
271 "*prop": "colModel[]"
273 var xtype = 'TextField';
274 if (row.type == 'number') {
275 xtype = 'NumberField';
277 if (row.type == 'date') {
280 if (e.Type == 'text') {
283 if (row.name == 'id') {
286 // what about booleans.. -> checkboxes..
293 width : row.type == 'string' ? 200 : 75,
297 if (xtype == 'TextArea') {
298 form[row.name].height = 100;
300 if (xtype == 'Hidden') {
301 delete form[row.name].fieldLabel;
302 delete form[row.name].width;
310 allowBlank : 'false',
312 emptyText : 'Select ' + table,
313 forceSelection : true,
315 loadingText: 'Searching...',
318 qtip: 'Select ' + table,
320 triggerAction : 'all',
327 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
328 queryParam : '',// SET WHEN USED
329 fieldLabel : table, // SET WHEN USED
331 displayField : '', // SET WHEN USED eg. project_id_name
332 hiddenName : '', // SET WHEN USED eg. project_id
333 name : '', // SET WHEN USED eg. project_id_name
341 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
343 '|beforeload' : 'function (_self, o)' +
345 " o.params = o.params || {};\n" +
346 " // set more here\n" +
352 'xtype' : 'HttpProxy',
355 '|url' : "baseURL + '/Roo/" + table + ".php'",
360 'xtype' : 'JsonReader',
364 'totalProperty' : 'total',
365 '|fields' : JSON.stringify(combofields)
376 //print(JSON.stringify(reader,null,4));
380 combofields : combofields,
382 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
384 firstTxtCol : firstTxtCol,
388 //console.dump(schema );
395 // merge in the linked tables..
396 readers.forEach(function(reader) {
397 if (typeof(ini[reader.table]) == 'undefined') {
401 print("OVERLAY - " + reader.table);
403 for (var col in ini[reader.table]) {
404 var kv = ini[reader.table][col].split(':');
407 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
411 // merge in data (eg. project_id => project_id_*****
413 add.oreader.forEach(function(or) {
415 name : col + '_' + or.name,
420 // col is mapped to something..
421 var combofields = add.combofields;
422 if (add.combofields.length < 2) {
425 if (typeof(reader.form[col]) == 'undefined') {
426 print (JSON.stringify(reader.form, null,4));
427 print("missing linked column " + col);
431 var combofields_name = add.combofields[1].name;
432 var old = reader.form[col];
433 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
434 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
435 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
436 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
437 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
438 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
439 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
447 //readers.forEach(function(reader) {
448 // delete reader.oreader;
455 //print(JSON.stringify(readers, null, 4));
457 readers.forEach(function(reader) {
460 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
461 if (!File.isDirectory(dir)) {
466 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
471 xtype : "JsonReader",
472 totalProperty : "total",
475 id : 'id', // maybe no..
477 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
481 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
482 JSON.stringify(jreader, null, 4)
487 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
488 if (!File.isDirectory(dir)) {
493 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
496 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
502 "title": reader.table,
504 "fitContainer": true,
505 "tableName": reader.table,
509 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
515 "autoExpandColumn": reader.firstTxtCol,
518 "|render": "function() \n" +
520 " _this.grid = this; \n" +
521 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
522 " if (_this.panel.active) {\n" +
523 " this.footer.onClick('first');\n" +
526 "|rowdblclick": "function (_self, rowIndex, e)\n" +
528 " if (!_this.dialog) return;\n" +
529 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
530 " _this.grid.footer.onClick('first');\n" +
538 "*prop": "dataSource",
541 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
547 "xtype": "HttpProxy",
549 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
557 "xtype": "PagingToolbar",
560 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
561 "emptyMsg": "No " + reader.table + " found",
572 "cls": "x-btn-text-icon",
573 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
575 "|click": "function()\n"+
577 " if (!_this.dialog) return;\n" +
578 " _this.dialog.show( { id : 0 } , function() {\n"+
579 " _this.grid.footer.onClick('first');\n"+
583 "|xns": "Roo.Toolbar"
588 "cls": "x-btn-text-icon",
589 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
591 "|click": "function()\n"+
593 " var s = _this.grid.getSelectionModel().getSelections();\n"+
594 " if (!s.length || (s.length > 1)) {\n"+
595 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
598 " if (!_this.dialog) return;\n" +
599 " _this.dialog.show(s[0].data, function() {\n"+
600 " _this.grid.footer.onClick('first');\n"+
606 "|xns": "Roo.Toolbar"
610 "cls": "x-btn-text-icon",
611 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
614 "|click": "function()\n"+
616 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
620 "|xns": "Roo.Toolbar"
624 ].concat( reader.colmodel)
634 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
635 if (!File.isDirectory(dir)) {
638 var formElements = [];
640 for (var k in reader.form) {
641 if (k == 'id') { // should really do primary key testing..
644 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
646 formElements.push(reader.form[k]);
648 if (reader.form['id']) {
649 formElements.push(reader.form['id']);
653 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
659 "|actioncomplete" : "function(_self,action)\n"+
661 " if (action.type == 'setdata') {\n"+
662 " //_this.dialog.el.mask(\"Loading\");\n"+
663 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
666 " if (action.type == 'load') {\n"+
667 " _this.dialog.el.unmask();\n"+
670 " if (action.type =='submit') {\n"+
672 " _this.dialog.el.unmask();\n"+
673 " _this.dialog.hide();\n"+
675 " if (_this.callback) {\n"+
676 " _this.callback.call(_this, _this.form.getValues());\n"+
678 " _this.form.reset();\n"+
683 "|rendered" : "function (form)\n"+
685 " _this.form= form;\n"+
689 style : "margin:10px;",
690 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
696 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
699 JSON.stringify( frmCfg, null, 4)
708 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
709 if (!File.isDirectory(dir)) {
713 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
716 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
719 JSON.stringify(reader.combo, null, 4)
730 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
731 if (!File.isDirectory(dir)) {
734 var formElements = [];
735 for (var k in reader.form) {
736 if (k == 'id') { // should really do primary key testing..
739 formElements.push(reader.form[k]);
741 formElements.push(reader.form['id']);
743 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
746 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
752 "collapsible": false,
753 "height": formHeight,
755 "title": "Edit / Create " + reader.table,
757 "xtype": "LayoutDialog",
762 "xtype": "LayoutRegion",
767 "xtype": "ContentPanel",
776 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
778 "*prop": "buttons[]",
785 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
787 "*prop": "buttons[]",