SaveReportUI.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. * SaveReportUI.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 SaveReportUI 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 saveCommand;
  55. private JComboBox repSelectOption;
  56. private static Logon sessionLogon;
  57. private static final String SAVE_REP_OUTPUT = "Save Report Output";
  58. private static final String SAVE_REP_VIEW_AS = "Save Report As";
  59. private static BaseClassWrapper selectedReport = null;
  60. private static boolean saveAs = false;
  61. // This is the constructor.
  62. public SaveReportUI(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 saveOption[] = { SAVE_REP_OUTPUT, SAVE_REP_VIEW_AS };
  106. saveCommand = new JComboBox(saveOption);
  107. saveCommand.setSelectedItem(null);
  108. saveCommand.addActionListener(new SaveCommandHandler());
  109. buttonPanel.add(saveCommand, BorderLayout.WEST);
  110. // Create and add the select report combo box
  111. BaseClassWrapper 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 Save Report Sample");
  174. File explainFile = new File("Java_SaveReportUI_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. SaveReport saveReport = new SaveReport();
  220. try
  221. {
  222. output =
  223. saveReport.saveReport(connect, selectedReport, saveAs);
  224. }
  225. catch (Exception ex)
  226. {
  227. System.out.println(ex.getMessage());
  228. output =
  229. "An error occurred.\nMake sure a "
  230. + "Report Name is selected and IBM Cognos is running.";
  231. }
  232. }
  233. if (output.compareTo("") != 0)
  234. {
  235. textAreaPane.setText("");
  236. textAreaPane.append(output);
  237. }
  238. }
  239. }
  240. // This is the reportType combo box event handler.
  241. private class SaveCommandHandler implements ActionListener
  242. {
  243. public void actionPerformed(ActionEvent repTypeSelectedEvent)
  244. {
  245. String chosenType = (String)saveCommand.getSelectedItem();
  246. if (chosenType == SAVE_REP_OUTPUT)
  247. {
  248. saveAs = false;
  249. }
  250. else if (chosenType == SAVE_REP_VIEW_AS)
  251. {
  252. saveAs = true;
  253. }
  254. else
  255. {
  256. //error, force SAVE_REP_VIEW_AS (default) ??
  257. saveAs = true;
  258. }
  259. }
  260. }
  261. private class ReportSelectionHandler implements ActionListener
  262. {
  263. public void actionPerformed(ActionEvent repSelectedEvent)
  264. {
  265. selectedReport = (BaseClassWrapper)repSelectOption.getSelectedItem();
  266. }
  267. }
  268. //This is a method for retrieving a list of the available reports to run
  269. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  270. {
  271. BaseClassWrapper reportAndQueryList[] = null;
  272. BaseClass reports[] = new BaseClass[0];
  273. BaseClass queries[] = new BaseClass[0];
  274. int reportAndQueryIndex = 0;
  275. int reportIndex = 0;
  276. int queryIndex = 0;
  277. if (connection == null)
  278. {
  279. System.out.println(
  280. "Invalid parameter passed to getListOfReports()\n");
  281. return null;
  282. }
  283. PropEnum props[] =
  284. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  285. Sort sortOptions[] = { new Sort()};
  286. sortOptions[0].setOrder(OrderEnum.ascending);
  287. sortOptions[0].setPropName(PropEnum.defaultName);
  288. if (!Logon.loggedIn(connect))
  289. {
  290. try
  291. {
  292. sessionLogon.logon(connect);
  293. }
  294. catch (Exception logonException)
  295. {}
  296. }
  297. try
  298. {
  299. reports =
  300. connection.getCMService().query(
  301. new SearchPathMultipleObject("/content//report"),
  302. props,
  303. sortOptions,
  304. new QueryOptions());
  305. queries =
  306. connection.getCMService().query(
  307. new SearchPathMultipleObject("/content//query"),
  308. props,
  309. sortOptions,
  310. new QueryOptions());
  311. }
  312. catch (java.rmi.RemoteException remoteEx)
  313. {
  314. System.out.println("Caught Remote Exception:\n");
  315. remoteEx.printStackTrace();
  316. }
  317. reportAndQueryList = new BaseClassWrapper[reports.length + queries.length];
  318. if ((reports != null) && (reports.length > 0))
  319. {
  320. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  321. {
  322. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(reports[reportIndex]);
  323. }
  324. }
  325. if ((queries != null) && (queries.length > 0))
  326. {
  327. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  328. {
  329. reportAndQueryList[reportAndQueryIndex++] =
  330. new BaseClassWrapper(queries[queryIndex]);
  331. }
  332. }
  333. return reportAndQueryList;
  334. }
  335. // Create the main method to execute the application.
  336. public static void main(String args[])
  337. {
  338. CRNConnect connection = new CRNConnect();
  339. connection.connectToCognosServer();
  340. sessionLogon = new Logon();
  341. String output = "";
  342. while (!Logon.loggedIn(connection))
  343. {
  344. output = sessionLogon.logon(connection);
  345. if (!Logon.loggedIn(connection))
  346. {
  347. int retry =
  348. JOptionPane.showConfirmDialog(
  349. null,
  350. "Login Failed. Please try again.",
  351. "Login Failed",
  352. JOptionPane.OK_CANCEL_OPTION);
  353. if (retry != JOptionPane.OK_OPTION)
  354. {
  355. System.exit(0);
  356. }
  357. }
  358. }
  359. SaveReportUI frame = new SaveReportUI("IBM Cognos Sample", connection);
  360. // Create a WindowAdapter so the application
  361. // is exited when the window is closed.
  362. frame.addWindowListener(new WindowAdapter()
  363. {
  364. public void windowClosing(WindowEvent e)
  365. {
  366. System.exit(0);
  367. }
  368. });
  369. frame.textAreaPane.setText(output);
  370. // Set the size of the frame and display it.
  371. frame.setSize(780, 440);
  372. frame.setVisible(true);
  373. frame.setResizable(true);
  374. }
  375. }