configuration_controller.js 3.0 KB

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