NewSchedulerUI.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. * NewSchedulerUI.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.io.File;
  25. import java.net.URL;
  26. import java.util.Date;
  27. import java.util.GregorianCalendar;
  28. import javax.swing.BorderFactory;
  29. import javax.swing.ImageIcon;
  30. import javax.swing.JButton;
  31. import javax.swing.JComboBox;
  32. import javax.swing.JEditorPane;
  33. import javax.swing.JFrame;
  34. import javax.swing.JLabel;
  35. import javax.swing.JMenu;
  36. import javax.swing.JMenuBar;
  37. import javax.swing.JMenuItem;
  38. import javax.swing.JOptionPane;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTextArea;
  42. import javax.swing.JTextField;
  43. import com.cognos.developer.schemas.bibus._3.AddressSMTP;
  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 NewSchedulerUI extends JFrame
  53. {
  54. private CRNConnect connect;
  55. // The following variables represent the dialog components.
  56. private JTextArea textAreaPane;
  57. private JTextField cmURL;
  58. private JTextField selectedSearchPath;
  59. private JButton scheduleReportButton;
  60. private JComboBox repSelectOption;
  61. private static Logon sessionLogon;
  62. private static BaseClassWrapper selectedReport = null;
  63. // This is the constructor.
  64. public NewSchedulerUI(String title, CRNConnect connection)
  65. {
  66. // Set the title of the frame, even before the variables are declared.
  67. super(title);
  68. connect = connection;
  69. addComponents();
  70. }
  71. // Add all components to the frame's panel.
  72. private void addComponents()
  73. {
  74. JMenuBar mBar = new JMenuBar();
  75. this.setJMenuBar(mBar);
  76. //declare menuItems
  77. JMenuItem exit;
  78. JMenuItem about;
  79. JMenuItem overview;
  80. //Add and populate the File menu.
  81. JMenu fileMenu = new JMenu("File");
  82. mBar.add(fileMenu);
  83. exit = new JMenuItem("Exit");
  84. fileMenu.add(exit);
  85. exit.addActionListener(new MenuHandler());
  86. //Add and populate the Help menu.
  87. JMenu helpMenu = new JMenu("Help");
  88. mBar.add(helpMenu);
  89. about = new JMenuItem("About");
  90. helpMenu.add(about);
  91. about.addActionListener(new MenuHandler());
  92. overview = new JMenuItem("Overview");
  93. helpMenu.add(overview);
  94. overview.addActionListener(new MenuHandler());
  95. JPanel mainPanel = createMainPanel();
  96. JPanel outputNavPanel = createOutputPanel();
  97. JPanel panel = new JPanel(new BorderLayout());
  98. panel.add(mainPanel, BorderLayout.NORTH);
  99. panel.add(outputNavPanel);
  100. setContentPane(panel);
  101. }
  102. private JPanel createOutputPanel()
  103. {
  104. //Create the text scrollPane
  105. textAreaPane = new JTextArea();
  106. JScrollPane textScrollPane = new JScrollPane(textAreaPane);
  107. textScrollPane.setVerticalScrollBarPolicy(
  108. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  109. textScrollPane.setPreferredSize(new Dimension(500, 275));
  110. //Create the output panel and its layout objects
  111. GridBagLayout layout = new GridBagLayout();
  112. JPanel outputPanel = new JPanel(layout);
  113. GridBagConstraints layoutConstraints = new GridBagConstraints();
  114. //Set the layout for the scroll pane and add it
  115. layoutConstraints.weightx = 1.0;
  116. layoutConstraints.weighty = 1.0;
  117. layoutConstraints.fill = GridBagConstraints.BOTH;
  118. layout.setConstraints(textScrollPane, layoutConstraints);
  119. outputPanel.add(textScrollPane);
  120. //put a border around the output and nav buttons
  121. outputPanel.setBorder(
  122. BorderFactory.createCompoundBorder(
  123. BorderFactory.createCompoundBorder(
  124. BorderFactory.createTitledBorder("Output"),
  125. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  126. outputPanel.getBorder()));
  127. return outputPanel;
  128. }
  129. private JPanel createMainPanel()
  130. {
  131. // Add the URL text field and label
  132. cmURL = new JTextField(CRNConnect.CM_URL.length() - 10);
  133. cmURL.setText(CRNConnect.CM_URL);
  134. cmURL.setEditable(false);
  135. //Put together a panel for the URL
  136. JPanel cmURLPanel = new JPanel();
  137. cmURLPanel.add(new JLabel("Server URL:"));
  138. cmURLPanel.add(cmURL);
  139. // Create the searchPath text field and label
  140. selectedSearchPath = new JTextField(CRNConnect.CM_URL.length() - 10); //same as above
  141. selectedSearchPath.setText("");
  142. selectedSearchPath.setEditable(false);
  143. selectedSearchPath.setAutoscrolls(true);
  144. //Put together a panel for the search path
  145. JPanel searchPathPanel = new JPanel();
  146. searchPathPanel.add(selectedSearchPath);
  147. //get the button panel
  148. JPanel buttonPanel = createMainButtonPanel();
  149. //
  150. // create the main panel and add the components
  151. //
  152. JPanel mainPanel = new JPanel(new GridLayout(3,0));
  153. // Add everything to the main panel
  154. mainPanel.add(cmURLPanel);
  155. mainPanel.add(buttonPanel);
  156. mainPanel.add(searchPathPanel);
  157. return mainPanel;
  158. }
  159. private JPanel createMainButtonPanel()
  160. {
  161. // Create the button Panel
  162. JPanel buttonPanel = new JPanel();
  163. // Create and add the select report combo box
  164. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  165. repSelectOption = new JComboBox(listOfReports);
  166. repSelectOption.setSelectedItem(null);
  167. repSelectOption.addActionListener(new ReportSelectionHandler());
  168. buttonPanel.add(repSelectOption, BorderLayout.CENTER);
  169. // Create and add the Button
  170. scheduleReportButton = new JButton("Schedule Report");
  171. scheduleReportButton.addActionListener(new allButtonsHandler());
  172. buttonPanel.add(scheduleReportButton, BorderLayout.EAST);
  173. return buttonPanel;
  174. }
  175. private class MenuHandler implements ActionListener
  176. {
  177. public void actionPerformed(ActionEvent e)
  178. {
  179. if (e.getActionCommand().startsWith("http://"))
  180. {
  181. connect.connectionChange(e.getActionCommand());
  182. }
  183. try
  184. {
  185. JMenuItem menuClicked = (JMenuItem)e.getSource();
  186. if (menuClicked.getText() == "Exit")
  187. {
  188. System.exit(0);
  189. }
  190. if (menuClicked.getText() == "About")
  191. {
  192. JOptionPane.showMessageDialog(
  193. ((JMenuItem)e.getSource()).getParent(),
  194. "IBM Cognos Sample Application\n\n"
  195. + "Version 1.0.0\n"
  196. + "This application uses the IBM Cognos Software Development Kit",
  197. "About IBM Cognos Samples",
  198. JOptionPane.INFORMATION_MESSAGE,
  199. new ImageIcon("../Common/about.gif"));
  200. }
  201. if (menuClicked.getText().compareTo("Overview") == 0)
  202. {
  203. JFrame explainWindow =
  204. new JFrame("Overview for NewScheduler");
  205. File explainFile = new File("Java_NewScheduler_Explain.html");
  206. if (! explainFile.exists())
  207. {
  208. JOptionPane.showMessageDialog(null, "Explain file not found");
  209. return;
  210. }
  211. URL explainURL =
  212. new URL("file:///" + explainFile.getAbsolutePath());
  213. JEditorPane explainPane = new JEditorPane();
  214. explainPane.setPage(explainURL);
  215. explainPane.setEditable(false);
  216. JScrollPane explainScroll =
  217. new JScrollPane(
  218. explainPane,
  219. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  220. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  221. explainWindow.getContentPane().add(explainScroll);
  222. explainWindow.setSize(640, 480);
  223. explainWindow.setVisible(true);
  224. }
  225. }
  226. catch (Exception ex)
  227. {}
  228. }
  229. }
  230. // The following is the button event handler.
  231. // Note: A SWITCH statement cannot be used here because we are comparing
  232. // objects.
  233. private class allButtonsHandler implements ActionListener
  234. {
  235. public void actionPerformed(ActionEvent e)
  236. {
  237. if (!Logon.loggedIn(connect))
  238. {
  239. try
  240. {
  241. sessionLogon.logon(connect);
  242. }
  243. catch (Exception logonException)
  244. {}
  245. }
  246. JButton buttonPressed = ((JButton)e.getSource());
  247. String output = new String();
  248. if (buttonPressed == scheduleReportButton)
  249. {
  250. NewScheduler scheduleReport = new NewScheduler();
  251. try
  252. {
  253. //Get a Date to use, relative to now
  254. Date scheduleStartDate = new Date();
  255. Date scheduleEndDate = new Date();
  256. //Set start for a minute from now
  257. long scheduleTimeInMillis = scheduleStartDate.getTime();
  258. scheduleStartDate.setTime(scheduleTimeInMillis + 60000);
  259. GregorianCalendar startTime = new GregorianCalendar();
  260. startTime.setTime(scheduleStartDate);
  261. //Set end for a minute after the startTime
  262. scheduleTimeInMillis = scheduleStartDate.getTime();
  263. scheduleEndDate.setTime(scheduleTimeInMillis + 360000);
  264. GregorianCalendar endTime = new GregorianCalendar();
  265. endTime.setTime(scheduleEndDate);
  266. textAreaPane.append(
  267. "Scheduling report: "
  268. + repSelectOption.getSelectedItem()
  269. + System.getProperty("line.separator"));
  270. //Run the selected report.
  271. output =
  272. scheduleReport.setSchedule(
  273. connect,
  274. (BaseClassWrapper)repSelectOption.getSelectedItem(),
  275. "HTML",
  276. "",
  277. "",
  278. "saveas",
  279. "",
  280. repSelectOption.getSelectedItem() + " View",
  281. new AddressSMTP[] {},
  282. 6,
  283. "",
  284. "january",
  285. "",
  286. "",
  287. "minute",
  288. "3",
  289. "ByDay",
  290. startTime,
  291. endTime,
  292. "onDate");
  293. }
  294. catch (java.rmi.RemoteException remoteEx)
  295. {
  296. System.out.println(remoteEx.getMessage());
  297. remoteEx.printStackTrace();
  298. output =
  299. "Run Report:\nAn error occurred\nMake sure a "
  300. + "Report Name is selected and IBM Cognos is running";
  301. }
  302. }
  303. if (output.compareTo("") != 0)
  304. {
  305. textAreaPane.setText("");
  306. textAreaPane.append(output);
  307. }
  308. }
  309. }
  310. private class ReportSelectionHandler implements ActionListener
  311. {
  312. public void actionPerformed(ActionEvent repSelectedEvent)
  313. {
  314. selectedReport = (BaseClassWrapper) repSelectOption.getSelectedItem();
  315. selectedSearchPath.setText(selectedReport.getBaseClassObject().getSearchPath().getValue());
  316. }
  317. }
  318. //This is a method for retrieving a list of the available reports to run
  319. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  320. {
  321. BaseClassWrapper reportAndQueryList[] = null;
  322. BaseClass reports[] = new BaseClass[0];
  323. BaseClass queries[] = new BaseClass[0];
  324. int reportAndQueryIndex = 0;
  325. int reportIndex = 0;
  326. int queryIndex = 0;
  327. if (connection.getCMService() == null)
  328. {
  329. System.out.println(
  330. "Invalid parameter passed to getListOfReports()\n");
  331. return null;
  332. }
  333. PropEnum props[] =
  334. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  335. Sort sortOptions[] = { new Sort()};
  336. sortOptions[0].setOrder(OrderEnum.ascending);
  337. sortOptions[0].setPropName(PropEnum.defaultName);
  338. if (!Logon.loggedIn(connect))
  339. {
  340. try
  341. {
  342. sessionLogon.logon(connect);
  343. }
  344. catch (Exception logonException)
  345. {}
  346. }
  347. try
  348. {
  349. reports =
  350. connection.getCMService().query(
  351. new SearchPathMultipleObject("/content//report"),
  352. props,
  353. sortOptions,
  354. new QueryOptions());
  355. queries =
  356. connection.getCMService().query(
  357. new SearchPathMultipleObject("/content//query"),
  358. props,
  359. sortOptions,
  360. new QueryOptions());
  361. }
  362. catch (java.rmi.RemoteException remoteEx)
  363. {
  364. System.out.println("Caught Remote Exception:\n");
  365. remoteEx.printStackTrace();
  366. }
  367. reportAndQueryList = new BaseClassWrapper[reports.length + queries.length];
  368. if ((reports != null) && (reports.length > 0))
  369. {
  370. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  371. {
  372. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(reports[reportIndex]);
  373. }
  374. }
  375. if ((queries != null) && (queries.length > 0))
  376. {
  377. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  378. {
  379. reportAndQueryList[reportAndQueryIndex++] =
  380. new BaseClassWrapper(queries[queryIndex]);
  381. }
  382. }
  383. return reportAndQueryList;
  384. }
  385. // Create the main method to execute the application.
  386. public static void main(String args[])
  387. {
  388. CRNConnect connection = new CRNConnect();
  389. connection.connectToCognosServer();
  390. sessionLogon = new Logon();
  391. String output = "";
  392. while (!Logon.loggedIn(connection))
  393. {
  394. output = sessionLogon.logon(connection);
  395. if (!Logon.loggedIn(connection))
  396. {
  397. int retry =
  398. JOptionPane.showConfirmDialog(
  399. null,
  400. "Login Failed. Please try again.",
  401. "Login Failed",
  402. JOptionPane.OK_CANCEL_OPTION);
  403. if (retry != JOptionPane.OK_OPTION)
  404. {
  405. System.exit(0);
  406. }
  407. }
  408. }
  409. NewSchedulerUI frame = new NewSchedulerUI("IBM Cognos Sample", connection);
  410. // Create a WindowAdapter so the application
  411. // is exited when the window is closed.
  412. frame.addWindowListener(new WindowAdapter()
  413. {
  414. public void windowClosing(WindowEvent e)
  415. {
  416. System.exit(0);
  417. }
  418. });
  419. frame.textAreaPane.setText(output);
  420. // Set the size of the frame and display it.
  421. frame.setSize(680, 440);
  422. frame.setVisible(true);
  423. frame.setResizable(true);
  424. }
  425. }