ExecReportsAtUI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * ExecReportsAtUI.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.GridBagConstraints;
  18. import java.awt.GridBagLayout;
  19. import java.awt.GridLayout;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.text.DateFormat;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Date;
  27. import java.util.GregorianCalendar;
  28. import java.io.File;
  29. import java.net.URL;
  30. import javax.swing.BorderFactory;
  31. import javax.swing.ImageIcon;
  32. import javax.swing.JButton;
  33. import javax.swing.JComboBox;
  34. import javax.swing.JEditorPane;
  35. import javax.swing.JFrame;
  36. import javax.swing.JLabel;
  37. import javax.swing.JMenu;
  38. import javax.swing.JMenuBar;
  39. import javax.swing.JMenuItem;
  40. import javax.swing.JOptionPane;
  41. import javax.swing.JPanel;
  42. import javax.swing.JScrollPane;
  43. import javax.swing.JTextField;
  44. import com.cognos.developer.schemas.bibus._3.BaseClass;
  45. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  46. import com.cognos.developer.schemas.bibus._3.PropEnum;
  47. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  48. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  49. import com.cognos.developer.schemas.bibus._3.Sort;
  50. // This Java class extends the JFrame class so that you can
  51. // display a window.
  52. public class ExecReportsAtUI extends JFrame
  53. {
  54. private CRNConnect connect;
  55. // The following variables represent the dialog components.
  56. private JEditorPane htmlAreaPane;
  57. private JTextField cmURL;
  58. private JTextField selectedSearchPath;
  59. private JButton runReportButton;
  60. private JComboBox repTypeOption;
  61. private JComboBox repSelectOption;
  62. private static Logon sessionLogon;
  63. private static final String REP_TYPE_HTML = "HTML";
  64. private static final String REP_TYPE_HTMLFRAG = "HTML Fragment";
  65. private static final String REP_TYPE_MHT = "HTML Web Archive";
  66. private static final String REP_TYPE_XML = "XML";
  67. private static final String REP_TYPE_PDF = "PDF";
  68. private static final String REP_TYPE_CSV = "CSV";
  69. private static final String REP_TYPE_XLWA = "Excel Web Archive";
  70. private static final int REP_TYPE_ENUM_HTML = 0;
  71. private static final int REP_TYPE_ENUM_XML = 1;
  72. private static final int REP_TYPE_ENUM_PDF = 2;
  73. private static final int REP_TYPE_ENUM_CSV = 3;
  74. private static final int REP_TYPE_ENUM_HTMLFRAG = 4;
  75. private static final int REP_TYPE_ENUM_MHT = 5;
  76. private static final int REP_TYPE_ENUM_XLWA = 6;
  77. private static int reportType = 0;
  78. private BaseClassWrapper selectedReport = null;
  79. private allButtonsHandler buttonListener = new allButtonsHandler();
  80. private File tmpHTMLFile = null;
  81. // This is the constructor.
  82. public ExecReportsAtUI(String title, CRNConnect connection)
  83. {
  84. // Set the title of the frame, even before the variables are declared.
  85. super(title);
  86. connect = connection;
  87. addComponents();
  88. }
  89. // Add all components to the frame's panel.
  90. private void addComponents()
  91. {
  92. //
  93. //Create and add menu components
  94. //
  95. JMenuBar mBar = new JMenuBar();
  96. this.setJMenuBar(mBar);
  97. //declare menuItems
  98. JMenuItem exit;
  99. JMenuItem about;
  100. JMenuItem overview;
  101. //Add and populate the File menu.
  102. JMenu fileMenu = new JMenu("File");
  103. mBar.add(fileMenu);
  104. exit = new JMenuItem("Exit");
  105. fileMenu.add(exit);
  106. exit.addActionListener(new MenuHandler());
  107. //Add and populate the Help menu.
  108. JMenu helpMenu = new JMenu("Help");
  109. mBar.add(helpMenu);
  110. about = new JMenuItem("About");
  111. helpMenu.add(about);
  112. about.addActionListener(new MenuHandler());
  113. overview = new JMenuItem("Overview");
  114. helpMenu.add(overview);
  115. overview.addActionListener(new MenuHandler());
  116. //Create panels
  117. JPanel mainPanel = createMainPanel();
  118. JPanel outputNavPanel = createOutputPanel();
  119. JPanel panel = new JPanel(new BorderLayout());
  120. panel.add(mainPanel, BorderLayout.NORTH);
  121. panel.add(outputNavPanel);
  122. setContentPane(panel);
  123. }
  124. private JPanel createOutputPanel()
  125. {
  126. //Create the html scrollPane
  127. htmlAreaPane = new JEditorPane();
  128. JScrollPane htmlScrollPane = new JScrollPane(htmlAreaPane);
  129. htmlScrollPane.setVerticalScrollBarPolicy(
  130. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  131. htmlScrollPane.setPreferredSize(new Dimension(500, 275));
  132. //Create the output panel and it's layout objects
  133. GridBagLayout layout = new GridBagLayout();
  134. JPanel outputNavPanel = new JPanel(layout);
  135. GridBagConstraints layoutConstraints = new GridBagConstraints();
  136. //Set the layout for the scroll pane and add it
  137. layoutConstraints.weightx = 1.0;
  138. layoutConstraints.weighty = 1.0;
  139. layoutConstraints.fill = GridBagConstraints.BOTH;
  140. layout.setConstraints(htmlScrollPane, layoutConstraints);
  141. outputNavPanel.add(htmlScrollPane);
  142. //put a border around the output and nav buttons
  143. outputNavPanel.setBorder(
  144. BorderFactory.createCompoundBorder(
  145. BorderFactory.createCompoundBorder(
  146. BorderFactory.createTitledBorder("Output"),
  147. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  148. outputNavPanel.getBorder()));
  149. return outputNavPanel;
  150. }
  151. private JPanel createMainPanel()
  152. {
  153. // Create the URL text field and label
  154. cmURL = new JTextField(CRNConnect.CM_URL.length() - 10);
  155. cmURL.setText(CRNConnect.CM_URL);
  156. cmURL.setEditable(false);
  157. //Put together a panel for the URL
  158. JPanel cmURLPanel = new JPanel();
  159. cmURLPanel.add(new JLabel("Server URL:"));
  160. cmURLPanel.add(cmURL);
  161. // Create the searchPath text field and label
  162. selectedSearchPath = new JTextField(CRNConnect.CM_URL.length() - 10);
  163. //same as above
  164. selectedSearchPath.setText("");
  165. selectedSearchPath.setEditable(false);
  166. selectedSearchPath.setAutoscrolls(true);
  167. //Put together a panel for the search path
  168. JPanel searchPathPanel = new JPanel();
  169. //searchPathPanel.add(new JLabel("SearchPath:"));
  170. searchPathPanel.add(selectedSearchPath);
  171. //get the button panel
  172. JPanel buttonPanel = createMainButtonPanel();
  173. //
  174. // create the main panel and add the components
  175. //
  176. JPanel mainPanel = new JPanel(new GridLayout(3, 0));
  177. // Add everything to the main panel
  178. mainPanel.add(cmURLPanel);
  179. mainPanel.add(buttonPanel);
  180. mainPanel.add(searchPathPanel);
  181. return mainPanel;
  182. }
  183. private JPanel createMainButtonPanel()
  184. {
  185. // Create the button Panel
  186. JPanel buttonPanel = new JPanel();
  187. //Create and add the report output type combo box
  188. String repType[] =
  189. {
  190. REP_TYPE_HTML,
  191. REP_TYPE_HTMLFRAG,
  192. REP_TYPE_MHT,
  193. REP_TYPE_XML,
  194. REP_TYPE_PDF,
  195. REP_TYPE_CSV,
  196. REP_TYPE_XLWA };
  197. repTypeOption = new JComboBox(repType);
  198. repTypeOption.setSelectedItem(null);
  199. repTypeOption.addActionListener(new ReportTypeSelectionHandler());
  200. buttonPanel.add(repTypeOption, BorderLayout.WEST);
  201. // Create and add the select report combo box
  202. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  203. repSelectOption = new JComboBox(listOfReports);
  204. repSelectOption.setSelectedItem(null);
  205. repSelectOption.addActionListener(new ReportSelectionHandler());
  206. buttonPanel.add(repSelectOption, BorderLayout.CENTER);
  207. // Create and add the Button
  208. runReportButton = new JButton("Execute At ...");
  209. runReportButton.addActionListener(buttonListener);
  210. runReportButton.setToolTipText(
  211. "Click this button to have the selected report execute 2 minutes from now");
  212. buttonPanel.add(runReportButton, BorderLayout.EAST);
  213. return buttonPanel;
  214. }
  215. private class MenuHandler implements ActionListener
  216. {
  217. public void actionPerformed(ActionEvent e)
  218. {
  219. if (e.getActionCommand().startsWith("http://"))
  220. {
  221. connect.connectionChange(e.getActionCommand());
  222. }
  223. try
  224. {
  225. JMenuItem menuClicked = (JMenuItem)e.getSource();
  226. if (menuClicked.getText() == "Exit")
  227. {
  228. System.exit(0);
  229. }
  230. if (menuClicked.getText() == "About")
  231. {
  232. JOptionPane.showMessageDialog(
  233. ((JMenuItem)e.getSource()).getParent(),
  234. "IBM Cognos Sample Application\n\n"
  235. + "Version 1.0.0\n"
  236. + "This application uses the IBM Cognos Software Development Kit",
  237. "About IBM Cognos Samples",
  238. JOptionPane.INFORMATION_MESSAGE,
  239. new ImageIcon("../Common/about.gif"));
  240. }
  241. if (menuClicked.getText().compareTo("Overview") == 0)
  242. {
  243. JFrame explainWindow =
  244. new JFrame("Overview for ExecuteAt Sample");
  245. File explainFile =
  246. new File("Java_ExecReportsAtUI_Explain.html");
  247. if (!explainFile.exists())
  248. {
  249. JOptionPane.showMessageDialog(
  250. null,
  251. "Explain file not found");
  252. return;
  253. }
  254. URL explainURL =
  255. new URL("file:///" + explainFile.getAbsolutePath());
  256. JEditorPane explainPane = new JEditorPane();
  257. explainPane.setPage(explainURL);
  258. explainPane.setEditable(false);
  259. JScrollPane explainScroll =
  260. new JScrollPane(
  261. explainPane,
  262. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  263. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  264. explainWindow.getContentPane().add(explainScroll);
  265. explainWindow.setSize(640, 480);
  266. explainWindow.setVisible(true);
  267. }
  268. }
  269. catch (Exception ex)
  270. {}
  271. }
  272. }
  273. // The following is the button event handler.
  274. // Note: A SWITCH statement cannot be used here because we are comparing
  275. // objects.
  276. private class allButtonsHandler implements ActionListener
  277. {
  278. private RunReport runReport = new RunReport();
  279. public void actionPerformed(ActionEvent e)
  280. {
  281. if (!Logon.loggedIn(connect))
  282. {
  283. try
  284. {
  285. sessionLogon.logon(connect);
  286. }
  287. catch (Exception logonException)
  288. {}
  289. }
  290. JButton buttonPressed = ((JButton)e.getSource());
  291. String output = new String();
  292. if (buttonPressed == runReportButton)
  293. {
  294. //runReport = new RunReport();
  295. try
  296. {
  297. //Get a Date to use, roughly now
  298. Date execDate = new Date();
  299. //Set execTime for 5 minutes after now
  300. long execTimeInMillis = execDate.getTime();
  301. execDate.setTime(execTimeInMillis + (2 * 60000));
  302. GregorianCalendar execTime = new GregorianCalendar();
  303. execTime.setTime(execDate);
  304. DateFormat dateFormatter = new SimpleDateFormat();
  305. //Run the selected report.
  306. output =
  307. runReport.runReportAt(
  308. connect,
  309. selectedReport,
  310. reportType,
  311. execTime);
  312. }
  313. catch (Exception ex)
  314. {
  315. htmlAreaPane.setContentType("text/html");
  316. output =
  317. "<html><head><title>"
  318. + ex.getMessage()
  319. + "</title></head><body><pre>"
  320. + ex.toString()
  321. + "</pre></body></html>";
  322. htmlAreaPane.setText(output);
  323. return;
  324. }
  325. }
  326. if (output.compareTo("") != 0)
  327. {
  328. htmlAreaPane.setText(output);
  329. }
  330. }
  331. }
  332. // This is the reportType combo box event handler.
  333. private class ReportTypeSelectionHandler implements ActionListener
  334. {
  335. public void actionPerformed(ActionEvent repTypeSelectedEvent)
  336. {
  337. String chosenType = (String)repTypeOption.getSelectedItem();
  338. if (chosenType == REP_TYPE_HTML)
  339. {
  340. reportType = REP_TYPE_ENUM_HTML;
  341. }
  342. else if (chosenType == REP_TYPE_XML)
  343. {
  344. reportType = REP_TYPE_ENUM_XML;
  345. }
  346. else if (chosenType == REP_TYPE_PDF)
  347. {
  348. reportType = REP_TYPE_ENUM_PDF;
  349. }
  350. else if (chosenType == REP_TYPE_CSV)
  351. {
  352. reportType = REP_TYPE_ENUM_CSV;
  353. }
  354. else if (chosenType == REP_TYPE_HTMLFRAG)
  355. {
  356. reportType = REP_TYPE_ENUM_HTMLFRAG;
  357. }
  358. else if (chosenType == REP_TYPE_MHT)
  359. {
  360. reportType = REP_TYPE_ENUM_MHT;
  361. }
  362. else if (chosenType == REP_TYPE_XLWA)
  363. {
  364. reportType = REP_TYPE_ENUM_XLWA;
  365. }
  366. else
  367. {
  368. //error, force HTML (default) ??
  369. reportType = REP_TYPE_ENUM_HTML;
  370. }
  371. }
  372. }
  373. private class ReportSelectionHandler implements ActionListener
  374. {
  375. public void actionPerformed(ActionEvent repSelectedEvent)
  376. {
  377. selectedReport =
  378. (BaseClassWrapper)repSelectOption.getSelectedItem();
  379. selectedSearchPath.setText(
  380. selectedReport.getBaseClassObject().getSearchPath().getValue());
  381. }
  382. }
  383. //This is a method for retrieving a list of the available reports to run
  384. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  385. {
  386. BaseClassWrapper reportAndQueryList[] = null;
  387. BaseClass reports[] = new BaseClass[0];
  388. BaseClass queries[] = new BaseClass[0];
  389. int reportAndQueryIndex = 0;
  390. int reportIndex = 0;
  391. int queryIndex = 0;
  392. if (connection == null)
  393. {
  394. System.out.println(
  395. "Invalid parameter passed to getListOfReports()\n");
  396. return null;
  397. }
  398. PropEnum props[] =
  399. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  400. Sort sortOptions[] = { new Sort()};
  401. sortOptions[0].setOrder(OrderEnum.ascending);
  402. sortOptions[0].setPropName(PropEnum.defaultName);
  403. if (!Logon.loggedIn(connect))
  404. {
  405. try
  406. {
  407. sessionLogon.logon(connect);
  408. }
  409. catch (Exception logonException)
  410. {}
  411. }
  412. SearchPathMultipleObject reportsPath =
  413. new SearchPathMultipleObject("/content//report");
  414. SearchPathMultipleObject queriesPath =
  415. new SearchPathMultipleObject("/content//query");
  416. try
  417. {
  418. reports =
  419. connection.getCMService().query(
  420. reportsPath,
  421. props,
  422. sortOptions,
  423. new QueryOptions());
  424. queries =
  425. connection.getCMService().query(
  426. queriesPath,
  427. props,
  428. sortOptions,
  429. new QueryOptions());
  430. }
  431. catch (java.rmi.RemoteException remoteEx)
  432. {
  433. System.out.println("Caught Remote Exception:\n");
  434. remoteEx.printStackTrace();
  435. }
  436. reportAndQueryList =
  437. new BaseClassWrapper[reports.length + queries.length];
  438. if ((reports != null) && (reports.length > 0))
  439. {
  440. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  441. {
  442. reportAndQueryList[reportAndQueryIndex++] =
  443. new BaseClassWrapper(reports[reportIndex]);
  444. }
  445. }
  446. if ((queries != null) && (queries.length > 0))
  447. {
  448. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  449. {
  450. reportAndQueryList[reportAndQueryIndex++] =
  451. new BaseClassWrapper(queries[queryIndex]);
  452. }
  453. }
  454. return reportAndQueryList;
  455. }
  456. // Create the main method to execute the application.
  457. public static void main(String args[])
  458. {
  459. CRNConnect connection = new CRNConnect();
  460. connection.connectToCognosServer();
  461. sessionLogon = new Logon();
  462. String output = "";
  463. while (!Logon.loggedIn(connection))
  464. {
  465. output = sessionLogon.logon(connection);
  466. if (!Logon.loggedIn(connection))
  467. {
  468. int retry =
  469. JOptionPane.showConfirmDialog(
  470. null,
  471. "Login Failed. Please try again.",
  472. "Login Failed",
  473. JOptionPane.OK_CANCEL_OPTION);
  474. if (retry != JOptionPane.OK_OPTION)
  475. {
  476. System.exit(0);
  477. }
  478. }
  479. }
  480. ExecReportsAtUI frame =
  481. new ExecReportsAtUI("IBM Cognos Sample", connection);
  482. // Create a WindowAdapter so the application
  483. // is exited when the window is closed.
  484. frame.addWindowListener(new WindowAdapter()
  485. {
  486. public void windowClosing(WindowEvent e)
  487. {
  488. System.exit(0);
  489. }
  490. });
  491. frame.htmlAreaPane.setText(
  492. output + System.getProperty("line.separator"));
  493. // Set the size of the frame and display it.
  494. frame.setSize(800, 440);
  495. frame.setVisible(true);
  496. frame.setResizable(true);
  497. }
  498. }