Builder/Provider/Database/generate.js
[app.Builder.js] / Builder / Provider / Database / generate.js
index 0cb9dd3..54ceb04 100644 (file)
@@ -12,6 +12,7 @@
  * 
  * write readers..
  * 
+ * usage: seed generate.js  '{"DB_NAME":"XXX","USERNAME":"YYY","PASSWORD":"ZZZ","INI":"/path/to/mydb.ini"}'
  * 
  */
 Gda  = imports.gi.Gda;
@@ -27,6 +28,7 @@ var prov = Gda.Config.list_providers ();
 //print(prov.dump_as_string());
 var args = Array.prototype.slice.call(Seed.argv);
 args.shift();args.shift();// remove first 2
+print(args.length );
 if (args.length < 1) {
     var sample = {
         DB_NAME : "XXX",
@@ -55,21 +57,26 @@ Gda.DataSelect.prototype.fetchAll = function()
     for (var i =0;i < this.get_n_columns(); i++) {
         cols.push(this.get_column_name(i));
     }
-    //console.dump(cols);
+    //print(JSON.stringify(cols, null,4));
     var iter = this.create_iter();
     var res = [];
-    while (iter.move_next()) {
+    //print(this.get_n_rows());
+    var _this = this;
+    for (var r = 0; r < this.get_n_rows(); r++) {
+        
+        // single clo..
+        //print("GOT ROW");
         if (cols.length == 1) {
-            res.push(iter.get_value_at(0).get_string());
+            res.push(this.get_value_at(0,r).get_string());
             continue;
         }
         var add = { };
         
         cols.forEach(function(n,i) {
-           var val = iter.get_value_at(i);
-           var type = GObject.type_name(val.g_type) ;
-           var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
-         //  print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
+            var val = _this.get_value_at(i,r);
+            var type = GObject.type_name(val.g_type) ;
+            var vs = type == 'GdaBinary' ? val.value.to_string(1024) : val.value;
+            print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
             //print (n + '=' + iter.get_value_at(i).value);
             add[n] = vs;
         });
@@ -103,18 +110,18 @@ var ini = { }
 
 function readIni(fn)
 {
-    var key_file = GLib.key_file_new();
-    if (!GLib.key_file_load_from_file (key_file, fn , GLib.KeyFileFlags.NONE )) {
+    var key_file = new GLib.KeyFile.c_new();
+    if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
         return;
     }
    
-    var groups = GLib.key_file_get_groups(key_file);
+    var groups = key_file.get_groups();
     groups.forEach(function(g) {
         ini[g] = {}
            
-        var keys = GLib.key_file_get_keys(key_file,g);
+        var keys = key_file.get_keys(g);
         keys.forEach(function(k) {
-            ini[g][k] = GLib.key_file_get_value(key_file,g,k);
+            ini[g][k] = key_file.get_value(g,k);
         })
     })
     
@@ -132,16 +139,21 @@ if (File.isDirectory(cfg.INI)) {
 
     //--- load ini files..
     // this is very specific.
-    var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter( 
-        function(e) { return e.match(/^Pman/); }
+    
+    var dirs = File.list( cfg.INI + '/Pman').filter( 
+        function(e) { 
+            if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
+                return false;
+            }
+            return true;
+        }
     );
     
+     
     dirs.forEach(function(d) {
         // this currently misses the web.*/Pman/XXXX/DataObjects..
-        var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
-        if (!File.isDirectory(path)) {
-            path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
-        }
+        var path = cfg.INI + '/Pman/' + d + '/DataObjects';
+         
         if (!File.isDirectory(path)) {
             return; //skip
         }
@@ -156,11 +168,21 @@ if (File.isDirectory(cfg.INI)) {
             readIni(path + '/' + i); 
             
         })
-
-        
-        
     });
+    // look at web.XXXX/Pman/XXX/DataObjects/*.ini
+    var inis = File.list(cfg.INI).filter(
+        function(e) { return e.match(/\.links\.ini$/); }
+    )
+    
+     inis.forEach(function(i) {
+        readIni(path + '/' + i); 
+        
+    })
+    
+    
 }
+print(JSON.stringify(ini, null,4));
  //console.dump(ini);
 
 
@@ -170,13 +192,7 @@ if (File.isDirectory(cfg.INI)) {
 
 
 
-
-
-
-
-
-
-
 
 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
 var readers = [];
@@ -184,6 +200,15 @@ tables.forEach(function(table) {
     //print(table);
     var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
     var reader = []; 
+    var colmodel = []; 
+    var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
+         
+    var form = {}
+       
+    var firstTxtCol = '';
+    
+    print(JSON.stringify(schema, null,4));
+    
     schema.forEach(function(e)  {
         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
         var row  = { }; 
@@ -205,17 +230,143 @@ tables.forEach(function(table) {
             };
         }
         row.type = map[e.Type];
+        
+        if (row.type == 'string' && !firstTxtCol.length) {
+            firstTxtCol = row.name;
+        }
+        
         if (row.type == 'date') {
             row.dateFormat = 'Y-m-d';
         }
         reader.push(row);
         
+        if (combofields.length == 1 && row.type == 'string') {
+            combofields.push(row);
+        }
+        
+        
+        var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
+        title  = title[0].toUpperCase() + title.substring(1);
+        
+        colmodel.push({
+            "xtype": "ColumnModel",
+            "header": title,
+            "width":  row.type == 'string' ? 200 : 75,
+            "dataIndex": row.name,
+            "|renderer": row.type != 'date' ? 
+                    "function(v) { return String.format('{0}', v); }" :
+                    "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
+            "|xns": "Roo.grid",
+            "*prop": "colModel[]"
+        });
+        var xtype = 'TextField';
+        if (row.type == 'number') {
+            xtype = 'NumberField';
+        }
+        if (row.type == 'date') {
+            xtype = 'DateField';
+        }
+        if (e.Type == 'text') {
+            xtype = 'TextArea';
+        }
+        if (e.name == 'id') {
+            xtype = 'Hidden';
+        }
+        // what about booleans.. -> checkboxes..
+        
+        
+        
+        form[row.name] = {
+            fieldLabel : title,
+            name : row.name,
+            width : row.type == 'string' ? 200 : 75,
+            '|xns' : 'Roo.form',
+            xtype : xtype
+        }
+        if (xtype == 'TextArea') {
+            form[row.name].height = 100;
+        }
+        
+        
     });
-    print(JSON.stringify(reader,null,4));
+    
+    var combo = {
+        '|xns' : 'Roo.form',
+        xtype: 'ComboBox',
+        allowBlank : 'false',
+        editable : 'false',
+        emptyText : 'Select ' + table,
+        forceSelection : true,
+        listWidth : 400,
+        loadingText: 'Searching...',
+        minChars : 2,
+        pageSize : 20,
+        qtip: 'Select ' + table,
+        selectOnFocus: true,
+        triggerAction : 'all',
+        typeAhead: true,
+        
+        width: 300,
+        
+        
+        
+        tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
+        queryParam : '',// SET WHEN USED
+        fieldLabel : table,  // SET WHEN USED
+        valueField : 'id',
+        displayField : '', // SET WHEN USED eg. project_id_name
+        hiddenName : '', // SET WHEN USED eg. project_id
+        name : '', // SET WHEN USED eg. project_id_name
+        items : [
+            {
+                    
+                '*prop' : 'store',
+                'xtype' : 'Store',
+                '|xns' : 'Roo.data',
+                listeners : {
+                    '|beforeload' : 'function (_self, o)' +
+                    "{\n" +
+                    "    o.params = o.params || {};\n" +
+                    "    // set more here\n" +
+                    "}\n"
+                },
+                items : [
+                    {
+                        '*prop' : 'proxy',
+                        'xtype' : 'HttpProxy',
+                        'method' : 'GET',
+                        '|xns' : 'Roo.data',
+                        '|url' : "baseURL + '/Roo/" + table + ".php'",
+                    },
+                    
+                    {
+                        '*prop' : 'reader',
+                        'xtype' : 'JsonReader',
+                        '|xns' : 'Roo.data',
+                        'id' : 'id',
+                        'root' : 'data',
+                        'totalProperty' : 'total',
+                        '|fields' : JSON.stringify(combofields)
+                        
+                    }
+                ]
+            }
+        ]
+    }
+    
+    
+    
+    
+    //print(JSON.stringify(reader,null,4));
     readers.push({
         table : table ,
+        combo : combo,
+        combofields : combofields,
         reader :  reader,
-        oreader : JSON.parse(JSON.stringify(reader)) // dupe it..
+        oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
+        colmodel : colmodel,
+        firstTxtCol : firstTxtCol,
+        form : form
     });
     
     //console.dump(schema );
@@ -231,26 +382,50 @@ readers.forEach(function(reader) {
      
         return;
     }
-   print("OVERLAY - " + reader.table);
+    print("OVERLAY - " + reader.table);
     // we have a map..
     for (var col in ini[reader.table]) {
         var kv = ini[reader.table][col].split(':');
         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
+        
+        // merge in data (eg. project_id => project_id_*****
+     
         add.oreader.forEach(function(or) {
             reader.reader.push({
                 name : col + '_' + or.name,
                 type : or.type
             });
         });
+        
+        // col is mapped to something..
+        var combofields = add.combofields;
+        if (add.combofields.length < 2) {
+            continue;
+        }
+        if (typeof(reader.form[col]) == 'undefined') {
+            print("missing linked column " + col);
+            continue;
+        }
+        
+        var combofields_name = add.combofields[1].name;
+        var old =   reader.form[col];
+        reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
+        reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
+        reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
+        reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
+        reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
+        reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
+        reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
+        
              
     };
     
     
 });
 
-readers.forEach(function(reader) {
-    delete reader.oreader;
-});
+//readers.forEach(function(reader) {
+//    delete reader.oreader;
+//});
 
  
 
@@ -265,6 +440,8 @@ readers.forEach(function(reader) {
     if (!File.isDirectory(dir)) {
         File.mkdir(dir);
     }
+    
+    // READERS
     print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
     
                 
@@ -275,7 +452,7 @@ readers.forEach(function(reader) {
         root : "data",
         '*prop' : "reader",
         id : 'id', // maybe no..
-        '|fields' :  JSON.stringify(reader.reader, null,4)
+        '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
     };
     
     File.write(
@@ -283,13 +460,17 @@ readers.forEach(function(reader) {
         JSON.stringify(jreader, null, 4)
     )
     
+    
+    // GRIDS
     dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel'; 
     if (!File.isDirectory(dir)) {
         File.mkdir(dir);
     }
+    
+
     print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
     
-      File.write(
+    File.write(
         dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
             
        
@@ -304,49 +485,209 @@ readers.forEach(function(reader) {
             "listeners": {
                 "|activate": "function() {\n    _this.panel = this;\n    if (_this.grid) {\n        _this.grid.footer.onClick('first');\n    }\n}"
             },
-        
             "items": [
                 {
-                    "*prop": "dataSource",
-                    "xtype": "Store",
-                    
-                    "|xns": "Roo.data",
+                    "*prop": "grid",
+                    "xtype": "Grid",
+                    "autoExpandColumn": reader.firstTxtCol,
+                    "loadMask": true,
+                    "listeners": {
+                        "|render": "function() \n" +
+                            "{\n" +
+                            "   _this.grid = this; \n" +
+                            "    //_this.dialog = Pman.Dialog.FILL_IN\n" +
+                            "    if (_this.panel.active) {\n" +
+                            "       this.footer.onClick('first');\n" +
+                            "    }\n" +
+                            "}"
+                    },
+                    "|xns": "Roo.grid",
+
                     "items": [
-                        
                         {
-                            "*prop": "proxy",
-                            "xtype": "HttpProxy",
-                            "method": "GET",
-                            "|url": "baseURL + '/Roo/" + reader.table ".php'",
-                            "|xns": "Roo.data"
+                            "*prop": "dataSource",
+                            "xtype": "Store",
+                            
+                            "|xns": "Roo.data",
+                            "items": [
+                                
+                                {
+                                    "*prop": "proxy",
+                                    "xtype": "HttpProxy",
+                                    "method": "GET",
+                                    "|url": "baseURL + '/Roo/" + reader.table + ".php'",
+                                    "|xns": "Roo.data"
+                                },
+                                jreader
+                            ]
                         },
-                        jreader
-                    ]
-                },
-                {
-                    "*prop": "footer",
-                    "xtype": "PagingToolbar",
-                    "pageSize": 25,
-                    "displayInfo": true,
-                    "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
-                    "emptyMsg": "No " + reader.table + " found",
-                    "|xns": "Roo"
-                },
-
-            
-            
+                        {
+                            "*prop": "footer",
+                            "xtype": "PagingToolbar",
+                            "pageSize": 25,
+                            "displayInfo": true,
+                            "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
+                            "emptyMsg": "No " + reader.table + " found",
+                            "|xns": "Roo"
+                        },
+                        {
+                            "*prop": "toolbar",
+                            "xtype": "Toolbar",
+                            "|xns": "Roo",
+                            "items": [
+                                {
+                                    "text": "Add",
+                                    "xtype": "Button",
+                                    "cls": "x-btn-text-icon",
+                                    "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
+                                    "listeners": {
+                                        "|click": "function()\n"+
+                                            "{\n"+
+                                            "   //yourdialog.show( { id : 0 } , function() {\n"+
+                                            "   //  _this.grid.footer.onClick('first');\n"+
+                                            "   //}); \n"+
+                                            "}\n"
+                                    },
+                                    "|xns": "Roo"
+                                },
+                                {
+                                    "text": "Edit",
+                                    "xtype": "Button",
+                                    "cls": "x-btn-text-icon",
+                                    "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
+                                    "listeners": {
+                                        "|click": "function()\n"+
+                                            "{\n"+
+                                            "    var s = _this.grid.getSelectionModel().getSelections();\n"+
+                                            "    if (!s.length || (s.length > 1))  {\n"+
+                                            "        Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
+                                            "        return;\n"+
+                                            "    }\n"+
+                                            "    \n"+
+                                            "    //_this.dialog.show(s[0].data, function() {\n"+
+                                            "    //    _this.grid.footer.onClick('first');\n"+
+                                            "    //   }); \n"+
+                                            "    \n"+
+                                            "}\n" 
+                                        
+                                    },
+                                    "|xns": "Roo"
+                                },
+                                {
+                                    "text": "Delete",
+                                    "cls": "x-btn-text-icon",
+                                    "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
+                                    "xtype": "Button",
+                                    "listeners": {
+                                        "|click": "function()\n"+
+                                            "{\n"+
+                                            "   //Pman.genericDelete(_this, _this.grid.tableName); \n"+
+                                            "}\n"+
+                                            "        "
+                                    },
+                                    "|xns": "Roo"
+                                }
+                            ]
+                        }, // end toolbar
+                    ].concat( reader.colmodel)
+                }
+            ]
             
             
         }, null, 4)
     )
     
+    /// FORMS..
     
+    dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form'; 
+    if (!File.isDirectory(dir)) {
+        File.mkdir(dir);
+    }
+    var formElements = [];
+    for (var k in reader.form) {
+        if (k == 'id') { // should really do primary key testing..
+            continue;
+        }
+        formElements.push(reader.form[k]);
+    }
+    formElements.push(reader.form['id']);
+
+    print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
     
+    File.write(
+        dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
+            
+       
+        JSON.stringify({
+            '|xns' : 'Roo.form',
+            xtype : "Form",
+            listeners : {
+                "|actioncomplete" : "function(_self,action)\n"+
+                    "{\n"+
+                    "    if (action.type == 'setdata') {\n"+
+                    "       //_this.dialog.el.mask(\"Loading\");\n"+
+                    "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
+                    "       return;\n"+
+                    "    }\n"+
+                    "    if (action.type == 'load') {\n"+
+                    "        _this.dialog.el.unmask();\n"+
+                    "        return;\n"+
+                    "    }\n"+
+                    "    if (action.type =='submit') {\n"+
+                    "    \n"+
+                    "        _this.dialog.el.unmask();\n"+
+                    "        _this.dialog.hide();\n"+
+                    "    \n"+
+                    "         if (_this.callback) {\n"+
+                    "            _this.callback.call(_this, _this.form.getValues());\n"+
+                    "         }\n"+
+                    "         _this.form.reset();\n"+
+                    "         return;\n"+
+                    "    }\n"+
+                    "}\n",
+                
+                "|rendered" : "function (form)\n"+
+                    "{\n"+
+                    "    _this.form= form;\n"+
+                    "}\n"
+            },
+            method : "POST",
+            style : "margin:10px;",
+            "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
+            items : formElements
+        }, null, 4)
+    );
+            
+            
+   
+   
+   
+     /// COMBO..
     
+    dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
+    if (!File.isDirectory(dir)) {
+        File.mkdir(dir);
+    }
+   
+    print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
     
-
-});
-
+    File.write(
+        dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
+            
+       
+        JSON.stringify(reader.combo, null, 4)
+    );
+            
+   
+   
+   
+   
+   
+   
+   
+   
+   
+});