SaveReport.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * SaveReport.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. */
  18. import javax.swing.JFrame;
  19. import javax.swing.JOptionPane;
  20. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  21. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  22. import com.cognos.developer.schemas.bibus._3.AsynchRequest;
  23. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  24. import com.cognos.developer.schemas.bibus._3.BaseParameter;
  25. import com.cognos.developer.schemas.bibus._3.MultilingualToken;
  26. import com.cognos.developer.schemas.bibus._3.Option;
  27. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  28. import com.cognos.developer.schemas.bibus._3.PropEnum;
  29. import com.cognos.developer.schemas.bibus._3.ReportSaveAsEnum;
  30. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  31. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  32. import com.cognos.developer.schemas.bibus._3.RunOptionSaveAs;
  33. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  34. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  35. public class SaveReport
  36. {
  37. /**
  38. * saveReport will execute a query/report and save either the report
  39. * or the report output back in the content store, as specified by the
  40. * saveAs parameter.
  41. *
  42. * @param connect
  43. * @param report
  44. * @param saveAs
  45. *
  46. * @return
  47. */
  48. public String saveReport(CRNConnect connect, BaseClassWrapper report, boolean saveAs)
  49. {
  50. String output = new String();
  51. String reportName = report.getBaseClassObject().getSearchPath().getValue();
  52. if ((connect != null) && (report != null))
  53. {
  54. PropEnum props[] =
  55. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  56. boolean success = false;
  57. // Get the list of parameters used by the report, including
  58. // optional parameters.
  59. ParameterValue reportParameters[] = new ParameterValue[] {};
  60. ReportParameters repParms = new ReportParameters();
  61. try
  62. {
  63. BaseParameter[] prm = repParms.getReportParameters(report, connect);
  64. if (prm != null && prm.length > 0)
  65. {
  66. reportParameters =
  67. ReportParameters.setReportParameters(prm);
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. System.out.println("An error occurred in the saveReport method.");
  73. e.printStackTrace();
  74. return "An error occurred in the saveReport method.";
  75. }
  76. output += "Running " + reportName + "\n";
  77. success =
  78. executeAndSave(
  79. reportName,
  80. connect,
  81. saveAs,
  82. reportParameters);
  83. if (!success)
  84. {
  85. output =
  86. output.concat(
  87. "Error occurred in saveReport()"
  88. + " - trying to execute report\n");
  89. }
  90. }
  91. else
  92. {
  93. output =
  94. output.concat("Invalid parameter(s) passed to saveReport.\n");
  95. }
  96. output += "Completed successfully.";
  97. return output;
  98. }
  99. /**
  100. * boolean executeAndSave( String reportName,
  101. * String reportPath,
  102. * boolean saveAs,
  103. * int reportType)
  104. *
  105. * This method executes a report and saves the output
  106. * or the report spec, as indicated by the saveAs flag
  107. *
  108. * @param reportName
  109. * Specifies search path of the report.
  110. * @param connect
  111. * Connection to Server
  112. * @param saveAs flag to indicate whether the report spec
  113. * should be saved under a new name, or
  114. * the output.
  115. * @param paramValues array of parameter values
  116. *
  117. * @return if the function succeeded
  118. *
  119. */
  120. public boolean executeAndSave(
  121. String reportName,
  122. CRNConnect connect,
  123. boolean saveAs,
  124. ParameterValue paramValues[])
  125. {
  126. Option executeRunOptions[] = new Option[3];
  127. Option saveAsRunOptions[] = new Option[1];
  128. RunOptionBoolean roSaveOutput = new RunOptionBoolean();
  129. RunOptionStringArray roOutputFormat = new RunOptionStringArray();
  130. RunOptionBoolean roPrompt = new RunOptionBoolean();
  131. AsynchReply asynchReply = null;
  132. AsynchRequest asynchRequest = null;
  133. String[] reportFormat = null;
  134. // We may want to save this output, but not by using
  135. // runOptions. That would be for another sample.
  136. roSaveOutput.setName(RunOptionEnum.saveOutput);
  137. roSaveOutput.setValue(false);
  138. //What format do we want the report in? HTML.
  139. reportFormat = new String[] { "HTML" };
  140. roOutputFormat.setName(RunOptionEnum.outputFormat);
  141. roOutputFormat.setValue(reportFormat);
  142. //Set the report not to prompt as we pass the parameter if any
  143. roPrompt.setName(RunOptionEnum.prompt);
  144. roPrompt.setValue(false);
  145. // Fill the array with the run options.
  146. executeRunOptions[0] = roSaveOutput;
  147. executeRunOptions[1] = roOutputFormat;
  148. executeRunOptions[2] = roPrompt;
  149. try
  150. {
  151. // Get the initial response
  152. if (reportName.startsWith("/content"))
  153. {
  154. asynchReply =
  155. connect.getReportService().run(
  156. new SearchPathSingleObject(reportName),
  157. paramValues,
  158. executeRunOptions);
  159. // If response is not immediately complete, call wait until complete
  160. if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  161. {
  162. while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  163. {
  164. //before calling wait, double check that it is okay
  165. if (hasSecondaryRequest(asynchReply, "wait"))
  166. {
  167. asynchReply =
  168. connect.getReportService().wait(
  169. asynchReply.getPrimaryRequest(),
  170. new ParameterValue[] {},
  171. new Option[] {});
  172. }
  173. else
  174. {
  175. //wait method not available as expected
  176. JFrame message = new JFrame();
  177. JOptionPane.showMessageDialog(
  178. message,
  179. "Error: Wait method not available as expected");
  180. System.out.println("\n\nError: Wait method not available as expected");
  181. return false;
  182. }
  183. }
  184. }
  185. asynchRequest = asynchReply.getPrimaryRequest();
  186. }
  187. else
  188. {
  189. JFrame message = new JFrame();
  190. JOptionPane.showMessageDialog(
  191. message,
  192. "Please supply a search path.");
  193. System.out.println("\n\nPlease supply a search path.");
  194. }
  195. }
  196. catch (java.rmi.RemoteException remoteEx)
  197. {
  198. System.out.println("Caught Remote Exception:\n");
  199. remoteEx.printStackTrace();
  200. }
  201. if (asynchRequest != null)
  202. {
  203. if (saveAs)
  204. {
  205. String newReportName = "";
  206. int position = reportName.indexOf("/report");
  207. if (position >= 0)
  208. {
  209. newReportName = "NewReportJ";
  210. }
  211. else
  212. {
  213. position = reportName.indexOf("/query");
  214. newReportName = "NewQueryJ";
  215. }
  216. String packagePath = reportName.substring(0, position);
  217. MultilingualToken nameTokens[] = new MultilingualToken[2];
  218. nameTokens[0] = new MultilingualToken();
  219. nameTokens[0].setLocale("en-us");
  220. nameTokens[0].setValue(newReportName);
  221. RunOptionSaveAs roSaveAs = new RunOptionSaveAs();
  222. roSaveAs.setName(RunOptionEnum.saveAs);
  223. roSaveAs.setObjectClass(ReportSaveAsEnum.reportView);
  224. roSaveAs.setObjectName(nameTokens);
  225. roSaveAs.setParentSearchPath(packagePath);
  226. saveAsRunOptions[0] = roSaveAs;
  227. try
  228. {
  229. //call deliver with saveAs option
  230. asynchReply = connect.getReportService().deliver(asynchRequest, new ParameterValue[] {}, saveAsRunOptions);
  231. // If response is not immediately complete, call wait until complete
  232. if ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete)) && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  233. {
  234. while ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  235. && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  236. {
  237. //before calling wait, double check that it is okay
  238. if (hasSecondaryRequest(asynchReply, "wait"))
  239. {
  240. asynchReply =
  241. connect.getReportService().wait(
  242. asynchReply.getPrimaryRequest(),
  243. new ParameterValue[] {},
  244. new Option[] {});
  245. }
  246. else
  247. {
  248. //wait method not available as expected
  249. JFrame message = new JFrame();
  250. JOptionPane.showMessageDialog(
  251. message,
  252. "Error: Wait method not available as expected");
  253. System.out.println("\n\nError: Wait method not available as expected");
  254. return false;
  255. }
  256. }
  257. }
  258. return true;
  259. }
  260. catch (java.rmi.RemoteException remoteEx)
  261. {
  262. System.out.println("Caught Remote Exception:\n");
  263. remoteEx.printStackTrace();
  264. }
  265. }
  266. else
  267. {
  268. Option saveRunOptions[] = new Option[1];
  269. RunOptionBoolean bSaveOutput = new RunOptionBoolean();
  270. bSaveOutput.setName(RunOptionEnum.saveOutput);
  271. bSaveOutput.setValue(true);
  272. saveRunOptions[0] = bSaveOutput;
  273. try
  274. {
  275. //call deliver with option to save
  276. asynchReply = connect.getReportService().deliver(asynchRequest, new ParameterValue[] {}, saveRunOptions);
  277. // If response is not immediately complete, call wait until complete
  278. if ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete)) && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  279. {
  280. while ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  281. && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  282. {
  283. //before calling wait, double check that it is okay
  284. if (hasSecondaryRequest(asynchReply, "wait"))
  285. {
  286. asynchReply =
  287. connect.getReportService().wait(
  288. asynchReply.getPrimaryRequest(),
  289. new ParameterValue[] {},
  290. new Option[] {});
  291. }
  292. else
  293. {
  294. //wait method not available as expected
  295. JFrame message = new JFrame();
  296. JOptionPane.showMessageDialog(
  297. message,
  298. "Error: Wait method not available as expected");
  299. System.out.println("\n\nError: Wait method not available as expected");
  300. return false;
  301. }
  302. }
  303. }
  304. return true;
  305. }
  306. catch (java.rmi.RemoteException remoteEx)
  307. {
  308. System.out.println("Caught Remote Exception:\n");
  309. remoteEx.printStackTrace();
  310. }
  311. }
  312. }
  313. else
  314. {
  315. System.out.println("execute() failed to return a valid report");
  316. }
  317. return false;
  318. }
  319. public boolean hasSecondaryRequest(
  320. AsynchReply response,
  321. String secondaryRequest)
  322. {
  323. AsynchSecondaryRequest[] secondaryRequests =
  324. response.getSecondaryRequests();
  325. for (int i = 0; i < secondaryRequests.length; i++)
  326. {
  327. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  328. == 0)
  329. {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. }