reportrunner.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  10. using System;
  11. using System.IO;
  12. using System.Text;
  13. using System.Web.Services.Protocols;
  14. using SamplesCommon;
  15. using cognosdotnet_10_2;
  16. namespace reportrunner
  17. {
  18. /// <summary>
  19. /// Run a report from the samples deployment archive, and save its output
  20. /// as HTML.
  21. ///
  22. /// Note that this application does no error handling; errors will cause
  23. /// ugly exception stack traces to appear on the console, and the
  24. /// application will exit ungracefully.
  25. /// </summary>
  26. class ReportRunner
  27. {
  28. /// <summary>
  29. /// The main entry point for the application.
  30. /// </summary>
  31. [STAThread]
  32. static void Main( string[] args )
  33. {
  34. // Change this URL if your server is in another location or a
  35. // different port.
  36. string serverHost = "localhost";
  37. string serverPort = "9300";
  38. // Change this search path if you want to run a different report.
  39. searchPathSingleObject reportPath = new searchPathSingleObject();
  40. reportPath.Value = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Product Description List']";
  41. // Output filename
  42. string outputPath = "reportrunner.html";
  43. // Specify a user to log in as.
  44. string userName = "";
  45. string userPassword = "";
  46. string userNamespace = "";
  47. // Parse the command-line arguments.
  48. bool exit = false;
  49. for( int idx = 0; idx < args.Length; idx++ ) {
  50. if( args[idx].CompareTo( "-host" ) == 0 ) {
  51. idx++;
  52. serverHost = args[idx];
  53. } else if( args[idx].CompareTo( "-port" ) == 0 ) {
  54. idx++;
  55. serverPort = args[idx];
  56. } else if( args[idx].CompareTo( "-report" ) == 0 ) {
  57. idx++;
  58. reportPath.Value = args[idx];
  59. } else if( args[idx].CompareTo( "-output" ) == 0 ) {
  60. idx++;
  61. outputPath = args[idx];
  62. } else if( args[idx].CompareTo( "-user" ) == 0 ) {
  63. idx++;
  64. userName = args[idx];
  65. } else if( args[idx].CompareTo( "-password" ) == 0 ) {
  66. idx++;
  67. userPassword = args[idx];
  68. } else if( args[idx].CompareTo( "-namespace" ) == 0 ) {
  69. idx++;
  70. userNamespace = args[idx];
  71. } else {
  72. Console.WriteLine( "Unknown argument: {0}\n", args[idx] );
  73. showUsage();
  74. exit = true;
  75. }
  76. }
  77. if( !exit ) {
  78. // Create a connection to the server.
  79. //
  80. // Note that creating the Service object takes "a
  81. // while" (based on your system speed)... if you're doing a GUI
  82. // application, it's a good idea to offload this into a separate
  83. // worker thread (see the ThreadPool class in the .NET Framework).
  84. string Server_URL = "http://" + serverHost + ":" + serverPort + "/p2pd/servlet/dispatch";
  85. Console.WriteLine( "Creating connection to {0}:{1}...", serverHost, serverPort );
  86. Console.WriteLine( "Server URL: {0}", Server_URL );
  87. reportService1 cBIRS = new reportService1();
  88. cBIRS.Url = Server_URL;
  89. Console.WriteLine( "... done." );
  90. // Set up the biBusHeader for a logon.
  91. if( userName.Length > 0 && userPassword.Length > 0 && userNamespace.Length > 0 ) {
  92. Console.WriteLine( "Logging on as {0} in the {1} namespace.", userName, userNamespace );
  93. setUpHeader( cBIRS, userName, userPassword, userNamespace );
  94. }
  95. // Set up the report parameters.
  96. parameterValue[] parameters = new parameterValue[]{};
  97. option[] runOptions = new option[5];
  98. runOptionBoolean saveOutput = new runOptionBoolean();
  99. saveOutput.name = runOptionEnum.saveOutput;
  100. saveOutput.value = false;
  101. runOptions[0] = saveOutput;
  102. // TODO: Output format should be specified on the command-line
  103. runOptionStringArray outputFormat = new runOptionStringArray();
  104. outputFormat.name = runOptionEnum.outputFormat;
  105. outputFormat.value = new string[]{ "HTML" };
  106. runOptions[1] = outputFormat;
  107. runOptionOutputEncapsulation outputEncapsulation = new runOptionOutputEncapsulation();
  108. outputEncapsulation.name = runOptionEnum.outputEncapsulation;
  109. outputEncapsulation.value = outputEncapsulationEnum.none;
  110. runOptions[2] = outputEncapsulation;
  111. asynchOptionInt primaryWait = new asynchOptionInt();
  112. primaryWait.name = asynchOptionEnum.primaryWaitThreshold;
  113. primaryWait.value = 0;
  114. runOptions[3] = primaryWait;
  115. asynchOptionInt secondaryWait = new asynchOptionInt();
  116. secondaryWait.name = asynchOptionEnum.secondaryWaitThreshold;
  117. secondaryWait.value = 0;
  118. runOptions[4] = secondaryWait;
  119. // Now, run the report.
  120. try {
  121. Console.WriteLine( "Running the report..." );
  122. Console.WriteLine( "Report search path is:" );
  123. Console.WriteLine( reportPath.Value );
  124. asynchReply res = cBIRS.run( reportPath, parameters, runOptions );
  125. Console.WriteLine( "... done." );
  126. // The report is finished, let's fetch the results and save them to
  127. // a file.
  128. string data = null;
  129. if( res.status == asynchReplyStatusEnum.complete )
  130. {
  131. for (int i = 0; i < res.details.Length; i++)
  132. {
  133. if (res.details[i] is asynchDetailReportOutput)
  134. {
  135. data = ( (asynchDetailReportOutput)res.details[i]).outputPages[0];
  136. }
  137. }
  138. Console.WriteLine( "Writing report output to {0}...", outputPath );
  139. FileStream fs = new FileStream( outputPath, FileMode.Create );
  140. // URIs in the output are relative on the server; we need to
  141. // make them absolute so they reference the server properly.
  142. string uriFix = "http://" + serverHost + ":" + serverPort + "/crn/";
  143. //foreach( string hunk in data ) {
  144. string fixed_hunk = data.Replace( "../", uriFix );
  145. byte[] hunk_data = UTF8Encoding.UTF8.GetBytes( fixed_hunk );
  146. fs.Write( hunk_data, 0, hunk_data.Length );
  147. //}
  148. fs.Close();
  149. Console.WriteLine( "... done." );
  150. }
  151. } catch( SoapException ex ) {
  152. string ex_str = Exceptions.CognosException.ConvertToString( ex );
  153. Console.WriteLine( "SOAP exception!\n" );
  154. Console.WriteLine( ex_str );
  155. } catch( Exception ex ) {
  156. Console.WriteLine( "Unhandled exception!" );
  157. Console.WriteLine( "Message: {0}", ex.Message );
  158. Console.WriteLine( "Stack trace:\n{0}", ex.StackTrace );
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// This function will prepare the biBusHeader.
  164. ///
  165. /// If no error was reported, then logon was successful.
  166. /// </summary>
  167. /// <param name="reportService1">An initialized report service object.</param>
  168. /// <param name="user">Log in as this User ID in the specified namespace.</param>
  169. /// <param name="pass">Password for user in the specified namespace.</param>
  170. /// <param name="name">The security namespace to log on to.</param>
  171. static void setUpHeader( reportService1 cBIRS, string user, string pass, string name ) {
  172. // Scrub the header to remove the conversation context.
  173. if( cBIRS.biBusHeaderValue != null ) {
  174. if( cBIRS.biBusHeaderValue.tracking != null ) {
  175. if( cBIRS.biBusHeaderValue.tracking.conversationContext != null ) {
  176. cBIRS.biBusHeaderValue.tracking.conversationContext = null;
  177. }
  178. }
  179. return;
  180. }
  181. // Set up a new biBusHeader for the "logon" action.
  182. cBIRS.biBusHeaderValue = new biBusHeader();
  183. cBIRS.biBusHeaderValue.CAM = new CAM();
  184. cBIRS.biBusHeaderValue.CAM.action = "logonAs";
  185. cBIRS.biBusHeaderValue.hdrSession = new hdrSession();
  186. formFieldVar[] ffs = new formFieldVar[3];
  187. ffs[0] = new formFieldVar();
  188. ffs[0].name = "CAMUsername";
  189. ffs[0].value = user;
  190. ffs[0].format = formatEnum.not_encrypted;
  191. ffs[1] = new formFieldVar();
  192. ffs[1].name = "CAMPassword";
  193. ffs[1].value = pass;
  194. ffs[1].format = formatEnum.not_encrypted;
  195. ffs[2] = new formFieldVar();
  196. ffs[2].name = "CAMNamespace";
  197. ffs[2].value = name;
  198. ffs[2].format = formatEnum.not_encrypted;
  199. cBIRS.biBusHeaderValue.hdrSession.formFieldVars = ffs;
  200. }
  201. static void showUsage() {
  202. Console.WriteLine( "Run a report and save the output as HTML.\n" );
  203. Console.WriteLine( "usage:\n" );
  204. Console.WriteLine( "-host hostName Host name of the server. " );
  205. Console.WriteLine( " Default: localhost" );
  206. Console.WriteLine( "-port portNumber Port number of the server 'host'." );
  207. Console.WriteLine( " Default: 80" );
  208. Console.WriteLine( "-report searchPath Search path to a report." );
  209. Console.WriteLine( " Default: /content/package[@name='GO Sales and Retailers']/folder[@name='Documentation Report Samples']/report[@name='Show Detailed Rows and Summaries']" );
  210. Console.WriteLine( "-output outputPath File name for the output HTML document." );
  211. Console.WriteLine( " Default: reportrunner.html" );
  212. Console.WriteLine( "-user userName User to log on as. Must exist in 'userNamespace'." );
  213. Console.WriteLine( " Default: none" );
  214. Console.WriteLine( "-password userPassword Password for 'userName' in 'userNamespace'." );
  215. Console.WriteLine( " Default: none" );
  216. Console.WriteLine( "-namespace userNamespace Security namespace to log on to." );
  217. Console.WriteLine( " Default: none" );
  218. }
  219. }
  220. }