Submit.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // * Submit.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: Creates a schedule on a report to be run at a specified time.
  15. using System;
  16. using System.Web.Services.Protocols;
  17. using SamplesCommon;
  18. using cognosdotnet_10_2;
  19. using System.Globalization;
  20. namespace Submit
  21. {
  22. /// <summary>
  23. /// Summary description for Class1.
  24. /// </summary>
  25. class Submit
  26. {
  27. public Submit(){}
  28. static void Main(string[] args)
  29. {
  30. string cBIUrl = "";
  31. contentManagerService1 cBICMS = null;
  32. SamplesConnect connectDlg = new SamplesConnect();
  33. SubmitDlg submitDlgObject = new SubmitDlg();
  34. if (args.GetLength(0) == 0 )
  35. {
  36. // GUI mode
  37. connectDlg.ShowDialog();
  38. if (connectDlg.IsConnectedToCBI() == true)
  39. {
  40. cBICMS = connectDlg.CBICMS;
  41. cBIUrl = connectDlg.CBIURL;
  42. submitDlgObject.setConnection(connectDlg, cBIUrl);
  43. submitDlgObject.setReportList(BaseClassWrapper.buildReportQueryList(cBICMS));
  44. submitDlgObject.setSelectedReportIndex(0);
  45. submitDlgObject.ShowDialog();
  46. }
  47. }
  48. }
  49. public bool doSubmit(SamplesConnect connection,
  50. string reportPath,
  51. bool submitAtFlag,
  52. ref string resultMessage)
  53. {
  54. if (connection.IsConnectedToCBI() == false)
  55. {
  56. resultMessage = "The Server connection provided is not valid.";
  57. return false;
  58. }
  59. runOption[] runOpts = new runOption[3];
  60. runOptionBoolean roSaveOutput = new runOptionBoolean();
  61. runOptionBoolean roPrompt = new runOptionBoolean();
  62. runOptionStringArray roOutputFormat = new runOptionStringArray();
  63. // We want to save this output
  64. roSaveOutput.name = runOptionEnum.saveOutput;
  65. roSaveOutput.value = true;
  66. //Set the report not to prompt as we pass the parameter if any
  67. roPrompt.name = runOptionEnum.prompt;
  68. roPrompt.value = false;
  69. //What format do we want the report in: PDF, HTML, or XML?
  70. roOutputFormat.name = runOptionEnum.outputFormat;
  71. roOutputFormat.value = new string[] { "HTML" };
  72. // Fill the array with the run options.
  73. runOpts[0] = roSaveOutput;
  74. runOpts[1] = roOutputFormat;
  75. runOpts[2] = roPrompt;
  76. string jobName = "NewJob";
  77. string jobPath =
  78. "/content/folder[@name='Samples']/folder[@name='Models']/package"
  79. + "[@name='GO Data Warehouse (query)']"
  80. + "/jobDefinition[@name='"
  81. + jobName
  82. + "']";
  83. searchPathSingleObject cmJobPath = new searchPathSingleObject();
  84. cmJobPath.Value = jobPath;
  85. // set submit time to now + 2 minutes
  86. System.DateTime startTime = new DateTime();
  87. startTime = DateTime.Now;
  88. startTime = startTime.AddMinutes(2);
  89. if (!createJob(connection, jobPath, jobName, new string[] { reportPath }, ref resultMessage))
  90. {
  91. return false;
  92. }
  93. // submit the job for execution
  94. parameterValue[] reportParameters = new parameterValue[] {};
  95. string reportEventID = "-1";
  96. if (submitAtFlag)
  97. {
  98. reportEventID = connection.CBIEMS.runAt(startTime, cmJobPath, reportParameters, runOpts);
  99. }
  100. else
  101. {
  102. asynchReply jobRunResponse = connection.CBIJS.run(cmJobPath, reportParameters, runOpts);
  103. if ( (!jobRunResponse.status.Equals(asynchReplyStatusEnum.complete))
  104. && (!jobRunResponse.status.Equals(asynchReplyStatusEnum.conversationComplete)) )
  105. {
  106. while ( (!jobRunResponse.status.Equals(asynchReplyStatusEnum.complete))
  107. && (!jobRunResponse.status.Equals(asynchReplyStatusEnum.conversationComplete)) )
  108. {
  109. //before calling wait, double check that it is okay
  110. if (!hasSecondaryRequest(jobRunResponse, "wait"))
  111. {
  112. resultMessage = "The Server did something wrong";
  113. return false;
  114. }
  115. jobRunResponse =
  116. connection.CBIJS.wait(
  117. jobRunResponse.primaryRequest,
  118. new parameterValue[] {},
  119. new option[] {});
  120. }
  121. }
  122. if (jobRunResponse.details != null)
  123. {
  124. for (int i = 0; i < jobRunResponse.details.Length; i++)
  125. {
  126. if (jobRunResponse.details[i] is asynchDetailEventID)
  127. {
  128. reportEventID = ( (asynchDetailEventID) jobRunResponse.details[i]).eventID;
  129. }
  130. }
  131. }
  132. }
  133. propEnum[] propsQ = new propEnum[2];
  134. propsQ[0] = propEnum.searchPath;
  135. propsQ[1] = propEnum.defaultName;
  136. searchPathMultipleObject cmJobQPath = new searchPathMultipleObject();
  137. cmJobQPath.Value = jobPath;
  138. connection.CBICMS.query(cmJobQPath, propsQ, new sort[] {}, new queryOptions());
  139. // show the eventID returned for the submitted report
  140. resultMessage = "The eventID for this job is: " + reportEventID + "\n";
  141. return true;
  142. }
  143. public bool createJob(
  144. SamplesConnect connection,
  145. string jobPath,
  146. string jobName,
  147. string[] reports,
  148. ref string resultMessage)
  149. {
  150. jobDefinition myJob = new jobDefinition();
  151. runOptionBoolean saveOutput = new runOptionBoolean();
  152. tokenProp jobNameProp = new tokenProp();
  153. optionArrayProp jobrunOptions = new optionArrayProp();
  154. baseClass[] jobsToAdd = new baseClass[1];
  155. baseClass[] bcJob = new baseClass[1];
  156. runOption[] runOptions = new runOption[1];
  157. baseClass[] parents = new baseClass[] {};
  158. string sPath;
  159. propEnum[] requestedProperties = new propEnum[3];
  160. addOptions addOpts = new addOptions();
  161. nmtokenProp stepSequenceType = new nmtokenProp();
  162. saveOutput.name = runOptionEnum.saveOutput;
  163. saveOutput.value = true;
  164. runOptions[0] = saveOutput;
  165. jobrunOptions.value = runOptions;
  166. myJob.options = jobrunOptions;
  167. jobNameProp.value = jobName;
  168. myJob.defaultName = jobNameProp;
  169. //Identify the parent object.
  170. sPath = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']";
  171. requestedProperties[0] = propEnum.searchPath;
  172. requestedProperties[1] = propEnum.defaultName;
  173. requestedProperties[2] = propEnum.parent;
  174. stepSequenceType.value = "parallel";
  175. searchPathMultipleObject cmParentPath = new searchPathMultipleObject();
  176. cmParentPath.Value = sPath;
  177. parents = connection.CBICMS.query(cmParentPath, requestedProperties, new sort[] {}, new queryOptions());
  178. if (parents.GetLength(0) <= 0)
  179. {
  180. resultMessage += "Error: Unable to retrieve parent objects.\n" +
  181. "Failed to create job in content store.";
  182. return false;
  183. }
  184. myJob.sequencing = stepSequenceType;
  185. myJob.parent = parents[0].parent;
  186. jobsToAdd[0] = myJob;
  187. addOpts.updateAction = updateActionEnum.replace;
  188. searchPathSingleObject cmSPath = new searchPathSingleObject();
  189. cmSPath.Value = sPath;
  190. bcJob[0] = connection.CBICMS.add(cmSPath, jobsToAdd, addOpts)[0];
  191. //now, add one jobStepDefinition for each report
  192. jobStepDefinition[] steps = new jobStepDefinition[reports.GetLength(0)];
  193. for (int i = 0; i < reports.GetLength(0); i++)
  194. {
  195. baseClassArrayProp bcap = new baseClassArrayProp();
  196. steps[i] = new jobStepDefinition();
  197. stringProp searchPath = new stringProp();
  198. baseClass temp = (baseClass)new report();
  199. searchPath.value = reports[i];
  200. temp.searchPath = searchPath;
  201. baseClass[] bca = new baseClass[1];
  202. bca[0] = temp;
  203. bcap.value = bca;
  204. steps[i].stepObject = bcap;
  205. //now, add each definition to the job
  206. addHelper(connection, steps[i], jobPath);
  207. }
  208. return true;
  209. }
  210. /**
  211. * Add an object to the Content Store.
  212. *
  213. * @param bc An object that extends baseClass, such as a Report.
  214. *
  215. * @return The new object.
  216. */
  217. public baseClass[] addHelper(SamplesConnect connection, jobStepDefinition bc, string path)
  218. {
  219. addOptions addOpts = new addOptions();
  220. baseClass[] jobSteps = new baseClass[1];
  221. addOpts.updateAction = updateActionEnum.replace;
  222. jobSteps[0] = bc;
  223. baseClass[] newbc = new baseClass[1];
  224. searchPathSingleObject addPath = new searchPathSingleObject();
  225. addPath.Value = path;
  226. newbc[0] = connection.CBICMS.add(addPath, jobSteps, addOpts)[0];
  227. return newbc;
  228. }
  229. public bool hasSecondaryRequest(asynchReply response, string secondaryRequest)
  230. {
  231. asynchSecondaryRequest[] secondaryRequests =
  232. response.secondaryRequests;
  233. for (int i = 0; i < secondaryRequests.Length; i++)
  234. {
  235. if (secondaryRequests[i].name.CompareTo(secondaryRequest)
  236. == 0)
  237. {
  238. return true;
  239. }
  240. }
  241. return false;
  242. }
  243. }
  244. }