# Common Setup Example

The following coding example performs common task such as create a user session, retrieve a map, create a data selection, read dataitems, and remove a data selection from a parent model.

// 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

/**

*

* 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 = com.sas.iquery.metadata.business.InformationMapFactory.getInstance().newInformationMap(sessionContext, m_IQService, 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.

*<p>

* The choosing of which data item to return from a query is done through

* the BusinessQuery interface. Look at the Standard Table example in the

* main Javadoc for setting a result item on the BusinessQuery.

*

* @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 = map.getObjects(false, 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 information map

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

}