CreateReportDlg.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. using System;
  9. using System.Xml;
  10. using System.Drawing;
  11. using System.Collections;
  12. using System.ComponentModel;
  13. using System.Windows.Forms;
  14. using SamplesCommon;
  15. using cognosdotnet_10_2;
  16. using System.Web.Services.Protocols;
  17. namespace CreateReport
  18. {
  19. /// <summary>
  20. /// Summary description for CreateReportDlg.
  21. /// </summary>
  22. public class CreateReportDlg : System.Windows.Forms.Form
  23. {
  24. private System.Windows.Forms.MainMenu mainMenu1;
  25. private System.Windows.Forms.MenuItem menuItem1;
  26. private System.Windows.Forms.MenuItem menuItem2;
  27. private System.Windows.Forms.MenuItem menuItem3;
  28. private System.Windows.Forms.MenuItem menuItem4;
  29. private System.Windows.Forms.Label label1;
  30. private System.Windows.Forms.GroupBox groupBox1;
  31. private System.Windows.Forms.Button CreateReportButton;
  32. private IContainer components;
  33. private System.Windows.Forms.RichTextBox resultsDisplayWindowRTB;
  34. private System.Windows.Forms.Label label2;
  35. private System.Windows.Forms.TextBox targetLocationTB;
  36. private System.Windows.Forms.TextBox serverUrlTB;
  37. public static SamplesConnect cBIServer = null;
  38. public CreateReportDlg()
  39. {
  40. //
  41. // Required for Windows Form Designer support
  42. //
  43. InitializeComponent();
  44. //
  45. // TODO: Add any constructor code after InitializeComponent call
  46. //
  47. }
  48. /// <summary>
  49. /// Clean up any resources being used.
  50. /// </summary>
  51. protected override void Dispose( bool disposing )
  52. {
  53. if( disposing )
  54. {
  55. if(components != null)
  56. {
  57. components.Dispose();
  58. }
  59. }
  60. base.Dispose( disposing );
  61. }
  62. #region Windows Form Designer generated code
  63. /// <summary>
  64. /// Required method for Designer support - do not modify
  65. /// the contents of this method with the code editor.
  66. /// </summary>
  67. private void InitializeComponent()
  68. {
  69. this.components = new System.ComponentModel.Container();
  70. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CreateReportDlg));
  71. this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
  72. this.menuItem1 = new System.Windows.Forms.MenuItem();
  73. this.menuItem2 = new System.Windows.Forms.MenuItem();
  74. this.menuItem3 = new System.Windows.Forms.MenuItem();
  75. this.menuItem4 = new System.Windows.Forms.MenuItem();
  76. this.label1 = new System.Windows.Forms.Label();
  77. this.serverUrlTB = new System.Windows.Forms.TextBox();
  78. this.CreateReportButton = new System.Windows.Forms.Button();
  79. this.groupBox1 = new System.Windows.Forms.GroupBox();
  80. this.resultsDisplayWindowRTB = new System.Windows.Forms.RichTextBox();
  81. this.label2 = new System.Windows.Forms.Label();
  82. this.targetLocationTB = new System.Windows.Forms.TextBox();
  83. this.groupBox1.SuspendLayout();
  84. this.SuspendLayout();
  85. //
  86. // mainMenu1
  87. //
  88. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  89. this.menuItem1,
  90. this.menuItem3});
  91. //
  92. // menuItem1
  93. //
  94. this.menuItem1.Index = 0;
  95. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  96. this.menuItem2});
  97. this.menuItem1.Text = "File";
  98. //
  99. // menuItem2
  100. //
  101. this.menuItem2.Index = 0;
  102. this.menuItem2.Text = "Exit";
  103. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  104. //
  105. // menuItem3
  106. //
  107. this.menuItem3.Index = 1;
  108. this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  109. this.menuItem4});
  110. this.menuItem3.Text = "Help";
  111. //
  112. // menuItem4
  113. //
  114. this.menuItem4.Index = 0;
  115. this.menuItem4.Text = "About";
  116. this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
  117. //
  118. // label1
  119. //
  120. this.label1.Location = new System.Drawing.Point(24, 8);
  121. this.label1.Name = "label1";
  122. this.label1.Size = new System.Drawing.Size(153, 16);
  123. this.label1.TabIndex = 0;
  124. this.label1.Text = "Server URL";
  125. //
  126. // serverUrlTB
  127. //
  128. this.serverUrlTB.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  129. | System.Windows.Forms.AnchorStyles.Right)));
  130. this.serverUrlTB.BackColor = System.Drawing.SystemColors.Control;
  131. this.serverUrlTB.Location = new System.Drawing.Point(24, 24);
  132. this.serverUrlTB.Name = "serverUrlTB";
  133. this.serverUrlTB.Size = new System.Drawing.Size(432, 20);
  134. this.serverUrlTB.TabIndex = 1;
  135. this.serverUrlTB.Text = "http://localhost:9300/p2pd/servlet/dispatch";
  136. //
  137. // CreateReportButton
  138. //
  139. this.CreateReportButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  140. this.CreateReportButton.Location = new System.Drawing.Point(328, 112);
  141. this.CreateReportButton.Name = "CreateReportButton";
  142. this.CreateReportButton.Size = new System.Drawing.Size(128, 23);
  143. this.CreateReportButton.TabIndex = 2;
  144. this.CreateReportButton.Text = "Create Report";
  145. this.CreateReportButton.Click += new System.EventHandler(this.CreateReportButton_Click);
  146. //
  147. // groupBox1
  148. //
  149. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  150. | System.Windows.Forms.AnchorStyles.Left)
  151. | System.Windows.Forms.AnchorStyles.Right)));
  152. this.groupBox1.Controls.Add(this.resultsDisplayWindowRTB);
  153. this.groupBox1.Location = new System.Drawing.Point(8, 136);
  154. this.groupBox1.Name = "groupBox1";
  155. this.groupBox1.Size = new System.Drawing.Size(464, 184);
  156. this.groupBox1.TabIndex = 3;
  157. this.groupBox1.TabStop = false;
  158. this.groupBox1.Text = "Results Display Window";
  159. //
  160. // resultsDisplayWindowRTB
  161. //
  162. this.resultsDisplayWindowRTB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  163. | System.Windows.Forms.AnchorStyles.Left)
  164. | System.Windows.Forms.AnchorStyles.Right)));
  165. this.resultsDisplayWindowRTB.BackColor = System.Drawing.SystemColors.Control;
  166. this.resultsDisplayWindowRTB.Location = new System.Drawing.Point(8, 16);
  167. this.resultsDisplayWindowRTB.Name = "resultsDisplayWindowRTB";
  168. this.resultsDisplayWindowRTB.Size = new System.Drawing.Size(448, 160);
  169. this.resultsDisplayWindowRTB.TabIndex = 0;
  170. this.resultsDisplayWindowRTB.Text = "";
  171. //
  172. // label2
  173. //
  174. this.label2.Location = new System.Drawing.Point(24, 56);
  175. this.label2.Name = "label2";
  176. this.label2.Size = new System.Drawing.Size(88, 16);
  177. this.label2.TabIndex = 4;
  178. this.label2.Text = "Target Location";
  179. //
  180. // targetLocationTB
  181. //
  182. this.targetLocationTB.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  183. | System.Windows.Forms.AnchorStyles.Right)));
  184. this.targetLocationTB.Location = new System.Drawing.Point(24, 72);
  185. this.targetLocationTB.Name = "targetLocationTB";
  186. this.targetLocationTB.Size = new System.Drawing.Size(432, 20);
  187. this.targetLocationTB.TabIndex = 5;
  188. this.targetLocationTB.Text = "CAMID(\"::Anonymous\")/folder[@name=\'My Folders\']";
  189. //
  190. // CreateReportDlg
  191. //
  192. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  193. this.ClientSize = new System.Drawing.Size(480, 329);
  194. this.Controls.Add(this.targetLocationTB);
  195. this.Controls.Add(this.serverUrlTB);
  196. this.Controls.Add(this.label2);
  197. this.Controls.Add(this.groupBox1);
  198. this.Controls.Add(this.CreateReportButton);
  199. this.Controls.Add(this.label1);
  200. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  201. this.Menu = this.mainMenu1;
  202. this.Name = "CreateReportDlg";
  203. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  204. this.Text = "Create Report";
  205. this.groupBox1.ResumeLayout(false);
  206. this.ResumeLayout(false);
  207. this.PerformLayout();
  208. }
  209. #endregion
  210. private void menuItem2_Click(object sender, System.EventArgs e)
  211. {
  212. this.Close();
  213. }
  214. private void menuItem4_Click(object sender, System.EventArgs e)
  215. {
  216. SamplesAbout about = new SamplesAbout();
  217. about.applicationName = "CreateReport";
  218. about.applicationVersion = "1.1";
  219. about.Show();
  220. }
  221. // This function is the entry point and the main driver for this sample.
  222. private void CreateReportButton_Click(object sender, System.EventArgs e)
  223. {
  224. try
  225. {
  226. clearResultsWindow();
  227. CreateReport crObject = new CreateReport();
  228. // 1. Prompt for the Package Name
  229. string packageName = crObject.getPackageName(cBIServer);
  230. if ( (packageName == null) || (0 == packageName.CompareTo("")) )
  231. {
  232. return;
  233. }
  234. displayMessage("The package name selected is: " + packageName);
  235. // 2. Prompt for the name of the report to be created
  236. string reportName = crObject.getReportName();
  237. if ( (reportName == null) || (0 == reportName.CompareTo("")) )
  238. {
  239. return;
  240. }
  241. displayMessage("The new report name will be : " + reportName);
  242. // 3. Build up and Display the list of tables and Columns
  243. // 3.1 Get the list of table names and columns for the selected package
  244. string packageSearchPath = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='" + packageName + "']";
  245. CreateReportDlg crObjectDlg = new CreateReportDlg();
  246. string metaDataResponse = crObjectDlg.getPackageMetaData(packageSearchPath);
  247. if (0 == metaDataResponse.CompareTo(""))
  248. {
  249. return;
  250. }
  251. TableData[] tableNameColumnMap = crObjectDlg.buildUpTableData(metaDataResponse);
  252. if (tableNameColumnMap == null)
  253. {
  254. return;
  255. }
  256. // 3.2 Populate the UI with the table names and columns
  257. CreateReportTableDlg crTableDlg = new CreateReportTableDlg(tableNameColumnMap, packageName, reportName);
  258. crTableDlg.populateTableNameList();
  259. crTableDlg.selectTableIndex(0);
  260. crTableDlg.ShowDialog();
  261. if (crTableDlg.isOKed)
  262. {
  263. // 4. Create the specification XML for the new report
  264. // and add it to the content store
  265. reportServiceSpecification reportSpec = crObject.createReportSpec(cBIServer,
  266. packageName,
  267. targetLocationTB.Text,
  268. reportName,
  269. crTableDlg.getSelectedTableName(),
  270. crTableDlg.getSelectedColumnList());
  271. if (reportSpec != null)
  272. {
  273. displayMessage("...the report \"" + reportName + "\" has been successfully added to the content store.");
  274. displayMessage("\nExecuting report specification...");
  275. }
  276. else
  277. {
  278. displayMessage("...failed adding the report \"" + reportName + "\" to the content store.");
  279. return;
  280. }
  281. // 5. Execute the specification
  282. bool status = crObject.executeReportSpec(cBIServer, reportSpec);
  283. if (status)
  284. {
  285. displayMessage("...the report \"" + reportName + "\" executed successfully.");
  286. }
  287. else
  288. {
  289. displayMessage("...failed executing the report \"" + reportName + "\".");
  290. return;
  291. }
  292. }
  293. else
  294. {
  295. return;
  296. }
  297. }
  298. catch(SoapException ex)
  299. {
  300. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - CreateReportButton_Click" );
  301. return;
  302. }
  303. catch(System.Exception ex)
  304. {
  305. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - CreateReportButton_Click" );
  306. return;
  307. }
  308. }
  309. public void setConnection(SamplesConnect connection)
  310. {
  311. cBIServer = connection;
  312. serverUrlTB.Text = connection.CBIURL;
  313. }
  314. public void displayMessage(string message)
  315. {
  316. resultsDisplayWindowRTB.AppendText(message + "\n");
  317. }
  318. public void clearResultsWindow()
  319. {
  320. resultsDisplayWindowRTB.Clear();
  321. }
  322. //********************************************************
  323. // * Get the metadata for the selected OR default (first)
  324. // * package listed
  325. //********************************************************
  326. public string getPackageMetaData(string packageName)
  327. {
  328. try
  329. {
  330. // Get the metadata for the selected OR default (second) package listed
  331. string metaDataRequest = "<metadataRequest connection=\""
  332. + packageName
  333. + "\">"
  334. + "<Metadata Depth=\"\" "
  335. + "no_collections=\"1\">"
  336. + "<Properties>"
  337. + "<Property name=\"*/@name\"/>"
  338. + "<Property name=\"*/@datatype\"/>"
  339. + "<Property name=\"*/@_path\"/>"
  340. + "<Property name=\"*/@_ref\"/>"
  341. + "<Property name=\"*/@usage\"/>"
  342. + "<Property name=\"*/folder\"/>"
  343. + "<Property name=\"./querySubject\"/>"
  344. + "<Property name=\"./queryItem\"/>"
  345. + "<Property name=\"*/measure\"/>"
  346. + "<Property name=\"*/member\"/>"
  347. + "<Property name=\"*/level\"/>"
  348. + "<Property name=\"*/fact\"/>"
  349. + "<Property name=\"*/hierarchy\"/>"
  350. + "</Properties>"
  351. + "</Metadata>"
  352. + "</metadataRequest>";
  353. reportServiceMetadataSpecification mdSpec = new reportServiceMetadataSpecification();
  354. mdSpec.value = new specification();
  355. mdSpec.value.Value = metaDataRequest;
  356. asynchReply mdResponse = cBIServer.CBIRS.runSpecification(mdSpec,new parameterValue[] {}, new option [] {});
  357. asynchDetailReportMetadata reportMD = null;
  358. for (int i = 0; i < mdResponse.details.Length; i++)
  359. {
  360. if (mdResponse.details[i] is asynchDetailReportMetadata)
  361. {
  362. reportMD = (asynchDetailReportMetadata) mdResponse.details[i];
  363. break;
  364. }
  365. }
  366. string metaDataString = reportMD.metadata.Value;
  367. return metaDataString;
  368. }
  369. catch(SoapException ex)
  370. {
  371. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - getPackageMetaData" );
  372. return "";
  373. }
  374. catch(System.Exception ex)
  375. {
  376. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - getPackageMetaData" );
  377. return "";
  378. }
  379. }
  380. public TableData[] buildUpTableData(string metaDataResponse)
  381. {
  382. try
  383. {
  384. string tableName = "";
  385. XmlNodeWrapper[] tableColumns = null;
  386. TableData[] tableNameColumnMap = null;
  387. // Load the metadata XML into a DOM
  388. XmlDocument metaDataDocument = new XmlDocument();
  389. XmlNodeList tableNodes;
  390. XmlNode tableNode;
  391. XmlNode tableAttributeNode;
  392. metaDataDocument.LoadXml(metaDataResponse);
  393. // Parse the XML and build up the table objects
  394. XmlElement root = metaDataDocument.DocumentElement;
  395. tableNodes = root.SelectNodes("//querySubject");
  396. if (tableNodes.Count == 0)
  397. {
  398. tableNodes = root.SelectNodes("//folder/folder");
  399. }
  400. int nbTables = tableNodes.Count;
  401. tableNameColumnMap = new TableData[nbTables];
  402. for (int i=0; i<nbTables; i++)
  403. {
  404. tableNode = tableNodes[i];
  405. tableAttributeNode = tableNode.Attributes.GetNamedItem("name");
  406. tableName = tableAttributeNode.Value;
  407. if (tableNode.HasChildNodes)
  408. {
  409. //XmlNodeList of report-insertable items
  410. XmlNodeList tableColumnList1 = tableNode.SelectNodes(".//measure");
  411. XmlNodeList tableColumnList2 = tableNode.SelectNodes(".//queryItem");
  412. tableColumns = new XmlNodeWrapper[tableColumnList1.Count + tableColumnList2.Count];
  413. //add first list of nodes, if any
  414. for (int j=0; j<tableColumnList1.Count; j++)
  415. {
  416. XmlNode tableColumnNode = tableColumnList1[j];
  417. XmlNodeWrapper tableColumnAttrNode = new XmlNodeWrapper(tableColumnNode);
  418. tableColumns[j] = tableColumnAttrNode;
  419. }
  420. //add second list of nodes, if any
  421. for (int j=tableColumnList1.Count;j < tableColumnList1.Count+tableColumnList2.Count; j++)
  422. {
  423. XmlNode tableColumnNode = tableColumnList2[j-tableColumnList1.Count];
  424. XmlNodeWrapper tableColumnAttrNode = new XmlNodeWrapper(tableColumnNode);
  425. tableColumns[j] = tableColumnAttrNode;
  426. }
  427. }
  428. tableNameColumnMap[i] = new TableData(tableName, tableColumns);
  429. }
  430. return tableNameColumnMap;
  431. }
  432. catch(SoapException ex)
  433. {
  434. SamplesException.ShowExceptionMessage( ex, true, "Create Report Sample - buildUpTableData" );
  435. return null;
  436. }
  437. catch(System.Exception ex)
  438. {
  439. SamplesException.ShowExceptionMessage( ex.Message, true, "Create Report Sample - buildUpTableData" );
  440. return null;
  441. }
  442. }
  443. }
  444. }