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 var key_file = new GLib.KeyFile();
126 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
130 var groups = key_file.get_groups();
131 groups.forEach(function(g) {
134 var keys = key_file.get_keys(g);
135 keys.forEach(function(k) {
136 ini[g][k] = key_file.get_value(g,k);
141 if (File.isFile(cfg.INI)) {
142 if (cfg.INI.match(/links\.ini$/)) {
145 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
149 if (File.isDirectory(cfg.INI)) {
152 //--- load ini files..
153 // this is very specific.
155 var dirs = File.list( cfg.INI + '/Pman').filter(
157 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
165 dirs.forEach(function(d) {
166 // this currently misses the web.*/Pman/XXXX/DataObjects..
167 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
169 if (!File.isDirectory(path)) {
172 var inis = File.list(path).filter(
173 function(e) { return e.match(/\.links\.ini$/); }
179 inis.forEach(function(i) {
180 readIni(path + '/' + i);
185 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
186 var inis = File.list(cfg.INI).filter(
187 function(e) { return e.match(/\.links\.ini$/); }
190 inis.forEach(function(i) {
191 readIni(path + '/' + i);
197 //print(JSON.stringify(ini, null,4));
203 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
209 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
211 tables.forEach(function(table) {
213 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
216 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
220 var firstTxtCol = '';
222 //print(JSON.stringify(schema, null,4));
224 schema.forEach(function(e) {
225 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
237 if (typeof(map[e.Type]) == 'undefined') {
240 name: "ArgumentError",
241 message: "Unknown mapping for type : " + e.Type
244 row.type = map[e.Type];
246 if (row.type == 'string' && !firstTxtCol.length) {
247 firstTxtCol = row.name;
250 if (row.type == 'date') {
251 row.dateFormat = 'Y-m-d';
255 if (combofields.length == 1 && row.type == 'string') {
256 combofields.push(row);
260 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
261 title = title[0].toUpperCase() + title.substring(1);
264 "xtype": "ColumnModel",
266 "width": row.type == 'string' ? 200 : 75,
267 "dataIndex": row.name,
268 "|renderer": row.type != 'date' ?
269 "function(v) { return String.format('{0}', v); }" :
270 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
272 "*prop": "colModel[]"
274 var xtype = 'TextField';
275 if (row.type == 'number') {
276 xtype = 'NumberField';
278 if (row.type == 'date') {
281 if (e.Type == 'text') {
284 if (row.name == 'id') {
287 // what about booleans.. -> checkboxes..
294 width : row.type == 'string' ? 200 : 75,
298 if (xtype == 'TextArea') {
299 form[row.name].height = 100;
301 if (xtype == 'Hidden') {
302 delete form[row.name].fieldLabel;
303 delete form[row.name].width;
311 allowBlank : 'false',
313 emptyText : 'Select ' + table,
314 forceSelection : true,
316 loadingText: 'Searching...',
319 qtip: 'Select ' + table,
321 triggerAction : 'all',
328 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
329 queryParam : '',// SET WHEN USED
330 fieldLabel : table, // SET WHEN USED
332 displayField : '', // SET WHEN USED eg. project_id_name
333 hiddenName : '', // SET WHEN USED eg. project_id
334 name : '', // SET WHEN USED eg. project_id_name
342 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
344 '|beforeload' : 'function (_self, o)' +
346 " o.params = o.params || {};\n" +
347 " // set more here\n" +
353 'xtype' : 'HttpProxy',
356 '|url' : "baseURL + '/Roo/" + table + ".php'",
361 'xtype' : 'JsonReader',
365 'totalProperty' : 'total',
366 '|fields' : JSON.stringify(combofields)
377 //print(JSON.stringify(reader,null,4));
381 combofields : combofields,
383 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
385 firstTxtCol : firstTxtCol,
389 //console.dump(schema );
396 // merge in the linked tables..
397 readers.forEach(function(reader) {
398 if (typeof(ini[reader.table]) == 'undefined') {
402 print("OVERLAY - " + reader.table);
404 for (var col in ini[reader.table]) {
405 var kv = ini[reader.table][col].split(':');
408 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
412 // merge in data (eg. project_id => project_id_*****
414 add.oreader.forEach(function(or) {
416 name : col + '_' + or.name,
421 // col is mapped to something..
422 var combofields = add.combofields;
423 if (add.combofields.length < 2) {
426 if (typeof(reader.form[col]) == 'undefined') {
427 print (JSON.stringify(reader.form, null,4));
428 print("missing linked column " + col);
432 var combofields_name = add.combofields[1].name;
433 var old = reader.form[col];
434 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
435 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
436 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
437 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
438 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
439 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
440 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
448 //readers.forEach(function(reader) {
449 // delete reader.oreader;
456 //print(JSON.stringify(readers, null, 4));
458 readers.forEach(function(reader) {
461 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
462 if (!File.isDirectory(dir)) {
463 print("mkdir " + dir);
468 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
473 xtype : "JsonReader",
474 totalProperty : "total",
477 id : 'id', // maybe no..
479 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
483 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
484 JSON.stringify(jreader, null, 4)
489 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
490 if (!File.isDirectory(dir)) {
491 print("mkdir " + dir);
496 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
499 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
505 "title": reader.table,
507 "fitContainer": true,
508 "tableName": reader.table,
512 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
518 "autoExpandColumn": reader.firstTxtCol,
521 "|render": "function() \n" +
523 " _this.grid = this; \n" +
524 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
525 " if (_this.panel.active) {\n" +
526 " this.footer.onClick('first');\n" +
529 "|rowdblclick": "function (_self, rowIndex, e)\n" +
531 " if (!_this.dialog) return;\n" +
532 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
533 " _this.grid.footer.onClick('first');\n" +
541 "*prop": "dataSource",
544 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
550 "xtype": "HttpProxy",
552 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
560 "xtype": "PagingToolbar",
563 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
564 "emptyMsg": "No " + reader.table + " found",
575 "cls": "x-btn-text-icon",
576 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
578 "|click": "function()\n"+
580 " if (!_this.dialog) return;\n" +
581 " _this.dialog.show( { id : 0 } , function() {\n"+
582 " _this.grid.footer.onClick('first');\n"+
586 "|xns": "Roo.Toolbar"
591 "cls": "x-btn-text-icon",
592 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
594 "|click": "function()\n"+
596 " var s = _this.grid.getSelectionModel().getSelections();\n"+
597 " if (!s.length || (s.length > 1)) {\n"+
598 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
601 " if (!_this.dialog) return;\n" +
602 " _this.dialog.show(s[0].data, function() {\n"+
603 " _this.grid.footer.onClick('first');\n"+
609 "|xns": "Roo.Toolbar"
613 "cls": "x-btn-text-icon",
614 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
617 "|click": "function()\n"+
619 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
623 "|xns": "Roo.Toolbar"
627 ].concat( reader.colmodel)
637 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
638 if (!File.isDirectory(dir)) {
639 print("mkdir " + dir);
642 var formElements = [];
644 for (var k in reader.form) {
645 if (k == 'id') { // should really do primary key testing..
648 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
650 formElements.push(reader.form[k]);
652 if (reader.form['id']) {
653 formElements.push(reader.form['id']);
657 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
663 "|actioncomplete" : "function(_self,action)\n"+
665 " if (action.type == 'setdata') {\n"+
666 " //_this.dialog.el.mask(\"Loading\");\n"+
667 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
670 " if (action.type == 'load') {\n"+
671 " _this.dialog.el.unmask();\n"+
674 " if (action.type =='submit') {\n"+
676 " _this.dialog.el.unmask();\n"+
677 " _this.dialog.hide();\n"+
679 " if (_this.callback) {\n"+
680 " _this.callback.call(_this, _this.form.getValues());\n"+
682 " _this.form.reset();\n"+
687 "|rendered" : "function (form)\n"+
689 " _this.form= form;\n"+
693 style : "margin:10px;",
694 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
700 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
703 JSON.stringify( frmCfg, null, 4)
712 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
713 if (!File.isDirectory(dir)) {
714 print("mkdir " + dir);
718 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
721 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
724 JSON.stringify(reader.combo, null, 4)
735 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
736 if (!File.isDirectory(dir)) {
737 print("mkdir " + dir);
740 var formElements = [];
741 for (var k in reader.form) {
742 if (k == 'id') { // should really do primary key testing..
745 formElements.push(reader.form[k]);
747 formElements.push(reader.form['id']);
749 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
752 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
758 "collapsible": false,
759 "height": formHeight,
761 "title": "Edit / Create " + reader.table,
763 "xtype": "LayoutDialog",
768 "xtype": "LayoutRegion",
773 "xtype": "ContentPanel",
782 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
784 "*prop": "buttons[]",
791 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
793 "*prop": "buttons[]",