CMTester.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 get information about an
  12. // object using the query method.
  13. //
  14. // Use this method to request objects from Content Manager.
  15. using System;
  16. using System.Text;
  17. using System.Web.Services.Protocols;
  18. using System.Threading;
  19. using System.Windows.Forms;
  20. using SamplesCommon;
  21. using cognosdotnet_10_2;
  22. namespace CMTester
  23. {
  24. /// <summary>
  25. /// Demonstrate the query() method.
  26. /// </summary>
  27. public class CMTester
  28. {
  29. /// <summary>
  30. /// Create a CMTester object.
  31. /// </summary>
  32. public CMTester()
  33. {
  34. }
  35. public static contentManagerService1 cBICMS = null;
  36. public static SamplesWindow gui = null;
  37. /// <summary>
  38. /// This C# method returns a string that contains either the
  39. /// information about the specified objects (if the request succeeded)
  40. /// or an error message (if the request failed).
  41. /// </summary>
  42. /// <param name="crn">The connection to the Content Manager service.</param>
  43. /// <returns>The searchPath, defaultName, creationTime and version of the specified objects.</returns>
  44. public string contentMgrTester( contentManagerService1 cmService )
  45. {
  46. try
  47. {
  48. propEnum[] props = new propEnum[] { propEnum.searchPath,
  49. propEnum.defaultName,
  50. propEnum.creationTime,
  51. propEnum.version };
  52. searchPathMultipleObject homePath = new searchPathMultipleObject();
  53. homePath.Value = "~";
  54. baseClass[] bc = cmService.query( homePath, props, new sort[]{}, new queryOptions() );
  55. StringBuilder output = new StringBuilder();
  56. if( bc.Length > 0 )
  57. {
  58. for( int i = 0; i < bc.Length; i++ )
  59. {
  60. account myAccount = (account)bc[i];
  61. output.AppendFormat( "The defaultName is: {0}\n", myAccount.defaultName.value );
  62. output.AppendFormat( "The searchPath is: {0}\n", myAccount.searchPath.value );
  63. output.AppendFormat( "The creationTime is: {0}\n", myAccount.creationTime.value.ToString() );
  64. output.AppendFormat( "The version is: {0}\n\n", myAccount.version.value );
  65. output.Append( "Content Manager is responding and operational." );
  66. }
  67. }
  68. else
  69. {
  70. output.Append( "\nError occurred in function contentMgrTester." );
  71. }
  72. return output.ToString();
  73. }
  74. catch( SoapException ex )
  75. {
  76. string str = SamplesException.FormatException( ex ) + "\n\nCM Tester:\nCannot connect to CM.\nCheck if CRN is running.";
  77. return str;
  78. }
  79. catch( NullReferenceException )
  80. {
  81. string str = "Invalid data passed to contentMgrTester.";
  82. return str;
  83. }
  84. }
  85. /// <summary>
  86. /// Application entry-point. Decide if we're running in GUI mode or
  87. /// command-line mode, and behave accordingly.
  88. /// </summary>
  89. /// <param name="args">The command-line arguments.</param>
  90. public static void Main( string[] args )
  91. {
  92. try
  93. {
  94. if( args[0].CompareTo( "--test" ) == 0 )
  95. {
  96. // Run command-line version.
  97. cBICMS = new contentManagerService1();
  98. cBICMS.Url = "http://localhost:9300/p2pd/servlet/dispatch";
  99. CMTester test = new CMTester();
  100. string output = test.contentMgrTester( cBICMS );
  101. Console.WriteLine( output );
  102. }
  103. else
  104. {
  105. // Complain.
  106. Console.WriteLine( "Unknown argument: {0}", args[0] );
  107. Console.WriteLine( "Use '--test' to run an automated test." );
  108. }
  109. }
  110. catch( IndexOutOfRangeException )
  111. {
  112. // If it's not a test, we should run the GUI.
  113. gui = new SamplesWindow();
  114. gui.Activated += new EventHandler( gui_Activated );
  115. gui.Actions.Click += new EventHandler( Actions_Click );
  116. Application.Run( gui );
  117. }
  118. }
  119. private static bool wasActivated = false;
  120. /// <summary>
  121. /// The UI window has been activated; we need to set up some values
  122. /// and create the service connection.
  123. /// </summary>
  124. /// <param name="sender">The object that sent this event.</param>
  125. /// <param name="e">Additional event arguments. (not used)</param>
  126. private static void gui_Activated( object sender, EventArgs e )
  127. {
  128. if( !wasActivated )
  129. {
  130. wasActivated = true;
  131. SamplesWindow ui = (SamplesWindow)sender;
  132. ui.applicationName = "CM Tester";
  133. ui.applicationTitle = "Content Manager Test";
  134. ui.applicationAction = "Query";
  135. ui.applicationVersion = "1.0";
  136. ui.AddText( "Connecting to Server..." );
  137. ThreadPool.QueueUserWorkItem( new WaitCallback( createConnection ) );
  138. }
  139. }
  140. /// <summary>
  141. /// Create the service connection.
  142. ///
  143. /// We do this in a separate thread so the UI isn't tied up waiting
  144. /// for the service constructor to return. Under .NET,
  145. /// that can take a while.
  146. /// </summary>
  147. /// <param name="state">State for the thread. (not used)</param>
  148. private static void createConnection( object state )
  149. {
  150. try
  151. {
  152. gui.Actions.Enabled = false;
  153. cBICMS = new contentManagerService1();
  154. gui.AddText( "... done creating connection." );
  155. gui.Actions.Enabled = true;
  156. }
  157. catch( SoapException ex )
  158. {
  159. SamplesException.ShowExceptionMessage( ex, true ," ");
  160. gui.Close();
  161. }
  162. }
  163. /// <summary>
  164. /// The user has clicked on the Query button, so do the query.
  165. /// </summary>
  166. /// <param name="sender">The object that sent this event. (not used)</param>
  167. /// <param name="e">Additional event arguments. (not used)</param>
  168. private static void Actions_Click( object sender, EventArgs e )
  169. {
  170. gui.Actions.Enabled = false;
  171. gui.AddText( "Sending query..." );
  172. Boolean bTestAnonymous = false;
  173. // Use the URL specified in the GUI...
  174. cBICMS.Url = gui.serverUrl;
  175. //Test for Anonymous Authentication
  176. try
  177. {
  178. searchPathMultipleObject homePath = new searchPathMultipleObject();
  179. homePath.Value = "~";
  180. baseClass[] bc = cBICMS.query ( homePath, new propEnum[]{} , new sort[]{}, new queryOptions () );
  181. if (bc != null)
  182. bTestAnonymous = true;
  183. else
  184. bTestAnonymous = false;
  185. }
  186. catch( Exception ex )
  187. {
  188. ex.Message.ToString ();
  189. }
  190. if (bTestAnonymous == true)
  191. {
  192. gui.Actions.Enabled = true;
  193. CMTester test = new CMTester();
  194. string output = test.contentMgrTester( cBICMS );
  195. gui.AddTextLines( output );
  196. gui.AddText( "... done query." );
  197. }
  198. else
  199. {
  200. {
  201. // Attempt to log on.
  202. SamplesLogon logon = new SamplesLogon( cBICMS );
  203. logon.ShowDialog( gui );
  204. if( !logon.loggedOn )
  205. {
  206. gui.AddText( "Unable to log on." );
  207. return;
  208. }
  209. else
  210. {
  211. gui.Actions.Enabled = true;
  212. CMTester test = new CMTester();
  213. string output = test.contentMgrTester( cBICMS );
  214. gui.AddTextLines( output );
  215. gui.AddText( "... done query." );
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }