dimcache.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: pps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2017
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. /***********************************/
  9. /* Dim cache */
  10. function dimensionCache() {
  11. var dimensions = new Array();
  12. var topCategories = new Array();
  13. var requestQueue = new Array();
  14. var outstandingRequest = false;
  15. var numEnqueueRequests = 0;
  16. //Public methods
  17. this.addDimension = function(code, categoryName, fullCategoryName, ppdsId, nonselectable) {
  18. var dimIdx = dimensions.length;
  19. dimensions[dimIdx] = new Array();
  20. topCategories[dimIdx] = new catNode(code, categoryName, fullCategoryName, dimIdx, ppdsId, false, false, false, nonselectable, false);
  21. dimensions[dimIdx][code] = topCategories[dimIdx];
  22. }
  23. this.getDimTopNode = function(dim) {
  24. return topCategories[dim];
  25. }
  26. this.addCategory = function(dim, catNode) {
  27. if (dimensions[dim] && !dimensions[dim][catNode.getCode()])
  28. dimensions[dim][catNode.getCode()] = catNode;
  29. }
  30. this.removeCategory = function(dim, catNode) {
  31. if (dimensions[dim] && dimensions[dim][catNode.getCode()]) {
  32. //remove the reference from each parent
  33. var parents = catNode.getParents();
  34. for (var i = 0; i < parents.length; i++) {
  35. dimensions[dim][parents[i]].removeChild(catNode.getCode());
  36. }
  37. var children = catNode.getChildren();
  38. for (var i = 0; i < children.length; i++) {
  39. dimensions[dim][children[i]].removeParent(catNode.getCode());
  40. }
  41. dimensions[dim][catNode.getCode()] = null;
  42. }
  43. }
  44. this.getCategory = function(dim, catCode) {
  45. if (!dimensions[dim] && (topCategories[dim].getCode() != catCode)) {
  46. return null;
  47. }
  48. return dimensions[dim][catCode];
  49. }
  50. this.createCategory = function(dim, catCode, catName, fullCatName, ppdsId, alternate, isLeaf, calculated, nonselectable, customSubset) {
  51. if (dimensions[dim] && !dimensions[dim][catCode]) {
  52. dimensions[dim][catCode] = new catNode(catCode, catName, fullCatName, dim, ppdsId, alternate, isLeaf, calculated, nonselectable, customSubset);
  53. }
  54. return dimensions[dim][catCode];
  55. }
  56. //Request handling routines
  57. //since the requests are recieved in a browser frame, and a frame will only allow
  58. //1 request to be recieved at a time, we have to maintain a queue of requests to
  59. //ensure that we send any subsequent requests that were (requested) during the
  60. //processing of a request, when the frame becomes available.
  61. this.sendRequest = function(request) {
  62. pushRequest(request);
  63. if (!outstandingRequest) {
  64. sendNextRequest();
  65. outstandingRequest = true;
  66. }
  67. }
  68. this.requestFinished = function() {
  69. if (numEnqueueRequests) {
  70. sendNextRequest();
  71. } else {
  72. outstandingRequest = false;
  73. }
  74. }
  75. function pushRequest(request) {
  76. requestQueue[numEnqueueRequests] = request;
  77. numEnqueueRequests++;
  78. }
  79. function popRequest() {
  80. var request = requestQueue[numEnqueueRequests - 1];
  81. numEnqueueRequests--;
  82. return request;
  83. }
  84. function sendNextRequest(request) {
  85. var hf = getXtabFrame().document.getElementById("fhidden");
  86. var target = hf.target;
  87. var cnct = hf.CNCT.value;
  88. hf.CNCT.value = 16;
  89. hf.RA.value = 999;
  90. hf.CO.value = popRequest();
  91. hf.target = getDimCacheFrame().name;
  92. hf.submit();
  93. hf.target = target;
  94. hf.CO.value = "";
  95. hf.CNCT.value = cnct;
  96. }
  97. this.isLoading = function() {
  98. return outstandingRequest;
  99. }
  100. }
  101. function catNode(code, categoryName, fullCategoryName, dimIdx, ppdsId, alternate, isLeaf, calculated, nonselectable, customSubset, measurePlaceholder) {
  102. //Constructor
  103. var _code = code;
  104. var _categoryName = categoryName;
  105. var _fullName = fullCategoryName;
  106. var _dimIdx = dimIdx;
  107. var _ppdsId = ppdsId;
  108. var children = new Array();
  109. var allChildrenLoaded = false;
  110. var allNonCustomSubsetsLoaded = false;
  111. var _alternate = alternate;
  112. var _customSubset = customSubset;
  113. var _isLeaf = isLeaf;
  114. var parents = new Array();
  115. var allParentsLoaded = false;
  116. var loading = false;
  117. var _calculated = calculated;
  118. var _nonselectable = nonselectable;
  119. var _measurePlaceholder = measurePlaceholder;
  120. //Public Methods
  121. this.getCode = function() {
  122. return _code;
  123. }
  124. this.getLabel = function() {
  125. return _categoryName;
  126. }
  127. this.setLabel = function(label) {
  128. _categoryName = label;
  129. }
  130. this.getFullName = function() {
  131. return _fullName;
  132. }
  133. this.getDimension = function() {
  134. return _dimIdx;
  135. }
  136. this.getPPDSID = function() {
  137. return _ppdsId;
  138. }
  139. this.isLeaf = function() {
  140. return _isLeaf;
  141. }
  142. this.setIsLeaf = function(isLeaf) {
  143. _isLeaf = isLeaf;
  144. }
  145. this.isCalculated = function() {
  146. return _calculated;
  147. }
  148. this.isNonSelectable = function() {
  149. return _nonselectable;
  150. }
  151. this.isTopNode = isTopNode;
  152. this.isAlternateParent = function() {
  153. return _alternate;
  154. }
  155. this.isCustomSubset = function() {
  156. return _customSubset;
  157. }
  158. this.isMeasurePlaceholder = function() {
  159. return _measurePlaceholder;
  160. }
  161. this.childrenLoaded = function(firstItem) {
  162. return childrenLoaded(firstItem);
  163. }
  164. this.getNumChildrenLoaded = function() {
  165. return children.length;
  166. }
  167. this.allChildrenLoaded = function() {
  168. return allChildrenLoaded;
  169. }
  170. this.allNonCustomSubsetsLoaded = function() {
  171. return allNonCustomSubsetsLoaded;
  172. }
  173. this.ancestorsLoaded = ancestorsLoaded;
  174. this.isLoading = function() {
  175. return loading;
  176. }
  177. this.allLoaded = function() {
  178. return (!loading && childrenLoaded() && siblingsLoaded() && ancestorsLoaded());
  179. }
  180. this.loadChildren = function() {
  181. if (!loading && !childrenLoaded(children.length))
  182. loadData(true,false,false);
  183. }
  184. this.clearChildren = function() {
  185. children = new Array();
  186. allChildrenLoaded = false;
  187. allNonCustomSubsetsLoaded = false;
  188. }
  189. this.getChildren = function() {
  190. return children;
  191. }
  192. this.removeChild = function(childCode) {
  193. var idx = 0;
  194. while (idx < children.length && children[idx] != childCode)
  195. idx++;
  196. if (idx < children.length)
  197. children.splice(idx,1);
  198. }
  199. this.loadAllRelated = function() {
  200. if (!loading)
  201. loadData(!childrenLoaded(),!siblingsLoaded(),!ancestorsLoaded());
  202. }
  203. this.loadParents = function() {
  204. if (!parentsLoaded() && !loading)
  205. loadData(false,false,true);
  206. }
  207. this.getParents = function() {
  208. return parents;
  209. }
  210. this.getParent = function() {
  211. return parents[0];
  212. }
  213. this.removeParent = function(parentCode) {
  214. var idx = 0;
  215. while (idx < parents.length && parents[idx] != parentCode)
  216. idx++;
  217. if (idx < parents.length)
  218. parents.splice(idx,1);
  219. }
  220. this.getSiblings = function() {
  221. if (isTopNode()) { //If we are the top node, we are the only sibling.
  222. var siblings = new Array();
  223. siblings[0] = this;
  224. return siblings;
  225. } else if (siblingsLoaded()) {
  226. return dimCache.getCategory(_dimIdx, parents[0]).getChildren();
  227. } else {
  228. return null;
  229. }
  230. }
  231. this.setDirectParent = function(parent) {
  232. parents[0] = parent;
  233. }
  234. this.storeParents = storeParents;
  235. this.storeFirstChildren = function(nodeChildren) {
  236. if (nodeChildren.length > children.length) {
  237. nodeChildren = nodeChildren.splice(children.length,nodeChildren.length - children.length);
  238. storeChildren(nodeChildren);
  239. }
  240. }
  241. this.setMoreChildren = function() {
  242. if (allChildrenLoaded) {
  243. allChildrenLoaded = false;
  244. }
  245. }
  246. this.findHierarchyRoot = function() {
  247. var catNode = dimCache.getCategory(_dimIdx, _code); //Get this node
  248. while (!catNode.isAlternateParent() && !catNode.isTopNode())
  249. catNode = dimCache.getCategory(_dimIdx, catNode.getParent());
  250. return catNode.getCode();
  251. }
  252. //Private Methods
  253. //These methods try and determine (To the best of their knowledge) properties
  254. //about the node.
  255. function isTopNode() {
  256. return (dimCache.getDimTopNode(_dimIdx).getPPDSID() == _ppdsId);
  257. }
  258. function parentsLoaded() {
  259. return (isTopNode() || allParentsLoaded);
  260. }
  261. function ancestorsLoaded() {
  262. return (isTopNode() || (parentsLoaded() && dimCache.getCategory(_dimIdx, parents[0]).ancestorsLoaded()));
  263. }
  264. function childrenLoaded(firstChild) {
  265. if (!firstChild)
  266. firstChild = 0;
  267. if (_isLeaf)
  268. return true;
  269. else {
  270. return (allChildrenLoaded || (firstChild + parseInt(getGlobal("childLimit"))) <= children.length);
  271. }
  272. }
  273. function siblingsLoaded() {
  274. return (isTopNode() || (parentsLoaded() && dimCache.getCategory(_dimIdx, parents[0]).childrenLoaded(0)));
  275. }
  276. function loadData(getChildren, getSiblings, getParents) {
  277. if (!getChildren && !getSiblings && !getParents) {
  278. return;
  279. }
  280. loading = true;
  281. var nextToLoad = 0;
  282. if (!getSiblings)
  283. nextToLoad = children.length;
  284. submitRequest(_ppdsId, _dimIdx, _code, nextToLoad, getChildren, getSiblings, getParents);
  285. }
  286. this.storeData = function() {
  287. var results = getDimCacheFrame();
  288. var children = results.children;
  289. var siblings = results.siblings;
  290. var parents = results.parents1;
  291. //Store children first
  292. if (children)
  293. storeChildren(children);
  294. //Store Parents next
  295. if (parents)
  296. storeParents("parents", 1);
  297. //Store Siblings last
  298. if (siblings)
  299. storeSiblings(siblings);
  300. loading = false;
  301. }
  302. function storeChildren(nodeChildren) {
  303. var limitHit = false;
  304. var i = 0;
  305. var customSubset = false;
  306. for (var i = 0; i < nodeChildren.length; i++) {
  307. var limitHit;
  308. if(typeof nodeChildren[i]["limit"] != "undefined")
  309. limitHit = nodeChildren[i]["limit"];
  310. allNonCustomSubsetsLoaded = (nodeChildren[i]["all_non_cs_loaded"] != null);
  311. if (!limitHit) {
  312. var child = dimCache.getCategory(_dimIdx, nodeChildren[i]["code"]);
  313. if (!child) {
  314. var alternate = (nodeChildren[i]["alternate"] != null);
  315. var leaf = (nodeChildren[i]["src"] == null);
  316. var calculated = (nodeChildren[i]["calculated"] != null);
  317. var nonselectable = (nodeChildren[i]["nonselectable"] != null);
  318. customSubset = (nodeChildren[i]["custom_subset"] != null);
  319. var measurePlaceholder = (nodeChildren[i]["measure_placeholder"] != null);
  320. child = new catNode(nodeChildren[i]["code"], nodeChildren[i]["name"], nodeChildren[i]["tag"], _dimIdx, nodeChildren[i]["id"], alternate, leaf, calculated, nonselectable, customSubset, measurePlaceholder);
  321. dimCache.addCategory(_dimIdx, child);
  322. }
  323. var parent = dimCache.getCategory(_dimIdx, child.getParent());
  324. if (!parent || parent.isAlternateParent())
  325. child.setDirectParent(_code);
  326. children[children.length] = child.getCode();
  327. } else {
  328. setGlobal("childLimit",limitHit);
  329. }
  330. }
  331. if (customSubset)
  332. allNonCustomSubsetsLoaded = true;
  333. if (!limitHit) {
  334. //We reached the end
  335. allChildrenLoaded = true;
  336. allNonCustomSubsetsLoaded = true;
  337. }
  338. }
  339. function storeParents(sParents, level) {
  340. var nodeParents = eval("getDimCacheFrame()." + sParents + level);
  341. for (var i = 0; i < nodeParents.length; i++) {
  342. var parent = dimCache.getCategory(_dimIdx, nodeParents[i]["code"]);
  343. if (!parent) {
  344. var alternate = (nodeParents[i]["alternate"] != null);
  345. var nonselectable = (nodeParents[i]["nonselectable"] != null);
  346. var customSubset = (nodeParents[i]["custom_subset"] != null);
  347. var measurePlaceholder = (nodeParents[i]["measure_placeholder"] != null);
  348. parent = new catNode(nodeParents[i]["code"], nodeParents[i]["name"], nodeParents[i]["tag"], _dimIdx, nodeParents[i]["id"], alternate, false, false, nonselectable, customSubset, measurePlaceholder);
  349. dimCache.addCategory(_dimIdx, parent);
  350. }
  351. parents[i] = parent.getCode();
  352. var nextLevel = eval("getDimCacheFrame()." + sParents + (level+1));
  353. if (nextLevel)
  354. parent.storeParents(sParents, level+1);
  355. allParentsLoaded = true;
  356. }
  357. }
  358. function storeSiblings(nodeSiblings) {
  359. dimCache.getCategory(_dimIdx, parents[0]).storeFirstChildren(nodeSiblings);
  360. }
  361. function submitRequest(ppdsId, dimIdx, code, startIndex, getchildren, getSiblings, getParents)
  362. {
  363. if (!startIndex)
  364. startIndex = 0;
  365. var cmd = 'N:' + ppdsId;
  366. cmd += "," + dimIdx + "," + code + "\t" + (getchildren? "1" : "0") + "\t" + (getSiblings? "1" : "0") + "\t" + (getParents? "1" : "0") + "\t" + startIndex;
  367. dimCache.sendRequest(cmd);
  368. }
  369. }