EmailUI.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. * EmailUI.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. */
  15. import java.awt.BorderLayout;
  16. import java.awt.Dimension;
  17. import java.awt.GridLayout;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import java.io.File;
  23. import java.net.URL;
  24. import javax.swing.BorderFactory;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.JButton;
  27. import javax.swing.JComboBox;
  28. import javax.swing.JEditorPane;
  29. import javax.swing.JFrame;
  30. import javax.swing.JLabel;
  31. import javax.swing.JMenu;
  32. import javax.swing.JMenuBar;
  33. import javax.swing.JMenuItem;
  34. import javax.swing.JOptionPane;
  35. import javax.swing.JPanel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JTextArea;
  38. import javax.swing.JTextField;
  39. import com.cognos.developer.schemas.bibus._3.AddressSMTP;
  40. import com.cognos.developer.schemas.bibus._3.BaseClass;
  41. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  42. import com.cognos.developer.schemas.bibus._3.PropEnum;
  43. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  44. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  45. import com.cognos.developer.schemas.bibus._3.Sort;
  46. // This Java class extends the JFrame class so that you can
  47. // display a window.
  48. public class EmailUI extends JFrame
  49. {
  50. private CRNConnect connect;
  51. private CSHandlers csh = new CSHandlers();
  52. // The following variables represent the dialog components.
  53. private JTextArea textAreaPane;
  54. private JTextField cmURL;
  55. private JButton emailButton;
  56. private JComboBox emailTypeOption;
  57. private JComboBox emailSelectOption;
  58. private static Logon sessionLogon;
  59. private static final int REP_TYPE_ENUM_HTML = 0;
  60. private static int reportType = 0;
  61. private static final String EMAIL_ONE_USER = "Email Selected User";
  62. private static final String EMAIL_ALL_CONTACTS =
  63. "Email All IBM Contacts";
  64. private static final int EMAIL_ENUM_ONE_USER = 1;
  65. private static final int EMAIL_ENUM_ALL_CONTACTS = 2;
  66. private static int emailType = 1;
  67. private static BaseClassWrapper selectedReport = null;
  68. // This is the constructor.
  69. public EmailUI(String title, CRNConnect connection)
  70. {
  71. // Set the title of the frame, even before the variables are declared.
  72. super(title);
  73. connect = connection;
  74. addComponents();
  75. }
  76. // Add all components to the frame's panel.
  77. private void addComponents()
  78. {
  79. JMenuBar mBar = new JMenuBar();
  80. this.setJMenuBar(mBar);
  81. //declare menuItems
  82. JMenuItem exit;
  83. JMenuItem about;
  84. JMenuItem overview;
  85. //Add and populate the File menu.
  86. JMenu fileMenu = new JMenu("File");
  87. mBar.add(fileMenu);
  88. exit = new JMenuItem("Exit");
  89. fileMenu.add(exit);
  90. exit.addActionListener(new MenuHandler());
  91. //Add and populate the Help menu.
  92. JMenu helpMenu = new JMenu("Help");
  93. mBar.add(helpMenu);
  94. about = new JMenuItem("About");
  95. helpMenu.add(about);
  96. about.addActionListener(new MenuHandler());
  97. overview = new JMenuItem("Overview");
  98. helpMenu.add(overview);
  99. overview.addActionListener(new MenuHandler());
  100. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  101. // create a cmURL panel
  102. JPanel cmURLPanel = new JPanel();
  103. // Add the URL text field and label
  104. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  105. cmURL.setText(CRNConnect.CM_URL);
  106. cmURL.setEditable(false);
  107. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  108. cmURLPanel.add(cmURL, BorderLayout.EAST);
  109. // Create the button Panel
  110. JPanel buttonPanel = new JPanel();
  111. //Create and add the report output type combo box
  112. String emailTypes[] = { EMAIL_ONE_USER, EMAIL_ALL_CONTACTS };
  113. emailTypeOption = new JComboBox(emailTypes);
  114. emailTypeOption.setSelectedItem(null);
  115. emailTypeOption.addActionListener(new EmailTypeSelectionHandler());
  116. buttonPanel.add(emailTypeOption, BorderLayout.WEST);
  117. // Create and add the select report combo box
  118. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  119. emailSelectOption = new JComboBox(listOfReports);
  120. emailSelectOption.setSelectedItem(null);
  121. emailSelectOption.addActionListener(new ReportSelectionHandler());
  122. buttonPanel.add(emailSelectOption, BorderLayout.CENTER);
  123. // Create and add the Button
  124. emailButton = new JButton("Email");
  125. emailButton.addActionListener(new allButtonsHandler());
  126. buttonPanel.add(emailButton, BorderLayout.EAST);
  127. // Add the status text pane.
  128. textAreaPane = new JTextArea();
  129. // Add the panels to the mainPanel
  130. mainPanel.add(cmURLPanel);
  131. mainPanel.add(buttonPanel);
  132. mainPanel.add(textAreaPane);
  133. //Add the ScrollPane to outputPanel
  134. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  135. areaScrollPane.setVerticalScrollBarPolicy(
  136. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  137. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  138. areaScrollPane.setBorder(
  139. BorderFactory.createCompoundBorder(
  140. BorderFactory.createCompoundBorder(
  141. BorderFactory.createTitledBorder("Output"),
  142. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  143. areaScrollPane.getBorder()));
  144. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  145. outputPanel.add(areaScrollPane);
  146. JPanel panel = new JPanel(new BorderLayout());
  147. panel.add(mainPanel, BorderLayout.NORTH);
  148. panel.add(outputPanel);
  149. setContentPane(panel);
  150. }
  151. private class MenuHandler implements ActionListener
  152. {
  153. public void actionPerformed(ActionEvent e)
  154. {
  155. if (e.getActionCommand().startsWith("http://"))
  156. {
  157. connect.connectionChange(e.getActionCommand());
  158. }
  159. try
  160. {
  161. JMenuItem menuClicked = (JMenuItem)e.getSource();
  162. if (menuClicked.getText() == "Exit")
  163. {
  164. System.exit(0);
  165. }
  166. if (menuClicked.getText() == "About")
  167. {
  168. JOptionPane.showMessageDialog(
  169. ((JMenuItem)e.getSource()).getParent(),
  170. "IBM Cognos Sample Application\n\n"
  171. + "Version 1.0.0\n"
  172. + "This application uses the IBM Cognos Software Development Kit",
  173. "About IBM Cognos Samples",
  174. JOptionPane.INFORMATION_MESSAGE,
  175. new ImageIcon("../Common/about.gif"));
  176. }
  177. if (menuClicked.getText().compareTo("Overview") == 0)
  178. {
  179. JFrame explainWindow =
  180. new JFrame("Overview for Email Sample");
  181. File explainFile = new File("Java_EmailUI_Explain.html");
  182. if (! explainFile.exists())
  183. {
  184. JOptionPane.showMessageDialog(null, "Explain file not found");
  185. return;
  186. }
  187. URL explainURL =
  188. new URL("file:///" + explainFile.getAbsolutePath());
  189. JEditorPane explainPane = new JEditorPane();
  190. explainPane.setPage(explainURL);
  191. explainPane.setEditable(false);
  192. JScrollPane explainScroll =
  193. new JScrollPane(
  194. explainPane,
  195. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  196. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  197. explainWindow.getContentPane().add(explainScroll);
  198. explainWindow.setSize(640, 480);
  199. explainWindow.setVisible(true);
  200. }
  201. }
  202. catch (Exception ex)
  203. {}
  204. }
  205. }
  206. // The following is the button event handler.
  207. // Note: A SWITCH statement cannot be used here because we are comparing
  208. // objects.
  209. private class allButtonsHandler implements ActionListener
  210. {
  211. public void actionPerformed(ActionEvent e)
  212. {
  213. if (!Logon.loggedIn(connect))
  214. {
  215. try
  216. {
  217. sessionLogon.logon(connect);
  218. }
  219. catch (Exception logonException)
  220. {}
  221. }
  222. JButton buttonPressed = ((JButton)e.getSource());
  223. String output = new String();
  224. if (buttonPressed == emailButton)
  225. {
  226. Email newEmail = new Email();
  227. //For this sample, hard wire HTML format
  228. reportType = REP_TYPE_ENUM_HTML;
  229. try
  230. {
  231. if (emailType == EMAIL_ENUM_ONE_USER)
  232. {
  233. String selectedValue =
  234. JOptionPane.showInputDialog(
  235. "Provide e-mail address:");
  236. if (selectedValue != null)
  237. {
  238. AddressSMTP[] emailAddress = { new AddressSMTP(selectedValue) };
  239. newEmail.emailReport(
  240. connect,
  241. selectedReport,
  242. "message body",
  243. "subject",
  244. reportType,
  245. emailAddress,
  246. null);
  247. }
  248. }
  249. else if (emailType == EMAIL_ENUM_ALL_CONTACTS)
  250. {
  251. newEmail.emailReport(
  252. connect,
  253. selectedReport,
  254. "message body",
  255. "subject",
  256. reportType,
  257. null,
  258. null);
  259. }
  260. }
  261. catch (Exception ex)
  262. {
  263. System.out.println(ex.getMessage());
  264. output =
  265. "An error occurred.\nMake sure a "
  266. + "Report Name is selected and IBM Cognos is running";
  267. }
  268. }
  269. if (output.compareTo("") != 0)
  270. {
  271. textAreaPane.setText("");
  272. textAreaPane.append(output);
  273. }
  274. }
  275. }
  276. // This is the emailType combo box event handler.
  277. private class EmailTypeSelectionHandler implements ActionListener
  278. {
  279. public void actionPerformed(ActionEvent emailTypeSelectedEvent)
  280. {
  281. String chosenType = (String)emailTypeOption.getSelectedItem();
  282. if (chosenType == EMAIL_ONE_USER)
  283. {
  284. emailType = EMAIL_ENUM_ONE_USER;
  285. }
  286. else if (chosenType == EMAIL_ALL_CONTACTS)
  287. {
  288. emailType = EMAIL_ENUM_ALL_CONTACTS;
  289. }
  290. else
  291. {
  292. //error, force ONE_USER (default) ??
  293. emailType = EMAIL_ENUM_ONE_USER;
  294. }
  295. }
  296. }
  297. private class ReportSelectionHandler implements ActionListener
  298. {
  299. public void actionPerformed(ActionEvent repSelectedEvent)
  300. {
  301. selectedReport = (BaseClassWrapper)emailSelectOption.getSelectedItem();
  302. }
  303. }
  304. //This is a method for retrieving a list of the available reports to run
  305. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  306. {
  307. BaseClassWrapper reportAndQueryList[] = null;
  308. BaseClass reports[] = new BaseClass[0];
  309. BaseClass queries[] = new BaseClass[0];
  310. int reportAndQueryIndex = 0;
  311. int reportIndex = 0;
  312. int queryIndex = 0;
  313. if (connection.getCMService() == null)
  314. {
  315. System.out.println(
  316. "Invalid parameter passed to getListOfReports()\n");
  317. return null;
  318. }
  319. PropEnum props[] =
  320. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  321. Sort sortOptions[] = { new Sort()};
  322. sortOptions[0].setOrder(OrderEnum.ascending);
  323. sortOptions[0].setPropName(PropEnum.defaultName);
  324. if (!Logon.loggedIn(connection))
  325. {
  326. try
  327. {
  328. sessionLogon.logon(connection);
  329. }
  330. catch (Exception logonException)
  331. {}
  332. }
  333. try
  334. {
  335. reports =
  336. connection.getCMService().query(
  337. new SearchPathMultipleObject("/content//report"),
  338. props,
  339. sortOptions,
  340. new QueryOptions());
  341. queries =
  342. connection.getCMService().query(
  343. new SearchPathMultipleObject("/content//query"),
  344. props,
  345. sortOptions,
  346. new QueryOptions());
  347. }
  348. catch (java.rmi.RemoteException remoteEx)
  349. {
  350. System.out.println("Caught Remote Exception:\n");
  351. remoteEx.printStackTrace();
  352. }
  353. reportAndQueryList = new BaseClassWrapper[reports.length + queries.length];
  354. if ((reports != null) && (reports.length > 0))
  355. {
  356. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  357. {
  358. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(reports[reportIndex]);
  359. }
  360. }
  361. if ((queries != null) && (queries.length > 0))
  362. {
  363. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  364. {
  365. reportAndQueryList[reportAndQueryIndex++] =
  366. new BaseClassWrapper(queries[queryIndex]);
  367. }
  368. }
  369. return reportAndQueryList;
  370. }
  371. // Create the main method to execute the application.
  372. public static void main(String args[])
  373. {
  374. CRNConnect connection = new CRNConnect();
  375. connection.connectToCognosServer();
  376. sessionLogon = new Logon();
  377. String output = "";
  378. while (!Logon.loggedIn(connection))
  379. {
  380. output = sessionLogon.logon(connection);
  381. if (!Logon.loggedIn(connection))
  382. {
  383. int retry =
  384. JOptionPane.showConfirmDialog(
  385. null,
  386. "Login Failed. Please try again.",
  387. "Login Failed",
  388. JOptionPane.OK_CANCEL_OPTION);
  389. if (retry != JOptionPane.OK_OPTION)
  390. {
  391. System.exit(0);
  392. }
  393. }
  394. }
  395. EmailUI frame = new EmailUI("IBM Cognos Sample", connection);
  396. // Create a WindowAdapter so the application
  397. // is exited when the window is closed.
  398. frame.addWindowListener(new WindowAdapter()
  399. {
  400. public void windowClosing(WindowEvent e)
  401. {
  402. System.exit(0);
  403. }
  404. });
  405. frame.textAreaPane.setText(output);
  406. // Set the size of the frame and display it.
  407. frame.setSize(850, 440);
  408. frame.setVisible(true);
  409. frame.setResizable(true);
  410. }
  411. }