PrintReport.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. // * PrintReport.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: Prints a report.
  15. using System;
  16. using System.Web.Services.Protocols;
  17. using SamplesCommon;
  18. using cognosdotnet_10_2;
  19. namespace PrintReport
  20. {
  21. /// <summary>
  22. /// Summary description for PrintReport.
  23. /// </summary>
  24. class PrintReport
  25. {
  26. /// <summary>
  27. /// The main entry point for the application.
  28. /// </summary>
  29. ///
  30. public PrintReport(){}
  31. static void Main(string[] args)
  32. {
  33. string cBIUrl = "";
  34. contentManagerService1 cmService = null;
  35. SamplesConnect connectDlg = new SamplesConnect();
  36. PrintReportDlg printReportDlgObject = new PrintReportDlg();
  37. if (args.GetLength(0) == 0 )
  38. {
  39. // GUI mode
  40. connectDlg.ShowDialog();
  41. if (connectDlg.IsConnectedToCBI() == true)
  42. {
  43. cmService = connectDlg.CBICMS;
  44. cBIUrl = connectDlg.CBIURL;
  45. printReportDlgObject.setConnection(connectDlg, cBIUrl);
  46. printReportDlgObject.setReportList(BaseClassWrapper.buildReportQueryList(cmService));
  47. printReportDlgObject.setSelectedReportIndex(0);
  48. printReportDlgObject.ShowDialog();
  49. }
  50. }
  51. }
  52. public baseClass[] getAvailablePrinters(SamplesConnect connection, ref string returnMessage)
  53. {
  54. try
  55. {
  56. baseClass[] bcPrinters = new baseClass[1];
  57. propEnum[] props =
  58. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.printerAddress };
  59. searchPathMultipleObject printersSearchPath = new searchPathMultipleObject();
  60. printersSearchPath.Value = "CAMID(\":\")/printer";
  61. bcPrinters = connection.CBICMS.query(printersSearchPath, props, new sort[] {}, new queryOptions());
  62. if ( (bcPrinters == null) || (bcPrinters.GetLength(0) == 0) )
  63. {
  64. returnMessage = "There are no printers defined on the Server.";
  65. }
  66. return bcPrinters;
  67. }
  68. catch(SoapException ex)
  69. {
  70. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - getAvailablePrinters" );
  71. returnMessage = "getAvailablePrinters failed with error: " + ex.Message;
  72. return null;
  73. }
  74. catch(System.Exception ex)
  75. {
  76. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - getAvailablePrinters" );
  77. returnMessage = "getAvailablePrinters failed with error: " + ex.Message;
  78. return null;
  79. }
  80. }
  81. public bool addPrinter(SamplesConnect connection, string printerName, string printerAddress, ref string returnMessage)
  82. {
  83. try
  84. {
  85. stringProp strpropLocation = new stringProp();
  86. tokenProp tokenPrinterName = new tokenProp();
  87. printer objNewPrinter = new printer();
  88. baseClass[] bcParent = new baseClass[1];
  89. baseClass[] bcAddPrinter = new baseClass[1];
  90. baseClass[] bcNewPrinter = new baseClass[1];
  91. addOptions add_options = new addOptions();
  92. propEnum[] props =
  93. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.parent };
  94. searchPathMultipleObject printersSearchString = new searchPathMultipleObject();
  95. printersSearchString.Value = "CAMID(\":\")/printer";
  96. baseClass[] bcPrinters = connection.CBICMS.query(printersSearchString, props, new sort[] {}, new queryOptions());
  97. if ( (bcPrinters != null) && (bcPrinters.GetLength(0) > 0) )
  98. {
  99. int nbPrinters = bcPrinters.GetLength(0);
  100. for (int i=0; i<nbPrinters; i++)
  101. {
  102. printerName = printerName.ToLower();
  103. string existingPrinterName = bcPrinters[i].defaultName.value.ToLower();
  104. if (0 == printerName.CompareTo(existingPrinterName) )
  105. {
  106. returnMessage = "A printer with this name already exists.";
  107. return false;
  108. }
  109. }
  110. }
  111. searchPathMultipleObject printerSiblingPath = new searchPathMultipleObject();
  112. printerSiblingPath.Value = "CAMID(\":\")";
  113. tokenPrinterName.value = printerName;
  114. strpropLocation.value = printerAddress;
  115. //Do not set the searchPath when you add an object to the content store.
  116. objNewPrinter.defaultName = tokenPrinterName;
  117. objNewPrinter.printerAddress = strpropLocation;
  118. //Identify the printer parent object.
  119. bcParent = connection.CBICMS.query(printerSiblingPath, props, new sort[] {}, new queryOptions());
  120. searchPathSingleObject printerParentPath = new searchPathSingleObject();
  121. printerParentPath.Value = bcParent[0].searchPath.value;
  122. bcAddPrinter[0] = objNewPrinter;
  123. add_options.updateAction = updateActionEnum.replace;
  124. // sn_dg_sdk_method_contentManagerService_add_start_0
  125. bcNewPrinter = connection.CBICMS.add(printerParentPath, bcAddPrinter, add_options);
  126. // sn_dg_sdk_method_contentManagerService_add_end_0
  127. returnMessage = "The printer \"" + printerName + "\" was added successfully.";
  128. }
  129. catch(SoapException ex)
  130. {
  131. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - AddPrinter" );
  132. returnMessage = "AddPrinter failed with error: " + ex.Message;
  133. return false;
  134. }
  135. catch(System.Exception ex)
  136. {
  137. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - AddPrinter" );
  138. returnMessage = "AddPrinter failed with error: " + ex.Message;
  139. return false;
  140. }
  141. return true;
  142. }
  143. public bool deletePrinter(SamplesConnect connection, string printerToDelete, ref string returnMessage)
  144. {
  145. try
  146. {
  147. deleteOptions delete_options = new deleteOptions();
  148. baseClass[] bcaDeletePrinter = new baseClass[1];
  149. propEnum[] props =
  150. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.parent };
  151. //Set the force option to true. When the force option is true,
  152. //a selected object will be deleted if the current user has either write
  153. //or setPolicy permission for the following:
  154. //- the selected object
  155. //- the parent of the selected object
  156. //- every descendant of the selected object
  157. delete_options.force = true;
  158. searchPathMultipleObject printerToDeleteSearchPath = new searchPathMultipleObject();
  159. printerToDeleteSearchPath.Value = "CAMID(\":\")/printer[@name='" + printerToDelete + "']";
  160. // sn_dg_sdk_method_contentManagerService_query_start_0
  161. bcaDeletePrinter = connection.CBICMS.query(printerToDeleteSearchPath, props, new sort[] {}, new queryOptions());
  162. // sn_dg_sdk_method_contentManagerService_query_end_0
  163. if ( (bcaDeletePrinter == null) || (bcaDeletePrinter.GetLength(0) == 0) )
  164. {
  165. returnMessage = "Unable to retrieve the printer \"" + printerToDelete + ".";
  166. return false;
  167. }
  168. // sn_dg_sdk_method_contentManagerService_delete_start_0
  169. int result = connection.CBICMS.delete(bcaDeletePrinter, delete_options);
  170. if (0 >= result)
  171. {
  172. returnMessage = "Error occured while deleting the printer.";
  173. return false;
  174. }
  175. // sn_dg_sdk_method_contentManagerService_delete_end_0
  176. returnMessage = "The printer \"" + printerToDelete + " was successfully deleted.";
  177. }
  178. catch(SoapException ex)
  179. {
  180. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - deletePrinter" );
  181. returnMessage = "DeletePrinter failed with error: " + ex.Message;
  182. return false;
  183. }
  184. catch(System.Exception ex)
  185. {
  186. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - deletePrinter" );
  187. returnMessage = "DeletePrinter failed with error: " + ex.Message;
  188. return false;
  189. }
  190. return true;
  191. }
  192. public bool changePrinterName(SamplesConnect connection, string originalName, string newPrinterName, ref string returnMessage)
  193. {
  194. try
  195. {
  196. baseClass[] bcAddPrinter = new baseClass[1];
  197. baseClass[] bcNewPrinterName = new baseClass[1];
  198. propEnum[] props =
  199. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.parent };
  200. searchPathMultipleObject printersPath = new searchPathMultipleObject();
  201. printersPath.Value = "CAMID(\":\")/printer";
  202. baseClass[] bcPrinters = connection.CBICMS.query(printersPath, props, new sort[] {}, new queryOptions());
  203. bool oldPrinterExists = false;
  204. if ( (bcPrinters == null) || (bcPrinters.GetLength(0) == 0) )
  205. {
  206. returnMessage = "Unable to retreive printer list";
  207. return false;
  208. }
  209. for (int i=0; i < bcPrinters.GetLength(0); i++)
  210. {
  211. string existingPrinterName = bcPrinters[i].defaultName.value.ToLower();
  212. originalName = originalName.ToLower();
  213. if (0 == existingPrinterName.CompareTo(originalName))
  214. {
  215. oldPrinterExists = true;
  216. bcAddPrinter[0] = bcPrinters[i];
  217. bcAddPrinter[0].defaultName.value = newPrinterName;
  218. }
  219. if (0 == existingPrinterName.CompareTo(newPrinterName))
  220. {
  221. returnMessage = "The printer name \"" + newPrinterName + "\" already exists.";
  222. return false;
  223. }
  224. }
  225. if (!oldPrinterExists)
  226. {
  227. returnMessage = "The selected printer name \"" + originalName + "\" does not exist.";
  228. return false;
  229. }
  230. updateOptions updateOpts = new updateOptions();
  231. // sn_dg_sdk_method_contentManagerService_update_start_0
  232. bcNewPrinterName = connection.CBICMS.update(bcAddPrinter, updateOpts);
  233. // sn_dg_sdk_method_contentManagerService_update_end_0
  234. returnMessage = "The printer \"" + originalName + "\" has been renamed to \"" + newPrinterName + "\" successfully.";
  235. return true;
  236. }
  237. catch(SoapException ex)
  238. {
  239. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - changePrinterName" );
  240. returnMessage = "changePrinterName failed with error: " + ex.Message;
  241. return false;
  242. }
  243. catch(System.Exception ex)
  244. {
  245. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - changePrinterName" );
  246. returnMessage = "changePrinterName failed with error: " + ex.Message;
  247. return false;
  248. }
  249. }
  250. public bool changePrinterAddress(SamplesConnect connection, string printerName, string newNetworkAddress, ref string returnMessage)
  251. {
  252. try
  253. {
  254. bool oldPrinterExists = false;
  255. printer objPrinter = new printer();
  256. tokenProp strNewPrinterName = new tokenProp();
  257. baseClass[] bcPrinters = new baseClass[1];
  258. baseClass[] bcAddPrinter = new baseClass[1];
  259. baseClass[] bcNewPrinterAddress = new baseClass[1];
  260. stringProp propNetworkAddress = new stringProp();
  261. propEnum[] props =
  262. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.parent };
  263. searchPathMultipleObject printersSearchPath = new searchPathMultipleObject();
  264. printersSearchPath.Value = "CAMID(\":\")/printer";
  265. bcPrinters = connection.CBICMS.query(printersSearchPath, props, new sort[] {}, new queryOptions());
  266. if ( (bcPrinters == null) || (bcPrinters.GetLength(0) == 0) )
  267. {
  268. returnMessage = "Unable to retreive printer list";
  269. return false;
  270. }
  271. for (int i=0; i < bcPrinters.GetLength(0); i++)
  272. {
  273. string existingPrinterName = bcPrinters[i].defaultName.value.ToLower();
  274. printerName = printerName.ToLower();
  275. if (0 == existingPrinterName.CompareTo(printerName))
  276. {
  277. oldPrinterExists = true;
  278. objPrinter.defaultName = new tokenProp();
  279. objPrinter.defaultName.value = printerName;
  280. objPrinter.searchPath = new stringProp();
  281. objPrinter.searchPath.value = bcPrinters[i].searchPath.value;
  282. }
  283. }
  284. if (!oldPrinterExists)
  285. {
  286. returnMessage = "The printer to be modified: \"" + printerName + "\" does not exist.";
  287. return false;
  288. }
  289. propNetworkAddress.value = newNetworkAddress;
  290. objPrinter.printerAddress = new stringProp();
  291. objPrinter.printerAddress = propNetworkAddress;
  292. bcAddPrinter[0] = objPrinter;
  293. updateOptions updateOpts = new updateOptions();
  294. bcNewPrinterAddress = connection.CBICMS.update(bcAddPrinter, updateOpts );
  295. returnMessage = "The printer \"" + printerName + "\" is now set to the network address \"" + newNetworkAddress + ".";
  296. return true;
  297. }
  298. catch(SoapException ex)
  299. {
  300. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - changePrinterAddress" );
  301. returnMessage = "changePrinterAddress failed with error: " + ex.Message;
  302. return false;
  303. }
  304. catch(System.Exception ex)
  305. {
  306. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - changePrinterAddress" );
  307. returnMessage = "changePrinterAddress failed with error: " + ex.Message;
  308. return false;
  309. }
  310. }
  311. public bool startPrint(SamplesConnect connection, string printerSearchPath, string reportPath, ref string returnMessage)
  312. {
  313. try
  314. {
  315. runOptionBoolean saveOutputFlag = new runOptionBoolean();
  316. saveOutputFlag.name = runOptionEnum.saveOutput;
  317. saveOutputFlag.value = false;
  318. string[] stringFormat = new string[1];
  319. stringFormat[0] = "PDF";
  320. runOptionStringArray outputFormatStrArr = new runOptionStringArray();
  321. outputFormatStrArr.name = runOptionEnum.outputFormat;
  322. outputFormatStrArr.value = stringFormat;
  323. asynchOptionBoolean includePrimaryRequestFlag = new asynchOptionBoolean();
  324. includePrimaryRequestFlag.name = asynchOptionEnum.alwaysIncludePrimaryRequest;
  325. includePrimaryRequestFlag.value = true;
  326. asynchOptionInt primaryWaitThreshold = new asynchOptionInt();
  327. primaryWaitThreshold.name = asynchOptionEnum.primaryWaitThreshold;
  328. primaryWaitThreshold.value = 0;
  329. runOptionBoolean promptFlag = new runOptionBoolean();
  330. promptFlag.name = runOptionEnum.prompt;
  331. promptFlag.value = false;
  332. option[] runOpts = new option[5];
  333. runOpts[0] = saveOutputFlag;
  334. runOpts[1] = outputFormatStrArr;
  335. runOpts[2] = includePrimaryRequestFlag;
  336. runOpts[3] = primaryWaitThreshold;
  337. runOpts[4] = promptFlag;
  338. searchPathSingleObject reportSearchPath = new searchPathSingleObject();
  339. reportSearchPath.Value = reportPath;
  340. asynchReply reportResponse = connection.CBIRS.run(reportSearchPath, new parameterValue[] {}, runOpts);
  341. runOptionBoolean printRunOpts = new runOptionBoolean();
  342. printRunOpts.name = runOptionEnum.print;
  343. printRunOpts.value = true;
  344. runOptionString printerRunOpts = new runOptionString();
  345. printerRunOpts.name = runOptionEnum.printer;
  346. printerRunOpts.value = printerSearchPath;
  347. option[] printOpts = new option[2];
  348. printOpts[0] = printRunOpts;
  349. printOpts[1] = printerRunOpts;
  350. connection.CBIRS.deliver(reportResponse.primaryRequest, new parameterValue[] {}, printOpts);
  351. //If the request has not yet completed, keep waiting until it has finished
  352. // sn_dg_sdk_method_reportService_wait_start_0
  353. while (reportResponse.status != asynchReplyStatusEnum.complete)
  354. {
  355. reportResponse = connection.CBIRS.wait(reportResponse.primaryRequest, new parameterValue[] {}, new option[] {} );
  356. }
  357. // sn_dg_sdk_method_reportService_wait_end_0
  358. returnMessage = "The report \"" + reportPath + "\"has been submitted to the printer successfully.";
  359. return true;
  360. }
  361. catch(SoapException ex)
  362. {
  363. SamplesException.ShowExceptionMessage( ex, true, "Print Report Sample - startPrint" );
  364. returnMessage = "startPrint failed with error: " + ex.Message;
  365. return false;
  366. }
  367. catch(System.Exception ex)
  368. {
  369. SamplesException.ShowExceptionMessage( ex.Message, true, "Print Report Sample - startPrint" );
  370. returnMessage = "startPrint failed with error: " + ex.Message;
  371. return false;
  372. }
  373. }
  374. }
  375. }