ReportParameters.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. //
  11. // Description: This code sample demonstrates how to run and print a report with
  12. // prompts using the getParameters method.
  13. //
  14. // Use this method to return the list of parameters used by the
  15. // report, including optional parameters. This method also returns
  16. // parameters from the model and stored procedures that are used
  17. // by the report.
  18. using System;
  19. using System.Text;
  20. using System.Web.Services.Protocols;
  21. using System.Threading;
  22. using System.Windows.Forms;
  23. using SamplesCommon;
  24. using cognosdotnet_10_2;
  25. namespace ReportParameters
  26. {
  27. /// <summary>
  28. /// Demonstrate the getParameters() method.
  29. /// </summary>
  30. public class ReportParameters
  31. {
  32. /// <summary>
  33. /// Create a ReportParameters object.
  34. /// </summary>
  35. public ReportParameters()
  36. {
  37. }
  38. public static reportService1 repSvc = null;
  39. public static contentManagerService1 cmService = null;
  40. public static SamplesConnect connection = null;
  41. public static SamplesWindow gui = null;
  42. /// <summary>
  43. /// This C# method calls the getParameters method to
  44. /// return the list of parameters used by the report.
  45. /// </summary>
  46. /// <param name="reportPath">Specifies the search path of the report.</param>
  47. /// <param name="crn">Specifies the object that provides the connection to the report service.</param>
  48. /// <returns>An array of report parameters.</returns>
  49. public baseParameter[] getReportParameters( string reportPath, reportService1 cBIRS )
  50. {
  51. searchPathSingleObject cmReportPath = new searchPathSingleObject();
  52. cmReportPath.Value = reportPath;
  53. // sn_dg_prm_smpl_runreport_P1_start_1
  54. // sn_dg_sdk_method_reportService_getParameters_start_0
  55. asynchReply gpReply = cBIRS.getParameters( cmReportPath, new parameterValue[] {}, new runOption[]{} );
  56. // sn_dg_sdk_method_reportService_getParameters_end_0
  57. // sn_dg_prm_smpl_runreport_P1_end_1
  58. if ((gpReply.status != asynchReplyStatusEnum.complete)
  59. && (gpReply.status != asynchReplyStatusEnum.conversationComplete) )
  60. {
  61. while ( (gpReply.status != asynchReplyStatusEnum.complete)
  62. && (gpReply.status != asynchReplyStatusEnum.conversationComplete) )
  63. {
  64. gpReply = cBIRS.wait(gpReply.primaryRequest,new parameterValue[] {}, new option[] {});
  65. }
  66. }
  67. if (gpReply.details == null)
  68. {
  69. return null;
  70. }
  71. // sn_dg_sdk_method_reportService_getParameters_start_1
  72. for (int i = 0; i < gpReply.details.Length; i++)
  73. {
  74. if (gpReply.details[i] is asynchDetailParameters)
  75. {
  76. return ((asynchDetailParameters)gpReply.details[i]).parameters;
  77. }
  78. }
  79. // sn_dg_sdk_method_reportService_getParameters_end_1
  80. return null;
  81. }
  82. /// <summary>
  83. /// This C# method assigns values to each parameter for the specified report.
  84. /// </summary>
  85. /// <param name="reportName">The name of the report.</param>
  86. /// <param name="prm">An array of parameters.</param>
  87. /// <returns>An array of parameter values.</returns>
  88. public parameterValue[] setReportParameters( string reportName, baseParameter[] prm, bool isGui )
  89. {
  90. if( prm.Length > 0 )
  91. {
  92. parameterValue[] parms = new parameterValue[prm.Length];
  93. int pidx = 0;
  94. if( isGui )
  95. {
  96. SamplesInput inputter = new SamplesInput();
  97. // sn_dg_prm_smpl_runreport_P2_start_0
  98. foreach( parameter p in prm )
  99. {
  100. // Prompt the user to type a value for the parameter.
  101. // If the value is DateTime, the format must be in the ISO 8601
  102. // format. For example, a date and time of 2001-05-31T14:39:25.035Z
  103. // represents the thirty-first day of May in the year 2001. The time,
  104. // measured in Coordinated Universal Time (UTC) as indicated by the Z,
  105. // is 14 hours, 39 minutes, 25 seconds, and 35 milliseconds.
  106. string desc = "Enter a value for prompt '" + p.name + "' (of type " + p.type.ToString() + "):";
  107. string val = inputter.getInput( null, desc, p.type.ToString() );
  108. simpleParmValueItem item = new simpleParmValueItem();
  109. item.use = val;
  110. parmValueItem[] pvi = new parmValueItem[1];
  111. pvi[0] = item;
  112. parms[pidx] = new parameterValue();
  113. parms[pidx].name = p.name;
  114. parms[pidx].value = pvi;
  115. pidx = pidx+1;
  116. }
  117. // sn_dg_prm_smpl_runreport_P2_end_0
  118. }
  119. else
  120. { // is console
  121. foreach( parameter p in prm )
  122. {
  123. // Prompt the user to type a value for the parameter.
  124. // If the value is DateTime, the format must be in the ISO 8601
  125. // format. For example, a date and time of 2001-05-31T14:39:25.035Z
  126. // represents the thirty-first day of May in the year 2001. The time,
  127. // measured in Coordinated Universal Time (UTC) as indicated by the Z,
  128. // is 14 hours, 39 minutes, 25 seconds, and 35 milliseconds.
  129. string desc = "Enter a value for prompt \"" + p.name + "\" (of type " + p.type.ToString() + "):";
  130. Console.WriteLine( desc );
  131. string val = Console.ReadLine();
  132. simpleParmValueItem item = new simpleParmValueItem();
  133. item.use = val;
  134. parmValueItem[] pvi = new parmValueItem[1];
  135. pvi[0] = item;
  136. parms[pidx] = new parameterValue();
  137. parms[pidx].name = p.name;
  138. parms[pidx].value = pvi;
  139. pidx = pidx+1;
  140. }
  141. }
  142. return parms;
  143. }
  144. return null;
  145. }
  146. /// <summary>
  147. /// Application entry-point. Decide if we're running in GUI mode or
  148. /// command-line mode, and behave accordingly.
  149. /// </summary>
  150. /// <param name="args">The command-line arguments.</param>
  151. public static void Main( string[] args )
  152. {
  153. Control.CheckForIllegalCrossThreadCalls = false;
  154. try
  155. {
  156. if( args[0].CompareTo( "--test" ) == 0 )
  157. {
  158. // Run command-line version.
  159. ReportParameters test = new ReportParameters();
  160. baseParameter[] param = test.getReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", repSvc);
  161. foreach( parameter p in param )
  162. {
  163. Console.WriteLine( "Name: {0}, Type: {1}", p.name, p.type );
  164. }
  165. parameterValue[] pv = test.setReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", param, false);
  166. foreach( parameterValue p in pv )
  167. {
  168. Console.WriteLine( "Name: {0}, Value: {1}", p.name, p.value );
  169. }
  170. //Console.WriteLine( output );
  171. }
  172. else
  173. {
  174. // Complain.
  175. Console.WriteLine( "Unknown argument: {0}", args[0] );
  176. Console.WriteLine( "Use '--test' to run an automated test." );
  177. }
  178. }
  179. catch( IndexOutOfRangeException )
  180. {
  181. // If it's not a test, we should run the GUI.
  182. gui = new SamplesWindow();
  183. gui.Activated += new EventHandler( gui_Activated );
  184. gui.Actions.Click += new EventHandler( Actions_Click );
  185. Application.Run( gui );
  186. }
  187. }
  188. private static bool wasActivated = false;
  189. /// <summary>
  190. /// The UI window has been activated; we need to set up some values
  191. /// and create the service connections.
  192. /// </summary>
  193. /// <param name="sender">The object that sent this event.</param>
  194. /// <param name="e">Additional event arguments. (not used)</param>
  195. private static void gui_Activated( object sender, EventArgs e )
  196. {
  197. if( !wasActivated )
  198. {
  199. wasActivated = true;
  200. SamplesWindow ui = (SamplesWindow)sender;
  201. ui.applicationName = "Report Parameters";
  202. ui.applicationTitle = "Report Parameters";
  203. ui.applicationAction = "Go";
  204. ui.applicationVersion = "1.0";
  205. ui.AddText( "Connecting..." );
  206. ThreadPool.QueueUserWorkItem( new WaitCallback( createConnection ) );
  207. }
  208. }
  209. /// <summary>
  210. /// Create the service connections.
  211. ///
  212. /// We do this in a separate thread so the UI isn't tied up waiting
  213. /// for the service constructors to return. Under .NET,
  214. /// that can take a while.
  215. /// </summary>
  216. /// <param name="state">State for the thread. (not used)</param>
  217. private static void createConnection( object state )
  218. {
  219. try
  220. {
  221. gui.Actions.Enabled = false;
  222. repSvc = new reportService1();
  223. repSvc.Url = gui.serverUrl;
  224. cmService = new contentManagerService1();
  225. cmService.Url = gui.serverUrl;
  226. gui.AddText( "... done creating connection." );
  227. gui.Actions.Enabled = true;
  228. }
  229. catch( SoapException ex )
  230. {
  231. SamplesException.ShowExceptionMessage(ex,true,ex.Message);
  232. gui.Close();
  233. }
  234. }
  235. /// <summary>
  236. /// The user has clicked on the View button, so do the work.
  237. /// </summary>
  238. /// <param name="sender">The object that sent this event. (not used)</param>
  239. /// <param name="e">Additional event arguments. (not used)</param>
  240. private static void Actions_Click( object sender, EventArgs e )
  241. {
  242. gui.Actions.Enabled = false;
  243. gui.AddText( "Collecting parameters..." );
  244. Boolean bTestAnonymous = false;
  245. // Use the URL specified in the GUI...
  246. repSvc.Url = gui.serverUrl;
  247. cmService.Url = gui.serverUrl;
  248. //Test for Anonymous Authentication
  249. searchPathMultipleObject homeDir = new searchPathMultipleObject();
  250. homeDir.Value = "~";
  251. try
  252. {
  253. baseClass[] bc = cmService.query ( homeDir, new propEnum[]{} , new sort[]{}, new queryOptions () );
  254. if (bc != null)
  255. bTestAnonymous = true;
  256. else
  257. bTestAnonymous = false;
  258. }
  259. catch( Exception ex )
  260. {
  261. ex.Message.ToString ();
  262. }
  263. if (bTestAnonymous == true)
  264. {
  265. gui.Actions.Enabled = true;
  266. ReportParameters test = new ReportParameters();
  267. baseParameter[] param = test.getReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", repSvc);
  268. foreach( baseParameter p in param )
  269. {
  270. StringBuilder output = new StringBuilder();
  271. output.AppendFormat( "Name: {0}, Type: {1}", p.name, p.type );
  272. gui.AddText( output.ToString() );
  273. }
  274. parameterValue[] pv = test.setReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", param, true);
  275. foreach( parameterValue p in pv )
  276. {
  277. StringBuilder output = new StringBuilder();
  278. foreach( simpleParmValueItem pvi in p.value )
  279. {
  280. output.AppendFormat( "Name: {0}, Value: {1}", p.name, pvi.use );
  281. }
  282. gui.AddText( output.ToString() );
  283. }
  284. //gui.AddTextLines( output );
  285. gui.AddText( "... done query." );
  286. }
  287. else
  288. {
  289. // Attempt to log on.
  290. SamplesLogon logon = new SamplesLogon( cmService );
  291. logon.ShowDialog( gui );
  292. if( !logon.loggedOn )
  293. {
  294. gui.AddText( "Unable to log on." );
  295. return;
  296. }
  297. else
  298. {
  299. ReportParameters test = new ReportParameters();
  300. baseParameter[] param = test.getReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", repSvc);
  301. foreach( parameter p in param )
  302. {
  303. StringBuilder output = new StringBuilder();
  304. output.AppendFormat( "Name: {0}, Type: {1}", p.name, p.type );
  305. gui.AddText( output.ToString() );
  306. }
  307. parameterValue[] pv = test.setReportParameters("/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']/folder[@name='SDK Report Samples']/report[@name='Order Method Range List']", param, true);
  308. foreach( parameterValue p in pv )
  309. {
  310. StringBuilder output = new StringBuilder();
  311. foreach( simpleParmValueItem pvi in p.value )
  312. {
  313. output.AppendFormat( "Name: {0}, Value: {1}", p.name, pvi.use );
  314. }
  315. gui.AddText( output.ToString() );
  316. }
  317. //gui.AddTextLines( output );
  318. gui.AddText( "... done querying." );
  319. }
  320. }
  321. gui.Actions.Enabled = true;
  322. }
  323. }
  324. }