EditReportSpecUI.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. * EditReportSpecUI.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.GridBagLayout;
  19. import java.awt.GridBagConstraints;
  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.util.Vector;
  25. import java.io.File;
  26. import java.net.URL;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JButton;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JEditorPane;
  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 com.cognos.developer.schemas.bibus._3.AsynchDetailReportObject;
  43. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  44. import com.cognos.developer.schemas.bibus._3.BaseClass;
  45. import com.cognos.developer.schemas.bibus._3.Option;
  46. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  47. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  48. import com.cognos.developer.schemas.bibus._3.PropEnum;
  49. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  50. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  51. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  52. import com.cognos.developer.schemas.bibus._3.Sort;
  53. // This Java class extends the JFrame class so that you can
  54. // display a window.
  55. public class EditReportSpecUI extends JFrame
  56. {
  57. private CRNConnect connect;
  58. private CSHandlers csh = new CSHandlers();
  59. // The following variables represent the dialog components.
  60. private JTextArea textAreaPane;
  61. private JTextField cmURL;
  62. private JTextField selectedSearchPath;
  63. private JButton editReportSpecButton;
  64. private JComboBox repSelectOption;
  65. private static Logon sessionLogon;
  66. private static BaseClassWrapper selectedReport = null;
  67. // This is the constructor.
  68. public EditReportSpecUI(String title, CRNConnect connection)
  69. {
  70. // Set the title of the frame, even before the variables are declared.
  71. super(title);
  72. connect = connection;
  73. addComponents();
  74. }
  75. // Add all components to the frame's panel.
  76. private void addComponents()
  77. {
  78. JMenuBar mBar = new JMenuBar();
  79. this.setJMenuBar(mBar);
  80. //declare menuItems
  81. JMenuItem exit;
  82. JMenuItem about;
  83. JMenuItem overview;
  84. //Add and populate the File menu.
  85. JMenu fileMenu = new JMenu("File");
  86. mBar.add(fileMenu);
  87. exit = new JMenuItem("Exit");
  88. fileMenu.add(exit);
  89. exit.addActionListener(new MenuHandler());
  90. //Add and populate the Help menu.
  91. JMenu helpMenu = new JMenu("Help");
  92. mBar.add(helpMenu);
  93. about = new JMenuItem("About");
  94. helpMenu.add(about);
  95. about.addActionListener(new MenuHandler());
  96. overview = new JMenuItem("Overview");
  97. helpMenu.add(overview);
  98. overview.addActionListener(new MenuHandler());
  99. JPanel mainPanel = createMainPanel();
  100. JPanel outputNavPanel = createOutputPanel();
  101. JPanel panel = new JPanel(new BorderLayout());
  102. panel.add(mainPanel, BorderLayout.NORTH);
  103. panel.add(outputNavPanel);
  104. setContentPane(panel);
  105. }
  106. private JPanel createOutputPanel()
  107. {
  108. //Create the html scrollPane
  109. textAreaPane = new JTextArea();
  110. JScrollPane textScrollPane = new JScrollPane(textAreaPane);
  111. textScrollPane.setVerticalScrollBarPolicy(
  112. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  113. textScrollPane.setPreferredSize(new Dimension(500, 275));
  114. //Create the output panel and it's layout objects
  115. GridBagLayout layout = new GridBagLayout();
  116. JPanel outputNavPanel = new JPanel(layout);
  117. GridBagConstraints layoutConstraints = new GridBagConstraints();
  118. //Set the layout for the scroll pane and add it
  119. layoutConstraints.weightx = 1.0;
  120. layoutConstraints.weighty = 1.0;
  121. layoutConstraints.fill = GridBagConstraints.BOTH;
  122. layout.setConstraints(textScrollPane, layoutConstraints);
  123. outputNavPanel.add(textScrollPane);
  124. //put a border around the output and nav buttons
  125. outputNavPanel.setBorder(
  126. BorderFactory.createCompoundBorder(
  127. BorderFactory.createCompoundBorder(
  128. BorderFactory.createTitledBorder("Output"),
  129. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  130. outputNavPanel.getBorder()));
  131. return outputNavPanel;
  132. }
  133. private JPanel createMainPanel()
  134. {
  135. // Add the URL text field and label
  136. cmURL = new JTextField(CRNConnect.CM_URL.length() - 10);
  137. cmURL.setText(CRNConnect.CM_URL);
  138. cmURL.setEditable(false);
  139. //Put together a panel for the URL
  140. JPanel cmURLPanel = new JPanel();
  141. cmURLPanel.add(new JLabel("Server URL:"));
  142. cmURLPanel.add(cmURL);
  143. // Create the searchPath text field and label
  144. selectedSearchPath = new JTextField(CRNConnect.CM_URL.length() - 10); //same as above
  145. selectedSearchPath.setText("");
  146. selectedSearchPath.setEditable(false);
  147. selectedSearchPath.setAutoscrolls(true);
  148. //Put together a panel for the search path
  149. JPanel searchPathPanel = new JPanel();
  150. //searchPathPanel.add(new JLabel("SearchPath:"));
  151. searchPathPanel.add(selectedSearchPath);
  152. //get the button panel
  153. JPanel buttonPanel = createMainButtonPanel();
  154. //
  155. // create the main panel and add the components
  156. //
  157. JPanel mainPanel = new JPanel(new GridLayout(3,0));
  158. // Add everything to the main panel
  159. mainPanel.add(cmURLPanel);
  160. mainPanel.add(buttonPanel);
  161. mainPanel.add(searchPathPanel);
  162. return mainPanel;
  163. }
  164. private JPanel createMainButtonPanel()
  165. {
  166. // Create the button Panel
  167. JPanel buttonPanel = new JPanel();
  168. // Create and add the select report combo box
  169. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  170. repSelectOption = new JComboBox(listOfReports);
  171. repSelectOption.setSelectedItem(null);
  172. repSelectOption.addActionListener(new ReportSelectionHandler());
  173. buttonPanel.add(repSelectOption, BorderLayout.CENTER);
  174. // Create and add the Button
  175. editReportSpecButton = new JButton("Edit Report");
  176. editReportSpecButton.addActionListener(new allButtonsHandler());
  177. buttonPanel.add(editReportSpecButton, BorderLayout.EAST);
  178. return buttonPanel;
  179. }
  180. private class MenuHandler implements ActionListener
  181. {
  182. public void actionPerformed(ActionEvent e)
  183. {
  184. if (e.getActionCommand().startsWith("http://"))
  185. {
  186. connect.connectionChange(e.getActionCommand());
  187. }
  188. try
  189. {
  190. JMenuItem menuClicked = (JMenuItem)e.getSource();
  191. if (menuClicked.getText() == "Exit")
  192. {
  193. System.exit(0);
  194. }
  195. if (menuClicked.getText() == "About")
  196. {
  197. JOptionPane.showMessageDialog(
  198. ((JMenuItem)e.getSource()).getParent(),
  199. "IBM Cognos Sample Application\n\n"
  200. + "Version 1.0.0\n"
  201. + "This application uses the IBM Cognos Software Development Kit",
  202. "About IBM Cognos Samples",
  203. JOptionPane.INFORMATION_MESSAGE,
  204. new ImageIcon("../Common/about.gif"));
  205. }
  206. if (menuClicked.getText().compareTo("Overview") == 0)
  207. {
  208. JFrame explainWindow =
  209. new JFrame("Overview for Edit Report Specification Sample");
  210. File explainFile = new File("Java_EditReportSpecUI_Explain.html");
  211. if (! explainFile.exists())
  212. {
  213. JOptionPane.showMessageDialog(null, "Explain file not found");
  214. return;
  215. }
  216. URL explainURL =
  217. new URL("file:///" + explainFile.getAbsolutePath());
  218. JEditorPane explainPane = new JEditorPane();
  219. explainPane.setPage(explainURL);
  220. explainPane.setEditable(false);
  221. JScrollPane explainScroll =
  222. new JScrollPane(
  223. explainPane,
  224. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  225. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  226. explainWindow.getContentPane().add(explainScroll);
  227. explainWindow.setSize(640, 480);
  228. explainWindow.setVisible(true);
  229. }
  230. }
  231. catch (Exception ex)
  232. {}
  233. }
  234. }
  235. // The following is the button event handler.
  236. // Note: A SWITCH statement cannot be used here because we are comparing
  237. // objects.
  238. private class allButtonsHandler implements ActionListener
  239. {
  240. public void actionPerformed(ActionEvent e)
  241. {
  242. if (!Logon.loggedIn(connect))
  243. {
  244. try
  245. {
  246. sessionLogon.logon(connect);
  247. }
  248. catch (Exception logonException)
  249. {}
  250. }
  251. JButton buttonPressed = ((JButton)e.getSource());
  252. String output = new String();
  253. if (buttonPressed == editReportSpecButton)
  254. {
  255. try
  256. {
  257. EditReportSpec editReport = new EditReportSpec();
  258. String reportSpec =
  259. editReport.getReportSpec(connect, selectedReport);
  260. if ((reportSpec == null) || (reportSpec == ""))
  261. {
  262. output = "Edit Report:\nAn error occurred retrieving the specification for the selected report.";
  263. }
  264. else
  265. {
  266. output = editSpec(connect, selectedReport, reportSpec);
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. System.out.println(ex.getMessage());
  272. ex.printStackTrace();
  273. output =
  274. "Edit Report:\nAn error occurred\nMake sure a "
  275. + "Report Name is selected and IBM Cognos is running";
  276. }
  277. }
  278. if (output.compareTo("") != 0)
  279. {
  280. textAreaPane.setText("");
  281. textAreaPane.append(output);
  282. }
  283. }
  284. }
  285. private class ReportSelectionHandler implements ActionListener
  286. {
  287. public void actionPerformed(ActionEvent repSelectedEvent)
  288. {
  289. selectedReport = (BaseClassWrapper) repSelectOption.getSelectedItem();
  290. if (selectedReport != null)
  291. {
  292. selectedSearchPath.setText(selectedReport.getBaseClassObject().getSearchPath().getValue());
  293. return;
  294. }
  295. selectedSearchPath.setText("");
  296. }
  297. }
  298. //This is a method for retrieving a list of the available reports to run
  299. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  300. {
  301. BaseClassWrapper reportList[] = null;
  302. BaseClass reports[] = new BaseClass[0];
  303. int reportListIndex = 0;
  304. int reportIndex = 0;
  305. if (connection.getCMService() == null)
  306. {
  307. System.out.println(
  308. "Invalid parameter passed to getListOfReports()\n");
  309. return null;
  310. }
  311. PropEnum props[] =
  312. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName, PropEnum.parent };
  313. Sort sortOptions[] = { new Sort()};
  314. sortOptions[0].setOrder(OrderEnum.ascending);
  315. sortOptions[0].setPropName(PropEnum.defaultName);
  316. if (!Logon.loggedIn(connection))
  317. {
  318. try
  319. {
  320. sessionLogon.logon(connection);
  321. }
  322. catch (Exception logonException)
  323. {}
  324. }
  325. try
  326. {
  327. reports =
  328. connection.getCMService().query(
  329. new SearchPathMultipleObject("/content//report"),
  330. props,
  331. sortOptions,
  332. new QueryOptions());
  333. }
  334. catch (java.rmi.RemoteException remoteEx)
  335. {
  336. System.out.println("Caught Remote Exception:\n");
  337. remoteEx.printStackTrace();
  338. }
  339. reportList = new BaseClassWrapper[reports.length];
  340. if ((reports != null) && (reports.length > 0))
  341. {
  342. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  343. {
  344. reportList[reportListIndex++] = new BaseClassWrapper(reports[reportIndex]);
  345. }
  346. }
  347. return reportList;
  348. }
  349. /**
  350. * This java method is used to display a simple dialog box for
  351. * removing column and adding columns to a simple list report.
  352. * There is also a button to modify a column title.
  353. *
  354. * @param connection Connection to Server.
  355. * @param report Specifies a report object.
  356. * @param reportSpec Specifies a report specification XML document.
  357. */
  358. public String editSpec(CRNConnect connection, BaseClassWrapper oldReport, String reportSpec)
  359. {
  360. String output = "";
  361. String packageName = getPackageSearchPath(connection, oldReport);
  362. // Create a new report object which uses DOM to traverse the
  363. // report specification.
  364. ReportObject newReport =
  365. new ReportObject(connect, packageName, reportSpec);
  366. // Use the existing report specification to extract
  367. // all necessary information.
  368. // Get column names, expressions and titles
  369. Vector selectedDisplayColumns = newReport.getColumnNames();
  370. Vector selectedColumnExpression = newReport.getColumnExpressions();
  371. Vector selectedColumnTitles = newReport.getColumnTitles();
  372. // Keep the original list of columns, expression, and titles
  373. // for comparision after changes are made.
  374. Vector originalColumnExpression =
  375. (Vector)selectedColumnExpression.clone();
  376. Vector originalColumnReference = (Vector)selectedDisplayColumns.clone();
  377. Vector originalColumnTitles = (Vector)selectedColumnTitles.clone();
  378. Vector columnTitleToChange = new Vector();
  379. Vector columnTitleNewName = new Vector();
  380. // Return the existing metadata of the package to display
  381. // in the Report Wizard dialog box.
  382. MetaData md = new MetaData();
  383. Vector packageMetaData = md.parseMetaData(connection, newReport, packageName);
  384. // Get the package metadeta and the columns already in the report.
  385. ReportWizardDialog dialog =
  386. new ReportWizardDialog(
  387. null,
  388. "Report Wizard",
  389. true,
  390. packageMetaData,
  391. selectedDisplayColumns,
  392. selectedColumnExpression,
  393. selectedColumnTitles,
  394. columnTitleToChange,
  395. columnTitleNewName,
  396. true);
  397. dialog.setVisible(true);
  398. // Modify the column titles.
  399. newReport.modifyColumnTitles(columnTitleToChange, columnTitleNewName);
  400. // Compare the list of original columns to the list of new ones
  401. // and add or remove columns where appropriate.
  402. newReport.modifyColumns(
  403. originalColumnExpression,
  404. originalColumnReference,
  405. originalColumnTitles,
  406. selectedColumnExpression,
  407. selectedColumnTitles);
  408. // Add the xmlns namespace back into the report specification
  409. // before updating Content Manager.
  410. newReport.updateReportNS();
  411. newReport.saveReport(connection, oldReport.getBaseClassObject().getParent().getValue()[0], oldReport.toString());
  412. newReport.renderReport(connection, oldReport.getBaseClassObject().getSearchPath().getValue());
  413. output = "Report: " + oldReport + " updated successfully";
  414. return output;
  415. }
  416. public String getPackageSearchPath(CRNConnect connection, BaseClassWrapper report){
  417. String reportSearchPath = report.getSearchPath();
  418. String packagePath = "";
  419. try{
  420. AsynchReply response = connection.getReportService().query(new SearchPathSingleObject(reportSearchPath), new ParameterValue[] {}, new Option[] {});
  421. for (int i = 0; i < response.getDetails().length; i++)
  422. {
  423. if (response.getDetails()[i] instanceof AsynchDetailReportObject)
  424. {
  425. packagePath = ((AsynchDetailReportObject)response.getDetails()[i]).getReport().getMetadataModel().getValue()[0].getSearchPath().getValue();
  426. }
  427. }
  428. return packagePath;
  429. }
  430. catch (Exception e)
  431. {
  432. System.out.println(
  433. "An error occurred in the getPackageSearchPath Java method.\n" + e);
  434. return "An error occurred in the getPackageSearchPath Java method.";
  435. }
  436. // return packagePath;
  437. }
  438. // Create the main method to execute the application.
  439. public static void main(String args[])
  440. {
  441. CRNConnect connection = new CRNConnect();
  442. connection.connectToCognosServer();
  443. sessionLogon = new Logon();
  444. String output = "";
  445. while (!Logon.loggedIn(connection))
  446. {
  447. output = sessionLogon.logon(connection);
  448. if (!Logon.loggedIn(connection))
  449. {
  450. int retry =
  451. JOptionPane.showConfirmDialog(
  452. null,
  453. "Login Failed. Please try again.",
  454. "Login Failed",
  455. JOptionPane.OK_CANCEL_OPTION);
  456. if (retry != JOptionPane.OK_OPTION)
  457. {
  458. System.exit(0);
  459. }
  460. }
  461. }
  462. EditReportSpecUI frame = new EditReportSpecUI("IBM Cognos Sample", connection);
  463. // Create a WindowAdapter so the application
  464. // is exited when the window is closed.
  465. frame.addWindowListener(new WindowAdapter()
  466. {
  467. public void windowClosing(WindowEvent e)
  468. {
  469. System.exit(0);
  470. }
  471. });
  472. frame.textAreaPane.setText(output);
  473. // Set the size of the frame and display it.
  474. frame.setSize(640, 480);
  475. frame.setVisible(true);
  476. frame.setResizable(true);
  477. }
  478. }