RunReport.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. * RunReport.java
  10. *
  11. *
  12. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  13. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  14. *
  15. * Description: This code sample demonstrates how to run different types of reports using the following
  16. * methods:
  17. * - run
  18. * Use this method to run a report, query, or report view.
  19. * - wait
  20. * Use this method to notify the server that the issuer of the request is still
  21. * waiting for the output, and to request that the processing be continued.
  22. * - query
  23. * Use this method to request objects from Content Manager.
  24. * - getOutput
  25. * Use this method to request that the output be sent to the issuer
  26. * of the request.
  27. */
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30. import java.util.Calendar;
  31. import org.apache.axis.encoding.Base64;
  32. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput;
  33. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatus;
  34. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatusEnum;
  35. import com.cognos.developer.schemas.bibus._3.AsynchOptionBoolean;
  36. import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum;
  37. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  38. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  39. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  40. import com.cognos.developer.schemas.bibus._3.AuthoredReport;
  41. import com.cognos.developer.schemas.bibus._3.BaseClass;
  42. import com.cognos.developer.schemas.bibus._3.BaseParameter;
  43. import com.cognos.developer.schemas.bibus._3.DeliveryOptionEnum;
  44. import com.cognos.developer.schemas.bibus._3.DeliveryOptionString;
  45. import com.cognos.developer.schemas.bibus._3.Graphic;
  46. import com.cognos.developer.schemas.bibus._3.MultilingualString;
  47. import com.cognos.developer.schemas.bibus._3.Option;
  48. import com.cognos.developer.schemas.bibus._3.Output;
  49. import com.cognos.developer.schemas.bibus._3.Page;
  50. import com.cognos.developer.schemas.bibus._3.Parameter;
  51. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  52. import com.cognos.developer.schemas.bibus._3.PropEnum;
  53. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  54. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  55. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  56. import com.cognos.developer.schemas.bibus._3.RunOptionMultilingualString;
  57. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  58. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  59. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  60. import com.cognos.developer.schemas.bibus._3.Sort;
  61. public class RunReport
  62. {
  63. private static final int REP_HTML = 0;
  64. private static final int REP_XML = 1;
  65. private static final int REP_PDF = 2;
  66. private static final int REP_CSV = 3;
  67. private static final int REP_HTML_FRAG = 4;
  68. private static final int REP_MHT = 5;
  69. private static final int REP_XLWA = 6;
  70. static int tmpFileCounter = 0;
  71. private CRNConnect my_connection;
  72. private AsynchReply my_rsr;
  73. /**
  74. * runReport will execute query the content store of the report
  75. * or query and thenpass it off to be executed in the default browser.
  76. *
  77. * @param connect
  78. * Specifies the Connection to Server.
  79. * @param report
  80. * Specifies the name of the report or query.
  81. * @param reportType
  82. * Specifies the type of report.
  83. * @return
  84. * Returns the text message indicating whether the report or query ran successfully.
  85. */
  86. public String runReport(
  87. CRNConnect connect,
  88. BaseClassWrapper report,
  89. int reportType,
  90. boolean doBurst)
  91. throws java.rmi.RemoteException
  92. {
  93. String output = new String();
  94. if ((connect != null)
  95. && (report != null)
  96. && (connect.getDefaultSavePath() != null))
  97. {
  98. my_connection = connect;
  99. // sn_dg_prm_smpl_runreport_P1_start_0
  100. ParameterValue emptyParameterValues[] = new ParameterValue[] {};
  101. ParameterValue reportParameterValues[] = null;
  102. ReportParameters repParms = new ReportParameters();
  103. BaseParameter[] parametersInReportSpec = new Parameter[] {};
  104. try
  105. {
  106. parametersInReportSpec =
  107. repParms.getReportParameters(report, connect);
  108. }
  109. // sn_dg_prm_smpl_runreport_P1_end_0
  110. catch (java.rmi.RemoteException remoteEx)
  111. {
  112. System.out.println("Caught Remote Exception:\n");
  113. remoteEx.printStackTrace();
  114. }
  115. if (parametersInReportSpec != null
  116. && parametersInReportSpec.length > 0)
  117. {
  118. reportParameterValues =
  119. ReportParameters.setReportParameters(
  120. parametersInReportSpec);
  121. }
  122. if ((reportParameterValues == null)
  123. || (reportParameterValues.length <= 0))
  124. {
  125. reportParameterValues = emptyParameterValues;
  126. }
  127. output
  128. += executeReport(
  129. report,
  130. connect,
  131. reportType,
  132. reportParameterValues,
  133. doBurst,
  134. null);
  135. }
  136. return output;
  137. }
  138. /**
  139. * runReportAt will set up the report or query to be run at the
  140. * specified time.
  141. *
  142. * @param connect
  143. * Specifies the Connection to Server.
  144. * @param report
  145. * Specifies the name of the report or query.
  146. * @param reportType
  147. * Specifies the type of report.
  148. * @param execTime
  149. * Specifies the time to run.
  150. * @return
  151. * Returns the text message indicating whether the report or query ran successfully.
  152. */
  153. public String runReportAt(
  154. CRNConnect connect,
  155. BaseClassWrapper report,
  156. int reportType,
  157. Calendar execTime)
  158. throws java.rmi.RemoteException
  159. {
  160. String output = "runReportAt did not complete as expected";
  161. String resultEventID = new String();
  162. Option reportRunOptions[];
  163. String rSearchPath =
  164. report.getBaseClassObject().getSearchPath().getValue();
  165. if ((connect != null)
  166. && (report != null)
  167. && (connect.getDefaultSavePath() != null))
  168. {
  169. //Prepare the runOptions
  170. reportRunOptions = getDefaultRunOptions(reportType, null);
  171. ParameterValue emptyParameterValues[] = new ParameterValue[] {};
  172. ParameterValue reportParameterValues[] = null;
  173. ReportParameters repParms = new ReportParameters();
  174. BaseParameter[] parametersInReportSpec = new Parameter[] {};
  175. try
  176. {
  177. parametersInReportSpec =
  178. repParms.getReportParameters(report, connect);
  179. }
  180. catch (java.rmi.RemoteException remoteEx)
  181. {
  182. System.out.println("Caught Remote Exception:\n");
  183. remoteEx.printStackTrace();
  184. }
  185. if (parametersInReportSpec != null
  186. && parametersInReportSpec.length > 0)
  187. {
  188. reportParameterValues =
  189. ReportParameters.setReportParameters(
  190. parametersInReportSpec);
  191. }
  192. if ((reportParameterValues == null)
  193. || (reportParameterValues.length <= 0))
  194. {
  195. reportParameterValues = emptyParameterValues;
  196. }
  197. try
  198. {
  199. // sn_dg_sdk_method_eventManagementService_runAt_start_0
  200. resultEventID =
  201. connect.getEventMgmtService().runAt(
  202. execTime,
  203. new SearchPathSingleObject(rSearchPath),
  204. reportParameterValues,
  205. reportRunOptions);
  206. // sn_dg_sdk_method_eventManagementService_runAt_end_0
  207. output = "runAt set up event ID" + resultEventID;
  208. }
  209. catch (java.rmi.RemoteException remoteEx)
  210. {
  211. System.out.println("Caught Remote Exception:\n");
  212. remoteEx.printStackTrace();
  213. }
  214. }
  215. return output;
  216. }
  217. /**
  218. * This Java method executes the specified report and returns a boolean value
  219. * indicating whether the report or query ran successfully.
  220. * @param report
  221. * Specifies the report.
  222. * @param connect
  223. * Specifies the Connection to Server.
  224. * @param reportType
  225. * Specifies the output format of report: HTML, XML, PDF.
  226. * @param paramValueArray
  227. * Specifies the parameter values, if any, to use for the report
  228. * @param runOptions
  229. * Specifies the options, if any, to use for the report
  230. * @return
  231. * Returns true if the report was executed successfully and false if the report did not run.
  232. */
  233. // sn_dg_prm_smpl_runreport_P4_start_0
  234. public String executeReport(
  235. BaseClassWrapper report,
  236. CRNConnect connect,
  237. int reportType,
  238. ParameterValue paramValueArray[],
  239. boolean doBurst,
  240. Option runOptions[])
  241. throws java.rmi.RemoteException
  242. {
  243. Option execReportRunOptions[];
  244. AsynchReply rsr = null;
  245. //check for advanced routing server group
  246. String serverGroup = ((AuthoredReport)report.getBaseClassObject()).getRoutingServerGroup().getValue();
  247. if(serverGroup == null) {
  248. serverGroup = "";
  249. }
  250. String rSearchPath =
  251. report.getBaseClassObject().getSearchPath().getValue();
  252. String ERR_MESG =
  253. "run() failed to return a valid report in this format";
  254. //Prepare the runOptions
  255. if(doBurst)
  256. {
  257. execReportRunOptions = getBurstRunOptions(report, reportType, runOptions);
  258. }
  259. else
  260. {
  261. execReportRunOptions = getDefaultRunOptions(reportType, runOptions);
  262. }
  263. // sn_dg_sdk_method_reportService_run_start_1
  264. //Call run()
  265. rsr =
  266. connect.getReportService(true, serverGroup).run(
  267. new SearchPathSingleObject(rSearchPath),
  268. paramValueArray,
  269. execReportRunOptions);
  270. // sn_dg_sdk_method_reportService_run_end_1
  271. // If response is not immediately complete, call wait until complete
  272. if (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete)&&!rsr.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  273. {
  274. while (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete)&&!rsr.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  275. {
  276. //before calling wait, double check that it is okay
  277. if (!hasSecondaryRequest(rsr, "wait"))
  278. {
  279. return ERR_MESG;
  280. }
  281. rsr =
  282. connect.getReportService().wait(
  283. rsr.getPrimaryRequest(),
  284. new ParameterValue[] {},
  285. new Option[] {});
  286. }
  287. //After calling wait() it is necessary to check to make sure
  288. //the output is ready before retrieving it
  289. if (outputIsReady(rsr))
  290. {
  291. rsr =
  292. connect.getReportService().getOutput(
  293. rsr.getPrimaryRequest(),
  294. new ParameterValue[] {},
  295. new Option[] {});
  296. }
  297. else
  298. {
  299. return ERR_MESG;
  300. }
  301. }
  302. // sn_dg_prm_smpl_runreport_P4_end_0
  303. //rsr grab a copy of this final rsr for local use later
  304. my_rsr = rsr;
  305. if(doBurst)
  306. {
  307. return ("Bursted");
  308. }
  309. //Do something with the output
  310. return textOrBinaryOutput(connect, rsr, report.toString(), reportType);
  311. }
  312. public String textOrBinaryOutput(
  313. CRNConnect connect,
  314. AsynchReply rsr,
  315. String report,
  316. int reportType)
  317. {
  318. String textOutput = null;
  319. if (reportType == REP_PDF
  320. || reportType == REP_CSV
  321. || reportType == REP_XLWA)
  322. {
  323. textOutput =
  324. saveBinaryOutput(connect, rsr, report.toString(), reportType);
  325. }
  326. else if (
  327. reportType == REP_HTML
  328. || reportType == REP_XML
  329. || reportType == REP_HTML_FRAG
  330. || reportType == REP_MHT)
  331. {
  332. textOutput =
  333. getOutputPage(connect, rsr, report.toString(), reportType);
  334. }
  335. if (textOutput == null)
  336. {
  337. return "The server failed to return valid report output in this format";
  338. }
  339. return textOutput;
  340. }
  341. public boolean outputIsReady(AsynchReply response)
  342. {
  343. for (int i = 0; i < response.getDetails().length; i++)
  344. {
  345. if ((response.getDetails()[i] instanceof AsynchDetailReportStatus)
  346. && (((AsynchDetailReportStatus)response.getDetails()[i])
  347. .getStatus()
  348. == AsynchDetailReportStatusEnum.responseReady)
  349. && (hasSecondaryRequest(response, "getOutput")))
  350. {
  351. return true;
  352. }
  353. }
  354. return false;
  355. }
  356. public static boolean hasSecondaryRequest(
  357. AsynchReply response,
  358. String secondaryRequest)
  359. {
  360. AsynchSecondaryRequest[] secondaryRequests =
  361. response.getSecondaryRequests();
  362. for (int i = 0; i < secondaryRequests.length; i++)
  363. {
  364. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  365. == 0)
  366. {
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. public String saveBinaryOutput(
  373. CRNConnect connection,
  374. AsynchReply response,
  375. String reportString,
  376. int reportType)
  377. {
  378. byte binaryOutput[] = null;
  379. BaseClass additionalOutputObject = null;
  380. AsynchDetailReportOutput reportOutput = null;
  381. try
  382. {
  383. for (int i = 0; i < response.getDetails().length; i++)
  384. {
  385. if (response.getDetails()[i]
  386. instanceof AsynchDetailReportOutput)
  387. {
  388. reportOutput =
  389. (AsynchDetailReportOutput)response.getDetails()[i];
  390. break;
  391. }
  392. }
  393. // decode the PDF/CSV output to array of bytes.
  394. // binary output is not split into multiple pages, there will be only one
  395. if(reportType == REP_PDF
  396. || reportType == REP_CSV)
  397. {
  398. binaryOutput = Base64.decode(reportOutput.getOutputPages()[0]);
  399. }
  400. else
  401. {
  402. binaryOutput=(reportOutput.getOutputPages()[0]).getBytes();
  403. }
  404. if (binaryOutput == null)
  405. {
  406. return "Server failed to return a valid report in this format.";
  407. }
  408. //For some Binary output types (Excel) there may be additional output objects
  409. //that must be retrieved and saved with the "main" report output
  410. //
  411. //For the references to work correctly, the file name must be consistent
  412. //with the name used in the content store
  413. if (reportOutput.getOutputObjects().length > 0)
  414. {
  415. additionalOutputObject = reportOutput.getOutputObjects()[0];
  416. if (additionalOutputObject != null)
  417. {
  418. PropEnum[] props =
  419. new PropEnum[] {
  420. PropEnum.searchPath,
  421. PropEnum.defaultName,
  422. PropEnum.dataType,
  423. PropEnum.data,
  424. PropEnum.objectClass };
  425. SearchPathMultipleObject aopSearchPath = new SearchPathMultipleObject();
  426. aopSearchPath.set_value(additionalOutputObject.getSearchPath().getValue() + "/page");
  427. BaseClass[] additionalOutputPages =
  428. connection.getCMService().query(
  429. aopSearchPath,
  430. props,
  431. new Sort[] {},
  432. new QueryOptions());
  433. for (int i = 0; i < additionalOutputPages.length; i++)
  434. {
  435. if (additionalOutputPages[i] instanceof Page)
  436. {
  437. File oFile =
  438. new File(
  439. connection.getDefaultSavePath()
  440. + "/"
  441. + additionalOutputPages[i]
  442. .getDefaultName()
  443. .getValue());
  444. FileOutputStream fos = new FileOutputStream(oFile);
  445. fos.write(
  446. ((Page)additionalOutputPages[i]).getData().getValue());
  447. fos.flush();
  448. fos.close();
  449. }
  450. }
  451. }
  452. }
  453. //write the binary output to a file
  454. File oFile = getNewTempFile(null, reportString, reportType);
  455. FileOutputStream fos = new FileOutputStream(oFile);
  456. fos.write(binaryOutput);
  457. fos.flush();
  458. fos.close();
  459. //return filename where the main output was written
  460. return oFile.getAbsolutePath();
  461. }
  462. catch (java.io.FileNotFoundException fileNotFoundEx)
  463. {
  464. //fileNotFoundEx.printStackTrace();
  465. return fileNotFoundEx.toString();
  466. }
  467. catch (java.io.IOException ioEx)
  468. {
  469. //ioEx.printStackTrace();
  470. return ioEx.toString();
  471. }
  472. }
  473. public String getOutputPage(
  474. CRNConnect connect,
  475. AsynchReply response,
  476. String reportString,
  477. int reportType)
  478. {
  479. AsynchDetailReportOutput reportOutput = null;
  480. for (int i = 0; i < response.getDetails().length; i++)
  481. {
  482. if (response.getDetails()[i] instanceof AsynchDetailReportOutput)
  483. {
  484. reportOutput =
  485. (AsynchDetailReportOutput)response.getDetails()[i];
  486. break;
  487. }
  488. }
  489. if (reportOutput == null)
  490. {
  491. return "Server failed to return a valid report in this format.";
  492. }
  493. String textOutput = "";
  494. //If there are graphics in the output, it must be handled
  495. //differently than text based output
  496. if (reportOutput.getOutputObjects().length > 0)
  497. {
  498. textOutput = replaceLocalGraphicsInOutput(connect, reportOutput);
  499. }
  500. else
  501. {
  502. //text based output is split into pages -- return the current page
  503. String[] pages = reportOutput.getOutputPages();
  504. textOutput = pages[0].toString();
  505. }
  506. File oFile = getNewTempFile(null, reportString, reportType);
  507. try
  508. {
  509. //write the updated html output to a file
  510. FileOutputStream fos = new FileOutputStream(oFile);
  511. fos.write(textOutput.getBytes());
  512. fos.flush();
  513. fos.close();
  514. }
  515. catch (java.io.IOException ioEx)
  516. {}
  517. return oFile.getAbsolutePath();
  518. }
  519. public String replaceLocalGraphicsInOutput(
  520. CRNConnect connect,
  521. AsynchDetailReportOutput reportOutput)
  522. {
  523. // this section deals with executing reports with charts or graphics.
  524. // the img tag contains credentials that are not valid when the report
  525. // is not run through report or query studio therefore we must extract
  526. // the img from content mgr and then save it locally, then replace the
  527. // img tag returned from the server with a path to the local graphic.
  528. // TODO Only the first graphic is handled. For reports with multiple
  529. // graphics, a for loop to process all of them would be appropriate.
  530. BaseClass bcGraphic[];
  531. try
  532. {
  533. SearchPathMultipleObject graphicSearchPath = new SearchPathMultipleObject();
  534. graphicSearchPath.set_value(reportOutput.getOutputObjects()[0].getSearchPath().getValue());
  535. bcGraphic =
  536. connect.getCMService().query(
  537. graphicSearchPath,
  538. new PropEnum[] { PropEnum.searchPath },
  539. new Sort[] {},
  540. new QueryOptions());
  541. Output out = null;
  542. if ((bcGraphic.length > 0) && (bcGraphic[0] instanceof Output))
  543. {
  544. SearchPathMultipleObject outSearchPath = new SearchPathMultipleObject();
  545. out = (Output)bcGraphic[0];
  546. outSearchPath.set_value(out.getSearchPath().getValue() + "/graphic");
  547. Graphic g =
  548. (Graphic)connect.getCMService().query(
  549. outSearchPath,
  550. new PropEnum[] {
  551. PropEnum.searchPath,
  552. PropEnum.data,
  553. PropEnum.dataType },
  554. new Sort[] {},
  555. new QueryOptions())[0];
  556. String graphicFile =
  557. connect.getDefaultSavePath()
  558. + "/"
  559. + System.getProperty("file.separator")
  560. + "graphicToDisplay.png";
  561. // save graphic locally
  562. File gFile = new File(graphicFile);
  563. FileOutputStream fos = new FileOutputStream(gFile);
  564. fos.write(g.getData().getValue());
  565. fos.flush();
  566. fos.close();
  567. // return the HTML output, search&replace img tag with local image.
  568. String[] pages = reportOutput.getOutputPages();
  569. String html = pages[0].toString();
  570. String start = null;
  571. String end = null;
  572. int index = 0;
  573. index = html.indexOf("<img", 0);
  574. start = html.substring(0, index);
  575. end = html.substring(html.indexOf(">", index) + 1);
  576. html = start + "<img src='graphicToDisplay.png'>" + end;
  577. return html;
  578. }
  579. }
  580. catch (java.io.FileNotFoundException fileNotFoundEx)
  581. {
  582. return "Unable to open or create file to save graphics output";
  583. }
  584. catch (java.rmi.RemoteException remoteEx)
  585. {
  586. remoteEx.printStackTrace();
  587. return remoteEx.toString();
  588. }
  589. catch (java.io.IOException ioEx)
  590. {
  591. ioEx.printStackTrace();
  592. return ioEx.toString();
  593. }
  594. return "run() failed to return a valid report in this format";
  595. }
  596. // sn_dg_prm_smpl_runreport_P3_start_0
  597. public Option[] getDefaultRunOptions(int reportType, Option[] options)
  598. {
  599. // sn_dg_prm_smpl_runreport_P3_end_0
  600. if (options != null)
  601. {
  602. //check for mandatory options
  603. for (int i = 0; i < options.length; i++)
  604. {
  605. //ouputFormat
  606. if (options[i] instanceof RunOptionStringArray)
  607. {
  608. RunOptionStringArray tmpOption =
  609. (RunOptionStringArray)options[i];
  610. if (tmpOption.getName() == RunOptionEnum.prompt)
  611. {
  612. return options;
  613. }
  614. }
  615. }
  616. RunOptionStringArray outputFormat = new RunOptionStringArray();
  617. outputFormat.setName(RunOptionEnum.outputFormat);
  618. outputFormat.setValue(setFormatByType(reportType));
  619. Option[] newOptions = new Option[options.length + 2];
  620. for (int i = 0; i < options.length; i++)
  621. {
  622. newOptions[i] = options[i];
  623. }
  624. newOptions[options.length + 1] = outputFormat;
  625. return newOptions;
  626. }
  627. else
  628. {
  629. // sn_dg_prm_smpl_runreport_P3_start_1
  630. Option execReportRunOptions[] = new Option[6];
  631. RunOptionBoolean saveOutputRunOption = new RunOptionBoolean();
  632. RunOptionStringArray outputFormat = new RunOptionStringArray();
  633. RunOptionBoolean promptFlag = new RunOptionBoolean();
  634. AsynchOptionBoolean includePrimaryRequest = new AsynchOptionBoolean();
  635. //Set the option for saving the output to false
  636. saveOutputRunOption.setName(RunOptionEnum.saveOutput);
  637. saveOutputRunOption.setValue(false);
  638. //What format do we want the report in: PDF, HTML, or XML?
  639. outputFormat.setName(RunOptionEnum.outputFormat);
  640. String[] reportFormat = null;
  641. reportFormat = setFormatByType(reportType);
  642. outputFormat.setValue(reportFormat);
  643. //Set the report not to prompt as we pass the parameter (if any)
  644. promptFlag.setName(RunOptionEnum.prompt);
  645. promptFlag.setValue(false);
  646. //Set the option to always have the primaryRequest in the response
  647. includePrimaryRequest.setName(
  648. AsynchOptionEnum.alwaysIncludePrimaryRequest);
  649. includePrimaryRequest.setValue(true);
  650. // sn_dg_prm_smpl_runreport_P3_end_1
  651. // AsynchOptionInt primaryWaitThreshold = new AsynchOptionInt();
  652. // AsynchOptionInt secondaryWaitThreshold = new AsynchOptionInt();
  653. // primaryWaitThreshold.setName(AsynchOptionEnum.primaryWaitThreshold);
  654. // primaryWaitThreshold.setValue(1);
  655. // secondaryWaitThreshold.setName(AsynchOptionEnum.secondaryWaitThreshold);
  656. // secondaryWaitThreshold.setValue(1);
  657. // Fill the array with the rest of the run options.
  658. // sn_dg_prm_smpl_runreport_P3_start_2
  659. execReportRunOptions[0] = saveOutputRunOption;
  660. execReportRunOptions[1] = outputFormat;
  661. execReportRunOptions[2] = promptFlag;
  662. execReportRunOptions[3] = includePrimaryRequest;
  663. // sn_dg_prm_smpl_runreport_P3_end_2
  664. // execReportRunOptions[4] = primaryWaitThreshold;
  665. // execReportRunOptions[5] = secondaryWaitThreshold;
  666. // sn_dg_prm_smpl_runreport_P3_start_3
  667. return execReportRunOptions;
  668. // sn_dg_prm_smpl_runreport_P3_end_3
  669. }
  670. }
  671. public Option[] getBurstRunOptions(BaseClassWrapper report, int reportType, Option[] options)
  672. {
  673. if (options != null)
  674. {
  675. //check for mandatory options
  676. for (int i = 0; i < options.length; i++)
  677. {
  678. //ouputFormat
  679. if (options[i] instanceof RunOptionStringArray)
  680. {
  681. RunOptionStringArray tmpOption =
  682. (RunOptionStringArray)options[i];
  683. if (tmpOption.getName() == RunOptionEnum.prompt)
  684. {
  685. return options;
  686. }
  687. }
  688. }
  689. RunOptionStringArray outputFormat = new RunOptionStringArray();
  690. outputFormat.setName(RunOptionEnum.outputFormat);
  691. outputFormat.setValue(setFormatByType(reportType));
  692. Option[] newOptions = new Option[options.length + 2];
  693. for (int i = 0; i < options.length; i++)
  694. {
  695. newOptions[i] = options[i];
  696. }
  697. newOptions[options.length + 1] = outputFormat;
  698. return newOptions;
  699. }
  700. else
  701. {
  702. Option execReportRunOptions[] = new Option[8];
  703. RunOptionBoolean saveOutputRunOption = new RunOptionBoolean();
  704. RunOptionBoolean burstRunOption = new RunOptionBoolean();
  705. RunOptionBoolean sendByEmailRunOption = new RunOptionBoolean();
  706. DeliveryOptionString emailSubjectRunOption = new DeliveryOptionString();
  707. RunOptionBoolean emailAsAttachmentRunOption = new RunOptionBoolean();
  708. RunOptionStringArray outputFormat = new RunOptionStringArray();
  709. RunOptionBoolean promptFlag = new RunOptionBoolean();
  710. AsynchOptionBoolean includePrimaryRequest = new AsynchOptionBoolean();
  711. //Set the option for saving the output to false
  712. saveOutputRunOption.setName(RunOptionEnum.saveOutput);
  713. saveOutputRunOption.setValue(false);
  714. // Set the option for burst the report to true
  715. burstRunOption.setName(RunOptionEnum.burst);
  716. burstRunOption.setValue(true);
  717. // Set the option for send by email to true
  718. sendByEmailRunOption.setName(RunOptionEnum.email);
  719. sendByEmailRunOption.setValue(true);
  720. // Specifies the subject line of the emailed output
  721. emailSubjectRunOption.setName(DeliveryOptionEnum.subject);
  722. emailSubjectRunOption.setValue("Report: " + report.toString());
  723. // Set the option for an email attachment to true
  724. emailAsAttachmentRunOption.setName(RunOptionEnum.emailAsAttachment);
  725. emailAsAttachmentRunOption.setValue(true);
  726. // What format do we want the report in: PDF, HTML, or XML?
  727. outputFormat.setName(RunOptionEnum.outputFormat);
  728. String[] reportFormat = null;
  729. reportFormat = setFormatByType(reportType);
  730. outputFormat.setValue(reportFormat);
  731. // Set the report not to prompt as we pass the parameter (if any)
  732. promptFlag.setName(RunOptionEnum.prompt);
  733. promptFlag.setValue(false);
  734. //Set the option to always have the primaryRequest in the response
  735. includePrimaryRequest.setName(
  736. AsynchOptionEnum.alwaysIncludePrimaryRequest);
  737. includePrimaryRequest.setValue(true);
  738. // AsynchOptionInt primaryWaitThreshold = new AsynchOptionInt();
  739. // AsynchOptionInt secondaryWaitThreshold = new AsynchOptionInt();
  740. // primaryWaitThreshold.setName(AsynchOptionEnum.primaryWaitThreshold);
  741. // primaryWaitThreshold.setValue(1);
  742. // secondaryWaitThreshold.setName(AsynchOptionEnum.secondaryWaitThreshold);
  743. // secondaryWaitThreshold.setValue(1);
  744. // Fill the array with the rest of the run options.
  745. execReportRunOptions[0] = saveOutputRunOption;
  746. execReportRunOptions[1] = burstRunOption;
  747. execReportRunOptions[2] = sendByEmailRunOption;
  748. execReportRunOptions[3] = emailSubjectRunOption;
  749. execReportRunOptions[4] = emailAsAttachmentRunOption;
  750. execReportRunOptions[5] = outputFormat;
  751. execReportRunOptions[6] = promptFlag;
  752. execReportRunOptions[7] = includePrimaryRequest;
  753. return execReportRunOptions;
  754. }
  755. }
  756. public String nextPage(BaseClassWrapper report, int reportType)
  757. throws java.rmi.RemoteException
  758. {
  759. return getNextPage(my_connection, my_rsr, report, reportType);
  760. }
  761. /**
  762. * Next Page of a Report
  763. *
  764. * @param connection Specifies the object that provides the connection to
  765. * the server.
  766. *
  767. * @param rsr AsynchReply from previous request in conversation
  768. *
  769. * @param report report being run
  770. *
  771. * @param reportType output format for report
  772. *
  773. */
  774. public String getNextPage(
  775. CRNConnect connection,
  776. AsynchReply rsr,
  777. BaseClassWrapper report,
  778. int reportType)
  779. throws java.rmi.RemoteException
  780. {
  781. if ((connection == null) || (rsr == null))
  782. {
  783. return null;
  784. }
  785. // sn_dg_sdk_method_reportService_nextPage_start_1
  786. rsr =
  787. connection.getReportService().nextPage(
  788. rsr.getPrimaryRequest(),
  789. new ParameterValue[] {},
  790. new Option[] {});
  791. // sn_dg_sdk_method_reportService_nextPage_end_1
  792. if (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  793. {
  794. // sn_dg_sdk_method_reportService_wait_start_1
  795. while (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  796. {
  797. rsr =
  798. connection.getReportService().wait(
  799. rsr.getPrimaryRequest(),
  800. new ParameterValue[] {},
  801. new Option[] {});
  802. }
  803. // sn_dg_sdk_method_reportService_wait_end_1
  804. rsr =
  805. connection.getReportService().getOutput(
  806. rsr.getPrimaryRequest(),
  807. new ParameterValue[] {},
  808. new Option[] {});
  809. }
  810. my_rsr = rsr;
  811. return textOrBinaryOutput(
  812. connection,
  813. rsr,
  814. report.toString(),
  815. reportType);
  816. }
  817. public String previousPage(BaseClassWrapper report, int reportType)
  818. throws java.rmi.RemoteException
  819. {
  820. return getPreviousPage(my_connection, my_rsr, report, reportType);
  821. }
  822. /**
  823. * Previous Page of a Report
  824. *
  825. * @param connection Specifies the object that provides the connection to
  826. * the server.
  827. *
  828. * @param rsr AsynchReply from previous request in conversation
  829. *
  830. * @param report report being run
  831. *
  832. * @param reportType output format for report
  833. *
  834. */
  835. public String getPreviousPage(
  836. CRNConnect connection,
  837. AsynchReply rsr,
  838. BaseClassWrapper report,
  839. int reportType)
  840. throws java.rmi.RemoteException
  841. {
  842. if ((connection == null) || (rsr == null))
  843. {
  844. return "Invalid parameters passed to getPreviousPage()";
  845. }
  846. // sn_dg_sdk_method_reportService_previousPage_start_1
  847. rsr =
  848. connection.getReportService().previousPage(
  849. rsr.getPrimaryRequest(),
  850. new ParameterValue[] {},
  851. new Option[] {});
  852. // sn_dg_sdk_method_reportService_previousPage_end_1
  853. if (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  854. {
  855. while (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  856. {
  857. rsr =
  858. connection.getReportService().wait(
  859. rsr.getPrimaryRequest(),
  860. new ParameterValue[] {},
  861. new Option[] {});
  862. }
  863. rsr =
  864. connection.getReportService().getOutput(
  865. rsr.getPrimaryRequest(),
  866. new ParameterValue[] {},
  867. new Option[] {});
  868. }
  869. my_rsr = rsr;
  870. return textOrBinaryOutput(
  871. connection,
  872. rsr,
  873. report.toString(),
  874. reportType);
  875. }
  876. public String firstPage(BaseClassWrapper report, int reportType)
  877. throws java.rmi.RemoteException
  878. {
  879. return getFirstPage(my_connection, my_rsr, report, reportType);
  880. }
  881. /**
  882. * First Page of a Report
  883. *
  884. * @param connection Specifies the object that provides the connection to
  885. * the server.
  886. *
  887. * @param rsr AsynchReply from previous request in conversation
  888. *
  889. * @param report report being run
  890. *
  891. * @param reportType output format for report
  892. *
  893. */
  894. public String getFirstPage(
  895. CRNConnect connection,
  896. AsynchReply rsr,
  897. BaseClassWrapper report,
  898. int reportType)
  899. throws java.rmi.RemoteException
  900. {
  901. if ((connection == null) || (rsr == null))
  902. {
  903. return "Invalid parameters passed to getFirstPage()";
  904. }
  905. // sn_dg_sdk_method_reportService_firstPage_start_1
  906. rsr =
  907. connection.getReportService().firstPage(
  908. rsr.getPrimaryRequest(),
  909. new ParameterValue[] {},
  910. new Option[] {});
  911. // sn_dg_sdk_method_reportService_firstPage_end_1
  912. if (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  913. {
  914. while (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  915. {
  916. rsr =
  917. connection.getReportService().wait(
  918. rsr.getPrimaryRequest(),
  919. new ParameterValue[] {},
  920. new Option[] {});
  921. }
  922. // sn_dg_sdk_method_reportService_getOutput_start_1
  923. rsr =
  924. connection.getReportService().getOutput(
  925. rsr.getPrimaryRequest(),
  926. new ParameterValue[] {},
  927. new Option[] {});
  928. // sn_dg_sdk_method_reportService_getOutput_end_1
  929. }
  930. my_rsr = rsr;
  931. return textOrBinaryOutput(
  932. connection,
  933. rsr,
  934. report.toString(),
  935. reportType);
  936. }
  937. public String lastPage(BaseClassWrapper report, int reportType)
  938. throws java.rmi.RemoteException
  939. {
  940. return getLastPage(my_connection, my_rsr, report, reportType);
  941. }
  942. /**
  943. * Last Page of a Report
  944. *
  945. * @param connection Specifies the object that provides the connection to
  946. * the server.
  947. *
  948. * @param rsr AsynchReply from previous request in conversation
  949. *
  950. * @param report report being run
  951. *
  952. * @param reportType output format for report
  953. *
  954. */
  955. public String getLastPage(
  956. CRNConnect connection,
  957. AsynchReply rsr,
  958. BaseClassWrapper report,
  959. int reportType)
  960. throws java.rmi.RemoteException
  961. {
  962. if ((connection == null) || (rsr == null))
  963. {
  964. return "Invalid parameters passed to getLastPage()";
  965. }
  966. // sn_dg_sdk_method_reportService_lastPage_start_1
  967. rsr =
  968. connection.getReportService().lastPage(
  969. rsr.getPrimaryRequest(),
  970. new ParameterValue[] {},
  971. new Option[] {});
  972. // sn_dg_sdk_method_reportService_lastPage_end_1
  973. if (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  974. {
  975. while (rsr.getStatus() != AsynchReplyStatusEnum.complete)
  976. {
  977. rsr =
  978. connection.getReportService().wait(
  979. rsr.getPrimaryRequest(),
  980. new ParameterValue[] {},
  981. new Option[] {});
  982. }
  983. rsr =
  984. connection.getReportService().getOutput(
  985. rsr.getPrimaryRequest(),
  986. new ParameterValue[] {},
  987. new Option[] {});
  988. }
  989. my_rsr = rsr;
  990. return textOrBinaryOutput(
  991. connection,
  992. rsr,
  993. report.toString(),
  994. reportType);
  995. }
  996. public boolean hasNextPage() throws java.rmi.RemoteException
  997. {
  998. return hasNextPage(my_connection, my_rsr);
  999. }
  1000. /**
  1001. *
  1002. *
  1003. *
  1004. */
  1005. public boolean hasNextPage(CRNConnect connection, AsynchReply rsr)
  1006. throws java.rmi.RemoteException
  1007. {
  1008. if ((connection == null) || (rsr == null))
  1009. {
  1010. return false;
  1011. }
  1012. int numSecondaryRequests = rsr.getSecondaryRequests().length;
  1013. for (int i = 0; i < numSecondaryRequests; i++)
  1014. {
  1015. if ((rsr.getSecondaryRequests()[i].getName().toString())
  1016. .compareTo("nextPage")
  1017. == 0)
  1018. {
  1019. return true;
  1020. }
  1021. }
  1022. return false;
  1023. }
  1024. public boolean hasPreviousPage() throws java.rmi.RemoteException
  1025. {
  1026. return this.hasPrevioustPage(my_connection, my_rsr);
  1027. }
  1028. /**
  1029. *
  1030. *
  1031. *
  1032. */
  1033. public boolean hasPrevioustPage(CRNConnect connection, AsynchReply rsr)
  1034. throws java.rmi.RemoteException
  1035. {
  1036. if ((connection == null) || (rsr == null))
  1037. {
  1038. return false;
  1039. }
  1040. int numSecondaryRequests = rsr.getSecondaryRequests().length;
  1041. for (int i = 0; i < numSecondaryRequests; i++)
  1042. {
  1043. if ((rsr.getSecondaryRequests()[i].getName().toString())
  1044. .compareTo("previousPage")
  1045. == 0)
  1046. {
  1047. return true;
  1048. }
  1049. }
  1050. return false;
  1051. }
  1052. public boolean hasFirstPage() throws java.rmi.RemoteException
  1053. {
  1054. return hasFirstPage(my_connection, my_rsr);
  1055. }
  1056. /**
  1057. *
  1058. *
  1059. *
  1060. */
  1061. public boolean hasFirstPage(CRNConnect connection, AsynchReply rsr)
  1062. throws java.rmi.RemoteException
  1063. {
  1064. if ((connection == null) || (rsr == null))
  1065. {
  1066. return false;
  1067. }
  1068. int numSecondaryRequests = rsr.getSecondaryRequests().length;
  1069. for (int i = 0; i < numSecondaryRequests; i++)
  1070. {
  1071. if ((rsr.getSecondaryRequests()[i].getName().toString())
  1072. .compareTo("firstPage")
  1073. == 0)
  1074. {
  1075. return true;
  1076. }
  1077. }
  1078. return false;
  1079. }
  1080. public boolean hasLastPage() throws java.rmi.RemoteException
  1081. {
  1082. return hasLastPage(my_connection, my_rsr);
  1083. }
  1084. /**
  1085. *
  1086. *
  1087. *
  1088. */
  1089. public boolean hasLastPage(CRNConnect connection, AsynchReply rsr)
  1090. throws java.rmi.RemoteException
  1091. {
  1092. if ((connection == null) || (rsr == null))
  1093. {
  1094. return false;
  1095. }
  1096. int numSecondaryRequests = rsr.getSecondaryRequests().length;
  1097. for (int i = 0; i < numSecondaryRequests; i++)
  1098. {
  1099. if ((rsr.getSecondaryRequests()[i].getName().toString())
  1100. .compareTo("lastPage")
  1101. == 0)
  1102. {
  1103. return true;
  1104. }
  1105. }
  1106. return false;
  1107. }
  1108. public String[] setFormatByType(int fileType)
  1109. {
  1110. switch (fileType)
  1111. {
  1112. case REP_HTML :
  1113. return (new String[] { "HTML" });
  1114. case REP_XML :
  1115. return (new String[] { "XML" });
  1116. case REP_HTML_FRAG :
  1117. return new String[] { "HTMLFragment" };
  1118. case REP_MHT :
  1119. return new String[] { "MHT" };
  1120. case REP_XLWA :
  1121. return new String[] { "XLWA" };
  1122. case REP_PDF :
  1123. return (new String[] { "PDF" });
  1124. case REP_CSV :
  1125. return (new String[] { "CSV" });
  1126. default :
  1127. System.out.println("Incorrect report type passed.");
  1128. return null;
  1129. }
  1130. }
  1131. public String setFilenameByType(
  1132. String filePath,
  1133. String reportName,
  1134. int fileType)
  1135. {
  1136. switch (fileType)
  1137. {
  1138. case REP_HTML :
  1139. return filePath
  1140. + System.getProperty("file.separator")
  1141. + reportName
  1142. + ++tmpFileCounter
  1143. + "_output.html";
  1144. case REP_XML :
  1145. return filePath
  1146. + System.getProperty("file.separator")
  1147. + reportName
  1148. + ++tmpFileCounter
  1149. + "_output.xml";
  1150. case REP_PDF :
  1151. return filePath
  1152. + System.getProperty("file.separator")
  1153. + reportName
  1154. + ++tmpFileCounter
  1155. + "_output.pdf";
  1156. case REP_CSV :
  1157. return filePath
  1158. + System.getProperty("file.separator")
  1159. + reportName
  1160. + ++tmpFileCounter
  1161. + "_output.csv";
  1162. case REP_HTML_FRAG :
  1163. return filePath
  1164. + System.getProperty("file.separator")
  1165. + reportName
  1166. + ++tmpFileCounter
  1167. + "_output.html";
  1168. case REP_MHT :
  1169. return filePath
  1170. + System.getProperty("file.separator")
  1171. + reportName
  1172. + ++tmpFileCounter
  1173. + "_output.mht";
  1174. case REP_XLWA :
  1175. return filePath
  1176. + System.getProperty("file.separator")
  1177. + reportName
  1178. + ++tmpFileCounter
  1179. + "_output.xls";
  1180. default :
  1181. System.out.println("Incorrect report type passed");
  1182. return null;
  1183. }
  1184. }
  1185. public File getNewTempFile(
  1186. File oldTempFile,
  1187. String reportName,
  1188. int fileType)
  1189. {
  1190. File newTempFile = null;
  1191. if (oldTempFile != null)
  1192. {
  1193. oldTempFile.delete();
  1194. }
  1195. newTempFile =
  1196. new File(
  1197. setFilenameByType(
  1198. my_connection.getDefaultSavePath(),
  1199. reportName,
  1200. fileType));
  1201. return newTempFile;
  1202. }
  1203. }