SubmitReport.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2012
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * SubmitReport.java
  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 file contains methods for executing different types of
  15. * report
  16. */
  17. import java.util.Date;
  18. import java.util.GregorianCalendar;
  19. import com.cognos.developer.schemas.bibus._3.AddOptions;
  20. import com.cognos.developer.schemas.bibus._3.AsynchDetailEventID;
  21. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  22. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  23. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  24. import com.cognos.developer.schemas.bibus._3.BaseClass;
  25. import com.cognos.developer.schemas.bibus._3.BaseClassArrayProp;
  26. import com.cognos.developer.schemas.bibus._3.JobDefinition;
  27. import com.cognos.developer.schemas.bibus._3.JobStepDefinition;
  28. import com.cognos.developer.schemas.bibus._3.NmtokenProp;
  29. import com.cognos.developer.schemas.bibus._3.Option;
  30. import com.cognos.developer.schemas.bibus._3.OptionArrayProp;
  31. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  32. import com.cognos.developer.schemas.bibus._3.PropEnum;
  33. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  34. import com.cognos.developer.schemas.bibus._3.Report;
  35. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  36. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  37. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  38. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  39. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  40. import com.cognos.developer.schemas.bibus._3.Sort;
  41. import com.cognos.developer.schemas.bibus._3.StringProp;
  42. import com.cognos.developer.schemas.bibus._3.TokenProp;
  43. import com.cognos.developer.schemas.bibus._3.UpdateActionEnum;
  44. public class SubmitReport
  45. {
  46. /**
  47. * submitReport will submit a query/report for execution as a job
  48. *
  49. * @param connect Connection to Server
  50. * @param report Report to be added
  51. * @param submitAtFlag If true, set to run later
  52. *
  53. * @return
  54. */
  55. public String submitReport(
  56. CRNConnect connect,
  57. String report,
  58. boolean submitAtFlag)
  59. {
  60. AsynchReply asynchReply = null;
  61. String output = new String();
  62. int i = 0;
  63. if ((connect != null) && (report != null))
  64. {
  65. PropEnum props[] =
  66. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  67. ParameterValue reportParameters[] = new ParameterValue[] {};
  68. BaseClass repPth[] = new BaseClass[] {};
  69. String quotChar = "\'";
  70. if (report.indexOf(quotChar) >= 0)
  71. {
  72. quotChar = "\"";
  73. }
  74. try
  75. {
  76. repPth =
  77. connect.getCMService().query(
  78. new SearchPathMultipleObject("/content//report[@name="
  79. + quotChar
  80. + report
  81. + quotChar
  82. + "]"),
  83. props,
  84. new Sort[] {},
  85. new QueryOptions());
  86. }
  87. catch (org.apache.axis.AxisFault axisEx)
  88. {
  89. return ("Caught Axis Fault:\n");
  90. //return ("Caught Axis Fault:\n" + axisEx.toString());
  91. }
  92. catch (java.rmi.RemoteException remoteEx)
  93. {
  94. return ("Caught Remote Exception:\n");
  95. //return ("Caught Remote Exception:\n" + remoteEx.toString());
  96. }
  97. Option runOpts[] = new Option[3];
  98. RunOptionBoolean roSaveOutput = new RunOptionBoolean();
  99. RunOptionBoolean roPrompt = new RunOptionBoolean();
  100. RunOptionStringArray roOutputFormat = new RunOptionStringArray();
  101. // We want to save this output
  102. roSaveOutput.setName(RunOptionEnum.saveOutput);
  103. roSaveOutput.setValue(true);
  104. //Set the report not to prompt as we pass the parameter if any
  105. roPrompt.setName(RunOptionEnum.prompt);
  106. roPrompt.setValue(false);
  107. //What format do we want the report in: PDF, HTML, or XML?
  108. roOutputFormat.setName(RunOptionEnum.outputFormat);
  109. roOutputFormat.setValue(new String[] { "HTML" });
  110. // Fill the array with the run options.
  111. runOpts[0] = roSaveOutput;
  112. runOpts[1] = roOutputFormat;
  113. runOpts[2] = roPrompt;
  114. String jobName = "NewJobJ";
  115. quotChar = "\'";
  116. if (jobName.indexOf(quotChar) >= 0)
  117. {
  118. quotChar = "\"";
  119. }
  120. String jobPath =
  121. "/content/folder[@name='Samples']/folder[@name='Models']/package"
  122. + "[@name='GO Sales (query)']"
  123. + "/jobDefinition[@name="
  124. + quotChar
  125. + jobName
  126. + quotChar
  127. + "]";
  128. // set submit time to now + 15 minutes
  129. Date submitAtDate = new Date();
  130. long submitAtTimeInMillis = submitAtDate.getTime();
  131. submitAtDate.setTime(submitAtTimeInMillis + (15 * 60000));
  132. GregorianCalendar startTime = new GregorianCalendar();
  133. startTime.setTime(submitAtDate);
  134. if ((repPth != null) && (repPth.length > 0))
  135. {
  136. String reportName = repPth[0].getSearchPath().getValue();
  137. System.out.println("Submitting report: " + reportName);
  138. try
  139. {
  140. createJob(
  141. connect,
  142. jobPath,
  143. jobName,
  144. new String[] { reportName });
  145. // submit the job for execution
  146. String reportEventID = "-1";
  147. if (submitAtFlag)
  148. {
  149. reportEventID =
  150. connect.getEventMgmtService().runAt(
  151. startTime,
  152. new SearchPathSingleObject(jobPath),
  153. reportParameters,
  154. runOpts);
  155. }
  156. else
  157. {
  158. // sn_dg_sdk_method_jobService_run_start_1
  159. asynchReply =
  160. connect.getJobService().run(
  161. new SearchPathSingleObject(jobPath),
  162. reportParameters,
  163. runOpts);
  164. // sn_dg_sdk_method_jobService_run_end_1
  165. // If response is not immediately complete, call wait until complete
  166. if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete) &&
  167. !asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  168. {
  169. while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  170. {
  171. //before calling wait, double check that it is okay
  172. if (hasSecondaryRequest(asynchReply, "wait"))
  173. {
  174. // sn_dg_sdk_method_jobService_wait_start_1
  175. asynchReply =
  176. connect.getJobService().wait(
  177. asynchReply.getPrimaryRequest(),
  178. new ParameterValue[] {},
  179. new Option[] {});
  180. // sn_dg_sdk_method_jobService_wait_end_1
  181. }
  182. else
  183. {
  184. return "Error: Wait method not available as expected.";
  185. }
  186. }
  187. }
  188. // sn_dg_sdk_method_jobService_run_start_2
  189. for (i = 0; i < asynchReply.getDetails().length; i++)
  190. {
  191. if (asynchReply.getDetails()[i] instanceof AsynchDetailEventID)
  192. {
  193. reportEventID = ((AsynchDetailEventID)asynchReply.getDetails()[i]).getEventID();
  194. }
  195. }
  196. // sn_dg_sdk_method_jobService_run_end_2
  197. }
  198. PropEnum propsQ[] = new PropEnum[2];
  199. propsQ[0] = PropEnum.searchPath;
  200. propsQ[1] = PropEnum.defaultName;
  201. connect.getCMService().query(
  202. new SearchPathMultipleObject(jobPath),
  203. propsQ,
  204. new Sort[] {},
  205. new QueryOptions());
  206. // show the eventID returned for the submitted report
  207. output =
  208. "The eventID for this job is: "
  209. + reportEventID
  210. + "\n";
  211. }
  212. catch (java.rmi.RemoteException remoteEx)
  213. {
  214. output = output.concat("Error occurred in submitReport\n");
  215. remoteEx.printStackTrace();
  216. }
  217. }
  218. else
  219. {
  220. quotChar = "\'";
  221. if (jobName.indexOf(quotChar) >= 0)
  222. {
  223. quotChar = "\"";
  224. }
  225. // check to see if the selected report is a query.
  226. try
  227. {
  228. repPth =
  229. connect.getCMService().query(
  230. new SearchPathMultipleObject("/content//query[@name="
  231. + quotChar
  232. + report
  233. + quotChar
  234. + "]"),
  235. props,
  236. new Sort[] {},
  237. new QueryOptions());
  238. }
  239. catch (java.rmi.RemoteException remoteEx)
  240. {
  241. System.out.println("Caught Remote Exception:\n");
  242. remoteEx.printStackTrace();
  243. }
  244. String reportName = repPth[0].getSearchPath().getValue();
  245. System.out.println("Submitting query: " + reportName);
  246. try
  247. {
  248. createJob(
  249. connect,
  250. jobPath,
  251. jobName,
  252. new String[] { reportName });
  253. // submit the job for execution
  254. String reportEventID = "-1";
  255. if (submitAtFlag)
  256. {
  257. reportEventID =
  258. connect.getEventMgmtService().runAt(
  259. startTime,
  260. new SearchPathSingleObject(jobPath),
  261. reportParameters,
  262. runOpts);
  263. }
  264. else
  265. {
  266. asynchReply =
  267. connect.getJobService().run(
  268. new SearchPathSingleObject(jobPath),
  269. reportParameters,
  270. runOpts);
  271. // If response is not immediately complete, call wait until complete
  272. if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  273. {
  274. while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  275. {
  276. //before calling wait, double check that it is okay
  277. if (hasSecondaryRequest(asynchReply, "wait"))
  278. {
  279. asynchReply =
  280. connect.getJobService().wait(
  281. asynchReply.getPrimaryRequest(),
  282. new ParameterValue[] {},
  283. new Option[] {});
  284. }
  285. else
  286. {
  287. return "Error: Wait method not available as expected.";
  288. }
  289. }
  290. }
  291. for (i = 0; i < asynchReply.getDetails().length; i++)
  292. {
  293. if (asynchReply.getDetails()[i] instanceof AsynchDetailEventID)
  294. {
  295. reportEventID = ((AsynchDetailEventID)asynchReply.getDetails()[i]).getEventID();
  296. }
  297. }
  298. }
  299. BaseClass bcQuery[];
  300. PropEnum propsQ[] = new PropEnum[2];
  301. propsQ[0] = PropEnum.searchPath;
  302. propsQ[1] = PropEnum.defaultName;
  303. bcQuery =
  304. connect.getCMService().query(
  305. new SearchPathMultipleObject(jobPath),
  306. propsQ,
  307. new Sort[] {},
  308. new QueryOptions());
  309. if ( bcQuery == null || bcQuery.length == 0 )
  310. {
  311. output = "The new job was not created in the Content Store";
  312. return output;
  313. }
  314. // show the eventID returned for the submitted report
  315. output =
  316. "The eventID for this job is: " + reportEventID + ".\n";
  317. }
  318. catch (java.rmi.RemoteException remoteEx)
  319. {
  320. output =
  321. output.concat("Error occurred in submitReport()\n");
  322. remoteEx.printStackTrace();
  323. }
  324. }
  325. }
  326. else
  327. {
  328. output =
  329. output.concat("Invalid parameter(s) passed to submitReport.\n");
  330. }
  331. return output;
  332. }
  333. /**
  334. * Create a new job.
  335. *
  336. * @param connect Connection to Server
  337. * @param jobPath Search path for the new job.
  338. * @param jobName Name of the new job.
  339. * @param reports Search paths for the reports to run during this job.
  340. */
  341. public void createJob(
  342. CRNConnect connect,
  343. String jobPath,
  344. String jobName,
  345. String[] reports)
  346. {
  347. JobDefinition myJob = new JobDefinition();
  348. RunOptionBoolean saveOutput = new RunOptionBoolean();
  349. TokenProp jobNameProp = new TokenProp();
  350. OptionArrayProp jobRunOptions = new OptionArrayProp();
  351. BaseClass jobsToAdd[] = new BaseClass[1];
  352. BaseClass bcJob[] = new BaseClass[1];
  353. Option runOptions[] = new Option[1];
  354. BaseClass parents[] = new BaseClass[] {};
  355. String sPath;
  356. PropEnum requestedProperties[] = new PropEnum[3];
  357. AddOptions addOpts = new AddOptions();
  358. NmtokenProp stepSequenceType = new NmtokenProp();
  359. saveOutput.setName(RunOptionEnum.saveOutput);
  360. saveOutput.setValue(true);
  361. runOptions[0] = saveOutput;
  362. jobRunOptions.setValue(runOptions);
  363. myJob.setOptions(jobRunOptions);
  364. jobNameProp.setValue(jobName);
  365. myJob.setDefaultName(jobNameProp);
  366. //Identify the parent object.
  367. sPath = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Sales (query)']";
  368. requestedProperties[0] = PropEnum.searchPath;
  369. requestedProperties[1] = PropEnum.defaultName;
  370. requestedProperties[2] = PropEnum.parent;
  371. stepSequenceType.setValue("parallel");
  372. try
  373. {
  374. parents =
  375. connect.getCMService().query(
  376. new SearchPathMultipleObject(sPath),
  377. requestedProperties,
  378. new Sort[] {},
  379. new QueryOptions());
  380. }
  381. catch (java.rmi.RemoteException remoteEx)
  382. {
  383. System.out.println("Caught remote exception:\n");
  384. remoteEx.printStackTrace();
  385. return;
  386. }
  387. if (parents.length <= 0)
  388. {
  389. System.out.println(
  390. "Error: Unable to retrieve parent objects.\n"
  391. + "Failed to create job in content store.");
  392. return;
  393. }
  394. myJob.setSequencing(stepSequenceType);
  395. myJob.setParent(parents[0].getParent());
  396. jobsToAdd[0] = myJob;
  397. addOpts.setUpdateAction(UpdateActionEnum.replace);
  398. try
  399. {
  400. bcJob[0] = connect.getCMService().add(new SearchPathSingleObject(sPath), jobsToAdd, addOpts)[0];
  401. }
  402. catch (java.rmi.RemoteException remoteEx)
  403. {
  404. System.out.println("Caught remote exception:\n");
  405. remoteEx.printStackTrace();
  406. return;
  407. }
  408. //now, add one jobStepDefinition for each report
  409. JobStepDefinition steps[] = new JobStepDefinition[reports.length];
  410. for (int i = 0; i < reports.length; i++)
  411. {
  412. BaseClassArrayProp bcap = new BaseClassArrayProp();
  413. steps[i] = new JobStepDefinition();
  414. StringProp searchPath = new StringProp();
  415. BaseClass temp = (BaseClass)new Report();
  416. searchPath.setValue(reports[i]);
  417. temp.setSearchPath(searchPath);
  418. BaseClass bca[] = new BaseClass[1];
  419. bca[0] = temp;
  420. bcap.setValue(bca);
  421. steps[i].setStepObject(bcap);
  422. //now, add each definition to the job
  423. addHelper(connect, steps[i], jobPath);
  424. }
  425. }
  426. /**
  427. * Add an object to the Content Store.
  428. *
  429. * @param connect Connection to Server
  430. * @param bc An object that extends baseClass, such as a Report.
  431. * @param path Search path for the job
  432. *
  433. * @return The new object.
  434. */
  435. public BaseClass[] addHelper(
  436. CRNConnect connect,
  437. JobStepDefinition bc,
  438. String path)
  439. {
  440. AddOptions addOpts = new AddOptions();
  441. BaseClass jobSteps[] = new BaseClass[1];
  442. addOpts.setUpdateAction(UpdateActionEnum.replace);
  443. jobSteps[0] = bc;
  444. BaseClass newbc[] = new BaseClass[1];
  445. try
  446. {
  447. newbc[0] = connect.getCMService().add(new SearchPathSingleObject(path), jobSteps, addOpts)[0];
  448. }
  449. catch (java.rmi.RemoteException remoteEx)
  450. {
  451. System.out.println("Caught remote exception:\n");
  452. remoteEx.printStackTrace();
  453. }
  454. return newbc;
  455. }
  456. public boolean hasSecondaryRequest(
  457. AsynchReply response,
  458. String secondaryRequest)
  459. {
  460. AsynchSecondaryRequest[] secondaryRequests =
  461. response.getSecondaryRequests();
  462. for (int i = 0; i < secondaryRequests.length; i++)
  463. {
  464. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  465. == 0)
  466. {
  467. return true;
  468. }
  469. }
  470. return false;
  471. }
  472. }