SubmitReportUI.java 11 KB

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