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);
99 'datetime' : 'string',
107 'varchar' : 'string',
109 'longtext' : 'string',
110 'tinytext' : 'string',
111 'mediumtext' : 'string',
113 'timestamp' : 'number',
122 print('Read INI : ' + fn);
123 var key_file = new GLib.KeyFile.c_new();
124 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
128 var groups = key_file.get_groups();
129 groups.forEach(function(g) {
132 var keys = key_file.get_keys(g);
133 keys.forEach(function(k) {
134 ini[g][k] = key_file.get_value(g,k);
139 if (File.isFile(cfg.INI)) {
140 if (cfg.INI.match(/links\.ini$/)) {
143 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
147 if (File.isDirectory(cfg.INI)) {
150 //--- load ini files..
151 // this is very specific.
153 var dirs = File.list( cfg.INI + '/Pman').filter(
155 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
163 dirs.forEach(function(d) {
164 // this currently misses the web.*/Pman/XXXX/DataObjects..
165 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
167 if (!File.isDirectory(path)) {
170 var inis = File.list(path).filter(
171 function(e) { return e.match(/\.links\.ini$/); }
177 inis.forEach(function(i) {
178 readIni(path + '/' + i);
183 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
184 var inis = File.list(cfg.INI).filter(
185 function(e) { return e.match(/\.links\.ini$/); }
188 inis.forEach(function(i) {
189 readIni(path + '/' + i);
195 //print(JSON.stringify(ini, null,4));
201 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
207 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
209 tables.forEach(function(table) {
211 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
214 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
218 var firstTxtCol = '';
220 //print(JSON.stringify(schema, null,4));
222 schema.forEach(function(e) {
223 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
235 if (typeof(map[e.Type]) == 'undefined') {
238 name: "ArgumentError",
239 message: "Unknown mapping for type : " + e.Type
242 row.type = map[e.Type];
244 if (row.type == 'string' && !firstTxtCol.length) {
245 firstTxtCol = row.name;
248 if (row.type == 'date') {
249 row.dateFormat = 'Y-m-d';
253 if (combofields.length == 1 && row.type == 'string') {
254 combofields.push(row);
258 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
259 title = title[0].toUpperCase() + title.substring(1);
262 "xtype": "ColumnModel",
264 "width": row.type == 'string' ? 200 : 75,
265 "dataIndex": row.name,
266 "|renderer": row.type != 'date' ?
267 "function(v) { return String.format('{0}', v); }" :
268 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
270 "*prop": "colModel[]"
272 var xtype = 'TextField';
273 if (row.type == 'number') {
274 xtype = 'NumberField';
276 if (row.type == 'date') {
279 if (e.Type == 'text') {
282 if (row.name == 'id') {
285 // what about booleans.. -> checkboxes..
292 width : row.type == 'string' ? 200 : 75,
296 if (xtype == 'TextArea') {
297 form[row.name].height = 100;
299 if (xtype == 'Hidden') {
300 delete form[row.name].fieldLabel;
301 delete form[row.name].width;
309 allowBlank : 'false',
311 emptyText : 'Select ' + table,
312 forceSelection : true,
314 loadingText: 'Searching...',
317 qtip: 'Select ' + table,
319 triggerAction : 'all',
326 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
327 queryParam : '',// SET WHEN USED
328 fieldLabel : table, // SET WHEN USED
330 displayField : '', // SET WHEN USED eg. project_id_name
331 hiddenName : '', // SET WHEN USED eg. project_id
332 name : '', // SET WHEN USED eg. project_id_name
340 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
342 '|beforeload' : 'function (_self, o)' +
344 " o.params = o.params || {};\n" +
345 " // set more here\n" +
351 'xtype' : 'HttpProxy',
354 '|url' : "baseURL + '/Roo/" + table + ".php'",
359 'xtype' : 'JsonReader',
363 'totalProperty' : 'total',
364 '|fields' : JSON.stringify(combofields)
375 //print(JSON.stringify(reader,null,4));
379 combofields : combofields,
381 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
383 firstTxtCol : firstTxtCol,
387 //console.dump(schema );
394 // merge in the linked tables..
395 readers.forEach(function(reader) {
396 if (typeof(ini[reader.table]) == 'undefined') {
400 print("OVERLAY - " + reader.table);
402 for (var col in ini[reader.table]) {
403 var kv = ini[reader.table][col].split(':');
406 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
410 // merge in data (eg. project_id => project_id_*****
412 add.oreader.forEach(function(or) {
414 name : col + '_' + or.name,
419 // col is mapped to something..
420 var combofields = add.combofields;
421 if (add.combofields.length < 2) {
424 if (typeof(reader.form[col]) == 'undefined') {
425 print (JSON.stringify(reader.form, null,4));
426 print("missing linked column " + col);
430 var combofields_name = add.combofields[1].name;
431 var old = reader.form[col];
432 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
433 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
434 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
435 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
436 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
437 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
438 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
446 //readers.forEach(function(reader) {
447 // delete reader.oreader;
454 //print(JSON.stringify(readers, null, 4));
456 readers.forEach(function(reader) {
459 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
460 if (!File.isDirectory(dir)) {
465 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
470 xtype : "JsonReader",
471 totalProperty : "total",
474 id : 'id', // maybe no..
475 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
479 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
480 JSON.stringify(jreader, null, 4)
485 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
486 if (!File.isDirectory(dir)) {
491 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
494 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
500 "title": reader.table,
502 "fitContainer": true,
503 "tableName": reader.table,
506 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
512 "autoExpandColumn": reader.firstTxtCol,
515 "|render": "function() \n" +
517 " _this.grid = this; \n" +
518 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
519 " if (_this.panel.active) {\n" +
520 " this.footer.onClick('first');\n" +
528 "*prop": "dataSource",
536 "xtype": "HttpProxy",
538 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
546 "xtype": "PagingToolbar",
549 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
550 "emptyMsg": "No " + reader.table + " found",
561 "cls": "x-btn-text-icon",
562 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
564 "|click": "function()\n"+
566 " if (!_this.dialog) return;\n" +
567 " _this.dialog.show( { id : 0 } , function() {\n"+
568 " _this.grid.footer.onClick('first');\n"+
572 "|xns": "Roo.Toolbar"
577 "cls": "x-btn-text-icon",
578 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
580 "|click": "function()\n"+
582 " var s = _this.grid.getSelectionModel().getSelections();\n"+
583 " if (!s.length || (s.length > 1)) {\n"+
584 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
587 " if (!_this.dialog) return;\n" +
588 " _this.dialog.show(s[0].data, function() {\n"+
589 " _this.grid.footer.onClick('first');\n"+
595 "|xns": "Roo.Toolbar"
599 "cls": "x-btn-text-icon",
600 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
603 "|click": "function()\n"+
605 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
609 "|xns": "Roo.Toolbar"
613 ].concat( reader.colmodel)
623 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
624 if (!File.isDirectory(dir)) {
627 var formElements = [];
629 for (var k in reader.form) {
630 if (k == 'id') { // should really do primary key testing..
633 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
634 formElements.push(reader.form[k]);
636 formElements.push(reader.form['id']);
638 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
644 "|actioncomplete" : "function(_self,action)\n"+
646 " if (action.type == 'setdata') {\n"+
647 " //_this.dialog.el.mask(\"Loading\");\n"+
648 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
651 " if (action.type == 'load') {\n"+
652 " _this.dialog.el.unmask();\n"+
655 " if (action.type =='submit') {\n"+
657 " _this.dialog.el.unmask();\n"+
658 " _this.dialog.hide();\n"+
660 " if (_this.callback) {\n"+
661 " _this.callback.call(_this, _this.form.getValues());\n"+
663 " _this.form.reset();\n"+
668 "|rendered" : "function (form)\n"+
670 " _this.form= form;\n"+
674 style : "margin:10px;",
675 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
681 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
684 JSON.stringify( frmCfg, null, 4)
693 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
694 if (!File.isDirectory(dir)) {
698 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
701 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
704 JSON.stringify(reader.combo, null, 4)
715 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
716 if (!File.isDirectory(dir)) {
719 var formElements = [];
720 for (var k in reader.form) {
721 if (k == 'id') { // should really do primary key testing..
724 formElements.push(reader.form[k]);
726 formElements.push(reader.form['id']);
728 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
731 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
737 "collapsible": false,
738 "height": formHeight,
740 "title": "Edit / Create " + reader.table,
742 "xtype": "LayoutDialog",
747 "xtype": "LayoutRegion",
752 "xtype": "ContentPanel",
761 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
763 "*prop": "buttons[]",
770 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
772 "*prop": "buttons[]",