MicroStrategy ONE
Sample Web Objects Application
The section includes a sample Web Objects application that demonstrates the use of the Web Objects to connect to MicroStrategy Intelligence Server. The tasks involved are authenticating the user credentials, searching for a report, executing the report, and displaying the results. This example demonstrates the general points of using the API, many of which were discussed in the Introduction to Web Objects API section. The details of the particular interfaces and methods are examined in further sections. The code for the complete sample Web Objects application is provided towards the end.
Application flow
The major tasks performed in this simple application are as follows.
Normally, each of these tasks involves a set of user inputs to choose various options, rather than specifying the values of the parameters in the code.
Connecting to and authenticating against Intelligence Server
Before starting any action with the Web Objects API, an instance of the WebIServerSession object is created using WebObjectsFactory. User actions such as connecting to Intelligence Server and project are accessed from here.
//create factory object
WebObjectsFactory factory=WebObjectsFactory.getInstance();
WebIServerSession serverSession=factory.getIServerSession();
//set up session properties
serverSession.setServerName("e_davis");
serverSession.setServerPort(0);
serverSession.setProjectName("VMall");
serverSession.setLogin("administrator");
serverSession.setPassword("");
serverSession.setApplicationType(EnumDSSXMLApplicationType.DssXmlApplicationCustomApp);
try {
Log.logger.log(Level.INFO, "Session created with ID: "+serverSession.getSessionID());
} catch (WebObjectsException ex) {}
In the above example, the WebIServerSession interface is used to connect to Intelligence Server by supplying the name of Intelligence Server, name of the project, and user name. This interface can be used to manipulate a number of user properties such as the password.
Searching for a particular report
After connecting to Intelligence Server, find a report in the project and execute it. The search functionality of the Web Objects API exposes ways to locate an object within a project. The WebObjectSource interface is the starting point for object-related operations such as searching. Note that the WebObjectsFactory is used again to create an instance of the WebObjectSource object. Since a successful connection is established to the project, the object source is authenticated to perform actions against Intelligence Server.
//search for a particular report
WebObjectSource objectSource=factory.getObjectSource();
WebSearch webSearch=objectSource.getNewSearchObject();
webSearch.setNamePattern("Regional Sales by Year");
webSearch.setSearchFlags(EnumDSSXMLSearchFlags.DssXmlSearchNameWildCard+EnumDSSXMLSearchFlags.DssXmlSearchRootRecursive);
webSearch.setAsync(false);
try {
webSearch.submit();
} catch (WebObjectsException ex) {}
WebFolder resultsFolder=null;
try {
resultsFolder=webSearch.getResults();
} catch (WebObjectsException ex) {}
WebObjectInfo reportObject=resultsFolder.get(0);
This search example searches for a particular report (Regional Sales by Year) within the project. Here, many of the search options are blank so that they are set to their default values. The name of the report being searched, the search flags, and the fact that the search is to be synchronous, are specified.
The search flags dictate the behavior of the search. The following two flags are specified:
-
The first flag DssXmlSearchNameWildCard indicates that a wildcard search is performed on the name of the object. A wildcard flag search is a search where Intelligence Server searches for the string of characters in the object property specified. Wildcard characters such as the asterisk are permitted.
-
The second flag DssXmlSearchRootRecursive indicates that all subdirectories for the object specified need to be searched starting from the root folder. If required, you can specify a particular folder from where the search can start. In this case, the root of the project is used.
Since this type of search can be fairly time-consuming for a large project, it is possible for you to receive status updates on the progress of the search. However, in this simple case, you must wait while the search is taking place. To do this, the search is set to be synchronous.
Finally, the search is submitted and the results are retrieved. Since a search can retrieve many objects, these are placed into a folder object, which is a collection of MicroStrategy Objects such as reports, templates, filters, and other folders. In this simple case the logic is hard coded to retrieve the first object returned.
This search returns an instance of a metadata object. Each object in the metadata shares a common set of methods and properties that are represented by the WebObjectInfo interface. Properties exposed by this interface include the name and description of the report. In addition, the type and ID of the object are exposed as well. In this case, the type of the object is a report definition. The ID is a unique identifier representing that object within the metadata. It is a character string of length 32 with each character being a hexadecimal digit. Most operations in the API are done using object IDs, since it is a fast and efficient way to uniquely identity objects.
Executing a report
The WebReportSource object is used to execute, manipulate, and display reports. Again, the factory object is used to create an instance of this source object.
WebReportSource reportSource=factory.getReportSource();
WebReportInstance reportInstance=null;
try {
reportInstance=reportSource.getNewInstance(reportObject.getID());
} catch (WebObjectsException ex) {}
reportInstance.setAsync(false);
WebReportGrid reportGrid=null;
try {
WebReportData reportData=reportInstance.getResults();
reportGrid=reportData.getWebReportGrid();
} catch (WebObjectsException ex) {}
A report instance is used to execute a report. A report instance is different from a report definition since it represents the report data (if the report has been executed), in addition to the report definition. The report instance can also include manipulations that were not present in the original definition, such as pivots and sorts. Once a user has finished with a report instance, he can save the report instance to keep the modified report definition.
To create a report instance, the WebReportSource object is supplied with the report object ID. Before executing the report, specify that the execution is to be synchronous. This means that the program pauses until the report returns. The report is then submitted using the getResults method. In this simple case, the status of the report has not been checked to query whether the report executed successfully or was waiting on a user input (such as in the case of prompts). Finally, an instance of a WebReportGrid object is obtained. This object represents the data within the grid, which can be used to render a simple grid.
Retrieving the results of the report and displaying them
You have now connected to a project, located a report by its name, and executed that report. Finally, display the results in a simple grid. To do this, use the WebReportGrid instance, which can be obtained from the report instance.
The WebReportGrid interface exposes different areas of the grid such as the headers and the rows through separate objects.
WebGridRows gridRows=reportGrid.getGridRows();
WebGridHeaders columnHeaders=reportGrid.getColumnHeaders();
for (int i=0;i<columnHeaders.size();i++) {
for (int j=0;j<reportGrid.getRowTitles().size();j++) {
Log.logger.log(Level.INFO, "\t");
}
WebHeaders headers=columnHeaders.get(i);
for (int j=0;j<headers.size();j++) {
WebHeader header=headers.get(j);
Log.logger.log(Level.INFO, header.getDisplayName()+"\t");
}
Log.logger.log(Level.INFO, "\n");
}
for (int i=0;i<gridRows.size();i++) {
WebRow row=gridRows.get(i);
WebHeaders rowHeaders=row.getHeaderElements();
for (int j=0;j<rowHeaders.size();j++) {
WebHeader header=rowHeaders.get(j);
Log.logger.log(Level.INFO, header.getDisplayName()+"\t");
}
for (int j=0;j<row.size();j++) {
WebRowValue value=row.get(j);
Log.logger.log(Level.INFO, value.getValue()+"\t");
}
Log.logger.log(Level.INFO, "\n");
}
Code for the complete sample Web Objects application
This is the code for the complete sample Web Objects application.
/*
* Sample Web Objects Application
*
*/
package sdksamples;
import com.microstrategy.web.objects.*;
import com.microstrategy.webapi.*;
public class OverviewSample {
public static void runSample() {
//create factory object
WebObjectsFactory factory=WebObjectsFactory.getInstance();
WebIServerSession serverSession=factory.getIServerSession();
//set up session properties
serverSession.setServerName("IServer");
serverSession.setServerPort(0);
serverSession.setProjectName("MyProject");
serverSession.setLogin("administrator");
serverSession.setPassword("");
//initialize the session with the above parameters
try {
Log.logger.log(Level.INFO, "Session created with ID: "+serverSession.getSessionID());
} catch (WebObjectsException ex) {
handleError(null,"Error creating session:"+ex.getMessage());
}
//search for a particular report
WebObjectSource objectSource=factory.getObjectSource();
WebSearch webSearch=objectSource.getNewSearchObject();
webSearch.setNamePattern("h. Drilling");
webSearch.setSearchFlags(EnumDSSXMLSearchFlags.DssXmlSearchNameWildCard+EnumDSSXMLSearchFlags.DssXmlSearchRootRecursive);
SimpleList types=webSearch.types();
Integer type=new Integer(EnumDSSXMLObjectTypes.DssXmlTypeReportDefinition);
types.add(type);
webSearch.setAsync(false);
try {
webSearch.submit();
} catch (WebObjectsException ex) {
handleError(serverSession,"Error executing search:"+ex.getMessage());
}
WebFolder resultsFolder=null;
try {
resultsFolder=webSearch.getResults();
} catch (WebObjectsException ex) {
handleError(serverSession,"Error retrieving search results:"+ex.getMessage());
}
if ((resultsFolder==null) || resultsFolder.size()==0) {
handleError(serverSession,"No search results were found");
}
WebObjectInfo reportObject=resultsFolder.get(0);
if ((reportObject==null)) {
handleError(serverSession,"Invalid report object");
}
//execute the report
WebReportSource reportSource=factory.getReportSource();
WebReportInstance reportInstance=null;
try {
reportInstance=reportSource.getNewInstance(reportObject.getID());
} catch (WebObjectsException ex) {
OverviewSample.handleError(serverSession,"Error creating report instance: "+ex);
}
reportInstance.setAsync(false);
WebReportGrid reportGrid=null;
try {
WebReportData reportData=reportInstance.getResults();
reportGrid=reportData.getWebReportGrid();
} catch (WebObjectsException ex) {
handleError(serverSession,"Error retrieving the report data: "+ex);
}
Log.logger.log(Level.INFO, "Report returned with "+reportGrid.getGridRows().size()+" rows");
WebGridRows gridRows=reportGrid.getGridRows();
WebGridHeaders columnHeaders=reportGrid.getColumnHeaders();
for (int i=0;i<columnHeaders.size();i++) {
for (int j=0;j<reportGrid.getRowTitles().size();j++) {
Log.logger.log(Level.INFO, "\t");
}
WebHeaders headers=columnHeaders.get(i);
for (int j=0;j<headers.size();j++) {
WebHeader header=headers.get(j);
Log.logger.log(Level.INFO, header.getDisplayName()+"\t");
}
Log.logger.log(Level.INFO, "\n");
}
for (int i=0;i<gridRows.size();i++) {
WebRow row=gridRows.get(i);
WebHeaders rowHeaders=row.getHeaderElements();
for (int j=0;j<rowHeaders.size();j++) {
WebHeader header=rowHeaders.get(j);
Log.logger.log(Level.INFO, header.getDisplayName()+"\t");
}
for (int j=0;j<row.size();j++) {
WebRowValue value=row.get(j);
Log.logger.log(Level.INFO, value.getValue()+"\t");
}
Log.logger.log(Level.INFO, "\n");
}
//close the session to clean up resources on Intelligence Server
closeSession(serverSession);
}
public static void closeSession(WebIServerSession serverSession) {
try {
serverSession.closeSession();
} catch (WebObjectsException ex) {
Log.logger.log(Level.ERROR, "Error closing session:"+ex.getMessage());
return;
}
Log.logger.log(Level.INFO, "Session closed.");
}
public static void handleError(WebIServerSession serverSession,String message) {
Log.logger.log(Level.INFO, message);
closeSession(serverSession);
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
OverviewSample.runSample();
}
}