End-to-End Relational Example

 

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

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JScrollPane;

import com.sas.visuals.AutoSizingGridLayout;

import com.sas.services.session.SessionContextInterface;

/**

*

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

*/

public class EndtoEndRelationalExample {

// 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 EndtoEndRelationalExample() 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(sessionContext);

      

    // 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>/ClassExample(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);

 

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

    DisplayRelationalTableExample(ds);

 

    

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

    doneWithDataSelection(ds);

}

 

/**

*

* A simple method that will display data selection using relational data in a tabular format.

*

* @param result set generated from reading a information map using relational data.

*

* @throws Exception

*/

private void DisplayRelationalTableExample(com.sas.iquery.metadata.business.DataSelection dataSelection) throws Exception

{         

             // Use result set to setup table adapter that is used to visually display relational data

com.sas.storage.iquery.BusinessQueryToTableModelAdapter adapter = new com.sas.storage.iquery.BusinessQueryToTableModelAdapter();
adapter.setModel(dataSelection);

 

// Set up a Java Frame to display Simple Table

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() );

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

com.sas.swing.visuals.tableview.TableView rtable= new com.sas.swing.visuals.tableview.TableView.TableView();
rtable.setColumnSelectionAllowed(true);
rtable.setCellSelectionEnabled(true);
rtable.setModel(adapter);

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

javax.swing.JScrollPane relationPane = new JScrollPane(rtable);

table.add(BorderLayout.CENTER, relationPane);

tableWidth = rtable.getPreferredSize().width ;

tableHeight = rtable.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 InformationMap 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);

 

 

 

    

}

}