Render.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2010
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * Render.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 code sample demonstrates how to run a report and send
  15. * the output to a specific user using the following methods:
  16. * - run
  17. * Use this method to run a report, query, or report view.
  18. * - wait
  19. * Use this method to notify the server that the issuer of the request
  20. * is still waiting for the output, and to request that the processing
  21. * be continued.
  22. * - email
  23. * Use this method to request that the output of a report be emailed
  24. * to a user.
  25. * - query
  26. * Use this method to request objects from Content Manager.
  27. * - release
  28. * Use this method to remove inactive requests from the report service
  29. * cache earlier than they would be removed automatically by the system.
  30. * Removing abandoned requests makes resources available for
  31. * other requests, improving performance.
  32. */
  33. import java.io.File;
  34. import java.io.FileOutputStream;
  35. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput;
  36. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  37. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  38. import com.cognos.developer.schemas.bibus._3.BaseParameter;
  39. import com.cognos.developer.schemas.bibus._3.Option;
  40. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  41. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  42. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  43. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  44. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  45. public class Render
  46. {
  47. private static final int REP_HTML = 0;
  48. private static final int REP_XML = 1;
  49. private static final int REP_PDF = 2;
  50. private static final int REP_CSV = 3;
  51. private static final int REP_HTML_FRAG = 4;
  52. private static final int REP_MHT = 5;
  53. private static final int REP_SINGLEXLS = 6;
  54. private static final int REP_XLS = 7;
  55. private static final int REP_XLWA = 8;
  56. /**
  57. * @param connect
  58. * Specifies the object that provides the connection to
  59. * the server.
  60. * @param reportPath
  61. * Specifies the search path of the report.
  62. * @param response
  63. * Specifies the primary response. If no primary response is passed
  64. * to this method, this method calls the run method.
  65. * A primary response is necessary to issue a secondary request, such
  66. * as an render, because a secondary request can only continue
  67. * a conversation established by a primary request.
  68. */
  69. public String renderReport(
  70. CRNConnect connect,
  71. BaseClassWrapper report,
  72. AsynchReply response)
  73. {
  74. String output = "";
  75. try
  76. {
  77. // Get the list of parameters used by the report, including
  78. // optional parameters.
  79. ParameterValue reportParameters[] = new ParameterValue[] {};
  80. ReportParameters repParms = new ReportParameters();
  81. BaseParameter[] prm = repParms.getReportParameters(report, connect);
  82. if (prm != null && prm.length > 0)
  83. {
  84. reportParameters = ReportParameters.setReportParameters(prm);
  85. }
  86. SearchPathSingleObject reportSearchPath =
  87. new SearchPathSingleObject();
  88. reportSearchPath.set_value(
  89. report.getBaseClassObject().getSearchPath().getValue());
  90. // Set the run options for the execute method.
  91. Option[] runOptions = new Option[3];
  92. if (response == null)
  93. {
  94. //Execute the report, specify HTML output format
  95. //set the continueConversation option, to allow
  96. //subsequent requests
  97. runOptions[0] = setFormat(REP_HTML);
  98. runOptions[1] = setContinueConversation();
  99. runOptions[2] = setPrompting();
  100. response =
  101. connect.getReportService().run(
  102. reportSearchPath,
  103. reportParameters,
  104. runOptions);
  105. }
  106. output = renderResponse(connect, response);
  107. // sn_dg_sdk_method_reportService_release_start_1
  108. // release the conversation to free resources.
  109. connect.getReportService().release(response.getPrimaryRequest());
  110. // sn_dg_sdk_method_reportService_release_end_1
  111. response = null;
  112. return output;
  113. }
  114. catch (java.rmi.RemoteException remoteEx)
  115. {
  116. remoteEx.printStackTrace();
  117. return ("An error occurred in renderReport().");
  118. }
  119. }
  120. private RunOptionStringArray setFormat(int format)
  121. {
  122. RunOptionStringArray rof = new RunOptionStringArray();
  123. rof.setName(RunOptionEnum.outputFormat);
  124. rof.setValue(this.getReportFormat(format));
  125. return rof;
  126. }
  127. private String[] getReportFormat(int reportType)
  128. {
  129. switch (reportType)
  130. {
  131. case REP_HTML :
  132. return new String[] { "HTML" };
  133. case REP_HTML_FRAG :
  134. return new String[] { "HTMLFragment" };
  135. case REP_MHT :
  136. return new String[] { "MHT" };
  137. case REP_SINGLEXLS :
  138. return new String[] { "SingleXLS" };
  139. case REP_XLS :
  140. return new String[] { "XLS" };
  141. case REP_XLWA :
  142. return new String[] { "XLWA" };
  143. case REP_XML :
  144. return new String[] { "XML" };
  145. case REP_PDF :
  146. return new String[] { "PDF" };
  147. case REP_CSV :
  148. return new String[] { "CSV" };
  149. default :
  150. System.out.println("Invalid report output format.");
  151. return null;
  152. }
  153. }
  154. private RunOptionBoolean setContinueConversation()
  155. {
  156. RunOptionBoolean continueConversation = new RunOptionBoolean();
  157. continueConversation.setName(RunOptionEnum.continueConversation);
  158. continueConversation.setValue(true);
  159. return continueConversation;
  160. }
  161. private RunOptionBoolean setPrompting()
  162. {
  163. RunOptionBoolean promptFlag = new RunOptionBoolean();
  164. promptFlag.setName(RunOptionEnum.prompt);
  165. promptFlag.setValue(false);
  166. return promptFlag;
  167. }
  168. private String renderResponse(CRNConnect connect, AsynchReply response)
  169. {
  170. Option renderOpts[] = new Option[1];
  171. RunOptionStringArray roFormat = setFormat(REP_XML);
  172. renderOpts[0] = roFormat;
  173. AsynchReply renderedResp = null;
  174. try
  175. {
  176. if (RunReport.hasSecondaryRequest(response, "render"))
  177. {
  178. // sn_dg_sdk_method_reportService_render_start_1
  179. renderedResp =
  180. connect.getReportService().render(
  181. response.getPrimaryRequest(),
  182. new ParameterValue[] {},
  183. renderOpts);
  184. // sn_dg_sdk_method_reportService_render_end_1
  185. }
  186. else
  187. {
  188. return "Response status is not valid for sending a render request.\n";
  189. }
  190. if (!renderedResp
  191. .getStatus()
  192. .equals(AsynchReplyStatusEnum.complete))
  193. {
  194. while (!renderedResp
  195. .getStatus()
  196. .equals(AsynchReplyStatusEnum.complete))
  197. {
  198. renderedResp =
  199. connect.getReportService().wait(
  200. renderedResp.getPrimaryRequest(),
  201. new ParameterValue[] {},
  202. new Option[] {});
  203. }
  204. if (RunReport.hasSecondaryRequest(renderedResp, "getOutput"))
  205. {
  206. renderedResp =
  207. connect.getReportService().getOutput(
  208. renderedResp.getPrimaryRequest(),
  209. new ParameterValue[] {},
  210. new Option[] {});
  211. }
  212. }
  213. }
  214. catch (java.rmi.RemoteException remoteEx)
  215. {
  216. remoteEx.printStackTrace();
  217. return ("Caught Remote Exception:\n");
  218. }
  219. AsynchDetailReportOutput reportOutput = null;
  220. for (int i = 0; i < renderedResp.getDetails().length; i++)
  221. {
  222. if (renderedResp.getDetails()[i]
  223. instanceof AsynchDetailReportOutput)
  224. {
  225. reportOutput =
  226. (AsynchDetailReportOutput)renderedResp.getDetails()[i];
  227. break;
  228. }
  229. }
  230. if (reportOutput == null)
  231. {
  232. return "Server failed to return a valid report in this format.";
  233. }
  234. try
  235. {
  236. File oFile =
  237. new File(
  238. connect.getDefaultSavePath()
  239. + System.getProperty("file.separator")
  240. + "cmOut.xml");
  241. FileOutputStream fos = new FileOutputStream(oFile);
  242. fos.write(reportOutput.getOutputPages()[0].getBytes());
  243. fos.flush();
  244. fos.close();
  245. return ("Report Output written to file " + oFile + ".\n");
  246. }
  247. catch (java.io.FileNotFoundException fileNotFoundEx)
  248. {
  249. fileNotFoundEx.printStackTrace();
  250. return ("Unable to open/create file to save report output.\n");
  251. }
  252. catch (java.io.IOException ioEx)
  253. {
  254. ioEx.printStackTrace();
  255. return ("Caught IO Exception:\n" + ioEx.toString() + "\n");
  256. }
  257. }
  258. }