End-to-End Multi-Dimensional Example

 

The following example shows all of the code required to read from a map, define a table, create a result set, and display a simple table.

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import com.sas.swing.visuals.olaptableview.OLAPTableView;

import com.sas.visuals.AutoSizingGridLayout;

import java.util.List;

/*

* A simple program that will read from a information map, define a table, create a result set, and displays multi-dimension data in a simple tabular format. 

*/

public class EndtoEndOLAPExample {

// The user's logged-in BI Platform session

com.sas.services.session.SessionContextInterface sessionContext;

 

// The intelligent query metadata service -- which reads metadata from repositories

com.sas.iquery.metadata.IntelligentQueryMetadataServiceInterface m_IQService;

// All examples are based on this common setup method which requires an

// already logged-in session for the user running the code.

//

// For information on how to log in a user and obtain a session and

// for performing the prerequisite deployment of BI Platform Services prior

// to creating sessions, see documentation for the following packages:

// com.sas.services.deployment

// com.sas.services.discovery

// com.sas.services.session

// com.sas.services.user

 

 

public void EndtoEndOLAPExample() throws Exception

    //It is important that you get a userSession to pass into the following method.  This method will create a new service. This is      

      //required for any coding  using an information map.

      setup(usersession);

      

    // Set name of map and get information map for use.

    //This is required  for any coding using an information map.

    String pathToMap = "SBIP://<REPOSITORY_NAME>/<LOCATION_OF_MAP>/CarSalesExample(BriefInformationMap)";

    com.sas.iquery.metadata.business.InformationMap map = readMapFromRepository(pathToMap);

    // Set up DataSelection.    

   //This is required  for any coding using an information map and a dataselection from that map.

   

    com.sas.iquery.metadata.business.DataSelection ds = newDataSelectionBasedOnMap(map);

 

    // Get DataItem References.

    List dsDataItems = ds.getObjects(false, com.sas.iquery.metadata.business.DataItemReference.class);

   

    // Get result set for multi-dimensional.

    com.sas.iquery.execution2.ResultSetInterface results = SimpleOLAPTableDisplay(dsDataItems, ds);

 

    // Display a tabular representation of multi-dimensional data in a result set.

    DisplayOLAPTableExample(results);

 

     // Remove the data selection because we are done with the process.

    doneWithDataSelection(ds);

 

}

 

/**

*

* A simple method creates a result set using the data item from an information map that uses multi-dimension data.

*

* @param list of data items in an information map.

*

* @throws Exception

*/

private  com.sas.iquery.execution2.ResultSetInterface SimpleOLAPTableDisplay(java.util.List dsDataItems, com.sas.iquery.metadata.business.DataSelection ds) throws Exception

{         

          // Create connection and assign roles to data items.

          java.util.Map results =null;

com.sas.iquery.dataretrieval.QueryConnector m_queryConnector = new com.sas.iquery.dataretrieval.QueryConnector();

  for(int i = 0; i < dsDataItems.size(); i++)

{         com.sas.iquery.metadata.business.DataItem tdi = (com.sas.iquery.metadata.business.DataItem)dsDataItems.get(i);

String label = tdi.getLabel();

 

// Assign Column to Sales_sum measure, all other dataitem will be assigned a role of ROW

if ( label.equalsIgnoreCase("SALES_SUM") )

{ ds.addResultItem(tdi, Role.COLUMN);

}

else

{ ds.addResultItem(tdi, Role.ROW);

}

}

 

// Perform query and get results.

java.util.List selections = new java.util.ArrayList();

selections.add( ds );

ds.setID("SimpleOLAPTable");

results = m_queryConnector.retrieveQueryResultMap( selections );

 

// Get Results interface to display table if desired.

com.sas.iquery.execution2.ResultSetInterface resultSet = (com.sas.iquery.execution2.ResultSetInterface)results.get( ds.getID() );

return resultSet;

}

 

 

/**

*

* A simple method that will display multi-dimension results in a tabular format.

*

* @param result set generated from reading a information map using multi-dimension data.

*

* @throws Exception

*/

private void DisplayTableExample(com.sas.iquery.execution2.ResultSetInterface resultSet) throws Exception

