AddReportUI.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. * AddReportUI.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.io.FileInputStream;
  24. import java.io.IOException;
  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.JFileChooser;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JMenu;
  35. import javax.swing.JMenuBar;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.JPanel;
  39. import javax.swing.JScrollPane;
  40. import javax.swing.JTextArea;
  41. import javax.swing.JTextField;
  42. import javax.swing.filechooser.FileFilter;
  43. import com.cognos.developer.schemas.bibus._3.ReportServiceReportSpecification;
  44. import com.cognos.developer.schemas.bibus._3.Specification;
  45. // This Java class extends the JFrame class so that you can
  46. // display a window.
  47. public class AddReportUI 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 sampleOptionButton;
  54. private JComboBox sampleOption;
  55. private static Logon sessionLogon;
  56. private static final String VALIDATE_SPEC = "Validate Specification";
  57. private static final String ADD_SPEC_TO_CM =
  58. "Add Specification To Content Store";
  59. private static String selectedSampleOption = VALIDATE_SPEC;
  60. private static String specificationFile = "";
  61. // This is the constructor.
  62. public AddReportUI(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 optionType[] = { VALIDATE_SPEC, ADD_SPEC_TO_CM };
  106. sampleOption = new JComboBox(optionType);
  107. sampleOption.setSelectedItem(null);
  108. sampleOption.addActionListener(new sampleOptionSelectionHandler());
  109. buttonPanel.add(sampleOption, BorderLayout.WEST);
  110. // Create and add the Button
  111. sampleOptionButton = new JButton("Run Option");
  112. sampleOptionButton.addActionListener(new allButtonsHandler());
  113. buttonPanel.add(sampleOptionButton, BorderLayout.EAST);
  114. // Add the status text pane.
  115. textAreaPane = new JTextArea();
  116. // Add the panels to the mainPanel
  117. mainPanel.add(cmURLPanel);
  118. mainPanel.add(buttonPanel);
  119. mainPanel.add(textAreaPane);
  120. //Add the ScrollPane to outputPanel
  121. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  122. areaScrollPane.setVerticalScrollBarPolicy(
  123. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  124. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  125. areaScrollPane.setBorder(
  126. BorderFactory.createCompoundBorder(
  127. BorderFactory.createCompoundBorder(
  128. BorderFactory.createTitledBorder("Output"),
  129. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  130. areaScrollPane.getBorder()));
  131. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  132. outputPanel.add(areaScrollPane);
  133. JPanel panel = new JPanel(new BorderLayout());
  134. panel.add(mainPanel, BorderLayout.NORTH);
  135. panel.add(outputPanel);
  136. setContentPane(panel);
  137. }
  138. private class MenuHandler implements ActionListener
  139. {
  140. public void actionPerformed(ActionEvent e)
  141. {
  142. if (e.getActionCommand().startsWith("http://"))
  143. {
  144. connect.connectionChange(e.getActionCommand());
  145. }
  146. try
  147. {
  148. JMenuItem menuClicked = (JMenuItem)e.getSource();
  149. if (menuClicked.getText() == "Exit")
  150. {
  151. System.exit(0);
  152. }
  153. if (menuClicked.getText() == "About")
  154. {
  155. JOptionPane.showMessageDialog(
  156. ((JMenuItem)e.getSource()).getParent(),
  157. "IBM Cognos Sample Application\n\n"
  158. + "Version 1.0.0\n"
  159. + "This application uses the IBM Cognos Software Development Kit",
  160. "About IBM Cognos Samples",
  161. JOptionPane.INFORMATION_MESSAGE,
  162. new ImageIcon("../Common/about.gif"));
  163. }
  164. if (menuClicked.getText().compareTo("Overview") == 0)
  165. {
  166. JFrame explainWindow =
  167. new JFrame("Overview for Add Report Sample");
  168. File explainFile = new File("Java_AddReportUI_Explain.html");
  169. if (! explainFile.exists())
  170. {
  171. JOptionPane.showMessageDialog(null, "Explain file not found");
  172. return;
  173. }
  174. URL explainURL =
  175. new URL("file:///" + explainFile.getAbsolutePath());
  176. JEditorPane explainPane = new JEditorPane();
  177. explainPane.setPage(explainURL);
  178. explainPane.setEditable(false);
  179. JScrollPane explainScroll =
  180. new JScrollPane(
  181. explainPane,
  182. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  183. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  184. explainWindow.getContentPane().add(explainScroll);
  185. explainWindow.setSize(640, 480);
  186. explainWindow.setVisible(true);
  187. }
  188. }
  189. catch (Exception ex)
  190. {}
  191. }
  192. }
  193. // The following is the button event handler.
  194. // Note: A SWITCH statement cannot be used here because we are comparing
  195. // objects.
  196. private class allButtonsHandler implements ActionListener
  197. {
  198. public void actionPerformed(ActionEvent e)
  199. {
  200. if (!Logon.loggedIn(connect))
  201. {
  202. try
  203. {
  204. sessionLogon.logon(connect);
  205. }
  206. catch (Exception logonException)
  207. {
  208. textAreaPane.setText("");
  209. textAreaPane.append("Login Failed. Please try again.\n");
  210. return;
  211. }
  212. }
  213. JButton buttonPressed = ((JButton)e.getSource());
  214. String output = "";
  215. if (buttonPressed == sampleOptionButton)
  216. {
  217. AddReport reportAdder = new AddReport();
  218. String reportSpecStr = "";
  219. final JFileChooser fc =
  220. new JFileChooser(System.getProperty("user.dir"));
  221. fc.setFileFilter(new XMLFileFilter());
  222. fc.setAcceptAllFileFilterUsed(false);
  223. int fileSelectedOK = fc.showOpenDialog(null);
  224. if (fileSelectedOK != JFileChooser.APPROVE_OPTION)
  225. {
  226. textAreaPane.setText("");
  227. textAreaPane.append(
  228. "Please select a Report Specification.\n");
  229. return;
  230. }
  231. specificationFile = fc.getSelectedFile().getName();
  232. if ((specificationFile == null)
  233. || (specificationFile.length() == 0))
  234. {
  235. textAreaPane.setText("");
  236. textAreaPane.append(
  237. "The selected file name ["
  238. + specificationFile
  239. + "] is not valid.");
  240. return;
  241. }
  242. //Read the contents of the file
  243. File theFile = fc.getSelectedFile();
  244. FileInputStream fileIS = null;
  245. try
  246. {
  247. fileIS = new FileInputStream(theFile);
  248. int readOK = 0;
  249. while (readOK >= 0)
  250. {
  251. byte inBytes[] = new byte[1024];
  252. readOK = fileIS.read(inBytes);
  253. if (readOK != -1)
  254. {
  255. reportSpecStr += new String(inBytes, 0, readOK);
  256. }
  257. }
  258. }
  259. catch (IOException ioEx)
  260. {
  261. ioEx.printStackTrace();
  262. return;
  263. }
  264. ReportServiceReportSpecification newReportSpec = new ReportServiceReportSpecification();
  265. newReportSpec.setValue(new Specification(reportSpecStr));
  266. try
  267. {
  268. if (selectedSampleOption.compareTo(VALIDATE_SPEC) == 0)
  269. {
  270. output =
  271. reportAdder.validateReportSpec(
  272. connect,
  273. newReportSpec);
  274. }
  275. else if (
  276. selectedSampleOption.compareTo(ADD_SPEC_TO_CM) == 0)
  277. {
  278. String reportName =
  279. JOptionPane.showInputDialog(
  280. "Please enter a name for the new report.");
  281. if (reportName != null)
  282. {
  283. output =
  284. reportAdder.addSpecToCM(
  285. connect,
  286. newReportSpec,
  287. reportName);
  288. }
  289. else
  290. {
  291. output = "Action cancelled.";
  292. }
  293. }
  294. }
  295. catch (java.rmi.RemoteException remoteEx)
  296. {
  297. output = "Exception Caught:\n" + remoteEx;
  298. remoteEx.printStackTrace(System.out);
  299. }
  300. }
  301. if (output.compareTo("") != 0)
  302. {
  303. textAreaPane.setText("");
  304. textAreaPane.append(output);
  305. }
  306. }
  307. }
  308. private class sampleOptionSelectionHandler implements ActionListener
  309. {
  310. public void actionPerformed(ActionEvent optionSelectedEvent)
  311. {
  312. selectedSampleOption = (String)sampleOption.getSelectedItem();
  313. }
  314. }
  315. private class XMLFileFilter extends FileFilter
  316. {
  317. public boolean accept(File f) {
  318. if (f.isDirectory()) {
  319. return true;
  320. }
  321. String fileExt = null;
  322. String fileName = f.getName();
  323. int i = fileName.lastIndexOf(".");
  324. if (i > 0 && i < fileName.length() - 1) {
  325. fileExt = fileName.substring(i+1).toLowerCase();
  326. }
  327. if (fileExt != null) {
  328. if ( fileExt.equals("xml") )
  329. {
  330. return true;
  331. }
  332. else
  333. {
  334. return false;
  335. }
  336. }
  337. return false;
  338. }
  339. public String getDescription()
  340. {
  341. return "Only xml files";
  342. }
  343. }
  344. // Create the main method to execute the application.
  345. public static void main(String args[])
  346. {
  347. CRNConnect connection = new CRNConnect();
  348. connection.connectToCognosServer();
  349. sessionLogon = new Logon();
  350. String output = "";
  351. while (!Logon.loggedIn(connection))
  352. {
  353. output = sessionLogon.logon(connection);
  354. if (!Logon.loggedIn(connection))
  355. {
  356. int retry =
  357. JOptionPane.showConfirmDialog(
  358. null,
  359. "Login Failed. Please try again.",
  360. "Login Failed",
  361. JOptionPane.OK_CANCEL_OPTION);
  362. if (retry != JOptionPane.OK_OPTION)
  363. {
  364. System.exit(0);
  365. }
  366. }
  367. }
  368. AddReportUI frame = new AddReportUI("IBM Cognos Sample", connection);
  369. // Create a WindowAdapter so the application
  370. // is exited when the window is closed.
  371. frame.addWindowListener(new WindowAdapter()
  372. {
  373. public void windowClosing(WindowEvent e)
  374. {
  375. System.exit(0);
  376. }
  377. });
  378. frame.textAreaPane.setText(output);
  379. // Set the size of the frame and display it.
  380. frame.setSize(780, 440);
  381. frame.setVisible(true);
  382. frame.setResizable(true);
  383. }
  384. }