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 : 'DBTYPE' , arg_short : 't', description : 'Database Type (eg. MySQL or PostgreSQL ' },
56 { arg_long : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
57 { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
58 { arg_long : 'PASSWORD' , arg_short : 'p', description : '' , arg_default :'' },
59 { arg_long : 'INI' , arg_short : 'I', description : 'Either base directory which has Pman/***/DataObjects/***.ini or location of ini file.' },
66 var cfg = o.parse(Seed.argv);
67 print(JSON.stringify(cfg, null,4));
69 var cnc = Gda.Connection.open_from_string (cfg.DBTYPE,
70 "DB_NAME=" + cfg.DBNAME,
71 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
72 Gda.ConnectionOptions.NONE, null);
79 Gda.DataSelect.prototype.fetchAll = function()
83 for (var i =0;i < this.get_n_columns(); i++) {
84 cols.push(this.get_column_name(i));
86 //print(JSON.stringify(cols, null,4));
87 var iter = this.create_iter();
89 //print(this.get_n_rows());
91 for (var r = 0; r < this.get_n_rows(); r++) {
95 if (cols.length == 1) {
96 res.push(this.get_value_at(0,r).get_string());
101 cols.forEach(function(n,i) {
102 var val = _this.get_value_at(i,r);
103 var type = GObject.type_name(val.g_type) ;
104 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
105 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
106 //print (n + '=' + iter.get_value_at(i).value);
120 'time' : 'string', //bogus
129 'varchar' : 'string',
131 'longtext' : 'string',
132 'tinytext' : 'string',
133 'mediumtext' : 'string',
135 'timestamp' : 'number',
144 print('Read INI : ' + fn);
145 var key_file = new GLib.KeyFile.c_new();
146 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
150 var groups = key_file.get_groups();
151 groups.forEach(function(g) {
154 var keys = key_file.get_keys(g);
155 if (!keys) { return; }
156 keys.forEach(function(k) {
157 ini[g][k] = key_file.get_value(g,k);
162 if (File.isFile(cfg.INI)) {
163 if (cfg.INI.match(/links\.ini$/)) {
166 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
171 if (File.isDirectory(cfg.INI)) {
174 //--- load ini files..
175 // this is very specific.
177 var dirs = File.list( cfg.INI + '/Pman').filter(
179 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
187 dirs.forEach(function(d) {
188 // this currently misses the web.*/Pman/XXXX/DataObjects..
189 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
191 if (!File.isDirectory(path)) {
194 var inis = File.list(path).filter(
195 function(e) { return e.match(/\.links\.ini$/); }
201 inis.forEach(function(i) {
202 readIni(path + '/' + i);
207 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
208 var inis = File.list(cfg.INI).filter(
209 function(e) { return e.match(/\.links\.ini$/); }
212 inis.forEach(function(i) {
213 readIni(path + '/' + i);
219 //print(JSON.stringify(ini, null,4));
225 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
231 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
233 tables.forEach(function(table) {
235 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
238 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
242 var firstTxtCol = '';
244 //print(JSON.stringify(schema, null,4));
246 schema.forEach(function(e) {
247 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
259 if (typeof(map[e.Type]) == 'undefined') {
262 name: "ArgumentError",
263 message: "Unknown mapping for type : " + e.Type
266 row.type = map[e.Type];
268 if (row.type == 'string' && !firstTxtCol.length) {
269 firstTxtCol = row.name;
272 if (row.type == 'date') {
273 row.dateFormat = 'Y-m-d';
277 if (combofields.length == 1 && row.type == 'string') {
278 combofields.push(row);
282 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
283 title = title[0].toUpperCase() + title.substring(1);
286 "xtype": "ColumnModel",
288 "width": row.type == 'string' ? 200 : 75,
289 "dataIndex": row.name,
290 "|renderer": row.type != 'date' ?
291 "function(v) { return String.format('{0}', v); }" :
292 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
294 "*prop": "colModel[]"
296 var xtype = 'TextField';
297 if (row.type == 'number') {
298 xtype = 'NumberField';
300 if (row.type == 'date') {
303 if (e.Type == 'text') {
306 if (row.name == 'id') {
309 // what about booleans.. -> checkboxes..
316 width : row.type == 'string' ? 200 : 75,
320 if (xtype == 'TextArea') {
321 form[row.name].height = 100;
323 if (xtype == 'Hidden') {
324 delete form[row.name].fieldLabel;
325 delete form[row.name].width;
333 allowBlank : 'false',
335 emptyText : 'Select ' + table,
336 forceSelection : true,
338 loadingText: 'Searching...',
341 qtip: 'Select ' + table,
343 triggerAction : 'all',
350 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
351 queryParam : '',// SET WHEN USED
352 fieldLabel : table, // SET WHEN USED
354 displayField : '', // SET WHEN USED eg. project_id_name
355 hiddenName : '', // SET WHEN USED eg. project_id
356 name : '', // SET WHEN USED eg. project_id_name
364 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
366 '|beforeload' : 'function (_self, o)' +
368 " o.params = o.params || {};\n" +
369 " // set more here\n" +
375 'xtype' : 'HttpProxy',
378 '|url' : "baseURL + '/Roo/" + table + ".php'",
383 'xtype' : 'JsonReader',
387 'totalProperty' : 'total',
388 '|fields' : JSON.stringify(combofields)
399 //print(JSON.stringify(reader,null,4));
403 combofields : combofields,
405 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
407 firstTxtCol : firstTxtCol,
411 //console.dump(schema );
418 // merge in the linked tables..
419 readers.forEach(function(reader) {
420 if (typeof(ini[reader.table]) == 'undefined') {
424 print("OVERLAY - " + reader.table);
426 for (var col in ini[reader.table]) {
427 var kv = ini[reader.table][col].split(':');
430 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
434 // merge in data (eg. project_id => project_id_*****
436 add.oreader.forEach(function(or) {
438 name : col + '_' + or.name,
443 // col is mapped to something..
444 var combofields = add.combofields;
445 if (add.combofields.length < 2) {
448 if (typeof(reader.form[col]) == 'undefined') {
449 print (JSON.stringify(reader.form, null,4));
450 print("missing linked column " + col);
454 var combofields_name = add.combofields[1].name;
455 var old = reader.form[col];
456 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
457 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
458 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
459 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
460 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
461 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
462 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
470 //readers.forEach(function(reader) {
471 // delete reader.oreader;
478 //print(JSON.stringify(readers, null, 4));
480 readers.forEach(function(reader) {
483 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
484 if (!File.isDirectory(dir)) {
485 print("mkdir " + dir);
490 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
495 xtype : "JsonReader",
496 totalProperty : "total",
499 id : 'id', // maybe no..
501 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
505 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
506 JSON.stringify(jreader, null, 4)
511 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
512 if (!File.isDirectory(dir)) {
513 print("mkdir " + dir);
518 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
521 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
527 "title": reader.table,
529 "fitContainer": true,
530 "tableName": reader.table,
534 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
540 "autoExpandColumn": reader.firstTxtCol,
543 "|render": "function() \n" +
545 " _this.grid = this; \n" +
546 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
547 " if (_this.panel.active) {\n" +
548 " this.footer.onClick('first');\n" +
551 "|rowdblclick": "function (_self, rowIndex, e)\n" +
553 " if (!_this.dialog) return;\n" +
554 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
555 " _this.grid.footer.onClick('first');\n" +
563 "*prop": "dataSource",
566 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
572 "xtype": "HttpProxy",
574 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
582 "xtype": "PagingToolbar",
585 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
586 "emptyMsg": "No " + reader.table + " found",
597 "cls": "x-btn-text-icon",
598 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
600 "|click": "function()\n"+
602 " if (!_this.dialog) return;\n" +
603 " _this.dialog.show( { id : 0 } , function() {\n"+
604 " _this.grid.footer.onClick('first');\n"+
608 "|xns": "Roo.Toolbar"
613 "cls": "x-btn-text-icon",
614 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
616 "|click": "function()\n"+
618 " var s = _this.grid.getSelectionModel().getSelections();\n"+
619 " if (!s.length || (s.length > 1)) {\n"+
620 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
623 " if (!_this.dialog) return;\n" +
624 " _this.dialog.show(s[0].data, function() {\n"+
625 " _this.grid.footer.onClick('first');\n"+
631 "|xns": "Roo.Toolbar"
635 "cls": "x-btn-text-icon",
636 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
639 "|click": "function()\n"+
641 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
645 "|xns": "Roo.Toolbar"
649 ].concat( reader.colmodel)
659 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
660 if (!File.isDirectory(dir)) {
661 print("mkdir " + dir);
664 var formElements = [];
666 for (var k in reader.form) {
667 if (k == 'id') { // should really do primary key testing..
670 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
672 formElements.push(reader.form[k]);
674 if (reader.form['id']) {
675 formElements.push(reader.form['id']);
679 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
685 "|actioncomplete" : "function(_self,action)\n"+
687 " if (action.type == 'setdata') {\n"+
688 " //_this.dialog.el.mask(\"Loading\");\n"+
689 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
692 " if (action.type == 'load') {\n"+
693 " _this.dialog.el.unmask();\n"+
696 " if (action.type =='submit') {\n"+
698 " _this.dialog.el.unmask();\n"+
699 " _this.dialog.hide();\n"+
701 " if (_this.callback) {\n"+
702 " _this.callback.call(_this, _this.form.getValues());\n"+
704 " _this.form.reset();\n"+
709 "|rendered" : "function (form)\n"+
711 " _this.form= form;\n"+
715 style : "margin:10px;",
716 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
722 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
725 JSON.stringify( frmCfg, null, 4)
734 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
735 if (!File.isDirectory(dir)) {
736 print("mkdir " + dir);
740 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
743 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
746 JSON.stringify(reader.combo, null, 4)
757 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
758 if (!File.isDirectory(dir)) {
759 print("mkdir " + dir);
762 var formElements = [];
763 for (var k in reader.form) {
764 if (k == 'id') { // should really do primary key testing..
767 formElements.push(reader.form[k]);
769 formElements.push(reader.form['id']);
771 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
774 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
780 "collapsible": false,
781 "height": formHeight,
783 "title": "Edit / Create " + reader.table,
785 "xtype": "LayoutDialog",
790 "xtype": "LayoutRegion",
795 "xtype": "ContentPanel",
804 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
806 "*prop": "buttons[]",
813 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
815 "*prop": "buttons[]",