configuration_controller.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ConfigurationController = Em.Controller.extend({
  20. name: 'configurationController',
  21. /**
  22. * get configs by tags
  23. * return Deferred object with configs as argument
  24. * @param tags
  25. * @return {object}
  26. */
  27. getConfigsByTags: function (tags) {
  28. var storedTags = [];
  29. App.db.getConfigs().forEach(function (site) {
  30. storedTags.push({
  31. siteName: site.type,
  32. tagName: site.tag
  33. })
  34. });
  35. if (this.checkTagsChanges(tags, storedTags)) {
  36. return this.loadFromServer(tags);
  37. } else {
  38. return this.loadFromDB(tags.mapProperty('siteName'));
  39. }
  40. },
  41. /**
  42. * check whether tag versions have been changed
  43. * if they are different then return true
  44. * otherwise false
  45. * @param tags
  46. * @param storedTags
  47. * @return {Boolean}
  48. */
  49. checkTagsChanges: function (tags, storedTags) {
  50. var isDifferent = false;
  51. var i = 0;
  52. while (i < tags.length && !isDifferent) {
  53. var storedTag = storedTags.findProperty('siteName', tags[i].siteName);
  54. isDifferent = (!storedTag || storedTag.tagName !== tags[i].tagName);
  55. i++;
  56. }
  57. return isDifferent;
  58. },
  59. loadFromDB: function (siteNames) {
  60. var dfd = $.Deferred();
  61. var configs = App.db.getConfigs().filter(function (site) {
  62. return (siteNames.contains(site.type));
  63. });
  64. dfd.resolve(configs);
  65. return dfd.promise()
  66. },
  67. /**
  68. * load configs from server
  69. * and update them in local DB
  70. * @param tags
  71. * @return {Array}
  72. */
  73. loadFromServer: function (tags) {
  74. var dfd = $.Deferred();
  75. var loadedConfigs = [];
  76. var self = this;
  77. App.config.loadConfigsByTags(tags).done(function (data) {
  78. if (data.items) {
  79. data.items.forEach(function (item) {
  80. App.config.loadedConfigurationsCache[item.type + "_" + item.tag] = item.properties;
  81. loadedConfigs.push(item);
  82. });
  83. }
  84. }).complete(function () {
  85. self.saveToDB(loadedConfigs);
  86. dfd.resolve(loadedConfigs);
  87. });
  88. return dfd.promise();
  89. },
  90. /**
  91. * save properties obtained from server to local DB
  92. * @param loadedConfigs
  93. */
  94. saveToDB: function (loadedConfigs) {
  95. var storedConfigs = App.db.getConfigs();
  96. loadedConfigs.forEach(function (loadedSite) {
  97. var storedSite = storedConfigs.findProperty('type', loadedSite.type);
  98. if (storedSite) {
  99. storedSite.tag = loadedSite.tag;
  100. storedSite.properties = loadedSite.properties;
  101. storedSite.properties_attributes = loadedSite.properties_attributes;
  102. } else {
  103. storedConfigs.push(loadedSite);
  104. }
  105. });
  106. App.db.setConfigs(storedConfigs);
  107. }
  108. });