1 //<script type="text/javascript">
5 * Let's see if libgda can be used to generate our Readers for roo...
7 * Concept - conect to database..
15 * usage: seed generate.js '{"DB_NAME":"XXX","USERNAME":"YYY","PASSWORD":"ZZZ","INI":"/path/to/mydb.ini"}'
19 GObject = imports.gi.GObject;
21 GLib = imports.gi.GLib;
23 console = imports['../../../console.js'];
24 File = imports['../../../File.js'].File;
27 var prov = Gda.Config.list_providers ();
28 //print(prov.dump_as_string());
29 var args = Array.prototype.slice.call(Seed.argv);
30 args.shift();args.shift();// remove first 2
32 if (args.length < 1) {
37 INI : "/path/to/mydb.ini",
39 print("Usage : seed generate.js '" + JSON.stringify(sample) +"'");
42 var cfg = JSON.parse(args[0]);
44 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DB_NAME,
45 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
46 Gda.ConnectionOptions.NONE, null);
53 Gda.DataSelect.prototype.fetchAll = function()
57 for (var i =0;i < this.get_n_columns(); i++) {
58 cols.push(this.get_column_name(i));
61 var iter = this.create_iter();
63 while (iter.move_next()) {
64 if (cols.length == 1) {
65 res.push(iter.get_value_at(0).get_string());
70 cols.forEach(function(n,i) {
71 var val = iter.get_value_at(i);
72 var type = GObject.type_name(val.g_type) ;
73 var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
74 // print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
75 //print (n + '=' + iter.get_value_at(i).value);
88 'datetime' : 'string',
97 'longtext' : 'string',
98 'mediumtext' : 'string',
108 var key_file = new GLib.KeyFile.c_new();
109 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
113 var groups = key_file.get_groups();
114 groups.forEach(function(g) {
117 var keys = key_file.get_keys(g);
118 keys.forEach(function(k) {
119 ini[g][k] = key_file.get_value(g,k);
124 if (File.isFile(cfg.INI)) {
125 if (cfg.INI.match(/links\.ini$/)) {
128 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
132 if (File.isDirectory(cfg.INI)) {
135 //--- load ini files..
136 // this is very specific.
137 var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter(
138 function(e) { return e.match(/^Pman/); }
141 dirs.forEach(function(d) {
142 // this currently misses the web.*/Pman/XXXX/DataObjects..
143 var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
144 if (!File.isDirectory(path)) {
145 path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
148 if (!File.isDirectory(path)) {
151 var inis = File.list(path).filter(
152 function(e) { return e.match(/\.links\.ini$/); }
158 inis.forEach(function(i) {
159 readIni(path + '/' + i);
164 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
165 var inis = File.list(cfg.INI).filter(
166 function(e) { return e.match(/\.links\.ini$/); }
169 inis.forEach(function(i) {
170 readIni(path + '/' + i);
176 print(JSON.stringify(ini, null,4));
182 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
188 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
190 tables.forEach(function(table) {
192 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
195 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
199 var firstTxtCol = '';
203 schema.forEach(function(e) {
204 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
216 if (typeof(map[e.Type]) == 'undefined') {
219 name: "ArgumentError",
220 message: "Unknown mapping for type : " + e.Type
223 row.type = map[e.Type];
225 if (row.type == 'string' && !firstTxtCol.length) {
226 firstTxtCol = row.name;
229 if (row.type == 'date') {
230 row.dateFormat = 'Y-m-d';
234 if (combofields.length == 1 && row.type == 'string') {
235 combofields.push(row);
239 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
240 title = title[0].toUpperCase() + title.substring(1);
243 "xtype": "ColumnModel",
245 "width": row.type == 'string' ? 200 : 75,
246 "dataIndex": row.name,
247 "|renderer": row.type != 'date' ?
248 "function(v) { return String.format('{0}', v); }" :
249 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
251 "*prop": "colModel[]"
253 var xtype = 'TextField';
254 if (row.type == 'number') {
255 xtype = 'NumberField';
257 if (row.type == 'date') {
260 if (e.Type == 'text') {
263 if (e.name == 'id') {
266 // what about booleans.. -> checkboxes..
273 width : row.type == 'string' ? 200 : 75,
277 if (xtype == 'TextArea') {
278 form[row.name].height = 100;
287 allowBlank : 'false',
289 emptyText : 'Select ' + table,
290 forceSelection : true,
292 loadingText: 'Searching...',
295 qtip: 'Select ' + table,
297 triggerAction : 'all',
304 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
305 queryParam : '',// SET WHEN USED
306 fieldLabel : table, // SET WHEN USED
308 displayField : '', // SET WHEN USED eg. project_id_name
309 hiddenName : '', // SET WHEN USED eg. project_id
310 name : '', // SET WHEN USED eg. project_id_name
318 '|beforeload' : 'function (_self, o)' +
320 " o.params = o.params || {};\n" +
321 " // set more here\n" +
327 'xtype' : 'HttpProxy',
330 '|url' : "baseURL + '/Roo/" + table + ".php'",
335 'xtype' : 'JsonReader',
339 'totalProperty' : 'total',
340 '|fields' : JSON.stringify(combofields)
351 //print(JSON.stringify(reader,null,4));
355 combofields : combofields,
357 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
359 firstTxtCol : firstTxtCol,
363 //console.dump(schema );
370 // merge in the linked tables..
371 readers.forEach(function(reader) {
372 if (typeof(ini[reader.table]) == 'undefined') {
376 print("OVERLAY - " + reader.table);
378 for (var col in ini[reader.table]) {
379 var kv = ini[reader.table][col].split(':');
380 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
382 // merge in data (eg. project_id => project_id_*****
384 add.oreader.forEach(function(or) {
386 name : col + '_' + or.name,
391 // col is mapped to something..
392 var combofields = add.combofields;
393 if (add.combofields.length < 2) {
398 var combofields_name = add.combofields[1].name;
399 var old = reader.form[col];
400 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
401 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
402 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
403 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
404 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
405 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
406 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
414 //readers.forEach(function(reader) {
415 // delete reader.oreader;
422 //print(JSON.stringify(readers, null, 4));
424 readers.forEach(function(reader) {
427 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
428 if (!File.isDirectory(dir)) {
433 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
438 xtype : "JsonReader",
439 totalProperty : "total",
442 id : 'id', // maybe no..
443 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
447 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
448 JSON.stringify(jreader, null, 4)
453 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
454 if (!File.isDirectory(dir)) {
459 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
462 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
468 "title": reader.table,
470 "fitContainer": true,
471 "tableName": reader.table,
474 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
480 "autoExpandColumn": reader.firstTxtCol,
483 "|render": "function() \n" +
485 " _this.grid = this; \n" +
486 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
487 " if (_this.panel.active) {\n" +
488 " this.footer.onClick('first');\n" +
496 "*prop": "dataSource",
504 "xtype": "HttpProxy",
506 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
514 "xtype": "PagingToolbar",
517 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
518 "emptyMsg": "No " + reader.table + " found",
529 "cls": "x-btn-text-icon",
530 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
532 "|click": "function()\n"+
534 " //yourdialog.show( { id : 0 } , function() {\n"+
535 " // _this.grid.footer.onClick('first');\n"+
544 "cls": "x-btn-text-icon",
545 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
547 "|click": "function()\n"+
549 " var s = _this.grid.getSelectionModel().getSelections();\n"+
550 " if (!s.length || (s.length > 1)) {\n"+
551 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
555 " //_this.dialog.show(s[0].data, function() {\n"+
556 " // _this.grid.footer.onClick('first');\n"+
566 "cls": "x-btn-text-icon",
567 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
570 "|click": "function()\n"+
572 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
580 ].concat( reader.colmodel)
590 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
591 if (!File.isDirectory(dir)) {
594 var formElements = [];
595 for (var k in reader.form) {
596 if (k == 'id') { // should really do primary key testing..
599 formElements.push(reader.form[k]);
601 formElements.push(reader.form['id']);
603 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
606 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
613 "|actioncomplete" : "function(_self,action)\n"+
615 " if (action.type == 'setdata') {\n"+
616 " //_this.dialog.el.mask(\"Loading\");\n"+
617 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
620 " if (action.type == 'load') {\n"+
621 " _this.dialog.el.unmask();\n"+
624 " if (action.type =='submit') {\n"+
626 " _this.dialog.el.unmask();\n"+
627 " _this.dialog.hide();\n"+
629 " if (_this.callback) {\n"+
630 " _this.callback.call(_this, _this.form.getValues());\n"+
632 " _this.form.reset();\n"+
637 "|rendered" : "function (form)\n"+
639 " _this.form= form;\n"+
643 style : "margin:10px;",
644 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
655 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
656 if (!File.isDirectory(dir)) {
660 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
663 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
666 JSON.stringify(reader.combo, null, 4)