DeleteReportUI.java 12 KB

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