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
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
31 if (args.length < 1) {
36 INI : "/path/to/mydb.ini",
38 print("Usage : seed generate.js '" + JSON.stringify(sample) +"'");
41 var cfg = JSON.parse(args[0]);
43 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DB_NAME,
44 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
45 Gda.ConnectionOptions.NONE, null);
52 Gda.DataSelect.prototype.fetchAll = function()
56 for (var i =0;i < this.get_n_columns(); i++) {
57 cols.push(this.get_column_name(i));
60 var iter = this.create_iter();
62 while (iter.move_next()) {
63 if (cols.length == 1) {
64 res.push(iter.get_value_at(0).get_string());
69 cols.forEach(function(n,i) {
70 var val = iter.get_value_at(i);
71 var type = GObject.type_name(val.g_type) ;
72 var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
73 // print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
74 //print (n + '=' + iter.get_value_at(i).value);
87 'datetime' : 'string',
96 'longtext' : 'string',
97 'mediumtext' : 'string',
107 var key_file = GLib.key_file_new();
108 if (!GLib.key_file_load_from_file (key_file, fn , GLib.KeyFileFlags.NONE )) {
112 var groups = GLib.key_file_get_groups(key_file);
113 groups.forEach(function(g) {
116 var keys = GLib.key_file_get_keys(key_file,g);
117 keys.forEach(function(k) {
118 ini[g][k] = GLib.key_file_get_value(key_file,g,k);
123 if (File.isFile(cfg.INI)) {
124 if (cfg.INI.match(/links\.ini$/)) {
127 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
131 if (File.isDirectory(cfg.INI)) {
134 //--- load ini files..
135 // this is very specific.
136 var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter(
137 function(e) { return e.match(/^Pman/); }
140 dirs.forEach(function(d) {
141 // this currently misses the web.*/Pman/XXXX/DataObjects..
142 var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
143 if (!File.isDirectory(path)) {
144 path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
146 if (!File.isDirectory(path)) {
149 var inis = File.list(path).filter(
150 function(e) { return e.match(/\.links\.ini$/); }
156 inis.forEach(function(i) {
157 readIni(path + '/' + i);
165 print(JSON.stringify(ini, null,4));
171 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
177 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
179 tables.forEach(function(table) {
181 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
184 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
188 var firstTxtCol = '';
192 schema.forEach(function(e) {
193 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
205 if (typeof(map[e.Type]) == 'undefined') {
208 name: "ArgumentError",
209 message: "Unknown mapping for type : " + e.Type
212 row.type = map[e.Type];
214 if (row.type == 'string' && !firstTxtCol.length) {
215 firstTxtCol = row.name;
218 if (row.type == 'date') {
219 row.dateFormat = 'Y-m-d';
223 if (combofields.length == 1 && row.type == 'string') {
224 combofields.push(row);
228 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
229 title = title[0].toUpperCase() + title.substring(1);
232 "xtype": "ColumnModel",
234 "width": row.type == 'string' ? 200 : 75,
235 "dataIndex": row.name,
236 "|renderer": row.type != 'date' ?
237 "function(v) { return String.format('{0}', v); }" :
238 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
240 "*prop": "colModel[]"
242 var xtype = 'TextField';
243 if (row.type == 'number') {
244 xtype = 'NumberField';
246 if (row.type == 'date') {
249 if (e.Type == 'text') {
253 // what about booleans.. -> checkboxes..
260 width : row.type == 'string' ? 200 : 75,
264 if (xtype == 'TextArea') {
265 form[row.name].height = 100;
274 allowBlank : 'false',
276 emptyText : 'Select ' + table,
277 forceSelection : true,
279 loadingText: 'Searching...',
282 qtip: 'Select ' + table,
288 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
289 queryParam : '',// SET WHEN USED
290 fieldLabel : table, // SET WHEN USED
291 hiddenName : '', // SET WHEN USED eg. project_id
292 name : '', // SET WHEN USED eg. project_id_name
300 '|beforeload' : 'function (_self, o)' +
302 " o.params = o.params || {};\n" +
303 " // set more here\n" +
309 'xtype' : 'HttpProxy',
312 '|url' : "baseURL + '/Roo/" + table + ".php'",
317 'xtype' : 'JsonReader',
321 'totalProperty' : 'total',
322 '|fields' : JSON.stringify(combofields)
333 //print(JSON.stringify(reader,null,4));
337 combofields : combofields,
339 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
341 firstTxtCol : firstTxtCol,
345 //console.dump(schema );
352 // merge in the linked tables..
353 readers.forEach(function(reader) {
354 if (typeof(ini[reader.table]) == 'undefined') {
358 print("OVERLAY - " + reader.table);
360 for (var col in ini[reader.table]) {
361 var kv = ini[reader.table][col].split(':');
362 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
364 // merge in data (eg. project_id => project_id_*****
366 add.oreader.forEach(function(or) {
371 name : col + '_' + or.name,
376 // col is mapped to something..
377 var combofields = add.combofields;
378 var combofields_name = add.combofields[1].name;
379 var old = reader.form[col];
380 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
381 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
382 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
383 reader.form[col].hiddenName : old.name; // SET WHEN USED eg. project_id
384 reader.form[col].name : old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
385 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
393 //readers.forEach(function(reader) {
394 // delete reader.oreader;
401 //print(JSON.stringify(readers, null, 4));
403 readers.forEach(function(reader) {
406 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
407 if (!File.isDirectory(dir)) {
412 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
417 xtype : "JsonReader",
418 totalProperty : "total",
421 id : 'id', // maybe no..
422 '|fields' : JSON.stringify(reader.reader, null,4)
426 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
427 JSON.stringify(jreader, null, 4)
432 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
433 if (!File.isDirectory(dir)) {
438 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
441 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
447 "title": reader.table,
449 "fitContainer": true,
450 "tableName": reader.table,
453 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
459 "autoExpandColumn": reader.firstTxtCol,
462 "|render": "function() \n" +
464 " _this.grid = this; \n" +
465 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
466 " if (_this.panel.active) {\n" +
467 " this.footer.onClick('first');\n" +
475 "*prop": "dataSource",
483 "xtype": "HttpProxy",
485 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
493 "xtype": "PagingToolbar",
496 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
497 "emptyMsg": "No " + reader.table + " found",
508 "cls": "x-btn-text-icon",
509 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
511 "|click": "function()\n"+
513 " //yourdialog.show( { id : 0 } , function() {\n"+
514 " // _this.grid.footer.onClick('first');\n"+
523 "cls": "x-btn-text-icon",
524 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
526 "|click": "function()\n"+
528 " var s = _this.grid.getSelectionModel().getSelections();\n"+
529 " if (!s.length || (s.length > 1)) {\n"+
530 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
534 " //_this.dialog.show(s[0].data, function() {\n"+
535 " // _this.grid.footer.onClick('first');\n"+
545 "cls": "x-btn-text-icon",
546 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
549 "|click": "function()\n"+
551 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
559 ].concat( reader.colmodel)
569 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
570 if (!File.isDirectory(dir)) {
574 for (var k in reader.form) {
575 if (k == 'id') { // should really do primary key testing..
578 formElements.push(reader.form[k]);
581 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
584 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
591 "|actioncomplete" : "function(_self,action)\n"+
593 " if (action.type == 'setdata') {\n"+
594 " //_this.dialog.el.mask(\"Loading\");\n"+
595 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
598 " if (action.type == 'load') {\n"+
599 " _this.dialog.el.unmask();\n"+
602 " if (action.type =='submit') {\n"+
604 " _this.dialog.el.unmask();\n"+
605 " _this.dialog.hide();\n"+
607 " if (_this.callback) {\n"+
608 " _this.callback.call(_this, _this.form.getValues());\n"+
610 " _this.form.reset();\n"+
615 "|rendered" : "function (form)\n"+
621 style : "margin:10px;",
622 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",