database.factory.js 3.4 KB

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