QueryServiceUI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2011
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * QueryServiceUI.java
  10. *
  11. */
  12. import java.awt.BorderLayout;
  13. import java.awt.Dimension;
  14. import java.awt.GridBagConstraints;
  15. import java.awt.GridBagLayout;
  16. import java.awt.GridLayout;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.WindowAdapter;
  20. import java.awt.event.WindowEvent;
  21. import java.io.File;
  22. import java.net.URL;
  23. import javax.swing.BorderFactory;
  24. import javax.swing.ImageIcon;
  25. import javax.swing.JButton;
  26. import javax.swing.JComboBox;
  27. import javax.swing.JEditorPane;
  28. import javax.swing.JFrame;
  29. import javax.swing.JLabel;
  30. import javax.swing.JMenu;
  31. import javax.swing.JMenuBar;
  32. import javax.swing.JMenuItem;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTextArea;
  37. import javax.swing.JTextField;
  38. import com.cognos.developer.schemas.bibus._3.Dispatcher_Type;
  39. public class QueryServiceUI extends JFrame {
  40. private CRNConnect connect;
  41. private static QueryServiceUI frame;
  42. // The following variables represent the dialog components.
  43. private JTextArea textAreaPane;
  44. private JTextField cmURL;
  45. private JComboBox cubeSelection;
  46. private JButton runButton;
  47. private JComboBox queryServiceOptions;
  48. private static Logon sessionLogon;
  49. private static final String CUBE_GET_STATE = "Get Cube State";
  50. private static final String CUBE_START = "Start Cube";
  51. private static final String CUBE_STOP = "Stop Cube When Not In Use";
  52. private static final String CUBE_STOP_IMMEDIATE = "Stop Cube Immediately";
  53. private static final String CUBE_RESTART = "Restart Cube";
  54. private static final String CUBE_REFRESH_DATA = "Refresh Data Cache";
  55. private static final String CUBE_REFRESH_MEMBERS = "Refresh Member Cache";
  56. private static final String CUBE_REFRESH_SECURITY = "Refresh Cube Security";
  57. private static final int CUBE_ENUM_STATE = 1;
  58. private static final int CUBE_ENUM_START = 2;
  59. private static final int CUBE_ENUM_STOP = 3;
  60. private static final int CUBE_ENUM_STOP_IMMEDIATE = 4;
  61. private static final int CUBE_ENUM_RESTART = 5;
  62. private static final int CUBE_ENUM_DATA = 6;
  63. private static final int CUBE_ENUM_MEMBERS = 7;
  64. private static final int CUBE_ENUM_SECURITY = 8;
  65. private static int cubeActionType = 0;
  66. private BaseClassWrapper selectedCube = null;
  67. private String chosenMethod = null;
  68. public String cubeSearchPath_URL = null;
  69. QueryServiceTester newQSTester = new QueryServiceTester();
  70. // This is the constructor.
  71. public QueryServiceUI(String title, CRNConnect connection) {
  72. // Set the title of the frame, even before the variables are declared.
  73. super(title);
  74. connect = connection;
  75. addComponents();
  76. }
  77. // Add all components to the frame's panel.
  78. private void addComponents() {
  79. JMenuBar mBar = new JMenuBar();
  80. this.setJMenuBar(mBar);
  81. // declare menuItems
  82. JMenuItem exit;
  83. JMenuItem about;
  84. JMenuItem overview;
  85. // Add and populate the File menu.
  86. JMenu fileMenu = new JMenu("File");
  87. mBar.add(fileMenu);
  88. exit = new JMenuItem("Exit");
  89. fileMenu.add(exit);
  90. exit.addActionListener(new MenuHandler());
  91. // Add and populate the Help menu.
  92. JMenu helpMenu = new JMenu("Help");
  93. mBar.add(helpMenu);
  94. about = new JMenuItem("About");
  95. helpMenu.add(about);
  96. about.addActionListener(new MenuHandler());
  97. overview = new JMenuItem("Overview");
  98. helpMenu.add(overview);
  99. overview.addActionListener(new MenuHandler());
  100. JPanel mainPanel = createMainPanel();
  101. JPanel outputPanel = createOutputPanel();
  102. JPanel panel = new JPanel(new BorderLayout());
  103. panel.add(mainPanel, BorderLayout.NORTH);
  104. panel.add(outputPanel);
  105. setContentPane(panel);
  106. }
  107. private JPanel createOutputPanel() {
  108. // Add the status text pane.
  109. textAreaPane = new JTextArea();
  110. // Add the ScrollPane to outputPanel
  111. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  112. areaScrollPane
  113. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  114. areaScrollPane.setPreferredSize(new Dimension(500, 275));
  115. //Create the output panel and its layout objects
  116. GridBagLayout layout = new GridBagLayout(); //GridLayout(0, 1)
  117. JPanel outputNavPanel = new JPanel(layout);
  118. GridBagConstraints layoutConstraints = new GridBagConstraints();
  119. //Set the layout for the scroll pane and add it
  120. layoutConstraints.weightx = 1.0;
  121. layoutConstraints.weighty = 1.0;
  122. layoutConstraints.fill = GridBagConstraints.BOTH;
  123. layout.setConstraints(areaScrollPane, layoutConstraints);
  124. outputNavPanel.add(areaScrollPane);
  125. outputNavPanel.setBorder(BorderFactory.createCompoundBorder(
  126. BorderFactory.createCompoundBorder(BorderFactory
  127. .createTitledBorder("Output"), BorderFactory
  128. .createEmptyBorder(5, 5, 5, 5)), outputNavPanel
  129. .getBorder()));
  130. return outputNavPanel;
  131. }
  132. private JPanel createMainPanel() {
  133. // Add the URL text field and label
  134. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  135. cmURL.setText(CRNConnect.CM_URL);
  136. cmURL.setEditable(false);
  137. // Put together a panel for the URl
  138. JPanel cmURLPanel = new JPanel();
  139. cmURLPanel.add(new JLabel("Server URL:"));
  140. cmURLPanel.add(cmURL);
  141. BaseClassWrapper[] cubeList = newQSTester.getCubes(connect,
  142. "//rolapDataSource");
  143. cubeSelection = new JComboBox(cubeList);
  144. cubeSelection.setSelectedItem(cubeList[0]);
  145. cubeSelection.addActionListener(new CubeSearchPathHandler());
  146. // create cube search path panel
  147. JPanel cubePanel = new JPanel();
  148. cubePanel.add(new JLabel("Cube:"),
  149. BorderLayout.WEST);
  150. cubePanel.add(cubeSelection, BorderLayout.EAST);
  151. //get dropdown box and button panel
  152. JPanel optionButtonPanel = createMainButtonPanel();
  153. //create the main panel and add the components
  154. JPanel mainPanel = new JPanel(new GridLayout(3, 0));
  155. // Add the panels to the mainPanel
  156. mainPanel.add(cmURLPanel);
  157. mainPanel.add(cubePanel);
  158. mainPanel.add(optionButtonPanel);
  159. return mainPanel;
  160. }
  161. private JPanel createMainButtonPanel() {
  162. // Create the button Panel
  163. JPanel buttonPanel = new JPanel();
  164. // Create and add the package output type combo box
  165. String[] cubeOptions = { CUBE_GET_STATE, CUBE_START,
  166. CUBE_STOP, CUBE_STOP_IMMEDIATE, CUBE_RESTART,
  167. CUBE_REFRESH_DATA, CUBE_REFRESH_MEMBERS, CUBE_REFRESH_SECURITY };
  168. queryServiceOptions = new JComboBox(cubeOptions);
  169. queryServiceOptions.setSelectedItem(null);
  170. queryServiceOptions.addActionListener(new CubeSelectionHandler());
  171. buttonPanel.add(new JLabel("Query Service Actions:"), BorderLayout.WEST);
  172. buttonPanel.add(queryServiceOptions, BorderLayout.WEST);
  173. // Create and add the Button
  174. runButton = new JButton("Run Option");
  175. runButton.addActionListener(new allButtonsHandler());
  176. buttonPanel.add(runButton, BorderLayout.EAST);
  177. return buttonPanel;
  178. }
  179. // handle menu bar buttons
  180. private class MenuHandler implements ActionListener {
  181. public void actionPerformed(ActionEvent e) {
  182. if (e.getActionCommand().startsWith("http://")) {
  183. connect.connectionChange(e.getActionCommand());
  184. }
  185. try {
  186. JMenuItem menuClicked = (JMenuItem) e.getSource();
  187. if (menuClicked.getText() == "Exit") {
  188. System.exit(0);
  189. }
  190. if (menuClicked.getText() == "About") {
  191. JOptionPane.showMessageDialog(((JMenuItem) e.getSource())
  192. .getParent(), "IBM Cognos Sample Application\n\n"
  193. + "Version 1.0.0\n"
  194. + "This application uses the IBM Cognos Software Development Kit",
  195. "About IBM Cognos Samples",
  196. JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
  197. "../Common/about.gif"));
  198. }
  199. if (menuClicked.getText().compareTo("Overview") == 0) {
  200. JFrame explainWindow = new JFrame(
  201. "Overview for Query Service Sample");
  202. File explainFile = new File(
  203. "Java_QueryServiceUI_Explain.html");
  204. if (!explainFile.exists()) {
  205. JOptionPane.showMessageDialog(null,
  206. "Explain file not found");
  207. return;
  208. }
  209. URL explainURL = new URL("file:///"
  210. + explainFile.getAbsolutePath());
  211. JEditorPane explainPane = new JEditorPane();
  212. explainPane.setPage(explainURL);
  213. explainPane.setEditable(false);
  214. JScrollPane explainScroll = new JScrollPane(explainPane,
  215. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  216. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  217. explainWindow.getContentPane().add(explainScroll);
  218. explainWindow.setSize(640, 480);
  219. explainWindow.setVisible(true);
  220. }
  221. } catch (Exception ex) {
  222. }
  223. }
  224. }
  225. // The following is the button event handler.
  226. private class allButtonsHandler implements ActionListener {
  227. public void actionPerformed(ActionEvent e) {
  228. if (!Logon.loggedIn(connect)) {
  229. try {
  230. sessionLogon.logon(connect);
  231. } catch (Exception logonException) {
  232. System.out.println("The error: "
  233. + logonException.getMessage());
  234. }
  235. }
  236. JButton buttonPressed = ((JButton) e.getSource());
  237. // Click 'Run Option' button
  238. if (buttonPressed == runButton) {
  239. textAreaPane.setText("");
  240. BaseClassWrapper selectedCube =
  241. (BaseClassWrapper)cubeSelection.getSelectedItem();
  242. cubeSearchPath_URL = selectedCube.getSearchPath() ;
  243. if (cubeSearchPath_URL == null || cubeActionType == 0) {
  244. return;
  245. }
  246. textAreaPane.setText("");
  247. switch (cubeActionType) {
  248. case 1: // Get Cube State
  249. String cubeStateInfo = newQSTester.getCubeState(connect, selectedCube.toString());
  250. if (cubeStateInfo != null) {
  251. textAreaPane.setText("Get Cube State request result: ");
  252. textAreaPane.append("\n\n" + cubeStateInfo);
  253. }
  254. else {
  255. textAreaPane.setText("Failed to obtain cube state.");
  256. }
  257. break;
  258. case 2: // Start Cube
  259. String cubeRunRequestResult = newQSTester.startSingleCube(connect, selectedCube.toString());
  260. if (cubeRunRequestResult != null) {
  261. textAreaPane.setText("Start Cube request result: ");
  262. textAreaPane.append("\n\n" + cubeRunRequestResult);
  263. }
  264. else {
  265. textAreaPane.setText("Failed to run Start Request.");
  266. }
  267. break;
  268. case 3: // Stop Cube when not in use
  269. String cubeStopRequestResult = newQSTester.stopSingleCube(connect, selectedCube.toString(), false);
  270. if (cubeStopRequestResult != null) {
  271. textAreaPane.setText("Stop Cube request result: ");
  272. textAreaPane.append("\n\n" + cubeStopRequestResult);
  273. }
  274. else {
  275. textAreaPane.setText("Failed to run Stop Request.");
  276. }
  277. break;
  278. case 4: // Stop Cube Immediately
  279. String cubeForceStopRequestResult = newQSTester.stopSingleCube(connect, selectedCube.toString(), true);
  280. if (cubeForceStopRequestResult != null) {
  281. textAreaPane.setText("Stop Cube request result: ");
  282. textAreaPane.append("\n\n" + cubeForceStopRequestResult);
  283. }
  284. else {
  285. textAreaPane.setText("Failed to run Stop Immediately Request.");
  286. }
  287. break;
  288. case 5: // Restart Cube
  289. String cubeRestartRequestResult = newQSTester.restartSingleCube(connect, selectedCube.toString());
  290. if (cubeRestartRequestResult != null) {
  291. textAreaPane.setText("Restart Cube request result: ");
  292. textAreaPane.append("\n\n" + cubeRestartRequestResult);
  293. }
  294. else {
  295. textAreaPane.setText("Failed to run Restart Cube Request.");
  296. }
  297. break;
  298. case 6: // Refresh Data Cache
  299. String dataRefreshRequestResult = newQSTester.refreshDataCache(connect, selectedCube.toString());
  300. if (dataRefreshRequestResult != null) {
  301. textAreaPane.setText("Refresh Data Cache request result: ");
  302. textAreaPane.append("\n\n" + dataRefreshRequestResult);
  303. }
  304. else {
  305. textAreaPane.setText("Failed to run Refresh Data Request.");
  306. }
  307. break;
  308. case 7: // Refresh Member Cache
  309. String memberRefreshRequestResult = newQSTester.refreshMemberCache(connect, selectedCube.toString());
  310. if (memberRefreshRequestResult != null) {
  311. textAreaPane.setText("Refresh Member Cache request result: ");
  312. textAreaPane.append("\n\n" + memberRefreshRequestResult);
  313. }
  314. else {
  315. textAreaPane.setText("Failed to run Refresh Member Cache Request.");
  316. }
  317. break;
  318. case 8: // Refresh Security
  319. String securityRefreshRequestResult = newQSTester.refreshCubeSecurity(connect, selectedCube.toString());
  320. if (securityRefreshRequestResult != null) {
  321. textAreaPane.setText("Refresh Security request result: ");
  322. textAreaPane.append("\n\n" + securityRefreshRequestResult);
  323. }
  324. else {
  325. textAreaPane.setText("Failed to run Refresh Security Request.");
  326. }
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. // This is the the available cube combo box event handler
  333. private class CubeSearchPathHandler implements ActionListener {
  334. public void actionPerformed(ActionEvent cubeSearchPathEvent) {
  335. textAreaPane.setText("");
  336. }
  337. }
  338. // This is the cube option combo box event handler.
  339. private class CubeSelectionHandler implements ActionListener {
  340. public void actionPerformed(ActionEvent cubeSelectedEvent) {
  341. textAreaPane.setText("");
  342. chosenMethod = (String) queryServiceOptions.getSelectedItem();
  343. if (chosenMethod == CUBE_GET_STATE) {
  344. cubeActionType = CUBE_ENUM_STATE;
  345. } else if (chosenMethod == CUBE_START) {
  346. cubeActionType = CUBE_ENUM_START;
  347. } else if (chosenMethod == CUBE_STOP) {
  348. cubeActionType = CUBE_ENUM_STOP;
  349. } else if (chosenMethod == CUBE_STOP_IMMEDIATE) {
  350. cubeActionType = CUBE_ENUM_STOP_IMMEDIATE;
  351. } else if (chosenMethod == CUBE_RESTART) {
  352. cubeActionType = CUBE_ENUM_RESTART;
  353. } else if (chosenMethod == CUBE_REFRESH_DATA) {
  354. cubeActionType = CUBE_ENUM_DATA;
  355. } else if (chosenMethod == CUBE_REFRESH_MEMBERS) {
  356. cubeActionType = CUBE_ENUM_MEMBERS;
  357. } else if (chosenMethod == CUBE_REFRESH_SECURITY) {
  358. cubeActionType = CUBE_ENUM_SECURITY;
  359. }
  360. }
  361. }
  362. // Create the main method to execute the application.
  363. public static void main(String args[]) {
  364. CRNConnect connection = new CRNConnect();
  365. connection.connectToCognosServer();
  366. sessionLogon = new Logon();
  367. String logonOutput = "";
  368. while (!Logon.loggedIn(connection)) {
  369. logonOutput = sessionLogon.logon(connection);
  370. if (!Logon.loggedIn(connection)) {
  371. int retry = JOptionPane.showConfirmDialog(null,
  372. "Login Failed. Please try again.", "Login Failed",
  373. JOptionPane.OK_CANCEL_OPTION);
  374. if (retry != JOptionPane.OK_OPTION) {
  375. System.exit(0);
  376. }
  377. }
  378. }
  379. frame = new QueryServiceUI("IBM Cognos Sample", connection);
  380. // Create a WindowAdapter so the application
  381. // is exited when the window is closed.
  382. frame.addWindowListener(new WindowAdapter() {
  383. public void windowClosing(WindowEvent e) {
  384. System.exit(0);
  385. }
  386. });
  387. frame.textAreaPane.setText(logonOutput);
  388. // Set the size of the frame and display it.
  389. frame.setSize(750, 440);
  390. frame.setVisible(true);
  391. frame.setResizable(true);
  392. }
  393. }