![]() | ![]() | ![]() | ![]() | ![]() |
This example creates an OLAPTableView and configures it to display OLAP data by using SAS Foundation Services. A JButton is added and configured in order to bring up a dialog that enables users to select a table from a list of information maps. A JLabel shows the status of the actions that are being performed.
To create an empty OLAPTableView and connect to data, follow these steps:
The resulting code will look something like this:
...
Create an empty result set for the OLAPDataSet
...
OLAPDataSet model = new OLAPDataSet(resultSet);
OLAPTableModelAdapter adapter = new OLAPTableModelAdapter(model);
OLAPTableView tableView1 = new OLAPTableView(adapter);
...
|
The code shown on the Full Code tab will connect to data and create a BusinessQueryToOLAPDataSetAdapter that is based on the data selection and display a default OLAPTableView with that model.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a SAS Java Project, see SAS Note 32218.
private static final String SERVER_HOST = "[metadata host]";
private static final String SERVER_PORT = "8561";
private static final String SERVER_IDENTITY_ID = "saswbadm";
private static final String SERVER_IDENTITY_PASSWORD = "[password]";
private static final String SERVER_REPOSITORY_NAME = "Foundation";
private static final String DOMAIN = "DefaultAuth";
private static final String USERNAME = "sasdemo";
private static final String PASSWORD = "[password]";
package samples;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import com.sas.iquery.metadata.IntelligentQueryMetadataService;
import com.sas.iquery.metadata.IntelligentQueryMetadataServiceFactory;
import com.sas.iquery.metadata.business.DataSelection;
import com.sas.iquery.metadata.business.DataSelectionFactory;
import com.sas.iquery.metadata.business.InformationMap;
import com.sas.services.deployment.MetadataSourceInterface;
import com.sas.services.deployment.OMRMetadataSource;
import com.sas.services.deployment.ServiceLoader;
import com.sas.services.deployment.URLMetadataSource;
import com.sas.services.discovery.DiscoveryService;
import com.sas.services.discovery.DiscoveryServiceInterface;
import com.sas.services.discovery.ServiceTemplate;
import com.sas.services.information.metadata.PathUrl;
import com.sas.services.session.SessionContextInterface;
import com.sas.services.session.SessionServiceInterface;
import com.sas.services.user.UserContextInterface;
import com.sas.services.user.UserServiceInterface;
import com.sas.storage.iquery.BusinessQueryToOLAPDataSetAdapter;
import com.sas.storage.olap.AxisInterface;
import com.sas.storage.olap.OLAPException;
import com.sas.storage.olap.TupleElementInterface;
import com.sas.storage.olap.TupleInterface;
import com.sas.storage.olap.embedded.Axis;
import com.sas.storage.olap.embedded.OLAPDataSet;
import com.sas.storage.olap.embedded.ResultSet;
import com.sas.storage.olap.embedded.ResultSetMetadata;
import com.sas.storage.olap.embedded.Tuple;
import com.sas.storage.olap.embedded.TupleElement;
import com.sas.swing.models.OLAPTableModelAdapter;
import com.sas.swing.visuals.olaptableview.OLAPTableView;
import com.sas.swing.visuals.remotefileselector.InformationServicesSelectorPanel;
import com.sas.swing.visuals.remotefileselector.RemoteFileSelectorDialog;
import com.sas.swing.visuals.util.Util;
import com.sas.util.SasPasswordString;
public class ConnectingOLAPTableViewApp extends JFrame implements
java.awt.event.WindowListener {
private static final long serialVersionUID = 1L;
private static final String SERVER_HOST = "[metadata host]";
private static final String SERVER_PORT = "8561";
private static final String SERVER_IDENTITY_ID = "saswbadm";
private static final String SERVER_IDENTITY_PASSWORD = "[password]";
private static final String SERVER_REPOSITORY_NAME = "Foundation";
private static final String DOMAIN = "DefaultAuth";
private static final String USERNAME = "sasdemo";
private static final String PASSWORD = "[password]";
protected OLAPTableView olapTable;
//protected javax.swing.JScrollPane JScrollPane1;
protected javax.swing.JLabel JLabel1;
protected javax.swing.JLabel JLabel2;
protected javax.swing.JButton JButton1;
// Handles to services in SAS Foundation Services.
private DiscoveryServiceInterface discoveryService;
private SessionServiceInterface sessionService;
private UserServiceInterface userService;
// Handles to contexts in SAS Foundation Services.
private SessionContextInterface sessionContext;
private UserContextInterface userContext;
// Our internal BusinessQuery dataSelection.
private DataSelection dataSelection;
public static void main(String[] args) {
//set swing look and feel with system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
ConnectingOLAPTableViewApp simpleApp = new ConnectingOLAPTableViewApp();
simpleApp.createFrame(args).setVisible(true);
}
public JFrame createFrame(String[] args) {
JFrame frame = new JFrame(this.getClass().getName());
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
if (olapTable == null) {
try {
// create some dummy data to populate the table with initially
// this dummy table will display until the user chooses a table with the select table button
TupleInterface tuple1 = new Tuple(new TupleElementInterface[] {
new TupleElement("Column A"), new TupleElement("a") });
TupleInterface tuple2 = new Tuple(new TupleElementInterface[] {
new TupleElement("Column A"), new TupleElement("b") });
TupleInterface tuple3 = new Tuple(new TupleElementInterface[] {
new TupleElement("Column B"), new TupleElement("c") });
TupleInterface tuple4 = new Tuple(new TupleElementInterface[] {
new TupleElement("Column B"), new TupleElement("d") });
TupleInterface tuple5 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row A"), new TupleElement("a") });
TupleInterface tuple6 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row A"), new TupleElement("b") });
TupleInterface tuple7 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row B"), new TupleElement("c") });
TupleInterface tuple8 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row B"), new TupleElement("d") });
TupleInterface tuple9 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row C"), new TupleElement("e") });
TupleInterface tuple10 = new Tuple(new TupleElementInterface[] {
new TupleElement("Row C"), new TupleElement("f") });
Axis columnAxis = new Axis(AxisInterface.COLUMNS_AXIS, null,
new TupleInterface[] { tuple1, tuple2, tuple3, tuple4 });
columnAxis.setAxisHeaders(new String[] { "", "" });
Axis rowAxis = new Axis(AxisInterface.ROWS_AXIS, null,
new TupleInterface[] { tuple5, tuple6, tuple7, tuple8,
tuple9, tuple10 });
rowAxis.setAxisHeaders(new String[] { "", "" });
AxisInterface[] axes = { columnAxis, rowAxis };
//create an instance of result set metadata with the axes
ResultSetMetadata resultSetMetadata = new ResultSetMetadata(
axes);
ResultSet resultSet = new ResultSet();
resultSet.setResultSetMetadata(resultSetMetadata);
Object[] values = new Object[] { " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " " };
resultSet.setCells(0, 23, values);
// create a new OLAPDataSet with the result set
OLAPDataSet defaultEmptyOLAPTableModel = new OLAPDataSet(
resultSet);
// create an OLAPTableModelAdapter from that OLAPDataSet
OLAPTableModelAdapter adapter = new OLAPTableModelAdapter(
defaultEmptyOLAPTableModel);
// create a new OLAPTableView using the OLAPTableModelAdapter as the model
olapTable = new OLAPTableView(adapter);
// set the size of the OLAPTableView
olapTable.setPreferredScrollableViewportSize(new Dimension(300,
175));
} catch (OLAPException e) {
System.out.println(e);
}
}
// create a scroll pane and add it to the center of the content pane
JScrollPane scrollPane = new JScrollPane(olapTable);
contentPane.add(scrollPane, BorderLayout.CENTER);
// create a status bar - we will add it to the bottom of the content pane later
Container statusBar = new Container();
statusBar.setLayout(new BorderLayout());
JLabel1 = new JLabel(" Status: ");
JLabel2 = new JLabel();
statusBar.add(JLabel1, BorderLayout.WEST);
statusBar.add(JLabel2, BorderLayout.CENTER);
// create a container for the select table button and
// add blank areas to east and west so button won't stretch over whole window
Container selectTable = new Container();
selectTable.setLayout(new BorderLayout());
JButton1 = new JButton("Select Table");
JLabel dummyLabel1 = new JLabel();
dummyLabel1.setPreferredSize(new java.awt.Dimension(100, 30));
JLabel dummyLabel2 = new JLabel();
dummyLabel2.setPreferredSize(new java.awt.Dimension(100, 30));
selectTable.add(dummyLabel1, BorderLayout.WEST);
selectTable.add(JButton1, BorderLayout.CENTER);
selectTable.add(dummyLabel2, BorderLayout.EAST);
// add the select table button to the bottom of the status bar container
statusBar.add(selectTable, BorderLayout.SOUTH);
// add the status bar container to the bottom of the content pane
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.setSize(500, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
// initialize connections
initConnections();
return frame;
}
/*
* showException
* Display a modal dialog box indicating some type of exception
*/
public void showException(Exception e) {
java.lang.String name = "OLAPTableViewMain";
javax.swing.JOptionPane.showMessageDialog(this, "Fatal error : "
+ e.getMessage(), name, javax.swing.JOptionPane.ERROR_MESSAGE);
}
/**
* destroy
* Called to destroy the frame.
*/
public void destroy() {
try {
FSDisconnect();
} catch (Exception e) {
showException(e);
}
}
/**
* windowActivated
* Invoked when a window is activated.
*/
public void windowActivated(java.awt.event.WindowEvent event) {
}
/**
* windowClosed
* Invoked when a window has been closed.
*/
public void windowClosed(java.awt.event.WindowEvent event) {
destroy();
System.exit(0);
}
/**
* windowClosing
* Invoked when a window is in the process of closing.
*/
public void windowClosing(java.awt.event.WindowEvent event) {
dispose();
}
/**
* windowDeactivated
* Invoked when a window is de-activated.
*/
public void windowDeactivated(java.awt.event.WindowEvent event) {
}
/**
* windowDeiconified
* Invoked when a window is de-iconified.
*/
public void windowDeiconified(java.awt.event.WindowEvent event) {
}
/**
* windowIconified
* Invoked when a window is iconified.
*/
public void windowIconified(java.awt.event.WindowEvent event) {
}
/**
* windowOpened
* Invoked when a window has been opened
*/
public void windowOpened(java.awt.event.WindowEvent event) {
}
/**
* Deploys SAS Foundation Services using either a specified XML deployment file or by
* identifying a location in the SAS Metadata Server.
* @param xmlDeploymentFile The name of the XML deployment file to use (optional).
* If null, the services will be deployed using information in the SAS Metadata Server.
* @exception Exception
*/
public void FSDeploy(String xmlDeploymentFile) throws Exception {
if (discoveryService != null) {
return;
}
//Set JVM env variables to enable authentication via Platform Services;
com.sas.net.FileResourceLocator policy = new com.sas.net.FileResourceLocator(
"java.app.policy");
com.sas.net.FileResourceLocator login = new com.sas.net.FileResourceLocator(
"login.config");
try {
java.lang.System.setProperty("java.security.auth.login.config",
login.getResource().toString());
} catch (Exception e) {
throw new Exception(
"Cannot load the login.config file. Make sure it exists in the same directory as the application.");
}
try {
java.lang.System.setProperty("java.security.policy", policy
.getResource().toString());
} catch (Exception e) {
throw new Exception(
"Cannot load the policy file. Make sure it exists in the same directory as the application.");
}
final String applicationDeploymentName = "ADS Local Services";
final String serviceDeploymentGroupName = "BIP Core Services";
MetadataSourceInterface metadataSource = null;
// If an XML deployment file was specified...
if (xmlDeploymentFile != null) {
// Create a metadata source from the XML deployment file.
com.sas.net.ClassResourceLocator crl = new com.sas.net.ClassResourceLocator(
xmlDeploymentFile, this.getClass());
metadataSource = new URLMetadataSource(crl.getResource(),
applicationDeploymentName, serviceDeploymentGroupName);
} else {
// Create a metadata source for an Open Metadata Repository
final String serverIdentityPassword = SasPasswordString
.encode(SasPasswordString.SAS001_ENCODING,
SERVER_IDENTITY_PASSWORD);
metadataSource = new OMRMetadataSource(SERVER_HOST, SERVER_PORT,
SERVER_IDENTITY_ID, serverIdentityPassword,
SERVER_REPOSITORY_NAME, applicationDeploymentName,
serviceDeploymentGroupName);
}
// Obtain the local discovery service
discoveryService = DiscoveryService.defaultInstance();
// Deploy the services using the created metadata source.
ServiceLoader.deployServices(metadataSource, discoveryService);
}
/**
* Logs into the SAS Foundation Services that were obtained
* via the FSConnect method.
* @exception Exception
*/
public void FSLogin() throws Exception {
// Use the remote discovery services to find a user service.
userService = (UserServiceInterface) discoveryService
.findService(new ServiceTemplate(
new Class[] { com.sas.services.user.UserServiceInterface.class }));
// Get a user context from the user service by logging in.
final String password = SasPasswordString.encode(
SasPasswordString.SAS001_ENCODING, PASSWORD);
userContext = userService.newUser(USERNAME, password, DOMAIN);
// Use the remote discovery services to find a session service.
sessionService = (SessionServiceInterface) discoveryService
.findService(new ServiceTemplate(
new Class[] { com.sas.services.session.SessionServiceInterface.class }));
// Create a session for this user and bind it to the user context.
sessionContext = sessionService.newSessionContext(userContext);
userContext.setSessionContext(sessionContext);
}
/**
* Loads the BusinessQuery InformationMap identified by the mapName parameter,
* and creates and populates a BQ data selection object based on the contents
* of the InformationMap.
* @param mapName The name of the InformationMap to load.
* @exception Exception
* @return A boolean value representing whether or not the map was loaded.
*/
public boolean BQGetDataSelection(String mapName) throws Exception {
// Get a BusinessQuery metadata service object.
IntelligentQueryMetadataService queryService = (IntelligentQueryMetadataService) IntelligentQueryMetadataServiceFactory
.newService();
// Get a specific InformationMap from the metadata service object.
PathUrl pathURL = new PathUrl(mapName);
InformationMap informationMap = queryService.getInformationMap(
sessionContext, pathURL);
if (informationMap != null) {
// Determine the structure of the data in the InformationMap.
if (!informationMap.getStructure().isOLAP()) {
javax.swing.JOptionPane.showMessageDialog(this,
"The selected map doesn't represent OLAP data.",
"Error", javax.swing.JOptionPane.ERROR_MESSAGE);
return false;
}
// Create a dataSelection from the InformationMap and add ResultItems to it
// to create a sample. Since we're passing in null for the maxElements parameter,
// only 2 Hierarchies and 2 Measures will be used.
dataSelection = DataSelectionFactory.newSampleDataSelection(
informationMap, null);
}
return true;
}
/**
* Disconnects from the SAS Foundation Services and releases any used resources.
* @exception Exception
*/
public void FSDisconnect() throws Exception {
// Release the remote discovery service
if (discoveryService != null)
discoveryService.destroy();
}
/**
* This is the primary entry point for this example. It connects and logs into
* the SAS Foundation Services, then obtains a BusinessQuery data selection from
* the requested InformationMap.
*
* The data selection is then adapted to fit into the OLAPTableView object, where
* it is then appropriately rendered.
* @param mapName The name of the InformationMap to load.
*/
public void LoadInformationMap(String mapName) {
// try to load the information map chosen by the user
// update the status on the status bar in JLabel2
try {
JLabel2
.setText("Building DataSelection from requested InformationMap...");
if (BQGetDataSelection(mapName)) {
JLabel2
.setText("Building BusinessQuery/OLAP dataset adapter...");
BusinessQueryToOLAPDataSetAdapter businessQueryToOLAPDataSetAdapter = new BusinessQueryToOLAPDataSetAdapter(
dataSelection);
JLabel2.setText("Building OLAP table model adapter...");
final OLAPTableModelAdapter olapTableModelAdapter = new OLAPTableModelAdapter(
businessQueryToOLAPDataSetAdapter);
JLabel2.setText("Setting model for OLAPTableView...");
// Note that setting the model needs to be performed back in
// the original event queue thread.
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
olapTable.setModel(olapTableModelAdapter);
JLabel2.setText("Done.");
}
});
}
} catch (Exception e) {
showException(e);
}
}
public String populateList(String mapPath) {
// populate the list of available information maps
try {
JLabel2.setText("Deploying SAS Foundation Services...");
FSDeploy(null);
JLabel2.setText("Logging in to SAS Foundation Services...");
FSLogin();
java.util.List repos = userContext.getRepositories();
com.sas.services.information.RepositoryInterface reposInt = (com.sas.services.information.RepositoryInterface) repos
.get(0);
String startDir = "";
if (mapPath != null) {
int index = mapPath.lastIndexOf("/");
if (index != -1) {
startDir = mapPath.substring(0, index);
}
}
java.util.Map fileTypes = new java.util.HashMap();
fileTypes.put("Information Maps", "InformationMap");
InformationServicesSelectorPanel panel = new InformationServicesSelectorPanel(
startDir, reposInt, fileTypes, false);
RemoteFileSelectorDialog dialog = new RemoteFileSelectorDialog(Util
.getParentAWTFrame(this), "Select an Information Map",
true, 2, panel);
dialog.setVisible(true);
java.util.List items = new java.util.ArrayList();
items = panel.getReturnedInformation(true);
if (items != null && items.size() > 0) {
mapPath = items.get(0).toString();
}
return mapPath;
} catch (Exception e) {
showException(e);
return "";
}
}
public void initConnections() {
// set a listener on the select table button
TestMainInnerAdapter0 TestMainInnerAdapter0_Instance = new TestMainInnerAdapter0(
this);
JButton1.addActionListener(TestMainInnerAdapter0_Instance);
validate();
}
private class TestMainInnerAdapter0 implements
java.awt.event.ActionListener {
// bring up the window for selecting an information map when the user clicks the select table button
ConnectingOLAPTableViewApp m_container;
public TestMainInnerAdapter0(ConnectingOLAPTableViewApp container) {
this.m_container = container;
}
public void actionPerformed(java.awt.event.ActionEvent p0) {
try {
if (p0.getSource() == m_container.JButton1) {
String selection = populateList("
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
![]()
Empty OLAPTableView
![]()
OLAPTableView with Live Data
| Type: | Sample |
| Date Modified: | 2008-07-10 11:41:28 |
| Date Created: | 2006-01-11 14:32:49 |
| Product Family | Product | Host | Product Release | SAS Release | ||
| Starting | Ending | Starting | Ending | |||
| SAS System | SAS AppDev Studio | Microsoft Windows Server 2003 Standard Edition | 3.2 | 9.1 TS1M3 SP4 | ||
| Microsoft Windows XP Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Enterprise Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Datacenter Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows NT Workstation | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Datacenter Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Advanced Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft® Windows® for x64 | 3.2 | 9.1 TS1M3 SP4 | ||||
| Windows Vista | 3.2 | 9.1 TS1M3 SP4 | ||||




