DimensionManagementServiceTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2008, 2009
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * DimensionManagementServiceTest.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. * Description:
  15. *
  16. * This is a sample test application for the IBM Cognos
  17. * Dimension Management Service.
  18. *
  19. */
  20. import java.awt.BorderLayout;
  21. import java.awt.Font;
  22. import java.awt.GridBagConstraints;
  23. import java.awt.GridBagLayout;
  24. import java.awt.GridLayout;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.WindowAdapter;
  28. import java.awt.event.WindowEvent;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.io.PrintWriter;
  34. import java.io.StringWriter;
  35. import java.io.UnsupportedEncodingException;
  36. import java.rmi.RemoteException;
  37. import javax.swing.BorderFactory;
  38. import javax.swing.JButton;
  39. import javax.swing.JFileChooser;
  40. import javax.swing.JFrame;
  41. import javax.swing.JLabel;
  42. import javax.swing.JMenu;
  43. import javax.swing.JMenuBar;
  44. import javax.swing.JMenuItem;
  45. import javax.swing.JOptionPane;
  46. import javax.swing.JPanel;
  47. import javax.swing.JScrollPane;
  48. import javax.swing.JTextArea;
  49. import javax.swing.JTextField;
  50. import javax.swing.border.CompoundBorder;
  51. import org.dom4j.Document;
  52. import org.dom4j.DocumentHelper;
  53. import org.dom4j.io.OutputFormat;
  54. import org.dom4j.io.XMLWriter;
  55. import com.cognos.developer.schemas.bibus._3.AsynchDetail;
  56. import com.cognos.developer.schemas.bibus._3.AsynchDetailMIMEAttachment;
  57. import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum;
  58. import com.cognos.developer.schemas.bibus._3.AsynchOptionInt;
  59. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  60. import com.cognos.developer.schemas.bibus._3.DimensionManagementServiceSpecification;
  61. //import com.cognos.developer.schemas.bibus._3.DimensionManagementService_Port;
  62. import com.cognos.developer.schemas.bibus._3.Option;
  63. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  64. import com.cognos.developer.schemas.bibus._3.Specification;
  65. final class DimensionManagementServiceTest extends JFrame
  66. {
  67. private static final long serialVersionUID = 1L;
  68. private static final String APPLICATION_TITLE = "Dimension Management Service Testbench";
  69. private static DimensionManagementServiceTest theApp;
  70. private CRNConnect connection;
  71. private Logon sessionLogon;
  72. private String logonStatus;
  73. private JTextArea requestTextArea;
  74. private JTextArea responseTextArea;
  75. private JFileChooser fileChooser;
  76. public static void main( String args[] )
  77. {
  78. theApp = new DimensionManagementServiceTest();
  79. // exit the application on window closing.
  80. theApp.addWindowListener( new WindowAdapter()
  81. {
  82. public void windowClosing( WindowEvent e )
  83. {
  84. System.exit( 0 );
  85. }
  86. } );
  87. theApp.run();
  88. }
  89. private DimensionManagementServiceTest()
  90. {
  91. super( APPLICATION_TITLE );
  92. }
  93. private void run()
  94. {
  95. connectToCognosServer();
  96. logon();
  97. createUI();
  98. }
  99. private void connectToCognosServer()
  100. {
  101. connection = new CRNConnect();
  102. connection.connectToCognosServer();
  103. }
  104. private void logon()
  105. {
  106. // assert connection != null;
  107. sessionLogon = new Logon();
  108. logonStatus = "";
  109. while ( !Logon.loggedIn( connection ) )
  110. {
  111. logonStatus = sessionLogon.logon( connection );
  112. if ( !Logon.loggedIn( connection ) )
  113. {
  114. int retry = JOptionPane.showConfirmDialog( null, "Login Failed. Please try again.", "Login Failed", JOptionPane.OK_CANCEL_OPTION );
  115. if ( retry != JOptionPane.OK_OPTION )
  116. {
  117. System.exit( 0 );
  118. }
  119. }
  120. }
  121. }
  122. private void createUI()
  123. {
  124. fileChooser = new JFileChooser();
  125. fileChooser.setCurrentDirectory( new File( "C:\\") );
  126. createMenuBar();
  127. createTestPanel();
  128. // show main window
  129. this.setSize( 640, 480 );
  130. this.setResizable( true );
  131. this.setVisible( true );
  132. }
  133. private void createMenuBar()
  134. {
  135. JMenuBar menuBar = new JMenuBar();
  136. this.setJMenuBar( menuBar );
  137. menuBar.add( createFileMenu() );
  138. menuBar.add( createHelpMenu() );
  139. }
  140. private JMenu createFileMenu()
  141. {
  142. JMenu fileMenu = new JMenu( "File" );
  143. JMenuItem open = new JMenuItem( "Open" );
  144. open.addActionListener( new ActionListener()
  145. {
  146. public void actionPerformed( ActionEvent e )
  147. {
  148. int returnVal = fileChooser.showOpenDialog( theApp );
  149. if ( returnVal != JFileChooser.APPROVE_OPTION )
  150. {
  151. return;
  152. }
  153. File file = fileChooser.getSelectedFile();
  154. try
  155. {
  156. requestTextArea.setText( "" );
  157. responseTextArea.setText( "" );
  158. FileInputStream is = new FileInputStream( file );
  159. InputStreamReader reader = new InputStreamReader( is, "UTF-8" );
  160. requestTextArea.read( reader, null );
  161. }
  162. catch ( IOException ex )
  163. {
  164. responseTextArea.setText( "Failed to read '" + file.getName() + "'" );
  165. }
  166. }
  167. } );
  168. JMenuItem exit = new JMenuItem( "Exit" );
  169. exit.addActionListener( new ActionListener()
  170. {
  171. public void actionPerformed( ActionEvent e )
  172. {
  173. System.exit( 0 );
  174. }
  175. } );
  176. fileMenu.add( open );
  177. fileMenu.add( exit );
  178. return fileMenu;
  179. }
  180. private JMenu createHelpMenu()
  181. {
  182. JMenu helpMenu = new JMenu( "Help" );
  183. helpMenu.add( createAboutMenuItem() );
  184. return helpMenu;
  185. }
  186. private JMenuItem createAboutMenuItem()
  187. {
  188. JMenuItem about = new JMenuItem( "About" );
  189. about.addActionListener( new ActionListener()
  190. {
  191. public void actionPerformed( ActionEvent e )
  192. {
  193. JOptionPane.showMessageDialog( ((JMenuItem) e.getSource()).getParent(), APPLICATION_TITLE + "\n\n" + "Version 1.0.0\n" + "This application is used to test Dimension Management Service.", "About " + APPLICATION_TITLE, JOptionPane.INFORMATION_MESSAGE, null );
  194. }
  195. } );
  196. return about;
  197. }
  198. private void createTestPanel()
  199. {
  200. JPanel testPanel = new JPanel( new BorderLayout() );
  201. testPanel.add( createControlPanel(), BorderLayout.NORTH );
  202. testPanel.add( createRequestResponsePanel() );
  203. // Set the content of the JFrame
  204. setContentPane( testPanel );
  205. }
  206. private JPanel createControlPanel()
  207. {
  208. JPanel mainPanel = new JPanel( new GridLayout( 2, 0 ) );
  209. mainPanel.add( createURLPanel() );
  210. mainPanel.add( createButtonPanel() );
  211. return mainPanel;
  212. }
  213. private JPanel createURLPanel()
  214. {
  215. String url = CRNConnect.CM_URL;
  216. JTextField urlTextField = new JTextField( url.length() );
  217. urlTextField.setText( url );
  218. urlTextField.setEditable( false );
  219. JPanel urlPanel = new JPanel();
  220. urlPanel.add( new JLabel( "IBM Cognos URL:" ) );
  221. urlPanel.add( urlTextField );
  222. return urlPanel;
  223. }
  224. private JPanel createButtonPanel()
  225. {
  226. JPanel buttonPanel = new JPanel();
  227. buttonPanel.add( createRunButton(), BorderLayout.EAST );
  228. buttonPanel.add( createClearButton(), BorderLayout.WEST );
  229. return buttonPanel;
  230. }
  231. private JButton createRunButton()
  232. {
  233. JButton button = new JButton( "Run Request" );
  234. button.addActionListener( new ActionListener()
  235. {
  236. public void actionPerformed( ActionEvent event )
  237. {
  238. try
  239. {
  240. responseTextArea.setText( "" );
  241. String runRequestResult = runRequest( requestTextArea.getText() );
  242. responseTextArea.setText( runRequestResult );
  243. responseTextArea.setCaretPosition(0);
  244. }
  245. catch ( Exception e )
  246. {
  247. StringWriter sw = new StringWriter();
  248. PrintWriter pw = new PrintWriter( sw );
  249. e.printStackTrace( pw );
  250. responseTextArea.setText( sw.toString() );
  251. }
  252. }
  253. } );
  254. return button;
  255. }
  256. private JButton createClearButton()
  257. {
  258. JButton button = new JButton( "Clear" );
  259. button.addActionListener( new ActionListener()
  260. {
  261. public void actionPerformed( ActionEvent e )
  262. {
  263. requestTextArea.setText( "" );
  264. responseTextArea.setText( "" );
  265. }
  266. } );
  267. return button;
  268. }
  269. private JPanel createRequestResponsePanel()
  270. {
  271. JPanel panel = new JPanel( new GridLayout( 2, 0 ) );
  272. panel.add( createRequestPanel() );
  273. panel.add( createResponsePanel() );
  274. return panel;
  275. }
  276. /**
  277. * Create a panel for:
  278. * IBM Cognos Business Viewpoint XML API Request
  279. */
  280. private JPanel createRequestPanel()
  281. {
  282. requestTextArea = createTextArea();
  283. return createTextPanel( "XML API Request", requestTextArea );
  284. }
  285. /**
  286. * Create a panel for:
  287. * IBM Cognos Business Viewpoint XML API Response
  288. */
  289. private JPanel createResponsePanel()
  290. {
  291. responseTextArea = createTextArea();
  292. responseTextArea.setText( logonStatus ); // Show logon status upon startup
  293. return createTextPanel( "XML API Response", responseTextArea );
  294. }
  295. /**
  296. * Creates a text area for either Request or Response panels.
  297. */
  298. private JTextArea createTextArea()
  299. {
  300. JTextArea textArea = new JTextArea();
  301. textArea.setEditable( true );
  302. textArea.setLineWrap( true );
  303. textArea.setWrapStyleWord( false );
  304. textArea.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
  305. return textArea;
  306. }
  307. /**
  308. * Creates the panel to hold the supplied text area.
  309. *
  310. * We call this function twice, to create two identical panels:
  311. * one for Request, another one for Response.
  312. *
  313. * @param panelName Panel name.
  314. * @param textArea Text area to put inside the panel.
  315. * @return
  316. */
  317. private JPanel createTextPanel( String panelName, JTextArea textArea )
  318. {
  319. JScrollPane textScrollPane = new JScrollPane( textArea );
  320. textScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  321. //Create the output panel and it's layout objects
  322. GridBagLayout layout = new GridBagLayout();
  323. JPanel textPanel = new JPanel( layout );
  324. GridBagConstraints layoutConstraints = new GridBagConstraints();
  325. //Set the layout for the scroll pane and add it
  326. layoutConstraints.weightx = 1.0;
  327. layoutConstraints.weighty = 1.0;
  328. layoutConstraints.fill = GridBagConstraints.BOTH;
  329. layout.setConstraints( textScrollPane, layoutConstraints );
  330. textPanel.add( textScrollPane );
  331. // put a border around the output and navigation buttons
  332. CompoundBorder border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder( panelName ), BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ), textPanel.getBorder() );
  333. textPanel.setBorder( border );
  334. return textPanel;
  335. }
  336. /**
  337. * Submit XML API request to Dimension Management Service.
  338. *
  339. * @param request XML API request.
  340. * @return XMl API response.
  341. */
  342. private String runRequest( String request )
  343. {
  344. DimensionManagementServiceSpecification dimsSpec = new DimensionManagementServiceSpecification();
  345. dimsSpec.setValue( new Specification( request ) );
  346. ParameterValue[] parameters = new ParameterValue[] {};
  347. Option[] options = getRequestOptions();
  348. AsynchReply reply = null;
  349. try
  350. {
  351. reply = connection.getDimensionManagementService().runSpecification( dimsSpec, parameters, options );
  352. }
  353. catch ( RemoteException e )
  354. {
  355. StringWriter sw = new StringWriter();
  356. PrintWriter pw = new PrintWriter( sw );
  357. e.printStackTrace( pw );
  358. return sw.toString();
  359. }
  360. return getReplyAsString( reply );
  361. }
  362. /**
  363. * Return the options to pass on to runSpecification method. The options become part of
  364. * BI bus SOAP command, where they can be accessed by dispatcher and the services.
  365. *
  366. * @return array of Options.
  367. */
  368. private Option[] getRequestOptions()
  369. {
  370. AsynchOptionInt primaryThreshold = new AsynchOptionInt();
  371. primaryThreshold.setName( AsynchOptionEnum.primaryWaitThreshold );
  372. primaryThreshold.setValue( 0 );
  373. AsynchOptionInt secondaryThreshold = new AsynchOptionInt();
  374. secondaryThreshold.setName( AsynchOptionEnum.secondaryWaitThreshold );
  375. secondaryThreshold.setValue( 0 );
  376. Option[] options = new Option[2];
  377. options[0] = primaryThreshold;
  378. options[1] = secondaryThreshold;
  379. return options;
  380. }
  381. /**
  382. * Converts AsynchReply to XML.
  383. *
  384. * @param reply DimensionManagementService reply.
  385. * @return XML string.
  386. */
  387. private String getReplyAsString( AsynchReply reply )
  388. {
  389. if ( reply == null )
  390. {
  391. return "No reply.";
  392. }
  393. AsynchDetail[] details = reply.getDetails();
  394. for (int i = 0; i < details.length; i++)
  395. {
  396. if ( details[i] instanceof AsynchDetailMIMEAttachment )
  397. {
  398. try
  399. {
  400. AsynchDetailMIMEAttachment replyDetail = (AsynchDetailMIMEAttachment) details[i];
  401. String replyXML = new String( replyDetail.getData(), "UTF-8" );
  402. return prettyPrint( replyXML );
  403. }
  404. catch ( UnsupportedEncodingException e )
  405. {
  406. return "UnsupportedEncodingException occurred when trying to decode the response";
  407. }
  408. }
  409. }
  410. return "No reply details.";
  411. }
  412. /**
  413. * Pretty-print XML returned by Dimension Management Service.
  414. *
  415. * @param xml XML response.
  416. * @return Pretty-printed XML.
  417. */
  418. private String prettyPrint( String xml )
  419. {
  420. try
  421. {
  422. Document document = DocumentHelper.parseText( xml );
  423. OutputFormat format = OutputFormat.createPrettyPrint();
  424. StringWriter stringWriter = new StringWriter();
  425. XMLWriter writer = new XMLWriter( stringWriter, format );
  426. writer.write( document );
  427. writer.close();
  428. return stringWriter.toString();
  429. }
  430. catch ( Exception e )
  431. {
  432. // Failed to pretty-print. Return XML as is.
  433. return xml;
  434. }
  435. }
  436. }