Email.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. * Email.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. * - deliver
  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 com.cognos.developer.schemas.bibus._3.AddressSMTP;
  34. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  35. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  36. import com.cognos.developer.schemas.bibus._3.AsynchRequest;
  37. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  38. import com.cognos.developer.schemas.bibus._3.BaseClass;
  39. import com.cognos.developer.schemas.bibus._3.BaseParameter;
  40. import com.cognos.developer.schemas.bibus._3.Contact;
  41. import com.cognos.developer.schemas.bibus._3.DeliveryOptionAddressSMTPArray;
  42. import com.cognos.developer.schemas.bibus._3.DeliveryOptionEnum;
  43. import com.cognos.developer.schemas.bibus._3.DeliveryOptionMemoPart;
  44. import com.cognos.developer.schemas.bibus._3.DeliveryOptionString;
  45. import com.cognos.developer.schemas.bibus._3.MemoPartString;
  46. import com.cognos.developer.schemas.bibus._3.Option;
  47. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  48. import com.cognos.developer.schemas.bibus._3.PropEnum;
  49. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  50. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  51. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  52. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  53. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  54. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  55. import com.cognos.developer.schemas.bibus._3.SmtpContentDispositionEnum;
  56. import com.cognos.developer.schemas.bibus._3.Sort;
  57. public class Email
  58. {
  59. private static final int REP_HTML = 0;
  60. private static final int REP_XML = 1;
  61. private static final int REP_PDF = 2;
  62. private static final int REP_CSV = 3;
  63. /**
  64. * @param connection
  65. * Connection to Server
  66. * @param report
  67. * Specifies the report.
  68. * @param bodyText
  69. * Specifies the text for the body of the email message.
  70. * @param emailSubject
  71. * Specifies the subject of the email message.
  72. * @param emailFormat
  73. * Specifies the format of the email message.
  74. * @param emails
  75. * An array of email addresses.
  76. * @param response
  77. * Specifies the primary request. If no primary request is passed
  78. * to this method, this method calls the run method.
  79. * A primary request is necessary to issue a secondary request, such
  80. * as an email request, because a secondary request can only continue
  81. * a conversation established by a primary request.
  82. */
  83. public String emailReport(
  84. CRNConnect connection,
  85. BaseClassWrapper report,
  86. String bodyText,
  87. String emailSubject,
  88. int emailFormat,
  89. AddressSMTP[] emails,
  90. AsynchRequest response)
  91. {
  92. AsynchReply asynchReply = null;
  93. String reportPath = report.getBaseClassObject().getSearchPath().getValue();
  94. if (report == null)
  95. {
  96. return "No valid report selected";
  97. }
  98. try
  99. {
  100. // Get the list of parameters used by the report, including
  101. // optional parameters.
  102. ParameterValue reportParameters[] = new ParameterValue[] {};
  103. ReportParameters repParms = new ReportParameters();
  104. BaseParameter[] prm = repParms.getReportParameters(report, connection);
  105. if (prm != null && prm.length > 0)
  106. {
  107. reportParameters =
  108. ReportParameters.setReportParameters(prm);
  109. }
  110. // Set the run options for the execute method.
  111. Option[] execRunOptions = new Option[2];
  112. Option[] emailRunOptions = new Option[6];
  113. if (response == null)
  114. {
  115. //Execute the report, specify output format
  116. //set the continueConversation option, to allow
  117. //subsequent requests
  118. execRunOptions[0] = setEmailFormat(emailFormat);
  119. execRunOptions[1] = setNoPrompt();
  120. asynchReply =
  121. connection.getReportService().run(new SearchPathSingleObject(reportPath), reportParameters, execRunOptions);
  122. // If response is not immediately complete, call wait until complete
  123. if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  124. {
  125. while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  126. {
  127. //before calling wait, double check that it is okay
  128. if (hasSecondaryRequest(asynchReply, "wait"))
  129. {
  130. asynchReply =
  131. connection.getReportService().wait(
  132. asynchReply.getPrimaryRequest(),
  133. new ParameterValue[] {},
  134. new Option[] {});
  135. }
  136. else
  137. {
  138. return "Error: Wait method not available as expected.";
  139. }
  140. }
  141. }
  142. response = asynchReply.getPrimaryRequest();
  143. }
  144. if (response != null)
  145. {
  146. //Set the required fields for generating the email output
  147. emailRunOptions[0] = setDeliveryMethodEmail();
  148. emailRunOptions[1] = setEmailAttach();
  149. emailRunOptions[2] = setEmailSubject(emailSubject);
  150. // If no email addresses are specified, send the email
  151. // message to all contacts.
  152. if (emails != null && emails.length > 0)
  153. {
  154. emailRunOptions[3] = setEmailAddresses(emails);
  155. }
  156. else
  157. {
  158. emailRunOptions[3] = getContactEmails(connection);
  159. }
  160. emailRunOptions[4] = setEmailBody(bodyText);
  161. emailRunOptions[5] = setContinueConversation();
  162. //call email
  163. // sn_dg_sdk_method_reportService_deliver_start_1
  164. asynchReply = connection.getReportService().deliver(response, new ParameterValue[] {}, emailRunOptions);
  165. // sn_dg_sdk_method_reportService_deliver_end_1
  166. // If response is not immediately complete, call wait until complete
  167. if ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete)) && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  168. {
  169. while ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
  170. && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
  171. {
  172. //before calling wait, double check that it is okay
  173. if (hasSecondaryRequest(asynchReply, "wait"))
  174. {
  175. asynchReply =
  176. connection.getReportService().wait(
  177. asynchReply.getPrimaryRequest(),
  178. new ParameterValue[] {},
  179. new Option[] {});
  180. }
  181. else
  182. {
  183. return "Error: Wait method not available as expected.";
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. System.out.println(
  191. "Response null, unable to issue secondary request.");
  192. }
  193. System.out.println("The email Java method completed successfully.");
  194. return "Email method complete";
  195. }
  196. catch (Exception e)
  197. {
  198. System.out.println("An error occurred in the email Java method.");
  199. e.printStackTrace();
  200. return "An error occurred in the email Java method.";
  201. }
  202. }
  203. public DeliveryOptionAddressSMTPArray setEmailAddresses(AddressSMTP[] emails)
  204. {
  205. //The emails can also be taken from CM as group, user, etc.(see Email class)
  206. DeliveryOptionAddressSMTPArray emailAddress = new DeliveryOptionAddressSMTPArray();
  207. emailAddress.setName(DeliveryOptionEnum.toAddress);
  208. emailAddress.setValue(emails);
  209. return emailAddress;
  210. }
  211. public DeliveryOptionMemoPart setEmailBody(String text)
  212. {
  213. DeliveryOptionMemoPart bodyText = new DeliveryOptionMemoPart();
  214. MemoPartString memoText = new MemoPartString();
  215. memoText.setName("Body");
  216. memoText.setText(text);
  217. memoText.setContentDisposition(SmtpContentDispositionEnum.inline);
  218. bodyText.setName(DeliveryOptionEnum.memoPart);
  219. bodyText.setValue(memoText);
  220. return bodyText;
  221. }
  222. public DeliveryOptionString setEmailSubject(String myEmailSubject)
  223. {
  224. DeliveryOptionString subjectText = new DeliveryOptionString();
  225. subjectText.setName(DeliveryOptionEnum.subject);
  226. subjectText.setValue(myEmailSubject);
  227. return subjectText;
  228. }
  229. public RunOptionBoolean setEmailAttach()
  230. {
  231. RunOptionBoolean attach = new RunOptionBoolean();
  232. attach.setName(RunOptionEnum.emailAsAttachment);
  233. attach.setValue(true);
  234. return attach;
  235. }
  236. public RunOptionStringArray setEmailFormat(int emailFormat)
  237. {
  238. RunOptionStringArray rof = new RunOptionStringArray();
  239. rof.setName(RunOptionEnum.outputFormat);
  240. rof.setValue(this.getReportFormat(emailFormat));
  241. return rof;
  242. }
  243. public RunOptionBoolean setNoPrompt()
  244. {
  245. //Set the report not to prompt
  246. RunOptionBoolean promptFlag = new RunOptionBoolean();
  247. promptFlag.setName(RunOptionEnum.prompt);
  248. promptFlag.setValue(false);
  249. return promptFlag;
  250. }
  251. public String[] getReportFormat(int reportType)
  252. {
  253. switch (reportType)
  254. {
  255. case REP_HTML :
  256. return new String[] { "HTML" };
  257. case REP_XML :
  258. return new String[] { "XML" };
  259. case REP_PDF :
  260. return new String[] { "PDF" };
  261. case REP_CSV :
  262. return new String[] { "CSV" };
  263. default :
  264. System.out.println(
  265. "Invalid report output format."
  266. + " Must be one of: HTML, XML, PDF, CSV.");
  267. return null;
  268. }
  269. }
  270. public RunOptionBoolean setDeliveryMethodEmail()
  271. {
  272. RunOptionBoolean sendEmail = new RunOptionBoolean();
  273. sendEmail.setName(RunOptionEnum.email);
  274. sendEmail.setValue(true);
  275. return sendEmail;
  276. }
  277. public RunOptionBoolean setContinueConversation()
  278. {
  279. RunOptionBoolean continueConversation = new RunOptionBoolean();
  280. continueConversation.setName(RunOptionEnum.continueConversation);
  281. continueConversation.setValue(true);
  282. return continueConversation;
  283. }
  284. public Contact[] getContacts(CRNConnect connection)
  285. {
  286. BaseClass baseClassArray[] = new BaseClass[] {};
  287. PropEnum props[] =
  288. new PropEnum[] {
  289. PropEnum.searchPath,
  290. PropEnum.defaultName,
  291. PropEnum.email };
  292. try
  293. {
  294. baseClassArray =
  295. connection.getCMService().query(
  296. new SearchPathMultipleObject("CAMID(\":\")/contact"),
  297. props,
  298. new Sort[] {},
  299. new QueryOptions());
  300. }
  301. catch (java.rmi.RemoteException remoteEx)
  302. {
  303. remoteEx.printStackTrace();
  304. }
  305. Contact contacts[] = new Contact[baseClassArray.length];
  306. for (int i = 0; i < baseClassArray.length; i++)
  307. {
  308. contacts[i] = (Contact)baseClassArray[i];
  309. }
  310. return contacts;
  311. }
  312. public DeliveryOptionAddressSMTPArray getContactEmails(CRNConnect connection)
  313. {
  314. Contact[] contacts = getContacts(connection);
  315. AddressSMTP[] emailAddress = new AddressSMTP[contacts.length];
  316. for (int i = 0; i < contacts.length; i++)
  317. {
  318. if (contacts[i].getEmail().getValue() != null)
  319. {
  320. emailAddress[i] = new AddressSMTP(contacts[i].getEmail().getValue());
  321. }
  322. else
  323. {
  324. emailAddress[i] = new AddressSMTP("");
  325. }
  326. }
  327. DeliveryOptionAddressSMTPArray emails = new DeliveryOptionAddressSMTPArray();
  328. emails.setValue(emailAddress);
  329. emails.setName(DeliveryOptionEnum.toAddress);
  330. return emails;
  331. }
  332. // sn_dg_sdk_task_hasSecondaryRequest_start_0
  333. public boolean hasSecondaryRequest(
  334. AsynchReply response,
  335. String secondaryRequest)
  336. {
  337. AsynchSecondaryRequest[] secondaryRequests =
  338. response.getSecondaryRequests();
  339. for (int i = 0; i < secondaryRequests.length; i++)
  340. {
  341. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  342. == 0)
  343. {
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. // sn_dg_sdk_task_hasSecondaryRequest_end_0
  350. }