CMQuery.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // *
  9. // * cmQuery.sln
  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 delete reports using the
  15. // * following methods:
  16. // * - query: Use this method to request objects from Content Manager.
  17. // *
  18. // * To run this sample from the command line use:
  19. // * cmquery.exe -sp <searchPath> [-un <userName> -pwd <userPassword> -ns <userNamepsace>]
  20. // *
  21. using System;
  22. using System.Windows.Forms;
  23. using System.Web.Services.Protocols;
  24. using SamplesCommon;
  25. using cognosdotnet_10_2;
  26. namespace CMQuery
  27. {
  28. /// <summary>
  29. /// CMQuery is a simple command-line application that lets you explore
  30. /// the Content Manager/Content Store database.
  31. /// </summary>
  32. public class CMQuery
  33. {
  34. public CMQuery(){}
  35. static void Main(string[] args)
  36. {
  37. bool isGUImode = false;
  38. string BIServerURL = "";
  39. string userName = "";
  40. string userPassword = "";
  41. string userNamespace = "";
  42. string searchPath = "";
  43. CMQuery cmQueryObj = new CMQuery();
  44. CMQueryDlg cmQueryDlgObject = new CMQueryDlg();
  45. if (args.GetLength(0) == 0 )
  46. {
  47. // GUI mode
  48. isGUImode = true;
  49. SamplesConnect connectDlg = new SamplesConnect();
  50. connectDlg.ShowDialog();
  51. if (connectDlg.IsConnectedToCBI() == true)
  52. {
  53. cmQueryDlgObject.setConnection(connectDlg.CBICMS,
  54. connectDlg.CBIURL,
  55. userName,
  56. userPassword,
  57. userNamespace);
  58. cmQueryDlgObject.ShowDialog();
  59. }
  60. }
  61. else
  62. {
  63. // Command Line mode
  64. isGUImode = false;
  65. if (!cmQueryObj.getCLInput(args, ref searchPath,
  66. ref userName,
  67. ref userPassword,
  68. ref userNamespace) )
  69. {
  70. return;
  71. }
  72. SamplesConnect connectCL = new SamplesConnect();
  73. bool isAnonymous = true;
  74. if (!connectCL.makeCLConnection(ref BIServerURL,
  75. ref userName,
  76. ref userPassword,
  77. ref userNamespace,
  78. ref isAnonymous) )
  79. {
  80. return;
  81. }
  82. if (isAnonymous && (0 != userName.CompareTo("")) )
  83. {
  84. userName = "";
  85. Console.WriteLine("\n***Note: This sample ignores user credentials for Anonymous connections\n");
  86. }
  87. string output = cmQueryObj.doQuery(connectCL.CBICMS,
  88. searchPath,
  89. userName,
  90. userPassword,
  91. userNamespace, isGUImode);
  92. Console.WriteLine(output);
  93. }
  94. }
  95. public bool getCLInput(string[] args,
  96. ref string searchPath,
  97. ref string userName,
  98. ref string userPassword,
  99. ref string userNamespace)
  100. {
  101. try
  102. {
  103. // command line mode
  104. int argc = args.GetLength(0);
  105. for (int i=0; i<argc; i++)
  106. {
  107. if (0 == args[i].CompareTo("-sp"))
  108. {
  109. searchPath = args[++i];
  110. }
  111. else if (0 == args[i].CompareTo("-un"))
  112. {
  113. userName = args[++i];
  114. }
  115. else if (0 == args[i].CompareTo("-pwd"))
  116. {
  117. userPassword = args[++i];
  118. }
  119. else if (0 == args[i].CompareTo("-ns"))
  120. {
  121. userNamespace = args[++i];
  122. }
  123. else if (0 == args[i].CompareTo("-h"))
  124. {
  125. //display help
  126. Console.WriteLine("cmquery.exe -sp <searchPath> [-un <userName> -pwd <userPassword> -ns <userNamepsace>].");
  127. return false;
  128. }
  129. else
  130. {
  131. // invalid argument
  132. Console.WriteLine("Invalid argument: {0}", args[0]);
  133. Console.WriteLine("cmquery.exe -sp <searchPath> [-un <userName> -pwd <userPassword> -ns <userNamepsace>].");
  134. return false;
  135. }
  136. }
  137. }
  138. catch(System.Exception ex)
  139. {
  140. if ( (searchPath == null) || (0 == searchPath.CompareTo("")) )
  141. {
  142. searchPath = "/*";
  143. Console.WriteLine("\n***Note: The searchPath argument is missing, '" + searchPath + "' will be used instead.\n");
  144. return true;
  145. }
  146. SamplesException.ShowExceptionMessage("getCLInput() failed with error: " + ex.Message, false,
  147. "Content Manager Query Sample - doQuery()" );
  148. Console.WriteLine("\nValid arguments:\ncmquery.exe -sp <search Path> [-un <userName> -pwd <userPassword> -ns <userNamepsace>].");
  149. return false;
  150. }
  151. return true;
  152. }
  153. public string doQuery(contentManagerService1 cBICMS,
  154. string searchPath,
  155. string userName,
  156. string userPassword,
  157. string userNamespace,
  158. bool isGUImode)
  159. {
  160. string output = "";
  161. try
  162. {
  163. // Search properties: we need the defaultName and the searchPath.
  164. propEnum[] properties =
  165. { propEnum.defaultName, propEnum.searchPath };
  166. // Sort options: ascending sort on the defaultName property.
  167. sort[] sortBy = { new sort()};
  168. sortBy[0].order = orderEnum.ascending;
  169. sortBy[0].propName = propEnum.defaultName;
  170. // Query options; use the defaults.
  171. queryOptions options = new queryOptions();
  172. searchPathMultipleObject qPath = new searchPathMultipleObject();
  173. qPath.Value = searchPath;
  174. // Make the query.
  175. baseClass[] results =
  176. cBICMS.query(qPath, properties, sortBy, options);
  177. // Display the results.
  178. output += "Results:\n\n";
  179. for (int i = 0; i < results.GetLength(0); i++)
  180. {
  181. tokenProp theDefaultName = results[i].defaultName;
  182. stringProp theSearchPath = results[i].searchPath;
  183. output += "\t" + theDefaultName.value + "\t\t" + theSearchPath.value + "\n";
  184. }
  185. }
  186. catch(SoapException ex)
  187. {
  188. SamplesException.ShowExceptionMessage("\n" + ex, isGUImode, "Content Manager Query Sample - doQuery()" );
  189. return "The error occurred in doQuery()";
  190. }
  191. catch(System.Exception ex)
  192. {
  193. if (0 != ex.Message.CompareTo("INPUT_CANCELLED_BY_USER"))
  194. {
  195. SamplesException.ShowExceptionMessage("\n" + ex.Message, isGUImode, "Content Manager Query Sample - doQuery()" );
  196. }
  197. return "The error occurred in doQuery()";
  198. }
  199. return output;
  200. }
  201. }
  202. }