DeploymentUI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. *
  10. * DeploymentUI.java
  11. *
  12. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  13. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  14. *
  15. */
  16. import java.awt.BorderLayout;
  17. import java.awt.Dimension;
  18. import java.awt.GridBagConstraints;
  19. import java.awt.GridBagLayout;
  20. import java.awt.GridLayout;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.WindowAdapter;
  24. import java.awt.event.WindowEvent;
  25. import java.io.File;
  26. import java.net.URL;
  27. import java.util.HashMap;
  28. import javax.swing.BorderFactory;
  29. import javax.swing.ImageIcon;
  30. import javax.swing.JButton;
  31. import javax.swing.JComboBox;
  32. import javax.swing.JEditorPane;
  33. import javax.swing.JFrame;
  34. import javax.swing.JLabel;
  35. import javax.swing.JMenu;
  36. import javax.swing.JMenuBar;
  37. import javax.swing.JMenuItem;
  38. import javax.swing.JOptionPane;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTextArea;
  42. import javax.swing.JTextField;
  43. public class DeploymentUI extends JFrame {
  44. private CRNConnect connect;
  45. private static DeploymentUI frame;
  46. // The following variables represent the dialog components.
  47. private JTextArea textAreaPane;
  48. private JTextField cmURL;
  49. private JButton runButton;
  50. private JComboBox deployTypeOption;
  51. private static Logon sessionLogon;
  52. private static final String DEPLOY_ARCHIVE_LIST = "List Archives";
  53. private static final String DEPLOY_IMPORT = "Import";
  54. private static final String DEPLOY_EXPORT = "Export";
  55. private static final int DEPLOY_ENUM_LIST_ARCHIVES = 1;
  56. private static final int DEPLOY_ENUM_IMPORT = 2;
  57. private static final int DEPLOY_ENUM_EXPORT = 3;
  58. private static int deployType = 0;
  59. private HashMap selectedPackageNamePath=null;
  60. Deployment newDeploy = new Deployment();
  61. // This is the constructor.
  62. public DeploymentUI(String title, CRNConnect connection) {
  63. // Set the title of the frame, even before the variables are declared.
  64. super(title);
  65. connect = connection;
  66. addComponents();
  67. }
  68. // Add all components to the frame's panel.
  69. private void addComponents() {
  70. JMenuBar mBar = new JMenuBar();
  71. this.setJMenuBar(mBar);
  72. // declare menuItems
  73. JMenuItem exit;
  74. JMenuItem about;
  75. JMenuItem overview;
  76. // Add and populate the File menu.
  77. JMenu fileMenu = new JMenu("File");
  78. mBar.add(fileMenu);
  79. exit = new JMenuItem("Exit");
  80. fileMenu.add(exit);
  81. exit.addActionListener(new MenuHandler());
  82. // Add and populate the Help menu.
  83. JMenu helpMenu = new JMenu("Help");
  84. mBar.add(helpMenu);
  85. about = new JMenuItem("About");
  86. helpMenu.add(about);
  87. about.addActionListener(new MenuHandler());
  88. overview = new JMenuItem("Overview");
  89. helpMenu.add(overview);
  90. overview.addActionListener(new MenuHandler());
  91. JPanel mainPanel = createMainPanel();
  92. JPanel outputPanel = createOutputPanel();
  93. JPanel panel = new JPanel(new BorderLayout());
  94. panel.add(mainPanel, BorderLayout.NORTH);
  95. panel.add(outputPanel);
  96. setContentPane(panel);
  97. }
  98. private JPanel createOutputPanel() {
  99. // Add the status text pane.
  100. textAreaPane = new JTextArea();
  101. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  102. areaScrollPane
  103. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  104. areaScrollPane.setPreferredSize(new Dimension(500, 275));
  105. //Create the output panel and it's layout objects
  106. GridBagLayout layout = new GridBagLayout();
  107. JPanel outputPanel = new JPanel(layout);
  108. GridBagConstraints layoutConstraints = new GridBagConstraints();
  109. layoutConstraints.weightx = 1.0;
  110. layoutConstraints.weighty = 1.0;
  111. layoutConstraints.fill = GridBagConstraints.BOTH;
  112. layout.setConstraints(areaScrollPane, layoutConstraints);
  113. outputPanel.add(areaScrollPane);
  114. outputPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
  115. .createCompoundBorder(BorderFactory
  116. .createTitledBorder("Output"), BorderFactory
  117. .createEmptyBorder(5, 5, 5, 5)), outputPanel
  118. .getBorder()));
  119. return outputPanel;
  120. }
  121. private JPanel createMainPanel() {
  122. // Add the URL text field and label
  123. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  124. cmURL.setText(CRNConnect.CM_URL);
  125. cmURL.setEditable(false);
  126. // Put together a panel for the URl
  127. JPanel cmURLPanel = new JPanel();
  128. cmURLPanel.add(new JLabel("Server URL:"));
  129. cmURLPanel.add(cmURL);
  130. //get dropdown box and button panel
  131. JPanel optionButtonPanel = createMainButtonPanel();
  132. //create the main panel and add the components
  133. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  134. // Add the panels to the mainPanel
  135. mainPanel.add(cmURLPanel);
  136. mainPanel.add(optionButtonPanel);
  137. return mainPanel;
  138. }
  139. private JPanel createMainButtonPanel() {
  140. // Create the button Panel
  141. JPanel buttonPanel = new JPanel();
  142. // Create and add the report output type combo box
  143. String deployTypes[] = { DEPLOY_ARCHIVE_LIST, DEPLOY_IMPORT,
  144. DEPLOY_EXPORT };
  145. deployTypeOption = new JComboBox(deployTypes);
  146. deployTypeOption.setSelectedItem(null);
  147. deployTypeOption.addActionListener(new DeployTypeSelectionHandler());
  148. buttonPanel.add(new JLabel("Deployment Options:"), BorderLayout.WEST);
  149. buttonPanel.add(deployTypeOption, BorderLayout.WEST);
  150. // Create and add the Button
  151. runButton = new JButton("Run Option");
  152. runButton.addActionListener(new allButtonsHandler());
  153. buttonPanel.add(runButton, BorderLayout.EAST);
  154. return buttonPanel;
  155. }
  156. // handle menu bar buttons
  157. private class MenuHandler implements ActionListener {
  158. public void actionPerformed(ActionEvent e) {
  159. if (e.getActionCommand().startsWith("http://")) {
  160. connect.connectionChange(e.getActionCommand());
  161. }
  162. try {
  163. JMenuItem menuClicked = (JMenuItem) e.getSource();
  164. if (menuClicked.getText() == "Exit") {
  165. System.exit(0);
  166. }
  167. if (menuClicked.getText() == "About") {
  168. JOptionPane.showMessageDialog(((JMenuItem) e.getSource())
  169. .getParent(), "IBM Cognos Sample Application\n\n"
  170. + "Version 1.0.0\n"
  171. + "This application uses the IBM Cognos Software Development Kit",
  172. "About IBM Cognos Samples",
  173. JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
  174. "../Common/about.gif"));
  175. }
  176. if (menuClicked.getText().compareTo("Overview") == 0) {
  177. JFrame explainWindow = new JFrame(
  178. "Overview for Deployment Sample");
  179. File explainFile = new File(
  180. "Java_DeploymentUI_Explain.html");
  181. if (!explainFile.exists()) {
  182. JOptionPane.showMessageDialog(null,
  183. "Explain file not found");
  184. return;
  185. }
  186. URL explainURL = new URL("file:///"
  187. + explainFile.getAbsolutePath());
  188. JEditorPane explainPane = new JEditorPane();
  189. explainPane.setPage(explainURL);
  190. explainPane.setEditable(false);
  191. JScrollPane explainScroll = new JScrollPane(explainPane,
  192. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  193. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  194. explainWindow.getContentPane().add(explainScroll);
  195. explainWindow.setSize(640, 480);
  196. explainWindow.setVisible(true);
  197. }
  198. } catch (Exception ex) {
  199. }
  200. }
  201. }
  202. // The following is the button event handler.
  203. private class allButtonsHandler implements ActionListener {
  204. public void actionPerformed(ActionEvent e) {
  205. if (!Logon.loggedIn(connect)) {
  206. try {
  207. sessionLogon.logon(connect);
  208. } catch (Exception logonException) {
  209. }
  210. }
  211. JButton buttonPressed = ((JButton) e.getSource());
  212. String output = new String();
  213. if (buttonPressed == runButton) {
  214. if (deployType == 0) {
  215. textAreaPane.setText("");
  216. return;
  217. }
  218. try {
  219. String[] archivesList = newDeploy
  220. .getListOfArchives(connect);
  221. int len = archivesList.length;
  222. // get list archives
  223. if (deployType == DEPLOY_ENUM_LIST_ARCHIVES) {
  224. textAreaPane.setText("");
  225. deployTypeOption.setSelectedItem(null);
  226. if (len > 0) {
  227. for (int i = 0; i < archivesList.length; i++)
  228. textAreaPane.append(archivesList[i] + "\n");
  229. } else {
  230. textAreaPane.setText("No available archives.");
  231. }
  232. deployType = 0;
  233. }
  234. // Importing data into the content store
  235. else if (deployType == DEPLOY_ENUM_IMPORT) {
  236. textAreaPane.setText("");
  237. Object selectedArchive = null;
  238. String importArchiveName = null;
  239. do {
  240. importArchiveName = JOptionPane.showInputDialog(null,
  241. "Archive name: ",
  242. "Specify a name - New Import", 3);
  243. if (importArchiveName == null) {
  244. return;
  245. }
  246. } while (importArchiveName.length() == 0);
  247. if ((importArchiveName != null && importArchiveName.length() > 0)&&(len > 0)) {
  248. selectedArchive = JOptionPane.showInputDialog(null,
  249. "Select the deployment archive",
  250. "Deployment archive",
  251. JOptionPane.INFORMATION_MESSAGE, null,
  252. archivesList, null);
  253. } else // no available entries
  254. {
  255. JOptionPane.showMessageDialog(null, "No entries",
  256. "Select the deployment archive",
  257. JOptionPane.INFORMATION_MESSAGE);
  258. selectedArchive = null;
  259. }
  260. if (selectedArchive != null) {
  261. String oneArchive = ((String) selectedArchive)
  262. .trim();
  263. selectedPackageNamePath = newDeploy
  264. .getPubFolderContent(oneArchive, connect);
  265. String[] selectedPackage=getSelectedPackageName(selectedPackageNamePath);
  266. if (selectedPackage.length > 0) {
  267. packageDialog myDialog = new packageDialog(
  268. frame, "Package", true, importArchiveName, oneArchive,
  269. selectedPackage, connect, "import");
  270. myDialog.setVisible(true);
  271. String importDeployID = myDialog
  272. .getDeploymentID();
  273. deployTypeOption.setSelectedItem(null);
  274. if (importDeployID == null) {
  275. textAreaPane.setText("");
  276. } else if ((importDeployID
  277. .equalsIgnoreCase("Failed"))||(importDeployID.equalsIgnoreCase("-1"))) {
  278. textAreaPane
  279. .setText("Error occurred in Import.");
  280. } else {
  281. textAreaPane.setText("The import of "
  282. + oneArchive
  283. + " has been completed.");
  284. }
  285. deployType = 0;
  286. } else {
  287. int result = JOptionPane
  288. .showConfirmDialog(
  289. null,
  290. "The deployment archive is empty. Click Yes to continue the import or Cancel to return to your selection.",
  291. "New Import",
  292. JOptionPane.OK_CANCEL_OPTION);
  293. deployTypeOption.setSelectedItem(null);
  294. deployType = 0;
  295. if (result != 0) {
  296. return;
  297. }
  298. }
  299. }
  300. }
  301. // Exporting data from the content store
  302. else if (deployType == DEPLOY_ENUM_EXPORT) {
  303. textAreaPane.setText("");
  304. String archiveName = null;
  305. do {
  306. archiveName = JOptionPane.showInputDialog(null,
  307. "Archive name: ",
  308. "Specify a name - New Export", 3);
  309. if (archiveName == null) {
  310. return;
  311. }
  312. } while (archiveName.length() == 0);
  313. if (archiveName != null && archiveName.length() > 0) {
  314. String[] folderArr = newDeploy
  315. .getAllFolders(connect);
  316. if (folderArr.length != 0) {
  317. packageDialog pakDialog = new packageDialog(
  318. frame, "Package", true, archiveName, null,
  319. folderArr, connect, "export");
  320. pakDialog.setVisible(true);
  321. String exportDeployID = pakDialog
  322. .getDeploymentID();
  323. deployTypeOption.setSelectedItem(null);
  324. if (exportDeployID == null) {
  325. textAreaPane.setText("");
  326. } else if (exportDeployID
  327. .equalsIgnoreCase("-1")) {
  328. textAreaPane
  329. .setText("Failed to add a Deployment object.");
  330. } else if (exportDeployID
  331. .equalsIgnoreCase("Failed")) {
  332. textAreaPane
  333. .setText("Error occurred in Import.");
  334. } else {
  335. textAreaPane.setText("The export of "
  336. + archiveName
  337. + " has been completed.");
  338. }
  339. deployType = 0;
  340. } else {
  341. JOptionPane
  342. .showMessageDialog(
  343. null,
  344. "No public folder contents are available",
  345. "New Export", 1);
  346. deployTypeOption.setSelectedItem(null);
  347. deployType = 0;
  348. return;
  349. }
  350. }
  351. }
  352. } catch (Exception ex) {
  353. System.out.println(ex.getMessage());
  354. output = "An error occurred.\nMake sure a "
  355. + "deployment option is selected and IBM Cognos is running.";
  356. }
  357. }
  358. if (output.compareTo("") != 0) {
  359. textAreaPane.setText("");
  360. textAreaPane.append(output);
  361. }
  362. }
  363. }
  364. // This is the Deployment type combo box event handler.
  365. private class DeployTypeSelectionHandler implements ActionListener {
  366. public void actionPerformed(ActionEvent deployTypeSelectedEvent) {
  367. textAreaPane.setText("");
  368. String chosenType = (String) deployTypeOption.getSelectedItem();
  369. if (chosenType == DEPLOY_ARCHIVE_LIST) {
  370. deployType = DEPLOY_ENUM_LIST_ARCHIVES;
  371. } else if (chosenType == DEPLOY_IMPORT) {
  372. deployType = DEPLOY_ENUM_IMPORT;
  373. } else if (chosenType == DEPLOY_EXPORT) {
  374. deployType = DEPLOY_ENUM_EXPORT;
  375. }
  376. }
  377. }
  378. //Get selected package name from a HashMap
  379. private String[] getSelectedPackageName(HashMap selectedPackageNamePath)
  380. {
  381. String[] selectedPackage=null;
  382. if(!selectedPackageNamePath.isEmpty())
  383. {
  384. Object[] mySelectedPackage = (Object[]) selectedPackageNamePath
  385. .keySet().toArray();
  386. selectedPackage = new String[mySelectedPackage.length];
  387. for (int n = 0; n < mySelectedPackage.length; n++) {
  388. selectedPackage[n] = (String) mySelectedPackage[n];
  389. }
  390. }
  391. return selectedPackage;
  392. }
  393. // Create the main method to execute the application.
  394. public static void main(String args[]) {
  395. CRNConnect connection = new CRNConnect();
  396. connection.connectToCognosServer();
  397. sessionLogon = new Logon();
  398. String logonOutput = "";
  399. while (!Logon.loggedIn(connection)) {
  400. logonOutput = sessionLogon.logon(connection);
  401. if (!Logon.loggedIn(connection)) {
  402. int retry = JOptionPane.showConfirmDialog(null,
  403. "Login Failed. Please try again.", "Login Failed",
  404. JOptionPane.OK_CANCEL_OPTION);
  405. if (retry != JOptionPane.OK_OPTION) {
  406. System.exit(0);
  407. }
  408. }
  409. }
  410. frame = new DeploymentUI("IBM Cognos Sample", connection);
  411. // Create a WindowAdapter so the application
  412. // is exited when the window is closed.
  413. frame.addWindowListener(new WindowAdapter() {
  414. public void windowClosing(WindowEvent e) {
  415. System.exit(0);
  416. }
  417. });
  418. frame.textAreaPane.setText(logonOutput);
  419. // Set the size of the frame and display it.
  420. frame.setSize(750, 440);
  421. frame.setVisible(true);
  422. frame.setResizable(true);
  423. }
  424. }