CreateReport.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2010
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * CreateReport.cs
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. * Description: This code sample demonstrates how to create reports using the
  15. * following methods:
  16. * - query(searchPath, properties, sortBy, options)
  17. * - add(parentPath, object, options)
  18. * - runSpecification(specification, parameterValues, options)
  19. * - wait(conversation, parameterValues, options)
  20. *
  21. */
  22. using System;
  23. using System.IO;
  24. using System.Text;
  25. using System.Threading;
  26. using System.Windows.Forms;
  27. using System.Web.Services.Protocols;
  28. using SamplesCommon;
  29. using cognosdotnet_10_2;
  30. using System.Xml;
  31. namespace CreateReport
  32. {
  33. public class TableData
  34. {
  35. public TableData m_tableNameColumnMap;
  36. public string m_tableName = "";
  37. public XmlNodeWrapper[] m_columns = null;
  38. public TableData()
  39. {
  40. m_tableName = "";
  41. m_columns = null;
  42. }
  43. public TableData(string tbName, XmlNodeWrapper[] cols)
  44. {
  45. m_tableName = tbName;
  46. int nbCols = cols.GetLength(0);
  47. m_columns = new XmlNodeWrapper[nbCols];
  48. m_columns = cols;
  49. }
  50. public string getTableName()
  51. {
  52. return m_tableName;
  53. }
  54. public XmlNodeWrapper[] getColumns()
  55. {
  56. return m_columns;
  57. }
  58. }
  59. /// <summary>
  60. /// Summary description for Class1.
  61. /// </summary>
  62. class CreateReport
  63. {
  64. public CreateReport(){}
  65. static void Main(string[] args)
  66. {
  67. SamplesConnect connectDlg = new SamplesConnect();
  68. CreateReportDlg createReportDlgObject = new CreateReportDlg();
  69. if (args.GetLength(0) == 0 )
  70. {
  71. // GUI mode
  72. connectDlg.ShowDialog();
  73. if (connectDlg.IsConnectedToCBI() == true)
  74. {
  75. createReportDlgObject.setConnection(connectDlg);
  76. createReportDlgObject.ShowDialog();
  77. }
  78. }
  79. }
  80. public string getPackageName(SamplesConnect connection)
  81. {
  82. string packageName = "";
  83. try
  84. {
  85. // 1. Prompt for the Package Name
  86. PackageNameDlg packageNameDlg = new PackageNameDlg();
  87. packageNameDlg.setConnection(connection);
  88. // 1.1 get the list of baseClass packages
  89. sort[] sortOptions = { new sort() };
  90. sortOptions[0].order = orderEnum.ascending;
  91. sortOptions[0].propName = propEnum.defaultName;
  92. propEnum[] props = new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  93. searchPathMultipleObject packagesPath = new searchPathMultipleObject();
  94. packagesPath.Value = "/content//package";
  95. baseClass[] bc = connection.CBICMS.query(packagesPath, props, sortOptions, new queryOptions());
  96. // 1.2 Get the name of each package from the baseClass object
  97. // to display in the dropdown list.
  98. string[] packageNames = new string[bc.GetLength(0)];
  99. for (int i = 0; i < bc.GetLength(0); i++)
  100. {
  101. packageNames[i] = bc[i].defaultName.value;
  102. }
  103. packageNameDlg.setPackageNames(packageNames);
  104. packageNameDlg.setSelectedPackage(1);
  105. packageNameDlg.ShowDialog();
  106. if (packageNameDlg.isOKed)
  107. {
  108. packageName = packageNameDlg.getSelectedPackageName();
  109. }
  110. else
  111. {
  112. return "";
  113. }
  114. }
  115. catch(SoapException ex)
  116. {
  117. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - getPackageName" );
  118. return "";
  119. }
  120. catch(System.Exception ex)
  121. {
  122. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - getPackageName" );
  123. return "";
  124. }
  125. return packageName;
  126. }
  127. public string getReportName()
  128. {
  129. try
  130. {
  131. string reportName = "";
  132. // 2. Prompt for the new report name
  133. ReportNameDlg reportNameDlg = new ReportNameDlg();
  134. reportNameDlg.ShowDialog();
  135. if (reportNameDlg.isOKed)
  136. {
  137. // 2.1 Get the name of the report entered by the user
  138. reportName = reportNameDlg.getReportName();
  139. }
  140. else
  141. {
  142. return "";
  143. }
  144. return reportName;
  145. }
  146. catch(SoapException ex)
  147. {
  148. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - getReportName" );
  149. return "";
  150. }
  151. catch(System.Exception ex)
  152. {
  153. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - getReportName" );
  154. return "";
  155. }
  156. }
  157. public XmlNode buildListColumnNode(XmlDocument xmlDoc, string myNameSpace, string columnLabel, string columnValue)
  158. {
  159. XmlNode nextNode, currentNode, listColumnNode, styleNode, currentStyleNode;
  160. XmlAttribute attribNode;
  161. listColumnNode = xmlDoc.CreateNode(XmlNodeType.Element, "listColumn", myNameSpace);
  162. //Build listColumnTitle from the bottom up
  163. //dataItemLabel
  164. // sn_dg_prm_smpl_modifyreport_P2_start_0
  165. currentNode = xmlDoc.CreateNode(XmlNodeType.Element, "dataItemLabel", myNameSpace);
  166. attribNode = xmlDoc.CreateAttribute("refDataItem");
  167. attribNode.Value = columnLabel;
  168. currentNode.Attributes.Append(attribNode);
  169. // sn_dg_prm_smpl_modifyreport_P2_end_0
  170. //dataSource
  171. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "dataSource", myNameSpace);
  172. nextNode.AppendChild(currentNode);
  173. currentNode = nextNode;
  174. //textItem
  175. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "textItem", myNameSpace);
  176. nextNode.AppendChild(currentNode);
  177. currentNode = nextNode;
  178. //contents
  179. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "contents", myNameSpace);
  180. nextNode.AppendChild(currentNode);
  181. currentNode = nextNode;
  182. //style for listColumnTitle
  183. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyle", myNameSpace);
  184. attribNode = xmlDoc.CreateAttribute("refStyle");
  185. attribNode.Value = "lt";
  186. nextNode.Attributes.Append(attribNode);
  187. currentStyleNode = nextNode;
  188. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyles", myNameSpace);
  189. nextNode.AppendChild(currentStyleNode); //defaultStyle
  190. currentStyleNode = nextNode;
  191. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", myNameSpace);
  192. nextNode.AppendChild(currentStyleNode); //defaultStyles
  193. styleNode = nextNode;
  194. //listColumnTitle
  195. nextNode = xmlDoc.CreateNode(XmlNodeType.Element,"listColumnTitle", myNameSpace);
  196. nextNode.AppendChild(styleNode); //style
  197. nextNode.AppendChild(currentNode);
  198. //Add the listColumnTitle Element to the listColumnElement
  199. listColumnNode.AppendChild(nextNode);
  200. //Build listColumnBody from the bottom up
  201. //dataItemValue
  202. // sn_dg_prm_smpl_modifyreport_P2_start_1
  203. currentNode = xmlDoc.CreateNode(XmlNodeType.Element, "dataItemValue", myNameSpace);
  204. attribNode = xmlDoc.CreateAttribute("refDataItem");
  205. attribNode.Value = columnValue;
  206. currentNode.Attributes.Append(attribNode);
  207. // sn_dg_prm_smpl_modifyreport_P2_end_1
  208. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "dataSource", myNameSpace);
  209. nextNode.AppendChild(currentNode);
  210. currentNode = nextNode;
  211. //textItem
  212. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "textItem", myNameSpace);
  213. nextNode.AppendChild(currentNode);
  214. currentNode = nextNode;
  215. //contents
  216. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "contents", myNameSpace);
  217. nextNode.AppendChild(currentNode);
  218. currentNode = nextNode;
  219. //style for listColumnBody
  220. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyle", myNameSpace);
  221. attribNode = xmlDoc.CreateAttribute("refStyle");
  222. attribNode.Value = "lc";
  223. nextNode.Attributes.Append(attribNode);
  224. currentStyleNode = nextNode;
  225. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyles", myNameSpace);
  226. nextNode.AppendChild(currentStyleNode); //defaultStyle
  227. currentStyleNode = nextNode;
  228. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", myNameSpace);
  229. nextNode.AppendChild(currentStyleNode); //defaultStyles
  230. styleNode = nextNode;
  231. //listColumnBody
  232. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "listColumnBody", myNameSpace);
  233. nextNode.AppendChild(styleNode); //style
  234. nextNode.AppendChild(currentNode);
  235. //Add the listColumnBody to the listColumnElement
  236. listColumnNode.AppendChild(nextNode);
  237. return listColumnNode;
  238. }
  239. public XmlNode buildDataItemNode(XmlDocument xmlDoc, string myNameSpace, string name, string expression)
  240. {
  241. XmlNode expressionNode, dataItemNode;
  242. XmlAttribute attribNode;
  243. //expression
  244. expressionNode = xmlDoc.CreateNode(XmlNodeType.Element, "expression", myNameSpace);
  245. expressionNode.InnerText = expression;
  246. //dataItem
  247. dataItemNode = xmlDoc.CreateNode(XmlNodeType.Element, "dataItem", myNameSpace);
  248. attribNode = xmlDoc.CreateAttribute("name");
  249. attribNode.Value = name;
  250. dataItemNode.Attributes.Append(attribNode);
  251. attribNode = xmlDoc.CreateAttribute("aggregate");
  252. attribNode.Value = "none";
  253. dataItemNode.Attributes.Append(attribNode);
  254. dataItemNode.AppendChild(expressionNode);
  255. return dataItemNode;
  256. }
  257. public reportServiceSpecification createReportSpec(SamplesConnect connection, string packageName, string targetLocation, string newReportName, string tableName, XmlNodeWrapper[] selectedColumns)
  258. {
  259. try
  260. {
  261. // sn_dg_prm_smpl_modifyreport_P1_start_0
  262. XmlDocument xmlDoc = new XmlDocument();
  263. XmlNode root,
  264. currentNode,
  265. nextNode,
  266. listColumnsNode,
  267. cssNode,
  268. currentStyleNode,
  269. styleNode,
  270. selectionNode;
  271. XmlAttribute attribNode;
  272. string defaultPackageName, strName, strReportXML;
  273. report reportName = new report();
  274. addOptions addOptions = new addOptions();
  275. tokenProp tokenName = new tokenProp();
  276. anyTypeProp anyTypeName = new anyTypeProp();
  277. reportServiceReportSpecification rspecReport = new reportServiceReportSpecification();
  278. string myNS = "http://developer.cognos.com/schemas/report/15.0/";
  279. //
  280. //root report element
  281. //
  282. root = xmlDoc.CreateNode(XmlNodeType.Element, "report", myNS);
  283. attribNode = xmlDoc.CreateAttribute("expressionLocale");
  284. attribNode.Value = "en-us";
  285. root.Attributes.Append(attribNode);
  286. xmlDoc.AppendChild(root);
  287. //Add all the rest of the foundation report elements
  288. // sn_dg_prm_smpl_modifyreport_P1_end_0
  289. //Insert version comment
  290. currentNode = xmlDoc.CreateComment("RS:8.1");
  291. root.AppendChild(currentNode);
  292. //set modelPath and add to report
  293. defaultPackageName = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='"
  294. + packageName
  295. + "']/model[@name='model']";
  296. currentNode = xmlDoc.CreateNode(XmlNodeType.Element, "modelPath", myNS);
  297. currentNode.InnerText = defaultPackageName;
  298. root.AppendChild(currentNode);
  299. //
  300. //query structure, build from bottom up
  301. //
  302. //model
  303. currentNode = xmlDoc.CreateNode(XmlNodeType.Element, "model", myNS);
  304. //source
  305. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "source", myNS);
  306. nextNode.AppendChild(currentNode); //model
  307. currentNode = nextNode;
  308. //query
  309. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "query", myNS);
  310. attribNode = xmlDoc.CreateAttribute("name");
  311. attribNode.Value = "Query1";
  312. nextNode.Attributes.Append(attribNode);
  313. nextNode.AppendChild(currentNode); //source
  314. //selection - also part of query
  315. //we need to keep a reference to this element for
  316. //adding columns
  317. selectionNode = xmlDoc.CreateNode(XmlNodeType.Element, "selection", myNS);
  318. nextNode.AppendChild(selectionNode); //selection
  319. currentNode = nextNode;
  320. //queries
  321. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "queries", myNS);
  322. nextNode.AppendChild(currentNode); //query
  323. currentNode = nextNode;
  324. //add queries to root report element
  325. root.AppendChild(currentNode); //queries
  326. //
  327. //layout structure, build from bottom up
  328. //
  329. //CSS
  330. cssNode = xmlDoc.CreateNode(XmlNodeType.Element, "CSS", myNS);
  331. attribNode = xmlDoc.CreateAttribute("value");
  332. attribNode.Value = "border-collapse:collapse";
  333. cssNode.Attributes.Append(attribNode);
  334. //style for list
  335. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyle", myNS);
  336. attribNode = xmlDoc.CreateAttribute("refStyle");
  337. attribNode.Value = "ls";
  338. nextNode.Attributes.Append(attribNode);
  339. currentStyleNode = nextNode;
  340. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyles", myNS);
  341. nextNode.AppendChild(currentStyleNode); //defaultStyle
  342. currentStyleNode = nextNode;
  343. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", myNS);
  344. nextNode.AppendChild(currentStyleNode); //defaultStyles
  345. nextNode.AppendChild(cssNode); //css
  346. styleNode = nextNode;
  347. //list
  348. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "list", myNS);
  349. attribNode = xmlDoc.CreateAttribute("refQuery");
  350. attribNode.Value = "Query1";
  351. nextNode.Attributes.Append(attribNode);
  352. nextNode.AppendChild(styleNode); //style
  353. //listColumns -- also added to list
  354. //We need to keep a reference to this element for
  355. //adding columns
  356. listColumnsNode = xmlDoc.CreateNode(XmlNodeType.Element, "listColumns", myNS);
  357. nextNode.AppendChild(listColumnsNode); //listColumns
  358. currentNode = nextNode;
  359. //style for pageBody
  360. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyle", myNS);
  361. attribNode = xmlDoc.CreateAttribute("refStyle");
  362. attribNode.Value = "pb";
  363. nextNode.Attributes.Append(attribNode);
  364. currentStyleNode = nextNode;
  365. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyles", myNS);
  366. nextNode.AppendChild(currentStyleNode); //defaultStyle
  367. currentStyleNode = nextNode;
  368. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", myNS);
  369. nextNode.AppendChild(currentStyleNode); //defaultStyles
  370. styleNode = nextNode;
  371. //contents
  372. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "contents", myNS);
  373. nextNode.AppendChild(currentNode); //list
  374. currentNode = nextNode;
  375. //pageBody
  376. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "pageBody", myNS);
  377. nextNode.AppendChild(styleNode); //style
  378. nextNode.AppendChild(currentNode); //contents
  379. currentNode = nextNode;
  380. //style for page
  381. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyle", myNS);
  382. attribNode = xmlDoc.CreateAttribute("refStyle");
  383. attribNode.Value = "pg";
  384. nextNode.Attributes.Append(attribNode);
  385. currentStyleNode = nextNode;
  386. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "defaultStyles", myNS);
  387. nextNode.AppendChild(currentStyleNode); //defaultStyle
  388. currentStyleNode = nextNode;
  389. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", myNS);
  390. nextNode.AppendChild(currentStyleNode); //defaultStyles
  391. styleNode = nextNode;
  392. //page
  393. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "page", myNS);
  394. attribNode = xmlDoc.CreateAttribute("name");
  395. attribNode.Value = "Page1";
  396. nextNode.Attributes.Append(attribNode);
  397. nextNode.AppendChild(styleNode); //style
  398. nextNode.AppendChild(currentNode); //pageBody
  399. currentNode = nextNode;
  400. //reportPages
  401. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "reportPages", myNS);
  402. nextNode.AppendChild(currentNode); //page
  403. currentNode = nextNode;
  404. //layout
  405. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "layout", myNS);
  406. nextNode.AppendChild(currentNode); //reportPages
  407. currentNode = nextNode;
  408. //put layout in layouts and add to report
  409. nextNode = xmlDoc.CreateNode(XmlNodeType.Element, "layouts", myNS);
  410. nextNode.AppendChild(currentNode); //layout
  411. root.AppendChild(nextNode);
  412. //in a for loop, for each UI selected addition,
  413. //add a dataItem to the selectionElement
  414. //and add a listColumn to the listColumnsElement
  415. int nbSelectedColumns = selectedColumns.GetLength(0);
  416. for (int i=0; i<nbSelectedColumns; i++)
  417. {
  418. //dataItem
  419. selectionNode.AppendChild(buildDataItemNode(xmlDoc, myNS, selectedColumns[i].ToString(), selectedColumns[i].PATH)); //from UI selection
  420. //listColumns
  421. listColumnsNode.AppendChild(buildListColumnNode(xmlDoc, myNS, selectedColumns[i].ToString(), selectedColumns[i].ToString())); //ditto
  422. }
  423. //debugging info
  424. //Console.WriteLine("\n OUTER XML : " + xmlDoc.OuterXml);
  425. //Console.WriteLine("\n INNER XML : " + xmlDoc.InnerXml);
  426. strReportXML = root.OuterXml;
  427. strName = newReportName;
  428. //take all that xml and start building the report object
  429. // sn_dg_prm_smpl_modifyreport_P3_start_0
  430. tokenName.value = strName;
  431. anyTypeName.value = strReportXML;
  432. reportName.defaultName = tokenName;
  433. reportName.specification = anyTypeName;
  434. addOptions.updateAction = updateActionEnum.replace;
  435. reportName.specification.value = strReportXML;
  436. searchPathSingleObject targetPath = new searchPathSingleObject();
  437. targetPath.Value = targetLocation;
  438. connection.CBIRS.add(targetPath, reportName, addOptions);
  439. // sn_dg_prm_smpl_modifyreport_P3_end_0
  440. rspecReport.value = new specification();
  441. rspecReport.value.Value = reportName.specification.value;
  442. return rspecReport;
  443. }
  444. catch(SoapException ex)
  445. {
  446. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - createReportSpec" );
  447. return null;
  448. }
  449. catch(System.Exception ex)
  450. {
  451. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - createReportSpec" );
  452. return null;
  453. }
  454. }
  455. public bool executeReportSpec(SamplesConnect connection, reportServiceSpecification reportSpec)
  456. {
  457. try
  458. {
  459. //Run the specification and send the results to a file.
  460. option[] runOptions = new option[0];
  461. asynchReply executeResponse = new asynchReply();
  462. executeResponse = connection.CBIRS.runSpecification(reportSpec, new parameterValue[] {} , runOptions);
  463. //If the request has not yet completed, keep waiting until it has finished
  464. while ( executeResponse.status != asynchReplyStatusEnum.complete )
  465. {
  466. executeResponse = connection.CBIRS.wait(executeResponse.primaryRequest,new parameterValue[] {}, new option[] {});
  467. }
  468. return true;
  469. }
  470. catch(SoapException ex)
  471. {
  472. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - executeReportSpec" );
  473. return false;
  474. }
  475. catch(System.Exception ex)
  476. {
  477. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - executeReportSpec" );
  478. return false;
  479. }
  480. }
  481. }
  482. }