database.factory.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .factory('databaseFactory', function (odooFactory, configFactory, asyncLoopFactory) {
  6. var mappings = [
  7. {
  8. model: 'res.partner',
  9. table: 'partner'
  10. },
  11. {
  12. model: 'crm.lead',
  13. table: 'crm_lead'
  14. },
  15. {
  16. model: 'crm.case.stage',
  17. table: 'stages'
  18. }
  19. ];
  20. /**
  21. *
  22. */
  23. var buildCreateStatement = function (table, model) {
  24. var statement = 'CREATE TABLE IF NOT EXISTS ' + table + ' (\n';
  25. for (var key in model) {
  26. if (model.hasOwnProperty(key) && typeof model[key] == 'object') {
  27. if (model[key].type == 'float') {
  28. statement += key + ' REAL DEFAULT 0,\n';
  29. }
  30. if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'datetime') {
  31. statement += key + ' TEXT DEFAULT NULL,\n';
  32. }
  33. if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'one2many' || model[key].type == 'many2many') {
  34. statement += key + ' INTEGER DEFAULT 0,\n';
  35. }
  36. if (model[key].type == 'many2one') {
  37. continue;
  38. }
  39. }
  40. }
  41. statement += 'remote_id INTEGER DEFAULT 0,\n' +
  42. 'modified INTEGER DEFAULT 0,\n' +
  43. 'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n';
  44. return statement + ');'
  45. }
  46. /**
  47. *
  48. */
  49. var fetchMaps = function (success, error) {
  50. var maps = [];
  51. configFactory(function (configuration) {
  52. asyncLoopFactory(mappings.length, function (loop) {
  53. var map = mappings[loop.iteration()];
  54. odooFactory.fields(map.model, configuration, function (response) {
  55. maps.push({ table: map.table, model: response });
  56. loop.next();
  57. }, function (odooErr) {
  58. console.log(odooErr);
  59. loop.next();
  60. });
  61. }, function () {
  62. success(maps);
  63. });
  64. }, function (configErr) {
  65. error(configErr);
  66. });
  67. }
  68. /**
  69. *
  70. */
  71. var initTables = function (success, error) {
  72. fetchMaps(function (maps) {
  73. asyncLoopFactory(maps.length, function (loop) {
  74. var map = maps[loop.iteration()];
  75. var sql = buildCreateStatement(map.table, map.model);
  76. db.executeSql(sql, [], function (result) {
  77. console.log(result);
  78. loop.next();
  79. }, function (err) {
  80. console.log(err);
  81. loop.next();
  82. });
  83. }, function () {
  84. success(maps);
  85. });
  86. }, function (err) {
  87. error(err);
  88. });
  89. }
  90. return {
  91. fetchMaps: fetchMaps,
  92. initTables: initTables
  93. }
  94. });