123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- /**
- Licensed Materials - Property of IBM
- IBM Cognos Products: DOCS
- (C) Copyright IBM Corp. 2008, 2009
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
- IBM Corp.
- */
- /**
- * DimensionManagementServiceTest.java
- *
- * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
- * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
- *
- * Description:
- *
- * This is a sample test application for the IBM Cognos
- * Dimension Management Service.
- *
- */
- import java.awt.BorderLayout;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.io.UnsupportedEncodingException;
- import java.rmi.RemoteException;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.border.CompoundBorder;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.XMLWriter;
- import com.cognos.developer.schemas.bibus._3.AsynchDetail;
- import com.cognos.developer.schemas.bibus._3.AsynchDetailMIMEAttachment;
- import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum;
- import com.cognos.developer.schemas.bibus._3.AsynchOptionInt;
- import com.cognos.developer.schemas.bibus._3.AsynchReply;
- import com.cognos.developer.schemas.bibus._3.DimensionManagementServiceSpecification;
- //import com.cognos.developer.schemas.bibus._3.DimensionManagementService_Port;
- import com.cognos.developer.schemas.bibus._3.Option;
- import com.cognos.developer.schemas.bibus._3.ParameterValue;
- import com.cognos.developer.schemas.bibus._3.Specification;
- final class DimensionManagementServiceTest extends JFrame
- {
- private static final long serialVersionUID = 1L;
- private static final String APPLICATION_TITLE = "Dimension Management Service Testbench";
- private static DimensionManagementServiceTest theApp;
-
- private CRNConnect connection;
- private Logon sessionLogon;
- private String logonStatus;
- private JTextArea requestTextArea;
- private JTextArea responseTextArea;
-
- private JFileChooser fileChooser;
-
-
- public static void main( String args[] )
- {
- theApp = new DimensionManagementServiceTest();
- // exit the application on window closing.
- theApp.addWindowListener( new WindowAdapter()
- {
- public void windowClosing( WindowEvent e )
- {
- System.exit( 0 );
- }
- } );
- theApp.run();
- }
- private DimensionManagementServiceTest()
- {
- super( APPLICATION_TITLE );
- }
- private void run()
- {
- connectToCognosServer();
- logon();
- createUI();
- }
- private void connectToCognosServer()
- {
- connection = new CRNConnect();
- connection.connectToCognosServer();
- }
- private void logon()
- {
- // assert connection != null;
- sessionLogon = new Logon();
- logonStatus = "";
- while ( !Logon.loggedIn( connection ) )
- {
- logonStatus = sessionLogon.logon( connection );
- if ( !Logon.loggedIn( connection ) )
- {
- int retry = JOptionPane.showConfirmDialog( null, "Login Failed. Please try again.", "Login Failed", JOptionPane.OK_CANCEL_OPTION );
- if ( retry != JOptionPane.OK_OPTION )
- {
- System.exit( 0 );
- }
- }
- }
- }
- private void createUI()
- {
- fileChooser = new JFileChooser();
- fileChooser.setCurrentDirectory( new File( "C:\\") );
-
- createMenuBar();
- createTestPanel();
- // show main window
- this.setSize( 640, 480 );
- this.setResizable( true );
- this.setVisible( true );
- }
- private void createMenuBar()
- {
- JMenuBar menuBar = new JMenuBar();
- this.setJMenuBar( menuBar );
- menuBar.add( createFileMenu() );
- menuBar.add( createHelpMenu() );
- }
- private JMenu createFileMenu()
- {
- JMenu fileMenu = new JMenu( "File" );
- JMenuItem open = new JMenuItem( "Open" );
- open.addActionListener( new ActionListener()
- {
- public void actionPerformed( ActionEvent e )
- {
- int returnVal = fileChooser.showOpenDialog( theApp );
- if ( returnVal != JFileChooser.APPROVE_OPTION )
- {
- return;
- }
- File file = fileChooser.getSelectedFile();
- try
- {
- requestTextArea.setText( "" );
- responseTextArea.setText( "" );
- FileInputStream is = new FileInputStream( file );
- InputStreamReader reader = new InputStreamReader( is, "UTF-8" );
- requestTextArea.read( reader, null );
- }
- catch ( IOException ex )
- {
- responseTextArea.setText( "Failed to read '" + file.getName() + "'" );
- }
- }
- } );
- JMenuItem exit = new JMenuItem( "Exit" );
- exit.addActionListener( new ActionListener()
- {
- public void actionPerformed( ActionEvent e )
- {
- System.exit( 0 );
- }
- } );
- fileMenu.add( open );
- fileMenu.add( exit );
- return fileMenu;
- }
- private JMenu createHelpMenu()
- {
- JMenu helpMenu = new JMenu( "Help" );
- helpMenu.add( createAboutMenuItem() );
- return helpMenu;
- }
- private JMenuItem createAboutMenuItem()
- {
- JMenuItem about = new JMenuItem( "About" );
- about.addActionListener( new ActionListener()
- {
- public void actionPerformed( ActionEvent e )
- {
- 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 );
- }
- } );
- return about;
- }
- private void createTestPanel()
- {
- JPanel testPanel = new JPanel( new BorderLayout() );
- testPanel.add( createControlPanel(), BorderLayout.NORTH );
- testPanel.add( createRequestResponsePanel() );
- // Set the content of the JFrame
- setContentPane( testPanel );
- }
- private JPanel createControlPanel()
- {
- JPanel mainPanel = new JPanel( new GridLayout( 2, 0 ) );
- mainPanel.add( createURLPanel() );
- mainPanel.add( createButtonPanel() );
- return mainPanel;
- }
- private JPanel createURLPanel()
- {
- String url = CRNConnect.CM_URL;
-
- JTextField urlTextField = new JTextField( url.length() );
- urlTextField.setText( url );
- urlTextField.setEditable( false );
- JPanel urlPanel = new JPanel();
- urlPanel.add( new JLabel( "IBM Cognos URL:" ) );
- urlPanel.add( urlTextField );
- return urlPanel;
- }
- private JPanel createButtonPanel()
- {
- JPanel buttonPanel = new JPanel();
- buttonPanel.add( createRunButton(), BorderLayout.EAST );
- buttonPanel.add( createClearButton(), BorderLayout.WEST );
- return buttonPanel;
- }
- private JButton createRunButton()
- {
- JButton button = new JButton( "Run Request" );
- button.addActionListener( new ActionListener()
- {
- public void actionPerformed( ActionEvent event )
- {
- try
- {
- responseTextArea.setText( "" );
-
- String runRequestResult = runRequest( requestTextArea.getText() );
-
- responseTextArea.setText( runRequestResult );
- responseTextArea.setCaretPosition(0);
- }
- catch ( Exception e )
- {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter( sw );
- e.printStackTrace( pw );
- responseTextArea.setText( sw.toString() );
- }
- }
- } );
- return button;
- }
- private JButton createClearButton()
- {
- JButton button = new JButton( "Clear" );
- button.addActionListener( new ActionListener()
- {
- public void actionPerformed( ActionEvent e )
- {
- requestTextArea.setText( "" );
- responseTextArea.setText( "" );
- }
- } );
- return button;
- }
- private JPanel createRequestResponsePanel()
- {
- JPanel panel = new JPanel( new GridLayout( 2, 0 ) );
- panel.add( createRequestPanel() );
- panel.add( createResponsePanel() );
- return panel;
- }
- /**
- * Create a panel for:
- * IBM Cognos Business Viewpoint XML API Request
- */
- private JPanel createRequestPanel()
- {
- requestTextArea = createTextArea();
- return createTextPanel( "XML API Request", requestTextArea );
- }
- /**
- * Create a panel for:
- * IBM Cognos Business Viewpoint XML API Response
- */
- private JPanel createResponsePanel()
- {
- responseTextArea = createTextArea();
- responseTextArea.setText( logonStatus ); // Show logon status upon startup
- return createTextPanel( "XML API Response", responseTextArea );
- }
- /**
- * Creates a text area for either Request or Response panels.
- */
- private JTextArea createTextArea()
- {
- JTextArea textArea = new JTextArea();
- textArea.setEditable( true );
- textArea.setLineWrap( true );
- textArea.setWrapStyleWord( false );
- textArea.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
- return textArea;
- }
- /**
- * Creates the panel to hold the supplied text area.
- *
- * We call this function twice, to create two identical panels:
- * one for Request, another one for Response.
- *
- * @param panelName Panel name.
- * @param textArea Text area to put inside the panel.
- * @return
- */
- private JPanel createTextPanel( String panelName, JTextArea textArea )
- {
- JScrollPane textScrollPane = new JScrollPane( textArea );
- textScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
- //Create the output panel and it's layout objects
- GridBagLayout layout = new GridBagLayout();
- JPanel textPanel = new JPanel( layout );
- GridBagConstraints layoutConstraints = new GridBagConstraints();
- //Set the layout for the scroll pane and add it
- layoutConstraints.weightx = 1.0;
- layoutConstraints.weighty = 1.0;
- layoutConstraints.fill = GridBagConstraints.BOTH;
- layout.setConstraints( textScrollPane, layoutConstraints );
- textPanel.add( textScrollPane );
- // put a border around the output and navigation buttons
- CompoundBorder border = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder( panelName ), BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ), textPanel.getBorder() );
- textPanel.setBorder( border );
- return textPanel;
- }
- /**
- * Submit XML API request to Dimension Management Service.
- *
- * @param request XML API request.
- * @return XMl API response.
- */
- private String runRequest( String request )
- {
- DimensionManagementServiceSpecification dimsSpec = new DimensionManagementServiceSpecification();
- dimsSpec.setValue( new Specification( request ) );
- ParameterValue[] parameters = new ParameterValue[] {};
- Option[] options = getRequestOptions();
- AsynchReply reply = null;
-
- try
- {
- reply = connection.getDimensionManagementService().runSpecification( dimsSpec, parameters, options );
- }
- catch ( RemoteException e )
- {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter( sw );
- e.printStackTrace( pw );
- return sw.toString();
- }
- return getReplyAsString( reply );
- }
- /**
- * Return the options to pass on to runSpecification method. The options become part of
- * BI bus SOAP command, where they can be accessed by dispatcher and the services.
- *
- * @return array of Options.
- */
- private Option[] getRequestOptions()
- {
- AsynchOptionInt primaryThreshold = new AsynchOptionInt();
- primaryThreshold.setName( AsynchOptionEnum.primaryWaitThreshold );
- primaryThreshold.setValue( 0 );
- AsynchOptionInt secondaryThreshold = new AsynchOptionInt();
- secondaryThreshold.setName( AsynchOptionEnum.secondaryWaitThreshold );
- secondaryThreshold.setValue( 0 );
- Option[] options = new Option[2];
- options[0] = primaryThreshold;
- options[1] = secondaryThreshold;
- return options;
- }
- /**
- * Converts AsynchReply to XML.
- *
- * @param reply DimensionManagementService reply.
- * @return XML string.
- */
- private String getReplyAsString( AsynchReply reply )
- {
- if ( reply == null )
- {
- return "No reply.";
- }
- AsynchDetail[] details = reply.getDetails();
-
- for (int i = 0; i < details.length; i++)
-
- {
-
- if ( details[i] instanceof AsynchDetailMIMEAttachment )
- {
- try
- {
- AsynchDetailMIMEAttachment replyDetail = (AsynchDetailMIMEAttachment) details[i];
- String replyXML = new String( replyDetail.getData(), "UTF-8" );
-
- return prettyPrint( replyXML );
- }
- catch ( UnsupportedEncodingException e )
- {
- return "UnsupportedEncodingException occurred when trying to decode the response";
- }
- }
- }
- return "No reply details.";
- }
-
- /**
- * Pretty-print XML returned by Dimension Management Service.
- *
- * @param xml XML response.
- * @return Pretty-printed XML.
- */
- private String prettyPrint( String xml )
- {
- try
- {
- Document document = DocumentHelper.parseText( xml );
- OutputFormat format = OutputFormat.createPrettyPrint();
- StringWriter stringWriter = new StringWriter();
- XMLWriter writer = new XMLWriter( stringWriter, format );
- writer.write( document );
- writer.close();
- return stringWriter.toString();
- }
- catch ( Exception e )
- {
- // Failed to pretty-print. Return XML as is.
- return xml;
- }
- }
- }
|