CreateReportUI.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. * CreateReportUI.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.util.Vector;
  23. import java.io.File;
  24. import java.net.URL;
  25. import javax.swing.BorderFactory;
  26. import javax.swing.ImageIcon;
  27. import javax.swing.JButton;
  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. // This Java class extends the JFrame class so that you can
  41. // display a window.
  42. public class CreateReportUI extends JFrame
  43. {
  44. private CRNConnect connect;
  45. // The following variables represent the dialog components.
  46. private JTextArea textAreaPane;
  47. private JTextField cmURL;
  48. private JButton createReportButton;
  49. private static Logon sessionLogon;
  50. // This is the constructor.
  51. public CreateReportUI(String title, CRNConnect connection)
  52. {
  53. // Set the title of the frame, even before the variables are declared.
  54. super(title);
  55. connect = connection;
  56. addComponents();
  57. }
  58. // Add all components to the frame's panel.
  59. private void addComponents()
  60. {
  61. JMenuBar mBar = new JMenuBar();
  62. this.setJMenuBar(mBar);
  63. //declare menuItems
  64. JMenuItem exit;
  65. JMenuItem about;
  66. JMenuItem overview;
  67. //Add and populate the File menu.
  68. JMenu fileMenu = new JMenu("File");
  69. mBar.add(fileMenu);
  70. exit = new JMenuItem("Exit");
  71. fileMenu.add(exit);
  72. exit.addActionListener(new MenuHandler());
  73. //Add and populate the Help menu.
  74. JMenu helpMenu = new JMenu("Help");
  75. mBar.add(helpMenu);
  76. about = new JMenuItem("About");
  77. helpMenu.add(about);
  78. about.addActionListener(new MenuHandler());
  79. overview = new JMenuItem("Overview");
  80. helpMenu.add(overview);
  81. overview.addActionListener(new MenuHandler());
  82. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  83. // create a cmURL panel
  84. JPanel cmURLPanel = new JPanel();
  85. // Add the URL text field and label
  86. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  87. cmURL.setText(CRNConnect.CM_URL);
  88. cmURL.setEditable(false);
  89. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  90. cmURLPanel.add(cmURL, BorderLayout.EAST);
  91. // Create the button Panel
  92. JPanel buttonPanel = new JPanel();
  93. // Create and add the Button
  94. createReportButton = new JButton("Create Report");
  95. createReportButton.addActionListener(new allButtonsHandler());
  96. buttonPanel.add(createReportButton, BorderLayout.EAST);
  97. // Add the status text pane.
  98. textAreaPane = new JTextArea();
  99. // Add the panels to the mainPanel
  100. mainPanel.add(cmURLPanel);
  101. mainPanel.add(buttonPanel);
  102. mainPanel.add(textAreaPane);
  103. //Add the ScrollPane to outputPanel
  104. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  105. areaScrollPane.setVerticalScrollBarPolicy(
  106. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  107. areaScrollPane.setPreferredSize(new Dimension(200, 275));
  108. areaScrollPane.setBorder(
  109. BorderFactory.createCompoundBorder(
  110. BorderFactory.createCompoundBorder(
  111. BorderFactory.createTitledBorder("Output"),
  112. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  113. areaScrollPane.getBorder()));
  114. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  115. outputPanel.add(areaScrollPane);
  116. JPanel panel = new JPanel(new BorderLayout());
  117. panel.add(mainPanel, BorderLayout.NORTH);
  118. panel.add(outputPanel);
  119. setContentPane(panel);
  120. }
  121. private class MenuHandler implements ActionListener
  122. {
  123. public void actionPerformed(ActionEvent e)
  124. {
  125. if (e.getActionCommand().startsWith("http://"))
  126. {
  127. connect.connectionChange(e.getActionCommand());
  128. }
  129. try
  130. {
  131. JMenuItem menuClicked = (JMenuItem)e.getSource();
  132. if (menuClicked.getText() == "Exit")
  133. {
  134. System.exit(0);
  135. }
  136. if (menuClicked.getText() == "About")
  137. {
  138. JOptionPane.showMessageDialog(
  139. ((JMenuItem)e.getSource()).getParent(),
  140. "IBM Cognos Sample Application\n\n"
  141. + "Version 1.0.0\n"
  142. + "This application uses the IBM Cognos Software Development Kit",
  143. "About IBM Cognos Samples",
  144. JOptionPane.INFORMATION_MESSAGE,
  145. new ImageIcon("../Common/about.gif"));
  146. }
  147. if (menuClicked.getText().compareTo("Overview") == 0)
  148. {
  149. JFrame explainWindow =
  150. new JFrame("Overview for Create Report Sample");
  151. File explainFile = new File("Java_CreateReportUI_Explain.html");
  152. if (! explainFile.exists())
  153. {
  154. JOptionPane.showMessageDialog(null, "Explain file not found");
  155. return;
  156. }
  157. URL explainURL =
  158. new URL("file:///" + explainFile.getAbsolutePath());
  159. JEditorPane explainPane = new JEditorPane();
  160. explainPane.setPage(explainURL);
  161. explainPane.setEditable(false);
  162. JScrollPane explainScroll =
  163. new JScrollPane(
  164. explainPane,
  165. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  166. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  167. explainWindow.getContentPane().add(explainScroll);
  168. explainWindow.setSize(640, 480);
  169. explainWindow.setVisible(true);
  170. }
  171. }
  172. catch (Exception ex)
  173. {}
  174. }
  175. }
  176. // The following is the button event handler.
  177. // Note: A SWITCH statement cannot be used here because we are comparing
  178. // objects.
  179. private class allButtonsHandler implements ActionListener
  180. {
  181. public void actionPerformed(ActionEvent e)
  182. {
  183. if (!Logon.loggedIn(connect))
  184. {
  185. try
  186. {
  187. sessionLogon.logon(connect);
  188. }
  189. catch (Exception logonException)
  190. {}
  191. }
  192. JButton buttonPressed = ((JButton)e.getSource());
  193. String output = new String("");
  194. if (buttonPressed == createReportButton)
  195. {
  196. ReportObject newReport =
  197. new ReportObject(connect, null, null);
  198. try
  199. {
  200. // Get the list of available packages to select.
  201. ViewPackages packages = new ViewPackages();
  202. BaseClass[] bc = packages.getPackages(connect);
  203. String possibleValues[] = new String[bc.length];
  204. BaseClass defaultPackage = null;
  205. String defaultPackageName = null;
  206. // Get the name of each package from the BaseClass object
  207. // to display in the dropdown list.
  208. for (int i = 0; i < bc.length; i++)
  209. {
  210. possibleValues[i] = bc[i].getDefaultName().getValue();
  211. }
  212. //Display a prompt asking user to select a package.
  213. Object selectedValue =
  214. JOptionPane.showInputDialog(
  215. null,
  216. "Please choose a package",
  217. "New Report",
  218. JOptionPane.INFORMATION_MESSAGE,
  219. null,
  220. possibleValues,
  221. possibleValues[0]);
  222. // Retrieve the searchpath of the package needed
  223. // for metaData retrieval.
  224. for (int i = 0; i < possibleValues.length; i++)
  225. {
  226. String findPackage = bc[i].getDefaultName().getValue();
  227. if (((String)selectedValue).equals(findPackage))
  228. {
  229. defaultPackage = bc[i];
  230. break;
  231. }
  232. }
  233. //get package search path
  234. String sPackage = defaultPackage.getSearchPath().getValue();
  235. // Prompt the user for a report name.
  236. String reportName = null;
  237. reportName =
  238. JOptionPane.showInputDialog(
  239. "Please input a report name");
  240. if ((reportName == null) || (reportName.length() == 0))
  241. {
  242. textAreaPane.setText("");
  243. textAreaPane.append("Invalid input / Action cancelled");
  244. return;
  245. }
  246. MetaData md = new MetaData();
  247. Vector packageMetaData =
  248. md.parseMetaData(connect, newReport, sPackage);
  249. Vector selectedColumns = new Vector();
  250. Vector fullNameColumns = new Vector();
  251. ReportWizardDialog dialog =
  252. new ReportWizardDialog(
  253. null,
  254. "Report Wizard Dialog",
  255. true,
  256. packageMetaData,
  257. selectedColumns,
  258. fullNameColumns,
  259. new Vector(),
  260. new Vector(),
  261. new Vector(),
  262. false);
  263. dialog.setVisible(true);
  264. if (!fullNameColumns.isEmpty())
  265. {
  266. newReport.createReport(sPackage);
  267. newReport.addColumns(selectedColumns, fullNameColumns);
  268. newReport.saveReport(connect, defaultPackage, reportName);
  269. //build search path for report
  270. String reportPath = sPackage + "/report[@name='" + reportName + "']";
  271. newReport.renderReport(connect, reportPath);
  272. textAreaPane.append(
  273. "Report " + reportName + " successfully created." + System.getProperty("line.separator"));
  274. // Refresh the view of reports when the new report is created.
  275. ViewReports reports = new ViewReports();
  276. output = reports.viewReportsAndQueries(connect);
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. System.out.println(ex.getMessage());
  282. output = "\nCreate Report:\nAn error occurred\n";
  283. }
  284. }
  285. if (output.compareTo("") != 0)
  286. {
  287. textAreaPane.append(output);
  288. }
  289. }
  290. }
  291. // Create the main method to execute the application.
  292. public static void main(String args[])
  293. {
  294. CRNConnect connection = new CRNConnect();
  295. connection.connectToCognosServer();
  296. sessionLogon = new Logon();
  297. String output = "";
  298. while (!Logon.loggedIn(connection))
  299. {
  300. output = sessionLogon.logon(connection);
  301. if (!Logon.loggedIn(connection))
  302. {
  303. int retry =
  304. JOptionPane.showConfirmDialog(
  305. null,
  306. "Login Failed. Please try again.",
  307. "Login Failed",
  308. JOptionPane.OK_CANCEL_OPTION);
  309. if (retry != JOptionPane.OK_OPTION)
  310. {
  311. System.exit(0);
  312. }
  313. }
  314. }
  315. CreateReportUI frame = new CreateReportUI("IBM Cognos Sample", connection);
  316. // Create a WindowAdapter so the application
  317. // is exited when the window is closed.
  318. frame.addWindowListener(new WindowAdapter()
  319. {
  320. public void windowClosing(WindowEvent e)
  321. {
  322. System.exit(0);
  323. }
  324. });
  325. frame.textAreaPane.setText(output);
  326. // Set the size of the frame and display it.
  327. frame.setSize(780, 440);
  328. frame.setVisible(true);
  329. frame.setResizable(true);
  330. }
  331. }