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);
153 keys.forEach(function(k) {
154 ini[g][k] = key_file.get_value(g,k);
159 if (File.isFile(cfg.INI)) {
160 if (cfg.INI.match(/links\.ini$/)) {
163 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
167 if (File.isDirectory(cfg.INI)) {
170 //--- load ini files..
171 // this is very specific.
173 var dirs = File.list( cfg.INI + '/Pman').filter(
175 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
183 dirs.forEach(function(d) {
184 // this currently misses the web.*/Pman/XXXX/DataObjects..
185 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
187 if (!File.isDirectory(path)) {
190 var inis = File.list(path).filter(
191 function(e) { return e.match(/\.links\.ini$/); }
197 inis.forEach(function(i) {
198 readIni(path + '/' + i);
203 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
204 var inis = File.list(cfg.INI).filter(
205 function(e) { return e.match(/\.links\.ini$/); }
208 inis.forEach(function(i) {
209 readIni(path + '/' + i);
215 //print(JSON.stringify(ini, null,4));
221 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
227 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
229 tables.forEach(function(table) {
231 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
234 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
238 var firstTxtCol = '';
240 //print(JSON.stringify(schema, null,4));
242 schema.forEach(function(e) {
243 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
255 if (typeof(map[e.Type]) == 'undefined') {
258 name: "ArgumentError",
259 message: "Unknown mapping for type : " + e.Type
262 row.type = map[e.Type];
264 if (row.type == 'string' && !firstTxtCol.length) {
265 firstTxtCol = row.name;
268 if (row.type == 'date') {
269 row.dateFormat = 'Y-m-d';
273 if (combofields.length == 1 && row.type == 'string') {
274 combofields.push(row);
278 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
279 title = title[0].toUpperCase() + title.substring(1);
282 "xtype": "ColumnModel",
284 "width": row.type == 'string' ? 200 : 75,
285 "dataIndex": row.name,
286 "|renderer": row.type != 'date' ?
287 "function(v) { return String.format('{0}', v); }" :
288 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
290 "*prop": "colModel[]"
292 var xtype = 'TextField';
293 if (row.type == 'number') {
294 xtype = 'NumberField';
296 if (row.type == 'date') {
299 if (e.Type == 'text') {
302 if (row.name == 'id') {
305 // what about booleans.. -> checkboxes..
312 width : row.type == 'string' ? 200 : 75,
316 if (xtype == 'TextArea') {
317 form[row.name].height = 100;
319 if (xtype == 'Hidden') {
320 delete form[row.name].fieldLabel;
321 delete form[row.name].width;
329 allowBlank : 'false',
331 emptyText : 'Select ' + table,
332 forceSelection : true,
334 loadingText: 'Searching...',
337 qtip: 'Select ' + table,
339 triggerAction : 'all',
346 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
347 queryParam : '',// SET WHEN USED
348 fieldLabel : table, // SET WHEN USED
350 displayField : '', // SET WHEN USED eg. project_id_name
351 hiddenName : '', // SET WHEN USED eg. project_id
352 name : '', // SET WHEN USED eg. project_id_name
360 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
362 '|beforeload' : 'function (_self, o)' +
364 " o.params = o.params || {};\n" +
365 " // set more here\n" +
371 'xtype' : 'HttpProxy',
374 '|url' : "baseURL + '/Roo/" + table + ".php'",
379 'xtype' : 'JsonReader',
383 'totalProperty' : 'total',
384 '|fields' : JSON.stringify(combofields)
395 //print(JSON.stringify(reader,null,4));
399 combofields : combofields,
401 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
403 firstTxtCol : firstTxtCol,
407 //console.dump(schema );
414 // merge in the linked tables..
415 readers.forEach(function(reader) {
416 if (typeof(ini[reader.table]) == 'undefined') {
420 print("OVERLAY - " + reader.table);
422 for (var col in ini[reader.table]) {
423 var kv = ini[reader.table][col].split(':');
426 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
430 // merge in data (eg. project_id => project_id_*****
432 add.oreader.forEach(function(or) {
434 name : col + '_' + or.name,
439 // col is mapped to something..
440 var combofields = add.combofields;
441 if (add.combofields.length < 2) {
444 if (typeof(reader.form[col]) == 'undefined') {
445 print (JSON.stringify(reader.form, null,4));
446 print("missing linked column " + col);
450 var combofields_name = add.combofields[1].name;
451 var old = reader.form[col];
452 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
453 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
454 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
455 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
456 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
457 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
458 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
466 //readers.forEach(function(reader) {
467 // delete reader.oreader;
474 //print(JSON.stringify(readers, null, 4));
476 readers.forEach(function(reader) {
479 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
480 if (!File.isDirectory(dir)) {
481 print("mkdir " + dir);
486 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
491 xtype : "JsonReader",
492 totalProperty : "total",
495 id : 'id', // maybe no..
497 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
501 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
502 JSON.stringify(jreader, null, 4)
507 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
508 if (!File.isDirectory(dir)) {
509 print("mkdir " + dir);
514 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
517 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
523 "title": reader.table,
525 "fitContainer": true,
526 "tableName": reader.table,
530 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
536 "autoExpandColumn": reader.firstTxtCol,
539 "|render": "function() \n" +
541 " _this.grid = this; \n" +
542 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
543 " if (_this.panel.active) {\n" +
544 " this.footer.onClick('first');\n" +
547 "|rowdblclick": "function (_self, rowIndex, e)\n" +
549 " if (!_this.dialog) return;\n" +
550 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
551 " _this.grid.footer.onClick('first');\n" +
559 "*prop": "dataSource",
562 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
568 "xtype": "HttpProxy",
570 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
578 "xtype": "PagingToolbar",
581 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
582 "emptyMsg": "No " + reader.table + " found",
593 "cls": "x-btn-text-icon",
594 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
596 "|click": "function()\n"+
598 " if (!_this.dialog) return;\n" +
599 " _this.dialog.show( { id : 0 } , function() {\n"+
600 " _this.grid.footer.onClick('first');\n"+
604 "|xns": "Roo.Toolbar"
609 "cls": "x-btn-text-icon",
610 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
612 "|click": "function()\n"+
614 " var s = _this.grid.getSelectionModel().getSelections();\n"+
615 " if (!s.length || (s.length > 1)) {\n"+
616 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
619 " if (!_this.dialog) return;\n" +
620 " _this.dialog.show(s[0].data, function() {\n"+
621 " _this.grid.footer.onClick('first');\n"+
627 "|xns": "Roo.Toolbar"
631 "cls": "x-btn-text-icon",
632 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
635 "|click": "function()\n"+
637 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
641 "|xns": "Roo.Toolbar"
645 ].concat( reader.colmodel)
655 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
656 if (!File.isDirectory(dir)) {
657 print("mkdir " + dir);
660 var formElements = [];
662 for (var k in reader.form) {
663 if (k == 'id') { // should really do primary key testing..
666 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
668 formElements.push(reader.form[k]);
670 if (reader.form['id']) {
671 formElements.push(reader.form['id']);
675 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
681 "|actioncomplete" : "function(_self,action)\n"+
683 " if (action.type == 'setdata') {\n"+
684 " //_this.dialog.el.mask(\"Loading\");\n"+
685 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
688 " if (action.type == 'load') {\n"+
689 " _this.dialog.el.unmask();\n"+
692 " if (action.type =='submit') {\n"+
694 " _this.dialog.el.unmask();\n"+
695 " _this.dialog.hide();\n"+
697 " if (_this.callback) {\n"+
698 " _this.callback.call(_this, _this.form.getValues());\n"+
700 " _this.form.reset();\n"+
705 "|rendered" : "function (form)\n"+
707 " _this.form= form;\n"+
711 style : "margin:10px;",
712 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
718 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
721 JSON.stringify( frmCfg, null, 4)
730 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
731 if (!File.isDirectory(dir)) {
732 print("mkdir " + dir);
736 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
739 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
742 JSON.stringify(reader.combo, null, 4)
753 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
754 if (!File.isDirectory(dir)) {
755 print("mkdir " + dir);
758 var formElements = [];
759 for (var k in reader.form) {
760 if (k == 'id') { // should really do primary key testing..
763 formElements.push(reader.form[k]);
765 formElements.push(reader.form['id']);
767 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
770 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
776 "collapsible": false,
777 "height": formHeight,
779 "title": "Edit / Create " + reader.table,
781 "xtype": "LayoutDialog",
786 "xtype": "LayoutRegion",
791 "xtype": "ContentPanel",
800 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
802 "*prop": "buttons[]",
809 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
811 "*prop": "buttons[]",