AlertsUI.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. * AlertsUI.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.net.URL;
  24. import javax.swing.BorderFactory;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.JButton;
  27. import javax.swing.JEditorPane;
  28. import javax.swing.JComboBox;
  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. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  41. import com.cognos.developer.schemas.bibus._3.PropEnum;
  42. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  43. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  44. import com.cognos.developer.schemas.bibus._3.Sort;
  45. // This Java class extends the JFrame class so that you can
  46. // display a window.
  47. public class AlertsUI extends JFrame
  48. {
  49. private CRNConnect connect;
  50. private CSHandlers csh = new CSHandlers();
  51. // The following variables represent the dialog components.
  52. private JTextArea textAreaPane;
  53. private JTextField cmURL;
  54. private JButton notificationButton;
  55. private JComboBox actionTypeOption;
  56. private JComboBox reportSelectOption;
  57. private static Logon sessionLogon;
  58. private static final int REP_TYPE_ENUM_HTML = 0;
  59. private static int reportType = 0;
  60. private static final String ADD_NOTIFICATION = "Add Notification";
  61. private static final String DELETE_NOTIFICATION =
  62. "Delete Notification";
  63. private static final String CLEAR_NOTIFICATION = "Clear Notifications";
  64. private static final String DELETE_ALL_NOTIFICATION = "Delete All Notifications";
  65. private static final String QUERY_NOTIFICATION = "Query Notification";
  66. private static final int ADD_USER_TO_NOTIFICATION_LIST = 1;
  67. private static final int REMOVE_USER_FROM_NOTIFICATION_LIST = 2;
  68. private static final int REMOVE_USER_FROM_ALL_LISTS = 3;
  69. private static final int CLEAR_ALL_NOTIFICATIONS = 4;
  70. private static final int QUERY_NOTIFICATION_LIST = 5;
  71. private static int actionType = 1;
  72. private static BaseClassWrapper selectedReport = null;
  73. // This is the constructor.
  74. public AlertsUI(String title, CRNConnect connection)
  75. {
  76. // Set the title of the frame, even before the variables are declared.
  77. super(title);
  78. connect = connection;
  79. addComponents();
  80. }
  81. // Add all components to the frame's panel.
  82. private void addComponents()
  83. {
  84. JMenuBar mBar = new JMenuBar();
  85. this.setJMenuBar(mBar);
  86. //declare menuItems
  87. JMenuItem exit;
  88. JMenuItem about;
  89. JMenuItem overview;
  90. //Add and populate the File menu.
  91. JMenu fileMenu = new JMenu("File");
  92. mBar.add(fileMenu);
  93. exit = new JMenuItem("Exit");
  94. fileMenu.add(exit);
  95. exit.addActionListener(new MenuHandler());
  96. //Add and populate the Help menu.
  97. JMenu helpMenu = new JMenu("Help");
  98. mBar.add(helpMenu);
  99. about = new JMenuItem("About");
  100. helpMenu.add(about);
  101. about.addActionListener(new MenuHandler());
  102. overview = new JMenuItem("Overview");
  103. helpMenu.add(overview);
  104. overview.addActionListener(new MenuHandler());
  105. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  106. // create a cmURL panel
  107. JPanel cmURLPanel = new JPanel();
  108. // Add the URL text field and label
  109. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  110. cmURL.setText(CRNConnect.CM_URL);
  111. cmURL.setEditable(false);
  112. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  113. cmURLPanel.add(cmURL, BorderLayout.EAST);
  114. // Create the button Panel
  115. JPanel buttonPanel = new JPanel();
  116. //Create and add the report output type combo box
  117. String requestTypes[] = { ADD_NOTIFICATION, DELETE_NOTIFICATION, DELETE_ALL_NOTIFICATION, CLEAR_NOTIFICATION, QUERY_NOTIFICATION};
  118. actionTypeOption = new JComboBox(requestTypes);
  119. actionTypeOption.setSelectedItem(null);
  120. actionTypeOption.addActionListener(new AlertOptionSelectionHandler());
  121. buttonPanel.add(actionTypeOption, BorderLayout.WEST);
  122. // Create and add the select report combo box
  123. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  124. reportSelectOption = new JComboBox(listOfReports);
  125. reportSelectOption.setSelectedItem(null);
  126. reportSelectOption.addActionListener(new ReportSelectionHandler());
  127. buttonPanel.add(reportSelectOption, BorderLayout.CENTER);
  128. // Create and add the Button
  129. notificationButton = new JButton("Send Request");
  130. notificationButton.addActionListener(new allButtonsHandler());
  131. buttonPanel.add(notificationButton, BorderLayout.EAST);
  132. // Add the status text pane.
  133. textAreaPane = new JTextArea();
  134. // Add the panels to the mainPanel
  135. mainPanel.add(cmURLPanel);
  136. mainPanel.add(buttonPanel);
  137. mainPanel.add(textAreaPane);
  138. //Add the ScrollPane to outputPanel
  139. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  140. areaScrollPane.setVerticalScrollBarPolicy(
  141. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  142. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  143. areaScrollPane.setBorder(
  144. BorderFactory.createCompoundBorder(
  145. BorderFactory.createCompoundBorder(
  146. BorderFactory.createTitledBorder("Output"),
  147. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  148. areaScrollPane.getBorder()));
  149. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  150. outputPanel.add(areaScrollPane);
  151. JPanel panel = new JPanel(new BorderLayout());
  152. panel.add(mainPanel, BorderLayout.NORTH);
  153. panel.add(outputPanel);
  154. setContentPane(panel);
  155. }
  156. private class MenuHandler implements ActionListener
  157. {
  158. public void actionPerformed(ActionEvent e)
  159. {
  160. if (e.getActionCommand().startsWith("http://"))
  161. {
  162. connect.connectionChange(e.getActionCommand());
  163. }
  164. try
  165. {
  166. JMenuItem menuClicked = (JMenuItem)e.getSource();
  167. if (menuClicked.getText() == "Exit")
  168. {
  169. System.exit(0);
  170. }
  171. if (menuClicked.getText() == "About")
  172. {
  173. JOptionPane.showMessageDialog(
  174. ((JMenuItem)e.getSource()).getParent(),
  175. "IBM Cognos Sample Application\n\n"
  176. + "Version 1.0.0\n"
  177. + "This application uses the IBM Cognos Software Development Kit",
  178. "About IBM Cognos Samples",
  179. JOptionPane.INFORMATION_MESSAGE,
  180. new ImageIcon("../Common/about.gif"));
  181. }
  182. if (menuClicked.getText().compareTo("Overview") == 0)
  183. {
  184. JFrame explainWindow =
  185. new JFrame("Overview for Alerts Sample");
  186. File explainFile = new File("Java_AlertsUI_Explain.html");
  187. if (! explainFile.exists())
  188. {
  189. JOptionPane.showMessageDialog(null, "Explain file not found");
  190. return;
  191. }
  192. URL explainURL =
  193. new URL("file:///" + explainFile.getAbsolutePath());
  194. JEditorPane explainPane = new JEditorPane();
  195. explainPane.setPage(explainURL);
  196. explainPane.setEditable(false);
  197. JScrollPane explainScroll =
  198. new JScrollPane(
  199. explainPane,
  200. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  201. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  202. explainWindow.getContentPane().add(explainScroll);
  203. explainWindow.setSize(640, 480);
  204. explainWindow.setVisible(true);
  205. }
  206. }
  207. catch (Exception ex)
  208. {}
  209. }
  210. }
  211. // The following is the button event handler.
  212. // Note: A SWITCH statement cannot be used here because we are comparing
  213. // objects.
  214. private class allButtonsHandler implements ActionListener
  215. {
  216. public void actionPerformed(ActionEvent e)
  217. {
  218. if (!Logon.loggedIn(connect))
  219. {
  220. try
  221. {
  222. sessionLogon.logon(connect);
  223. }
  224. catch (Exception logonException)
  225. {}
  226. }
  227. JButton buttonPressed = ((JButton)e.getSource());
  228. String output = new String();
  229. if (buttonPressed == notificationButton)
  230. {
  231. ManageAlerts alertManager = new ManageAlerts();
  232. try
  233. {
  234. switch (actionType){
  235. case ADD_USER_TO_NOTIFICATION_LIST:
  236. output = alertManager.addNotificationForUser(connect, selectedReport);
  237. break;
  238. case REMOVE_USER_FROM_NOTIFICATION_LIST :
  239. output = alertManager.deleteSingleNotification(connect, selectedReport);
  240. break;
  241. case REMOVE_USER_FROM_ALL_LISTS :
  242. output = alertManager.deleteAllNotifications(connect);
  243. break;
  244. case CLEAR_ALL_NOTIFICATIONS :
  245. output = alertManager.clearNotifications(connect, selectedReport);
  246. break;
  247. case QUERY_NOTIFICATION_LIST :
  248. output = alertManager.queryNotification(connect, selectedReport);
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. System.out.println(ex.getMessage());
  254. output =
  255. "An error occurred.\nMake sure a "
  256. + "Report Name is selected and IBM Cognos is running";
  257. }
  258. }
  259. if (output.compareTo("") != 0)
  260. {
  261. textAreaPane.setText("");
  262. textAreaPane.append(output);
  263. }
  264. }
  265. }
  266. // This is the actionType combo box event handler.
  267. private class AlertOptionSelectionHandler implements ActionListener
  268. {
  269. public void actionPerformed(ActionEvent alertOptionSelectedEvent)
  270. {
  271. String chosenType = (String)actionTypeOption.getSelectedItem();
  272. if (chosenType == ADD_NOTIFICATION)
  273. {
  274. actionType = ADD_USER_TO_NOTIFICATION_LIST;
  275. }
  276. else if (chosenType == DELETE_NOTIFICATION)
  277. {
  278. actionType = REMOVE_USER_FROM_NOTIFICATION_LIST;
  279. }
  280. else if (chosenType == DELETE_ALL_NOTIFICATION)
  281. {
  282. actionType = REMOVE_USER_FROM_ALL_LISTS;
  283. }
  284. else if (chosenType == CLEAR_NOTIFICATION)
  285. {
  286. actionType = CLEAR_ALL_NOTIFICATIONS;
  287. }
  288. else if (chosenType == QUERY_NOTIFICATION)
  289. {
  290. actionType = QUERY_NOTIFICATION_LIST ;
  291. }
  292. else
  293. {
  294. //error, default to query notification
  295. actionType = QUERY_NOTIFICATION_LIST;
  296. }
  297. }
  298. }
  299. private class ReportSelectionHandler implements ActionListener
  300. {
  301. public void actionPerformed(ActionEvent repSelectedEvent)
  302. {
  303. selectedReport = (BaseClassWrapper)reportSelectOption.getSelectedItem();
  304. }
  305. }
  306. //This is a method for retrieving a list of the available reports for which to manage alerts
  307. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  308. {
  309. BaseClassWrapper reportList[] = null;
  310. BaseClass reports[] = new BaseClass[0];
  311. int reportIndex = 0;
  312. if (connection.getCMService() == null)
  313. {
  314. System.out.println(
  315. "Invalid parameter passed to getListOfReports()\n");
  316. return null;
  317. }
  318. PropEnum props[] =
  319. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  320. Sort sortOptions[] = { new Sort()};
  321. sortOptions[0].setOrder(OrderEnum.ascending);
  322. sortOptions[0].setPropName(PropEnum.defaultName);
  323. if (!Logon.loggedIn(connection))
  324. {
  325. try
  326. {
  327. sessionLogon.logon(connection);
  328. }
  329. catch (Exception logonException)
  330. {}
  331. }
  332. try
  333. {
  334. reports =
  335. connection.getCMService().query(
  336. new SearchPathMultipleObject("/content//report"),
  337. props,
  338. sortOptions,
  339. new QueryOptions());
  340. }
  341. catch (java.rmi.RemoteException remoteEx)
  342. {
  343. System.out.println("Caught Remote Exception:\n");
  344. remoteEx.printStackTrace();
  345. }
  346. reportList = new BaseClassWrapper[reports.length];
  347. if ((reports != null) && (reports.length > 0))
  348. {
  349. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  350. {
  351. reportList[reportIndex] = new BaseClassWrapper(reports[reportIndex]);
  352. }
  353. }
  354. return reportList;
  355. }
  356. // Create the main method to execute the application.
  357. public static void main(String args[])
  358. {
  359. CRNConnect connection = new CRNConnect();
  360. connection.connectToCognosServer();
  361. sessionLogon = new Logon();
  362. String output = "";
  363. while (!Logon.loggedIn(connection))
  364. {
  365. output = sessionLogon.logon(connection);
  366. if (!Logon.loggedIn(connection))
  367. {
  368. int retry =
  369. JOptionPane.showConfirmDialog(
  370. null,
  371. "Login Failed. Please try again.",
  372. "Login Failed",
  373. JOptionPane.OK_CANCEL_OPTION);
  374. if (retry != JOptionPane.OK_OPTION)
  375. {
  376. System.exit(0);
  377. }
  378. }
  379. }
  380. AlertsUI frame = new AlertsUI("IBM Cognos Sample", connection);
  381. // Create a WindowAdapter so the application
  382. // is exited when the window is closed.
  383. frame.addWindowListener(new WindowAdapter()
  384. {
  385. public void windowClosing(WindowEvent e)
  386. {
  387. System.exit(0);
  388. }
  389. });
  390. frame.textAreaPane.setText(output);
  391. // Set the size of the frame and display it.
  392. frame.setSize(850, 440);
  393. frame.setVisible(true);
  394. frame.setResizable(true);
  395. }
  396. }