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',
106 'varchar' : 'string',
108 'longtext' : 'string',
109 'tinytext' : 'string',
110 'mediumtext' : 'string',
112 'timestamp' : 'number',
121 var key_file = new GLib.KeyFile.c_new();
122 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
126 var groups = key_file.get_groups();
127 groups.forEach(function(g) {
130 var keys = key_file.get_keys(g);
131 keys.forEach(function(k) {
132 ini[g][k] = key_file.get_value(g,k);
137 if (File.isFile(cfg.INI)) {
138 if (cfg.INI.match(/links\.ini$/)) {
141 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
145 if (File.isDirectory(cfg.INI)) {
148 //--- load ini files..
149 // this is very specific.
151 var dirs = File.list( cfg.INI + '/Pman').filter(
153 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
161 dirs.forEach(function(d) {
162 // this currently misses the web.*/Pman/XXXX/DataObjects..
163 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
165 if (!File.isDirectory(path)) {
168 var inis = File.list(path).filter(
169 function(e) { return e.match(/\.links\.ini$/); }
175 inis.forEach(function(i) {
176 readIni(path + '/' + i);
181 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
182 var inis = File.list(cfg.INI).filter(
183 function(e) { return e.match(/\.links\.ini$/); }
186 inis.forEach(function(i) {
187 readIni(path + '/' + i);
193 print(JSON.stringify(ini, null,4));
199 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
205 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
207 tables.forEach(function(table) {
209 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
212 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
216 var firstTxtCol = '';
218 print(JSON.stringify(schema, null,4));
220 schema.forEach(function(e) {
221 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
233 if (typeof(map[e.Type]) == 'undefined') {
236 name: "ArgumentError",
237 message: "Unknown mapping for type : " + e.Type
240 row.type = map[e.Type];
242 if (row.type == 'string' && !firstTxtCol.length) {
243 firstTxtCol = row.name;
246 if (row.type == 'date') {
247 row.dateFormat = 'Y-m-d';
251 if (combofields.length == 1 && row.type == 'string') {
252 combofields.push(row);
256 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
257 title = title[0].toUpperCase() + title.substring(1);
260 "xtype": "ColumnModel",
262 "width": row.type == 'string' ? 200 : 75,
263 "dataIndex": row.name,
264 "|renderer": row.type != 'date' ?
265 "function(v) { return String.format('{0}', v); }" :
266 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
268 "*prop": "colModel[]"
270 var xtype = 'TextField';
271 if (row.type == 'number') {
272 xtype = 'NumberField';
274 if (row.type == 'date') {
277 if (e.Type == 'text') {
280 if (e.name == 'id') {
283 // what about booleans.. -> checkboxes..
290 width : row.type == 'string' ? 200 : 75,
294 if (xtype == 'TextArea') {
295 form[row.name].height = 100;
304 allowBlank : 'false',
306 emptyText : 'Select ' + table,
307 forceSelection : true,
309 loadingText: 'Searching...',
312 qtip: 'Select ' + table,
314 triggerAction : 'all',
321 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
322 queryParam : '',// SET WHEN USED
323 fieldLabel : table, // SET WHEN USED
325 displayField : '', // SET WHEN USED eg. project_id_name
326 hiddenName : '', // SET WHEN USED eg. project_id
327 name : '', // SET WHEN USED eg. project_id_name
335 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
337 '|beforeload' : 'function (_self, o)' +
339 " o.params = o.params || {};\n" +
340 " // set more here\n" +
346 'xtype' : 'HttpProxy',
349 '|url' : "baseURL + '/Roo/" + table + ".php'",
354 'xtype' : 'JsonReader',
358 'totalProperty' : 'total',
359 '|fields' : JSON.stringify(combofields)
370 //print(JSON.stringify(reader,null,4));
374 combofields : combofields,
376 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
378 firstTxtCol : firstTxtCol,
382 //console.dump(schema );
389 // merge in the linked tables..
390 readers.forEach(function(reader) {
391 if (typeof(ini[reader.table]) == 'undefined') {
395 print("OVERLAY - " + reader.table);
397 for (var col in ini[reader.table]) {
398 var kv = ini[reader.table][col].split(':');
401 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
405 // merge in data (eg. project_id => project_id_*****
407 add.oreader.forEach(function(or) {
409 name : col + '_' + or.name,
414 // col is mapped to something..
415 var combofields = add.combofields;
416 if (add.combofields.length < 2) {
419 if (typeof(reader.form[col]) == 'undefined') {
420 print("missing linked column " + col);
424 var combofields_name = add.combofields[1].name;
425 var old = reader.form[col];
426 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
427 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
428 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
429 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
430 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
431 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
432 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
440 //readers.forEach(function(reader) {
441 // delete reader.oreader;
448 //print(JSON.stringify(readers, null, 4));
450 readers.forEach(function(reader) {
453 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
454 if (!File.isDirectory(dir)) {
459 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
464 xtype : "JsonReader",
465 totalProperty : "total",
468 id : 'id', // maybe no..
469 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
473 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
474 JSON.stringify(jreader, null, 4)
479 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
480 if (!File.isDirectory(dir)) {
485 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
488 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
494 "title": reader.table,
496 "fitContainer": true,
497 "tableName": reader.table,
500 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
506 "autoExpandColumn": reader.firstTxtCol,
509 "|render": "function() \n" +
511 " _this.grid = this; \n" +
512 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
513 " if (_this.panel.active) {\n" +
514 " this.footer.onClick('first');\n" +
522 "*prop": "dataSource",
530 "xtype": "HttpProxy",
532 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
540 "xtype": "PagingToolbar",
543 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
544 "emptyMsg": "No " + reader.table + " found",
555 "cls": "x-btn-text-icon",
556 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
558 "|click": "function()\n"+
560 " //yourdialog.show( { id : 0 } , function() {\n"+
561 " // _this.grid.footer.onClick('first');\n"+
565 "|xns": "Roo.Toolbar"
570 "cls": "x-btn-text-icon",
571 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
573 "|click": "function()\n"+
575 " var s = _this.grid.getSelectionModel().getSelections();\n"+
576 " if (!s.length || (s.length > 1)) {\n"+
577 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
581 " //_this.dialog.show(s[0].data, function() {\n"+
582 " // _this.grid.footer.onClick('first');\n"+
588 "|xns": "Roo.Toolbar"
592 "cls": "x-btn-text-icon",
593 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
596 "|click": "function()\n"+
598 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
602 "|xns": "Roo.Toolbar"
606 ].concat( reader.colmodel)
616 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
617 if (!File.isDirectory(dir)) {
620 var formElements = [];
621 for (var k in reader.form) {
622 if (k == 'id') { // should really do primary key testing..
625 formElements.push(reader.form[k]);
627 formElements.push(reader.form['id']);
629 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
632 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
639 "|actioncomplete" : "function(_self,action)\n"+
641 " if (action.type == 'setdata') {\n"+
642 " //_this.dialog.el.mask(\"Loading\");\n"+
643 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
646 " if (action.type == 'load') {\n"+
647 " _this.dialog.el.unmask();\n"+
650 " if (action.type =='submit') {\n"+
652 " _this.dialog.el.unmask();\n"+
653 " _this.dialog.hide();\n"+
655 " if (_this.callback) {\n"+
656 " _this.callback.call(_this, _this.form.getValues());\n"+
658 " _this.form.reset();\n"+
663 "|rendered" : "function (form)\n"+
665 " _this.form= form;\n"+
669 style : "margin:10px;",
670 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
681 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
682 if (!File.isDirectory(dir)) {
686 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
689 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
692 JSON.stringify(reader.combo, null, 4)