cvSelectionXml.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2011
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*------------------------------------------------------------------------------------------
  13. Object model for the selection context information
  14. CSelectionXml = {
  15. queries: {
  16. <query_name>: { <!! SC_SingleQuery !!>
  17. selections: [
  18. { <!! SC_SingleSelection !!>
  19. rows: [{},...],
  20. cols: [{},...],
  21. measures: [{},...],
  22. sections: [{},...],
  23. layoutElementId: <string>
  24. },
  25. ...
  26. ],
  27. slicers: [ { <!! SC_SingleSlicer !!> },... ],
  28. filters: {
  29. detailFilters: [ { <!! SC_SingleDetailFilter !!> },... ],
  30. summaryFilters: [ { <!! SC_SingleSummaryFilter !!> },... ]
  31. }
  32. },
  33. ...
  34. },
  35. burstContext: {
  36. //TODO
  37. }
  38. }
  39. ------------------------------------------------------------------------------------------*/
  40. function CSelectionXml(burstID, contentLocale, expressionLocale) {
  41. this.queries = {};
  42. this.burstContext = burstID || "";
  43. this.expressionLocale = contentLocale || "";
  44. this.contentLocale = expressionLocale || "";
  45. }
  46. ///////////////////////////////////////////////////////////////
  47. // NOTE: shallow functions for defining structure. They'll be
  48. // treated as Objects within CSelectionXml.
  49. // i.e., they're not expected to do self-serialization
  50. ///////////////////////////////////////////////////////////////
  51. function SC_SingleSelection() {
  52. this.rows = [];
  53. this.cols = [];
  54. this.sections = [];
  55. this.measures = [];
  56. this.layoutElementId = "";
  57. }
  58. function SC_SingleQuery() {
  59. this.selections = [];
  60. this.slicers = [];
  61. this.filters = [];
  62. }
  63. function SC_SingleSlicer() {}
  64. function SC_SingleDetailFilter() {}
  65. function SC_SingleSummaryFilter() {}
  66. ////////////////////////////////////////////////////////////
  67. CSelectionXml.prototype.BuildSelectionFromController = function(sc) {
  68. if (sc) {
  69. var selectedObjects = sc.getAllSelectedObjects();
  70. for(var s = 0; s < selectedObjects.length; ++s) {
  71. var selection = selectedObjects[s];
  72. var selectionCtx = selection.getSelectedContextIds();
  73. var muns = selection.getMuns();
  74. var munCount = muns.length;
  75. var singleSelection = new SC_SingleSelection();
  76. singleSelection.layoutElementId = selection.getLayoutElementId();
  77. var sQuery = null;
  78. /********
  79. To make this more generic in the future, all measure-oriented checks will need to be removed.
  80. We should need to support member-only context, and other permutations.
  81. ********/
  82. for(var i = 0; i < munCount; ++i) {
  83. var j, ctxId, displayValue;
  84. if (i === 0 && munCount === 1) {
  85. for(j = 0; j < muns[i].length; ++j) {
  86. ctxId = selectionCtx[i][j];
  87. if (ctxId != 0)
  88. {
  89. // place the selceted cell in the measure section. The wizard will take
  90. // care of verifying that it's really a measure
  91. if(j===0) {
  92. //get the measure's query ref. This should be unique within a selection, except for sections.
  93. sQuery = sc.getRefQuery(ctxId);
  94. displayValue = selection.getDisplayValues()[j];
  95. this._buildMeasureSelection(sc, ctxId, singleSelection.measures, displayValue, j, selection.getLayoutType());
  96. } else {
  97. //ignore other measures on the list report
  98. if (sc.getUsageInfo(ctxId) != 2) {
  99. this._buildEdgeSelection(sc, ctxId, singleSelection.cols, j);
  100. }
  101. }
  102. }
  103. }
  104. } else {
  105. for(j = 0; j < muns[i].length; ++j) {
  106. ctxId = selectionCtx[i][j];
  107. if (ctxId != 0)
  108. {
  109. if (i === 0) {
  110. displayValue = selection.getDisplayValues()[j];
  111. sQuery = sc.getRefQuery(ctxId);
  112. this._buildMeasureSelection(sc, ctxId, singleSelection.measures, displayValue, j, selection.getLayoutType());
  113. //get the measure's query ref. This should be unique within a selection, except for sections.
  114. } else if (i === 1 ) {
  115. this._buildEdgeSelection(sc, ctxId, singleSelection.rows, j);
  116. } else if (i === 2) {
  117. this._buildEdgeSelection(sc, ctxId, singleSelection.cols, j);
  118. } else {
  119. this._buildSectionSelection(sc, ctxId, singleSelection.sections, j);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. this.AddSelection(sQuery, singleSelection);
  126. }
  127. }
  128. };
  129. CSelectionXml.prototype.AddSelection = function(queryName, context) {
  130. if (!this.queries[queryName]) {
  131. this.queries[queryName] = new SC_SingleQuery();
  132. }
  133. this.queries[queryName].selections.push(context);
  134. };
  135. CSelectionXml.prototype._buildMeasureSelection = function(sc, ctxId, measures, displayValue, idx, dataType) {
  136. if (dataType == "" || dataType == null)
  137. {
  138. dataType = "datavalue";
  139. }
  140. if (ctxId) {
  141. measures.push( {
  142. name: sc.getRefDataItem(ctxId),
  143. values: [ { use: sc.getUseValue(ctxId),
  144. display: displayValue }],
  145. order: idx,
  146. hun: sc.getHun(ctxId),
  147. dataType: dataType,
  148. usage: sc.getUsageInfo(ctxId),
  149. dtype: sc.getDataType(ctxId),
  150. selection: "true" //TODO: is this supposed to be anything else?
  151. });
  152. }
  153. };
  154. CSelectionXml.prototype._buildEdgeSelection = function(sc, ctxId, edges, idx) {
  155. if (ctxId) {
  156. edges.push( {
  157. name: sc.getRefDataItem(ctxId),
  158. values: [ { use: this.getUseValue(sc, ctxId),
  159. display: sc.getDisplayValue(ctxId) }],
  160. order: idx,
  161. lun: sc.getLun(ctxId),
  162. hun: sc.getHun(ctxId),
  163. dataType: "columnTitle",
  164. usage: sc.getUsageInfo(ctxId),
  165. dtype: sc.getDataType(ctxId)
  166. });
  167. }
  168. };
  169. CSelectionXml.prototype._buildSectionSelection = function(sc, ctxId, sections, idx) {
  170. if (ctxId) {
  171. sections.push( {
  172. name: sc.getRefDataItem(ctxId),
  173. values: [ { use: this.getUseValue(sc, ctxId),
  174. display: sc.getDisplayValue(ctxId) }],
  175. order: idx,
  176. lun: sc.getLun(ctxId),
  177. hun: sc.getHun(ctxId),
  178. dataType: "section",
  179. usage: sc.getUsageInfo(ctxId),
  180. dtype: sc.getDataType(ctxId),
  181. queryRef: sc.getRefQuery(ctxId)
  182. });
  183. }
  184. };
  185. /**
  186. * If we have a MUN then use it, otherwise use the useValue
  187. * @private
  188. */
  189. CSelectionXml.prototype.getUseValue = function(sc, ctxId)
  190. {
  191. var useValue = sc.getMun(ctxId);
  192. if (useValue == "")
  193. {
  194. useValue = sc.getUseValue(ctxId);
  195. }
  196. return useValue;
  197. };
  198. /*===================================================
  199. Serialization of the selection
  200. Context
  201. ====================================================*/
  202. CSelectionXml.prototype.toXml = function() {
  203. var xmlSelectionsDocument = XMLBuilderCreateXMLDocument("selections");
  204. var xmlSelections = xmlSelectionsDocument.documentElement;
  205. XMLBuilderSetAttributeNodeNS(xmlSelections, "xmlns:xs", "http://www.w3.org/2001/XMLSchema");
  206. XMLBuilderSetAttributeNodeNS(xmlSelections, "xmlns:bus", "http://developer.cognos.com/schemas/bibus/3/");
  207. XMLBuilderSetAttributeNodeNS(xmlSelections, "SOAP-ENC:arrayType", "bus:parameterValue[]", "http://schemas.xmlsoap.org/soap/encoding/");
  208. XMLBuilderSetAttributeNodeNS(xmlSelections, "xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
  209. XMLBuilderSetAttributeNodeNS(xmlSelections, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
  210. xmlSelections.setAttribute("contentLocale", this.contentLocale);
  211. xmlSelections.setAttribute("expressionLocale", this.expressionLocale);
  212. for(var q in this.queries) {
  213. this._queryToXml(xmlSelections, q, this.queries[q]);
  214. }
  215. this._burstToXml(xmlSelections);
  216. return XMLBuilderSerializeNode(xmlSelectionsDocument);
  217. };
  218. CSelectionXml.prototype._queryToXml = function(parent, name, obj) {
  219. var xmlQuery = parent.ownerDocument.createElement("query");
  220. xmlQuery.setAttribute("name", name);
  221. for(var selection = 0; selection < obj.selections.length; ++selection) {
  222. this._selectionToXml(xmlQuery, obj.selections[selection]);
  223. }
  224. for(var slicer = 0; slicer < obj.slicers.length; ++slicer) {
  225. this._slicersToXml(xmlQuery, obj.slicers[slicer]);
  226. }
  227. for(var filter = 0; filter < obj.selections.length; ++filter) {
  228. this._filtersToXml(xmlQuery, obj.selections[filter]);
  229. }
  230. parent.appendChild(xmlQuery);
  231. };
  232. CSelectionXml.prototype._selectionToXml = function(parent, selection) {
  233. var doc = parent.ownerDocument;
  234. var xmlSelection = doc.createElement("selection");
  235. parent.appendChild(xmlSelection);
  236. this._edgeToXml(xmlSelection, "row", selection.rows);
  237. this._edgeToXml(xmlSelection, "column", selection.cols);
  238. this._edgeToXml(xmlSelection, "measure", selection.measures);
  239. this._edgeToXml(xmlSelection, "section", selection.sections);
  240. var layoutElementId = doc.createElement("layoutElementId");
  241. layoutElementId.appendChild(doc.createTextNode(selection.layoutElementId));
  242. xmlSelection.appendChild(layoutElementId);
  243. };
  244. CSelectionXml.prototype._edgeToXml = function(parent, sEdge, aContext) {
  245. var doc = parent.ownerDocument;
  246. //row edge name: "row" + "s"
  247. var xmlEdgeContainer = doc.createElement(sEdge+'s');
  248. parent.appendChild(xmlEdgeContainer);
  249. for(var i = 0; i < aContext.length; ++i) {
  250. var xmlEdge = doc.createElement(sEdge);
  251. xmlEdgeContainer.appendChild(xmlEdge);
  252. var edge = aContext[i];
  253. for(var j in edge) {
  254. if (j !== "name" && j !== "values") {
  255. //add all the properties of the object as attributes, except "name" and "values" which
  256. //are added later. Check for null only. Nothing should be undefined, and we want to maintain
  257. //0 as a number.
  258. xmlEdge.setAttribute(j, edge[j] !== null ? edge[j] : "");
  259. }
  260. }
  261. this._itemToXml(xmlEdge, edge.name, edge.values);
  262. }
  263. };
  264. CSelectionXml.prototype._itemToXml = function(parent, name, values) {
  265. var doc = parent.ownerDocument;
  266. var xmlItem = doc.createElement("item");
  267. XMLBuilderSetAttributeNodeNS(xmlItem, "xsi:type", "bus:parameterValue", "http://www.w3.org/2001/XMLSchema-instance");
  268. var xmlBusName = XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:name", doc);
  269. XMLBuilderSetAttributeNodeNS(xmlBusName, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
  270. xmlBusName.appendChild(doc.createTextNode(name));
  271. xmlItem.appendChild(xmlBusName);
  272. var xmlBusValue = XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:value", doc);
  273. XMLBuilderSetAttributeNodeNS(xmlBusValue, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
  274. XMLBuilderSetAttributeNodeNS(xmlBusValue, "SOAP-ENC:arrayType", "bus:parmValueItem[]", "http://schemas.xmlsoap.org/soap/encoding/");
  275. xmlItem.appendChild(xmlBusValue);
  276. ///NOTE: We only expect one value currently, but we support a list
  277. for (var j = 0; j < values.length; j++)
  278. {
  279. var xmlValueItem = doc.createElement("item");
  280. XMLBuilderSetAttributeNodeNS(xmlValueItem, "xsi:type", "bus:simpleParmValueItem", "http://www.w3.org/2001/XMLSchema-instance");
  281. var xmlValueUse = XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:use", doc);
  282. XMLBuilderSetAttributeNodeNS(xmlValueUse, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
  283. if(values[j].use)
  284. {
  285. xmlValueUse.appendChild(doc.createTextNode(values[j].use));
  286. }
  287. else if (values[j].display)
  288. {
  289. xmlValueUse.appendChild(doc.createTextNode(values[j].display));
  290. }
  291. else
  292. {
  293. xmlValueUse.appendChild(doc.createTextNode(""));
  294. }
  295. var xmlValueDisplay = XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:display", doc);
  296. XMLBuilderSetAttributeNodeNS(xmlValueDisplay, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
  297. if (values[j].display)
  298. {
  299. xmlValueDisplay.appendChild(doc.createTextNode(values[j].display));
  300. }
  301. else
  302. {
  303. xmlValueDisplay.appendChild(doc.createTextNode(""));
  304. }
  305. xmlValueItem.appendChild(xmlValueUse);
  306. xmlValueItem.appendChild(xmlValueDisplay);
  307. xmlBusValue.appendChild(xmlValueItem);
  308. }
  309. parent.appendChild(xmlItem);
  310. };
  311. CSelectionXml.prototype._burstToXml = function(parent) {
  312. var doc = parent.ownerDocument;
  313. var burstContext = doc.createElement("burst-context");
  314. burstContext.appendChild(doc.createTextNode(this.burstContext));
  315. parent.appendChild(burstContext);
  316. };
  317. CSelectionXml.prototype._slicersToXml = function(parent, slicers) {
  318. //TODO: add later
  319. };
  320. CSelectionXml.prototype._filtersToXml = function(parent, filter) {
  321. //TODO: add later
  322. };