CFilterConverter.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /****************************************************************
  2. ** Licensed Materials - Property of IBM
  3. **
  4. ** BI and PM: qs
  5. **
  6. ** (C) Copyright IBM Corp. 2001, 2015
  7. **
  8. ** US Government Users Restricted Rights - Use, duplication or
  9. ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *****************************************************************/
  11. // Copyright (C) 2008 Cognos ULC, an IBM Company. All Rights Reserved.
  12. // Cognos and the Cognos logo are trademarks of Cognos ULC (formerly Cognos Incorporated) in the United States and/or other countries. IBM and the IBM logo are trademarks of International Business Machines Corporation in the United States, or other countries, or both. Other company, product, or service names may be trademarks or service marks of others.
  13. function CFilterConverter(oFilterNode)
  14. {
  15. this.m_oFilterNode = oFilterNode;
  16. this.m_bIsModelFilter = isModelFilter(oFilterNode.getAttribute('id'));
  17. this.m_bIncludeMissingValues = false;
  18. this.m_sFilterExpression = this.cleanupFilterExpression( XMLHelper_GetText( this.m_oFilterNode, true ) );
  19. this.m_sFilterType = this.getFilterType();
  20. this.m_oCachedConvertedFilterNode = null;
  21. this.m_bNeedsConversion = null;
  22. };
  23. CFilterConverter.prototype.convert = function ()
  24. {
  25. if (this.m_oCachedConvertedFilterNode === null && this.needsConversion())
  26. {
  27. this.m_oFilterNode.removeChild(this.m_oFilterNode.firstChild);
  28. switch (this.getFilterType())
  29. {
  30. case "picklist":
  31. this.m_oCachedConvertedFilterNode = this.convertPicklist();
  32. break;
  33. case "range":
  34. this.m_oCachedConvertedFilterNode = this.convertRange();
  35. break;
  36. }
  37. if (this.m_bIncludeMissingValues)
  38. {
  39. var oFEElement = XMLHelper_FindChildByTagName(this.m_oCachedConvertedFilterNode, "FE", true);
  40. if (oFEElement)
  41. {
  42. oFEElement.setAttribute("nullOption", "include");
  43. }
  44. }
  45. }
  46. return this.m_oCachedConvertedFilterNode;
  47. };
  48. CFilterConverter.prototype.convertPicklist = function()
  49. {
  50. var str = this.m_sFilterExpression;
  51. var filterNode = this.m_oFilterNode;
  52. var aValues = this.getPicklistValues();
  53. var oMQMgr = goApplicationManager.getMiniQueryManager();
  54. var oFEElement = XMLBuilderCreateElementNS("", "FE", oMQMgr.getDoc());
  55. this.m_oFilterNode.appendChild(oFEElement);
  56. oFEElement.setAttribute("filterType", "enum");
  57. oFEElement.setAttribute("dataType", "string");
  58. var sColumnId = this.m_oFilterNode.getAttribute("columnId");
  59. if (sColumnId && sColumnId.length)
  60. {
  61. oFEElement.setAttribute("dataItemName", sColumnId);
  62. }
  63. for (var i = 0; i < aValues.length; i++)
  64. {
  65. var sValue = aValues[i];
  66. var sPrompt = this.getPrompt(sValue);
  67. if (sPrompt.length)
  68. {
  69. oFEElement.setAttribute("prompt", sPrompt);
  70. }
  71. else
  72. {
  73. var oValueElement = XMLBuilderCreateElementNS("", "Value", oMQMgr.getDoc());
  74. oFEElement.appendChild(oValueElement);
  75. oValueElement.setAttribute("use", sValue);
  76. oValueElement.setAttribute("display", sValue);
  77. }
  78. }
  79. return this.m_oFilterNode;
  80. };
  81. CFilterConverter.prototype.getPicklistValues = function()
  82. {
  83. var str = this.m_sFilterExpression;
  84. str = str.replace(/^.+in\s\(/,"");
  85. str = str.replace(/\s*\)\s*$/,"");
  86. var sSeparator = ",";
  87. var oMQMgr = goApplicationManager.getMiniQueryManager();
  88. if (oMQMgr)
  89. {
  90. sSeparator = oMQMgr.getListSeparator();
  91. }
  92. if (str.search(/^\s*'.*'\s*$/) != -1)
  93. {
  94. var reSeparatorWithApos = new RegExp("'\\s*" + sSeparator + "\\s*'", "g");
  95. str = str.replace(reSeparatorWithApos, "\t\n");
  96. str = str.replace(/^\s*'/, ""); // Remove first '
  97. str = str.replace(/\s*'$/, ""); // Remove last '
  98. }
  99. else
  100. {
  101. var reSeparator = new RegExp("\\s*" + sSeparator + "\\s*", "g");
  102. str = str.replace(reSeparator, "\t\n");
  103. }
  104. if ( str.length < 1 )
  105. {
  106. return null;
  107. }
  108. values = str.split("\t\n");
  109. for (var i= 0; i < values.length; i++)
  110. {
  111. values[i] = values[i].replace(/''/g, "'");
  112. }
  113. return values;
  114. };
  115. CFilterConverter.prototype.convertRange = function()
  116. {
  117. var str = this.m_sFilterExpression;
  118. str = str.replace(/^.+>=/g,">=");
  119. str = str.replace(/^.+<=/g,"<=");
  120. str = str.replace(/^[^><]+=/g,"=");
  121. var oMQMgr = goApplicationManager.getMiniQueryManager();
  122. var oFEElement = XMLBuilderCreateElementNS("", "FE", oMQMgr.getDoc());
  123. this.m_oFilterNode.appendChild(oFEElement);
  124. oFEElement.setAttribute("filterType", "range");
  125. var sFromValue = this.getFromValue();
  126. var sToValue = this.getToValue();
  127. var sPrompt = this.getPrompt(sFromValue);
  128. if (sPrompt.length && sFromValue === sToValue)
  129. {
  130. oFEElement.setAttribute("prompt", sPrompt);
  131. }
  132. else
  133. {
  134. if (sFromValue !== "")
  135. {
  136. var oFromElement = XMLBuilderCreateElementNS("", "From", oMQMgr.getDoc());
  137. oFEElement.appendChild(oFromElement);
  138. oFromElement.setAttribute("use", sFromValue);
  139. oFromElement.setAttribute("display", sFromValue);
  140. }
  141. if (sToValue !== "")
  142. {
  143. var oToElement = XMLBuilderCreateElementNS("", "To", oMQMgr.getDoc());
  144. oFEElement.appendChild(oToElement);
  145. oToElement.setAttribute("use", sToValue);
  146. oToElement.setAttribute("display", sToValue);
  147. }
  148. }
  149. return this.m_oFilterNode;
  150. };
  151. CFilterConverter.prototype.getFromValue = function() { return this.getFromOrToValue("from"); }
  152. CFilterConverter.prototype.getToValue = function() { return this.getFromOrToValue("to"); }
  153. CFilterConverter.prototype.getFromOrToValue = function(to_from)
  154. {
  155. var str = this.m_sFilterExpression;
  156. var filterNode = this.m_oFilterNode;
  157. var aValues = str.split("in_range");
  158. if (aValues.length == 2)
  159. {
  160. aValues[1] = aValues[1].replace(/^\s*/g,"").replace(/.*\{/g, "").replace(/\}.*/g, "");
  161. var rangeStr = aValues[1];
  162. var colonIdx = rangeStr.indexOf(':');
  163. if (colonIdx == -1)
  164. {
  165. return rangeStr;
  166. }
  167. if (colonIdx == 0)
  168. {
  169. if (to_from == "to")
  170. {
  171. return rangeStr.substring(1);
  172. }
  173. }
  174. else if (rangeStr.charAt(rangeStr.length -1) == ':')
  175. {
  176. if (to_from == "from")
  177. {
  178. return rangeStr.substr(0, rangeStr.length -1);
  179. }
  180. }
  181. else
  182. {
  183. var a = rangeStr.split(":");
  184. if (a)
  185. {
  186. if (a.length == 2)
  187. {
  188. if (a[0] && to_from == "from")
  189. {
  190. return a[0];
  191. }
  192. if (a[1] && to_from == "to")
  193. {
  194. return a[1];
  195. }
  196. }
  197. else if (a.length == 3)
  198. {
  199. return rangeStr;
  200. }
  201. else
  202. {
  203. if (to_from == "from")
  204. {
  205. return rangeStr.substr(0, (rangeStr.length - 1) / 2);
  206. }
  207. else if (to_from == "to")
  208. {
  209. return rangeStr.substring(((rangeStr.length - 1) / 2) + 1);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. return "";
  216. };
  217. CFilterConverter.prototype.getBasedOn = function()
  218. {
  219. var str = this.m_sFilterExpression.match(/<filter[^<>]*>/gi);
  220. if (str != null && str[0] != null)
  221. {
  222. str = str[0];
  223. str = str.replace(/\s*use\s*="optional"\s*/g, "");
  224. if (str == "")
  225. {
  226. return "raw";
  227. }
  228. if (str.search(/summary\s*=\s*"/gi) > -1)
  229. {
  230. str = str.replace(/(.*summary\s*=\s*")|("\W*)/gi, "");
  231. if (str == "true")
  232. {
  233. return "details";
  234. }
  235. else if (str == "none")
  236. {
  237. return "raw";
  238. }
  239. else
  240. {
  241. return str;
  242. }
  243. }
  244. }
  245. return null;
  246. };
  247. CFilterConverter.prototype.getFilterType = function()
  248. {
  249. if (! this.m_sFilterType || ! this.m_sFilterType.length)
  250. {
  251. var sFilterType = "picklist";
  252. var sRangeExpression = this.m_sFilterExpression;
  253. if (sRangeExpression.search(/in_range/gi) > -1)
  254. {
  255. sFilterType = "range";
  256. }
  257. this.m_sFilterType = sFilterType;
  258. }
  259. return this.m_sFilterType;
  260. };
  261. CFilterConverter.prototype.cleanupFilterExpression = function(sFilterExpression)
  262. {
  263. sFilterExpression = sFilterExpression.replace(/<\/?filter\s*[\[\]\.="\w]*>/ig, "");
  264. if (! this.m_bIsModelFilter)
  265. {
  266. sFilterExpression = sFilterExpression.replace(/(\[[\w\s\.]+\]\.?)+/g, "");
  267. var sIncludeMissing = sFilterExpression.replace(/\s+or\s+.*\s+is missing/g, "");
  268. if (sIncludeMissing !== sFilterExpression)
  269. {
  270. this.m_bIncludeMissingValues = true;
  271. sFilterExpression = sIncludeMissing;
  272. }
  273. }
  274. return sFilterExpression;
  275. };
  276. CFilterConverter.prototype.getPrompt = function(sValue)
  277. {
  278. var sReturn = "";
  279. var sPrompt = sValue.replace(/^\?(.*)\?$/, "$1");
  280. if (sPrompt !== sValue)
  281. {
  282. sReturn = sPrompt;
  283. }
  284. return sReturn;
  285. };
  286. CFilterConverter.prototype.needsConversion = function()
  287. {
  288. if (this.m_bNeedsConversion === null && ! this.m_bIsModelFilter)
  289. {
  290. var bNeedsConversion = true;
  291. if (this.m_oCachedConvertedFilterNode === null)
  292. {
  293. if (! this.m_sFilterExpression || this.m_sFilterExpression.length === 0)
  294. {
  295. bNeedsConversion = false;
  296. }
  297. else
  298. {
  299. var oFEElement = XMLHelper_FindChildByTagName(this.m_oFilterNode, "FE", true);
  300. if (oFEElement !== null)
  301. {
  302. bNeedsConversion = false;
  303. }
  304. }
  305. }
  306. this.m_bNeedsConversion = bNeedsConversion;
  307. }
  308. return this.m_bNeedsConversion;
  309. };