{         

          // Use result set to setup OLAPDataSetInterface that is used to visually display OLAP data

com.sas.storage.olap.OLAPDataSetInterface olapDataSet = null;

olapDataSet = (com.sas.storage.olap.OLAPDataSetInterface)resultSet.getResultSet();

 

// Set up a Java Frame to display Simple Table

Object obj = olapDataSet;

javax.swing.JDialog m_testDialog;

javax.swing.JFrame m_mainFrame = null;

 

// Create the panel to hold the results of 

javax.swing.JPanel ButtonPanel = new javax.swing.JPanel( new FlowLayout(FlowLayout.RIGHT, 5, 10) );

javax.swing.JButton closeButton = new javax.swing.JButton("Close");

java.awt.Container table = new java.awt.Container();

int tableWidth = 0, tableHeight = 0;

table.setLayout( new BorderLayout() );

if ( obj instanceof com.sas.storage.olap.OLAPDataSetInterface )

{        table.setLayout(new AutoSizingGridLayout(0, 1, 5, 5, true, true, true));

com.sas.swing.models.OLAPTableModelAdapter adapter = new com.sas.swing.models.OLAPTableModelAdapter((com.sas.storage.olap.OLAPDataSetInterface)obj);

com.sas.swing.visuals.olaptableview.OLAPTableView olapData = new OLAPTableView();

olapData.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);

olapData.setModel( adapter );

javax.swing.JScrollPane olapPane = new javax.swing.JScrollPane(olapData);

table.add(BorderLayout.CENTER, olapPane);

tableWidth = olapData.getPreferredSize().width ;

tableHeight = (olapData.getPreferredSize().height) + 75 ;

 

// Fill in the dialog

String resultTitle = "Testing Dialog";

m_testDialog = new javax.swing.JDialog( m_mainFrame, resultTitle, true );

javax.swing.JPanel panel = new javax.swing.JPanel();

panel.setLayout( new BorderLayout() );

m_testDialog.setContentPane( panel );

ButtonPanel.add( closeButton );

closeButton.addActionListener( new ActionListener()

{ public void actionPerformed(ActionEvent event)

{ }

});

panel.add( BorderLayout.CENTER, table );

panel.add( BorderLayout.SOUTH, ButtonPanel );

tableHeight += ButtonPanel.getPreferredSize().height;

if ( tableHeight < 200 ) tableHeight = 200;

if ( tableWidth > 500 ) tableWidth = 500;

else if ( tableWidth < 300 ) tableWidth = 300;

if ( tableHeight > 600 ) tableHeight = 600;

m_testDialog.pack();

m_testDialog.setSize(tableWidth, tableHeight);

m_testDialog.setResizable( true );

m_testDialog.setVisible( true );

}

 

/**

*

* A simple method doing one-time setup needed to run one or more examples.

*

* @param session the logged-in BI Platform session for this user

*

* @throws Exception

*/

private void setup(com.sas.services.session.SessionContextInterface session) throws Exception {

 

// Save away the logged-in BI Platform session for this user

sessionContext = session;

 

// Create a new SAS Query Services' metadata service and save it away

m_IQService = com.sas.iquery.metadata.IntelligentQueryMetadataServiceFactory.newService();

}

 

/**

* Given a path to an information map in a repository, return the

* InformationMap. The path must be in the form of an SBIP URL as

* described by the com.sas.services.information.metadata.PathUrl class.

* @param pathToMap the path, given in String form, to the Information Map to return

* @return an information map or null if no map is at the given path

* @throws Exception

*/

private com.sas.iquery.metadata.business.InformationMap readMapFromRepository(String pathToMap) throws Exception {

 

// Get PathUrl form of path to the map

com.sas.services.information.metadata.PathUrl pathUrl = new com.sas.services.information.metadata.PathUrl(pathToMap);

 

// Get the full information map from the repository

com.sas.iquery.metadata.business.InformationMap map = m_IQService.getInformationMap(sessionContext, pathUrl);

 

return map;

}

/**

*

* Creates and returns a data selection based upon the given information map

* but having no portion of the query specification (BusinessQuery interface)

* set in it.

* <p>

* The DataSelection interface is composed of two interfaces ... one providing

* modeling capabilities (the BusinessModel interface) and one providing the

* query specification (the BusinessQuery interface). This method only deals

* with setting up the modeling portion of the data selection but does not

* provide for setting any part of the business query specification.

*

* @param map the information map to base the data selection on

* @return the new DataSelection created

* @throws Exception

*/

private com.sas.iquery.metadata.business.DataSelection newDataSelectionBasedOnMap(com.sas.iquery.metadata.business.InformationMap map) throws Exception {

 

// Create an "empty" new DataSelection based on the information map given.

com.sas.iquery.metadata.business.DataSelection ds = com.sas.iquery.metadata.business.DataSelectionFactory.newDataSelection(map);

// For this utility method, we get all of the data items in the information map

java.util.List mapDataItems = ds.getObjects(true, com.sas.iquery.metadata.business.DataItem.class);

 

// For all of the data items in the information map that we want to

// be able to use in the data selection and be able to tweak the

// data item's attributes (for example, change the format, etc.), we create

// a DataItemReference in the data selection for each DataItem in the map.

// The DataItemReference is a wrapper around the DataItem that allows for

// making some changes to a data item's usage in a data selection.

 

// In this simple method, we grab all data items and create DIRs for them.

// Doing so does not cause any significant performance penalty during query

// time. It only makes the data items available for having their attributes

// changeable when used in a data selection.

// If a DataItemReference is not created for a data item in a map, the data item

// from the map can still be used in the data selection but it's attributes

// should not be changed (since doing so can cause the base InformationMap

// to change if it is stored out).

 

for (    java.util.Iterator it=mapDataItems.iterator(); it.hasNext(); ) {

com.sas.iquery.metadata.business.DataItem mapDataItem = (com.sas.iquery.metadata.business.DataItem) it.next();

 

// Creates a new data item reference

com.sas.iquery.metadata.business.DataItemReference dir = ds.newDataItemReference(mapDataItem);

 

// Add it to the model of the data selection.

// The DIR is unusable in the data selection if this is not done.

ds.addBusinessItem(dir);

}

 

return ds;

}

 

/**

* When a data selection is no longer needed, it should be removed from

* it's parent model. In this sample code, it's parent model is an information map.

*

* @param doneWithThisDataSelection The data selection that is no longer needed.

* @throws Exception

*/

private void doneWithDataSelection(com.sas.iquery.metadata.business.DataSelection doneWithThisDataSelection) throws Exception {

// Remove the data selection from it's parent model's children

doneWithThisDataSelection.getParentBusinessModel().removeChild(doneWithThisDataSelection);

 

 

 

    

}