CapabilitiesUI.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. * CapabilitiesUI.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.JComboBox;
  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. 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. import com.cognos.developer.schemas.bibus._3.UserCapabilityEnum;
  46. // This Java class extends the JFrame class so that you can
  47. // display a window.
  48. public class CapabilitiesUI extends JFrame
  49. {
  50. private CRNConnect connect;
  51. // The following variables represent the dialog components.
  52. private JTextArea textAreaPane;
  53. private JTextField cmURL;
  54. private JButton capabilityButton;
  55. private JComboBox secObjSelectOption;
  56. private JComboBox packSelectOption;
  57. private static Logon sessionLogon;
  58. private static BaseClassWrapper selectedSecurityObject = null;
  59. private static BaseClassWrapper selectedPackage = null;
  60. //Set capability to update to CanUseReportStudio
  61. private static String secFuncPath =
  62. "/capability/securedFunction[@name='Report Studio']";
  63. private static UserCapabilityEnum capToUpdate = UserCapabilityEnum.canUseReportStudio;
  64. // This is the constructor.
  65. public CapabilitiesUI(String title, CRNConnect connection)
  66. {
  67. // Set the title of the frame, even before the variables are declared.
  68. super(title);
  69. connect = connection;
  70. addComponents();
  71. }
  72. // Add all components to the frame's panel.
  73. private void addComponents()
  74. {
  75. JMenuBar mBar = new JMenuBar();
  76. this.setJMenuBar(mBar);
  77. //declare menuItems
  78. JMenuItem exit;
  79. JMenuItem about;
  80. JMenuItem overview;
  81. //Add and populate the File menu.
  82. JMenu fileMenu = new JMenu("File");
  83. mBar.add(fileMenu);
  84. exit = new JMenuItem("Exit");
  85. fileMenu.add(exit);
  86. exit.addActionListener(new MenuHandler());
  87. //Add and populate the Help menu.
  88. JMenu helpMenu = new JMenu("Help");
  89. mBar.add(helpMenu);
  90. about = new JMenuItem("About");
  91. helpMenu.add(about);
  92. about.addActionListener(new MenuHandler());
  93. overview = new JMenuItem("Overview");
  94. helpMenu.add(overview);
  95. overview.addActionListener(new MenuHandler());
  96. // Create the select security object and packages combo boxes
  97. BaseClassWrapper listOfPackages[] = getListOfPackages(connect);
  98. BaseClassWrapper listOfSecObjects[] = getListOfSecObjects(connect);
  99. packSelectOption = new JComboBox(listOfPackages);
  100. packSelectOption.setSelectedItem(null);
  101. packSelectOption.addActionListener(new PackageSelectionHandler());
  102. secObjSelectOption = new JComboBox(listOfSecObjects);
  103. secObjSelectOption.setSelectedItem(null);
  104. secObjSelectOption.addActionListener(new SecObjSelectionHandler());
  105. // create a cmURL panel, Account panel, Package panel
  106. JPanel cmURLPanel = new JPanel();
  107. JPanel accountPanel = new JPanel();
  108. JPanel packagePanel = new JPanel();
  109. // Add the URL text field and label
  110. cmURL = new JTextField(CRNConnect.CM_URL.length()-10);
  111. cmURL.setText(CRNConnect.CM_URL);
  112. cmURL.setEditable(false);
  113. // Create the Button and Button Panel
  114. capabilityButton = new JButton("Set Capabilities");
  115. capabilityButton.addActionListener(new allButtonsHandler());
  116. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  117. cmURLPanel.add(cmURL, BorderLayout.CENTER);
  118. cmURLPanel.add(capabilityButton, BorderLayout.EAST);
  119. //set up account panel, package panel
  120. accountPanel.add(new JLabel("Account or Role:"), BorderLayout.WEST);
  121. accountPanel.add(secObjSelectOption, BorderLayout.CENTER);
  122. packagePanel.add(new JLabel("Package:"), BorderLayout.WEST);
  123. packagePanel.add(packSelectOption, BorderLayout.CENTER);
  124. //set up the command panel
  125. JPanel commandPane = new JPanel(new BorderLayout());
  126. commandPane.add(cmURLPanel, BorderLayout.NORTH);
  127. commandPane.add(accountPanel, BorderLayout.CENTER);
  128. commandPane.add(packagePanel, BorderLayout.SOUTH);
  129. // Add the status text pane.
  130. textAreaPane = new JTextArea();
  131. //Add the ScrollPane to outputPanel
  132. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  133. areaScrollPane.setVerticalScrollBarPolicy(
  134. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  135. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  136. areaScrollPane.setBorder(
  137. BorderFactory.createCompoundBorder(
  138. BorderFactory.createCompoundBorder(
  139. BorderFactory.createTitledBorder("Output"),
  140. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  141. areaScrollPane.getBorder()));
  142. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  143. outputPanel.add(areaScrollPane);
  144. JPanel panel = new JPanel(new BorderLayout());
  145. panel.add(commandPane, BorderLayout.NORTH);
  146. panel.add(outputPanel, BorderLayout.CENTER);
  147. setContentPane(panel);
  148. }
  149. private class MenuHandler implements ActionListener
  150. {
  151. public void actionPerformed(ActionEvent e)
  152. {
  153. if (e.getActionCommand().startsWith("http://"))
  154. {
  155. connect.connectionChange(e.getActionCommand());
  156. }
  157. try
  158. {
  159. JMenuItem menuClicked = (JMenuItem)e.getSource();
  160. if (menuClicked.getText() == "Exit")
  161. {
  162. System.exit(0);
  163. }
  164. if (menuClicked.getText() == "About")
  165. {
  166. JOptionPane.showMessageDialog(
  167. ((JMenuItem)e.getSource()).getParent(),
  168. "IBM Cognos Sample Application\n\n"
  169. + "Version 1.0.0\n"
  170. + "This application uses the IBM Cognos Software Development Kit",
  171. "About IBM Cognos Samples",
  172. JOptionPane.INFORMATION_MESSAGE,
  173. new ImageIcon("../Common/about.gif"));
  174. }
  175. if (menuClicked.getText().compareTo("Overview") == 0)
  176. {
  177. JFrame explainWindow =
  178. new JFrame("Overview for Capabilities Sample");
  179. File explainFile = new File("Java_CapabilitiesGUI_Explain.html");
  180. if (! explainFile.exists())
  181. {
  182. JOptionPane.showMessageDialog(null, "Explain file not found");
  183. return;
  184. }
  185. URL explainURL =
  186. new URL("file:///" + explainFile.getAbsolutePath());
  187. JEditorPane explainPane = new JEditorPane();
  188. explainPane.setPage(explainURL);
  189. explainPane.setEditable(false);
  190. JScrollPane explainScroll =
  191. new JScrollPane(
  192. explainPane,
  193. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  194. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  195. explainWindow.getContentPane().add(explainScroll);
  196. explainWindow.setSize(640, 480);
  197. explainWindow.setVisible(true);
  198. }
  199. }
  200. catch (Exception ex)
  201. {}
  202. }
  203. }
  204. // The following is the button event handler.
  205. // Note: A SWITCH statement cannot be used here because we are comparing
  206. // objects.
  207. private class allButtonsHandler implements ActionListener
  208. {
  209. public void actionPerformed(ActionEvent e)
  210. {
  211. if (!Logon.loggedIn(connect))
  212. {
  213. try
  214. {
  215. sessionLogon.logon(connect);
  216. }
  217. catch (Exception logonException)
  218. {}
  219. }
  220. JButton buttonPressed = ((JButton)e.getSource());
  221. String output = new String();
  222. if (buttonPressed == capabilityButton)
  223. {
  224. if(selectedSecurityObject != null & selectedPackage != null){
  225. Capabilities capability = new Capabilities();
  226. output = capability.updateSecuredFunction(connect, selectedSecurityObject, selectedPackage, secFuncPath, capToUpdate);
  227. }
  228. else output = "Security Object and Package not selected.";
  229. }
  230. if (output.compareTo("") != 0)
  231. {
  232. textAreaPane.setText("");
  233. textAreaPane.append(output);
  234. }
  235. }
  236. }
  237. private class PackageSelectionHandler implements ActionListener
  238. {
  239. public void actionPerformed(ActionEvent repSelectedEvent)
  240. {
  241. selectedPackage = (BaseClassWrapper) packSelectOption.getSelectedItem();
  242. }
  243. }
  244. private class SecObjSelectionHandler implements ActionListener
  245. {
  246. public void actionPerformed(ActionEvent repSelectedEvent)
  247. {
  248. selectedSecurityObject = (BaseClassWrapper) secObjSelectOption.getSelectedItem();
  249. }
  250. }
  251. //This is a method for retrieving a list of the available packages
  252. protected BaseClassWrapper[] getListOfPackages(CRNConnect connection)
  253. {
  254. BaseClassWrapper packagesList[] = null;
  255. BaseClass packages[] = new BaseClass[0];
  256. int packageIndex = 0;
  257. if (connection == null)
  258. {
  259. System.out.println(
  260. "Invalid parameter passed to getListOfPackages()\n");
  261. return null;
  262. }
  263. // The userCapabilityPolicies property specifies which users, groups, or
  264. // roles have specific capabilities for the package.
  265. // The effectiveUserCapabilities property specifies what capabilities the
  266. // current user has for the package and at the global level
  267. PropEnum props[] =
  268. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName, PropEnum.userCapabilityPolicies, PropEnum.effectiveUserCapabilities };
  269. Sort sortOptions[] = { new Sort()};
  270. sortOptions[0].setOrder(OrderEnum.ascending);
  271. sortOptions[0].setPropName(PropEnum.defaultName);
  272. if (!Logon.loggedIn(connect))
  273. {
  274. try
  275. {
  276. sessionLogon.logon(connect);
  277. }
  278. catch (Exception logonException)
  279. {}
  280. }
  281. try
  282. {
  283. SearchPathMultipleObject packagesPath = new SearchPathMultipleObject();
  284. packagesPath.set_value("content//package");
  285. packages =
  286. connection.getCMService().query(
  287. packagesPath,
  288. props,
  289. sortOptions,
  290. new QueryOptions());
  291. }
  292. catch (java.rmi.RemoteException remoteEx)
  293. {
  294. System.out.println("Caught Remote Exception:\n");
  295. remoteEx.printStackTrace();
  296. }
  297. packagesList = new BaseClassWrapper[packages.length];
  298. if ((packages != null) && (packages.length > 0))
  299. {
  300. for (packageIndex = 0; packageIndex < packages.length; packageIndex++)
  301. {
  302. packagesList[packageIndex] = new BaseClassWrapper(packages[packageIndex]);
  303. }
  304. }
  305. return packagesList;
  306. }
  307. //This is a method for retrieving a list of the available accounts and roles
  308. protected BaseClassWrapper[] getListOfSecObjects(CRNConnect connection)
  309. {
  310. BaseClassWrapper secObjectList[] = null;
  311. BaseClass accounts[] = new BaseClass[0];
  312. BaseClass roles[] = new BaseClass[0];
  313. int secObjectIndex = 0;
  314. int accountIndex = 0;
  315. int roleIndex = 0;
  316. if (connection == null)
  317. {
  318. System.out.println(
  319. "Invalid parameter passed to getListOfSecObjects()\n");
  320. return null;
  321. }
  322. PropEnum props[] =
  323. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  324. Sort sortOptions[] = { new Sort()};
  325. sortOptions[0].setOrder(OrderEnum.ascending);
  326. sortOptions[0].setPropName(PropEnum.defaultName);
  327. if (!Logon.loggedIn(connect))
  328. {
  329. try
  330. {
  331. sessionLogon.logon(connect);
  332. }
  333. catch (Exception logonException)
  334. {}
  335. }
  336. try
  337. {
  338. SearchPathMultipleObject accountsPath = new SearchPathMultipleObject();
  339. accountsPath.set_value("//account");
  340. SearchPathMultipleObject rolesPath = new SearchPathMultipleObject();
  341. rolesPath.set_value("//role");
  342. accounts =
  343. connection.getCMService().query(
  344. accountsPath,
  345. props,
  346. sortOptions,
  347. new QueryOptions());
  348. roles =
  349. connection.getCMService().query(
  350. rolesPath,
  351. props,
  352. sortOptions,
  353. new QueryOptions());
  354. }
  355. catch (java.rmi.RemoteException remoteEx)
  356. {
  357. System.out.println("Caught Remote Exception:\n");
  358. remoteEx.printStackTrace();
  359. }
  360. secObjectList = new BaseClassWrapper[accounts.length + roles.length];
  361. if ((accounts != null) && (accounts.length > 0))
  362. {
  363. for (accountIndex = 0; accountIndex < accounts.length; accountIndex++)
  364. {
  365. secObjectList[secObjectIndex++] = new BaseClassWrapper(accounts[accountIndex]);
  366. }
  367. }
  368. if ((roles != null) && (roles.length > 0))
  369. {
  370. for (roleIndex = 0; roleIndex < roles.length; roleIndex++)
  371. {
  372. secObjectList[secObjectIndex++] =
  373. new BaseClassWrapper(roles[roleIndex]);
  374. }
  375. }
  376. return secObjectList;
  377. }
  378. // Create the main method to execute the application.
  379. public static void main(String args[])
  380. {
  381. CRNConnect connection = new CRNConnect();
  382. connection.connectToCognosServer();
  383. sessionLogon = new Logon();
  384. String output = "";
  385. while (!Logon.loggedIn(connection))
  386. {
  387. output = sessionLogon.logon(connection);
  388. if (!Logon.loggedIn(connection))
  389. {
  390. int retry =
  391. JOptionPane.showConfirmDialog(
  392. null,
  393. "Login Failed. Please try again.",
  394. "Login Failed",
  395. JOptionPane.OK_CANCEL_OPTION);
  396. if (retry != JOptionPane.OK_OPTION)
  397. {
  398. System.exit(0);
  399. }
  400. }
  401. }
  402. CapabilitiesUI frame = new CapabilitiesUI("IBM Cognos Sample", connection);
  403. // Create a WindowAdapter so the application
  404. // is exited when the window is closed.
  405. frame.addWindowListener(new WindowAdapter()
  406. {
  407. public void windowClosing(WindowEvent e)
  408. {
  409. System.exit(0);
  410. }
  411. });
  412. frame.textAreaPane.setText(output);
  413. // Set the size of the frame and display it.
  414. frame.setSize(680, 440);
  415. frame.setVisible(true);
  416. frame.setResizable(true);
  417. }
  418. }