/src/aria/pageEngine/pageProviders/BasePageProvider.js

https://github.com/ariatemplates/ariatemplates · JavaScript · 276 lines · 119 code · 23 blank · 134 comment · 19 complexity · 6535af3739a0e3fa8836e1412618e443 MD5 · raw file

  1. /*
  2. * Copyright 2012 Amadeus s.a.s.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. var Aria = require("../../Aria");
  16. var ariaPageEnginePageProvidersPageProviderInterface = require("./PageProviderInterface");
  17. var ariaCoreDownloadMgr = require("../../core/DownloadMgr");
  18. var ariaUtilsJson = require("../../utils/Json");
  19. /**
  20. * Base Page provider for static site configuration and page definitions
  21. */
  22. module.exports = Aria.classDefinition({
  23. $classpath : "aria.pageEngine.pageProviders.BasePageProvider",
  24. $implements : [ariaPageEnginePageProvidersPageProviderInterface],
  25. /**
  26. * @param {aria.pageEngine.pageProviders.BasePageProviderBeans:Config} config
  27. */
  28. $constructor : function (config) {
  29. /**
  30. * Configuration for the base provider
  31. * @type aria.pageEngine.pageProviders.BasePageProviderBeans:Config
  32. * @private
  33. */
  34. this.__config = config;
  35. /**
  36. * Base location of pages
  37. * @type String
  38. * @private
  39. */
  40. this.__basePageUrl = null;
  41. if (!("cache" in config)) {
  42. config.cache = true;
  43. }
  44. /**
  45. * Map pageIds to urls and viceversa
  46. * @type Object
  47. * @private
  48. */
  49. this.__urlMap = {
  50. pageIdToUrl : {},
  51. urlToPageId : {}
  52. };
  53. },
  54. $prototype : {
  55. /**
  56. * @param {aria.pageEngine.CfgBeans:ExtendedCallback} callback
  57. */
  58. loadSiteConfig : function (callback) {
  59. this.__sendRequest(this.__config.siteConfigLocation, {
  60. callback : callback
  61. }, "site");
  62. },
  63. /**
  64. * @param {aria.pageEngine.CfgBeans:PageRequest} pageRequest
  65. * @param {aria.pageEngine.CfgBeans:ExtendedCallback} callback
  66. */
  67. loadPageDefinition : function (pageRequest, callback) {
  68. pageRequest = pageRequest || {};
  69. var pageId = this.__getPageId(pageRequest);
  70. pageRequest.pageId = pageId;
  71. this.__updateUrlMap(pageRequest);
  72. this.__basePageUrl = this.__basePageUrl
  73. || (this.__config.pageBaseLocation + "fake.json").replace(/fake\.json$/, "");
  74. var pageUrl = this.__basePageUrl + pageId + ".json";
  75. if (!this.__config.cache) {
  76. ariaCoreDownloadMgr.clearFile(pageUrl);
  77. }
  78. this.__sendRequest(pageUrl, {
  79. pageRequest : pageRequest,
  80. callback : callback
  81. }, "page");
  82. },
  83. /**
  84. * Process the site configuration before sending it back to the page engine
  85. * @param {Object} siteConf Site configuration retrieved from .json file
  86. * @return {aria.pageEngine.CfgBeans:Site}
  87. */
  88. processSiteConfig : function (siteConf) {
  89. return siteConf;
  90. },
  91. /**
  92. * Process the page definition before sending it back to the page engine
  93. * @param {Object} pageDef Page definition retrieved from .json file
  94. * @return {aria.pageEngine.CfgBeans:PageDefinition}
  95. */
  96. processPageDefinition : function (pageDef) {
  97. return pageDef;
  98. },
  99. /**
  100. * Send a request for the site or page json
  101. * @param {String} url location of the json
  102. * @param {Object} args passed to the callback
  103. *
  104. * <pre>
  105. * {
  106. * callback : object of type aria.pageEngine.CfgBeans:ExtendedCallback,
  107. * pageRequest : object of type aria.pageEngine.CfgBeans:PageRequest
  108. * }
  109. * </pre>
  110. *
  111. * @param {String} type either "site" or "page"
  112. * @private
  113. */
  114. __sendRequest : function (url, args, type) {
  115. ariaCoreDownloadMgr.loadFile(url, {
  116. fn : this.__onRawFileReceive,
  117. scope : this,
  118. args : {
  119. fn : (type == "page") ? this.__onPageSuccess : this.__onSiteSuccess,
  120. scope : this,
  121. args : args
  122. }
  123. });
  124. },
  125. /**
  126. * Retrieve the file content after it has been downloaded and parses it to turn it into a JSON object
  127. * @param {Object} res Response received from the loadFile method of aria.core.DownloadManager
  128. * @param {aria.core.CfgBeans:Callback} cb Callback to be called after the response has been parsed
  129. */
  130. __onRawFileReceive : function (res, cb) {
  131. if (res.downloadFailed) {
  132. this.__onFailure(res, cb.args);
  133. } else {
  134. var fileContent = ariaCoreDownloadMgr.getFileContent(res.logicalPaths[0]);
  135. var responseJSON = ariaUtilsJson.load(fileContent);
  136. if (responseJSON) {
  137. this.$callback(cb, responseJSON);
  138. } else {
  139. this.__onFailure(res, cb.args);
  140. }
  141. }
  142. },
  143. /**
  144. * Call the success callback by providing the site configuration as argument
  145. * @param {Object} res Site configuration retrieved from .json file
  146. * @param {Object} args
  147. *
  148. * <pre>
  149. * {
  150. * callback : object of type aria.pageEngine.CfgBeans:ExtendedCallback,
  151. * pageRequest : object of type aria.pageEngine.CfgBeans:PageRequest
  152. * }
  153. * </pre>
  154. *
  155. * @private
  156. */
  157. __onSiteSuccess : function (res, args) {
  158. var siteConfig = this.processSiteConfig(res);
  159. this.$callback(args.callback.onsuccess, siteConfig);
  160. },
  161. /**
  162. * Call the success callback by providing the page definition as argument. If it does not contain a url, the
  163. * pageId is aused as url. If the cache is enbled, it is filled with the response
  164. * @param {Object} pageDefinition Page definition retrieved from .json file
  165. * @param {Object} args
  166. *
  167. * <pre>
  168. * {
  169. * callback : object of type aria.pageEngine.CfgBeans.ExtendedCallback,
  170. * pageRequest : object of type aria.pageEngine.CfgBeans.PageRequest
  171. * }
  172. * </pre>
  173. *
  174. * @private
  175. */
  176. __onPageSuccess : function (pageDefinition, args) {
  177. var callback = args.callback, pageRequest = args.pageRequest;
  178. this.__updateUrlMap(pageDefinition);
  179. var url = this.__getUrl(pageRequest);
  180. pageDefinition.url = url;
  181. this.__updateUrlMap(pageDefinition);
  182. this.$callback(callback.onsuccess, this.processPageDefinition(pageDefinition));
  183. },
  184. /**
  185. * Call the failure callback
  186. * @param {Object} res response
  187. * @param {Object} callback
  188. *
  189. * <pre>
  190. * {
  191. * callback : object of type aria.pageEngine.CfgBeans.ExtendedCallback,
  192. * pageRequest : object of type aria.pageEngine.CfgBeans.PageRequest
  193. * }
  194. * </pre>
  195. *
  196. * @private
  197. */
  198. __onFailure : function (res, args) {
  199. this.$callback(args.callback.onfailure);
  200. },
  201. /**
  202. * Update the pageId <-> url associations
  203. * @param {aria.pageEngine.CfgBeans:PageRequest} pageRequest
  204. * @private
  205. */
  206. __updateUrlMap : function (pageRequest) {
  207. var pageId = pageRequest.pageId, url = pageRequest.url, urlMap = this.__urlMap;
  208. if (pageId && url) {
  209. urlMap.pageIdToUrl[pageId] = url;
  210. urlMap.urlToPageId[url] = pageId;
  211. var alternativeUrl = url.replace(/\/$/, "");
  212. urlMap.urlToPageId[alternativeUrl] = pageId;
  213. }
  214. },
  215. /**
  216. * Retrieve the pageId based on the pageRequest information, as well as the url map. As a default, the
  217. * homePageId is returned
  218. * @param {aria.pageEngine.CfgBeans:PageRequest} pageRequest
  219. * @return {String} the pageId
  220. * @private
  221. */
  222. __getPageId : function (pageRequest) {
  223. var map = this.__urlMap.urlToPageId, pageId = pageRequest.pageId, url = pageRequest.url;
  224. if (pageId) {
  225. return pageId;
  226. }
  227. if (url) {
  228. var returnUrl = map[url] || map[url + "/"] || map[url.replace(/\/$/, "")];
  229. if (returnUrl) {
  230. return returnUrl;
  231. }
  232. }
  233. return this.__config.homePageId;
  234. },
  235. /**
  236. * Retrieve the url based on the pageRequest information, as well as the url map. As a default, the "/[pageId]"
  237. * is returned
  238. * @param {aria.pageEngine.CfgBeans:PageRequest} pageRequest
  239. * @return {String} the pageId
  240. * @private
  241. */
  242. __getUrl : function (pageRequest) {
  243. var map = this.__urlMap.pageIdToUrl, pageId = pageRequest.pageId, url = pageRequest.url;
  244. if (url) {
  245. return url;
  246. }
  247. if (pageId && map[pageId]) {
  248. return map[pageId];
  249. }
  250. return "/" + pageId;
  251. }
  252. }
  253. });