configuration_controller.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. getConfigsByTags: function (tags) {
  22. var storedTags = [];
  23. App.db.getConfigs().forEach(function (site) {
  24. storedTags.push({
  25. siteName: site.type,
  26. tagName: site.tag
  27. })
  28. });
  29. if (this.checkTagsChanges(tags, storedTags)) {
  30. return this.loadFromServer(tags);
  31. } else {
  32. return this.loadFromDB(tags.mapProperty('siteName'));
  33. }
  34. },
  35. /**
  36. * check whether tag versions have been changed
  37. * if they are different then return true
  38. * otherwise false
  39. * @param tags
  40. * @param storedTags
  41. * @return {Boolean}
  42. */
  43. checkTagsChanges: function (tags, storedTags) {
  44. var isDifferent = false;
  45. var i = 0;
  46. while (i < tags.length && !isDifferent) {
  47. var storedTag = storedTags.findProperty('siteName', tags[i].siteName);
  48. isDifferent = (!storedTag || storedTag.tagName !== tags[i].tagName);
  49. i++;
  50. }
  51. return isDifferent;
  52. },
  53. loadFromDB: function (siteNames) {
  54. var configs = App.db.getConfigs();
  55. return configs.filter(function (site) {
  56. return (siteNames.contains(site.type));
  57. })
  58. },
  59. /**
  60. * load configs from server
  61. * and update them in local DB
  62. * @param tags
  63. * @return {Array}
  64. */
  65. loadFromServer: function (tags) {
  66. var loadedConfigs = App.config.loadConfigsByTags(tags);
  67. var storedConfigs = App.db.getConfigs();
  68. loadedConfigs.forEach(function (loadedSite) {
  69. var storedSite = storedConfigs.findProperty('type', loadedSite.type);
  70. if (storedSite) {
  71. storedSite.tag = loadedSite.tag;
  72. storedSite.properties = loadedSite.properties;
  73. } else {
  74. storedConfigs.push(loadedSite);
  75. }
  76. });
  77. App.db.setConfigs(storedConfigs);
  78. return loadedConfigs;
  79. }
  80. });