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
126 'varchar' : 'string',
128 'longtext' : 'string',
129 'tinytext' : 'string',
130 'mediumtext' : 'string',
132 'timestamp' : 'number',
141 print('Read INI : ' + fn);
142 var key_file = new GLib.KeyFile.c_new();
143 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
147 var groups = key_file.get_groups();
148 groups.forEach(function(g) {
151 var keys = key_file.get_keys(g).value;
152 keys.forEach(function(k) {
153 ini[g][k] = key_file.get_value(g,k);
158 if (File.isFile(cfg.INI)) {
159 if (cfg.INI.match(/links\.ini$/)) {
162 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
166 if (File.isDirectory(cfg.INI)) {
169 //--- load ini files..
170 // this is very specific.
172 var dirs = File.list( cfg.INI + '/Pman').filter(
174 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
182 dirs.forEach(function(d) {
183 // this currently misses the web.*/Pman/XXXX/DataObjects..
184 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
186 if (!File.isDirectory(path)) {
189 var inis = File.list(path).filter(
190 function(e) { return e.match(/\.links\.ini$/); }
196 inis.forEach(function(i) {
197 readIni(path + '/' + i);
202 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
203 var inis = File.list(cfg.INI).filter(
204 function(e) { return e.match(/\.links\.ini$/); }
207 inis.forEach(function(i) {
208 readIni(path + '/' + i);
214 //print(JSON.stringify(ini, null,4));
220 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
226 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
228 tables.forEach(function(table) {
230 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
233 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
237 var firstTxtCol = '';
239 //print(JSON.stringify(schema, null,4));
241 schema.forEach(function(e) {
242 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
254 if (typeof(map[e.Type]) == 'undefined') {
257 name: "ArgumentError",
258 message: "Unknown mapping for type : " + e.Type
261 row.type = map[e.Type];
263 if (row.type == 'string' && !firstTxtCol.length) {
264 firstTxtCol = row.name;
267 if (row.type == 'date') {
268 row.dateFormat = 'Y-m-d';
272 if (combofields.length == 1 && row.type == 'string') {
273 combofields.push(row);
277 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
278 title = title[0].toUpperCase() + title.substring(1);
281 "xtype": "ColumnModel",
283 "width": row.type == 'string' ? 200 : 75,
284 "dataIndex": row.name,
285 "|renderer": row.type != 'date' ?
286 "function(v) { return String.format('{0}', v); }" :
287 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
289 "*prop": "colModel[]"
291 var xtype = 'TextField';
292 if (row.type == 'number') {
293 xtype = 'NumberField';
295 if (row.type == 'date') {
298 if (e.Type == 'text') {
301 if (row.name == 'id') {
304 // what about booleans.. -> checkboxes..
311 width : row.type == 'string' ? 200 : 75,
315 if (xtype == 'TextArea') {
316 form[row.name].height = 100;
318 if (xtype == 'Hidden') {
319 delete form[row.name].fieldLabel;
320 delete form[row.name].width;
328 allowBlank : 'false',
330 emptyText : 'Select ' + table,
331 forceSelection : true,
333 loadingText: 'Searching...',
336 qtip: 'Select ' + table,
338 triggerAction : 'all',
345 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
346 queryParam : '',// SET WHEN USED
347 fieldLabel : table, // SET WHEN USED
349 displayField : '', // SET WHEN USED eg. project_id_name
350 hiddenName : '', // SET WHEN USED eg. project_id
351 name : '', // SET WHEN USED eg. project_id_name
359 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
361 '|beforeload' : 'function (_self, o)' +
363 " o.params = o.params || {};\n" +
364 " // set more here\n" +
370 'xtype' : 'HttpProxy',
373 '|url' : "baseURL + '/Roo/" + table + ".php'",
378 'xtype' : 'JsonReader',
382 'totalProperty' : 'total',
383 '|fields' : JSON.stringify(combofields)
394 //print(JSON.stringify(reader,null,4));
398 combofields : combofields,
400 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
402 firstTxtCol : firstTxtCol,
406 //console.dump(schema );
413 // merge in the linked tables..
414 readers.forEach(function(reader) {
415 if (typeof(ini[reader.table]) == 'undefined') {
419 print("OVERLAY - " + reader.table);
421 for (var col in ini[reader.table]) {
422 var kv = ini[reader.table][col].split(':');
425 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
429 // merge in data (eg. project_id => project_id_*****
431 add.oreader.forEach(function(or) {
433 name : col + '_' + or.name,
438 // col is mapped to something..
439 var combofields = add.combofields;
440 if (add.combofields.length < 2) {
443 if (typeof(reader.form[col]) == 'undefined') {
444 print (JSON.stringify(reader.form, null,4));
445 print("missing linked column " + col);
449 var combofields_name = add.combofields[1].name;
450 var old = reader.form[col];
451 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
452 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
453 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
454 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
455 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
456 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
457 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
465 //readers.forEach(function(reader) {
466 // delete reader.oreader;
473 //print(JSON.stringify(readers, null, 4));
475 readers.forEach(function(reader) {
478 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
479 if (!File.isDirectory(dir)) {
480 print("mkdir " + dir);
485 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
490 xtype : "JsonReader",
491 totalProperty : "total",
494 id : 'id', // maybe no..
496 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
500 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
501 JSON.stringify(jreader, null, 4)
506 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
507 if (!File.isDirectory(dir)) {
508 print("mkdir " + dir);
513 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
516 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
522 "title": reader.table,
524 "fitContainer": true,
525 "tableName": reader.table,
529 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
535 "autoExpandColumn": reader.firstTxtCol,
538 "|render": "function() \n" +
540 " _this.grid = this; \n" +
541 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
542 " if (_this.panel.active) {\n" +
543 " this.footer.onClick('first');\n" +
546 "|rowdblclick": "function (_self, rowIndex, e)\n" +
548 " if (!_this.dialog) return;\n" +
549 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
550 " _this.grid.footer.onClick('first');\n" +
558 "*prop": "dataSource",
561 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
567 "xtype": "HttpProxy",
569 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
577 "xtype": "PagingToolbar",
580 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
581 "emptyMsg": "No " + reader.table + " found",
592 "cls": "x-btn-text-icon",
593 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
595 "|click": "function()\n"+
597 " if (!_this.dialog) return;\n" +
598 " _this.dialog.show( { id : 0 } , function() {\n"+
599 " _this.grid.footer.onClick('first');\n"+
603 "|xns": "Roo.Toolbar"
608 "cls": "x-btn-text-icon",
609 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
611 "|click": "function()\n"+
613 " var s = _this.grid.getSelectionModel().getSelections();\n"+
614 " if (!s.length || (s.length > 1)) {\n"+
615 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
618 " if (!_this.dialog) return;\n" +
619 " _this.dialog.show(s[0].data, function() {\n"+
620 " _this.grid.footer.onClick('first');\n"+
626 "|xns": "Roo.Toolbar"
630 "cls": "x-btn-text-icon",
631 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
634 "|click": "function()\n"+
636 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
640 "|xns": "Roo.Toolbar"
644 ].concat( reader.colmodel)
654 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
655 if (!File.isDirectory(dir)) {
656 print("mkdir " + dir);
659 var formElements = [];
661 for (var k in reader.form) {
662 if (k == 'id') { // should really do primary key testing..
665 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
667 formElements.push(reader.form[k]);
669 if (reader.form['id']) {
670 formElements.push(reader.form['id']);
674 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
680 "|actioncomplete" : "function(_self,action)\n"+
682 " if (action.type == 'setdata') {\n"+
683 " //_this.dialog.el.mask(\"Loading\");\n"+
684 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
687 " if (action.type == 'load') {\n"+
688 " _this.dialog.el.unmask();\n"+
691 " if (action.type =='submit') {\n"+
693 " _this.dialog.el.unmask();\n"+
694 " _this.dialog.hide();\n"+
696 " if (_this.callback) {\n"+
697 " _this.callback.call(_this, _this.form.getValues());\n"+
699 " _this.form.reset();\n"+
704 "|rendered" : "function (form)\n"+
706 " _this.form= form;\n"+
710 style : "margin:10px;",
711 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
717 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
720 JSON.stringify( frmCfg, null, 4)
729 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
730 if (!File.isDirectory(dir)) {
731 print("mkdir " + dir);
735 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
738 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
741 JSON.stringify(reader.combo, null, 4)
752 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
753 if (!File.isDirectory(dir)) {
754 print("mkdir " + dir);
757 var formElements = [];
758 for (var k in reader.form) {
759 if (k == 'id') { // should really do primary key testing..
762 formElements.push(reader.form[k]);
764 formElements.push(reader.form['id']);
766 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
769 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
775 "collapsible": false,
776 "height": formHeight,
778 "title": "Edit / Create " + reader.table,
780 "xtype": "LayoutDialog",
785 "xtype": "LayoutRegion",
790 "xtype": "ContentPanel",
799 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
801 "*prop": "buttons[]",
808 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
810 "*prop": "buttons[]",