| Visual Data Explorer: Creating a new filter programmatically |
|
|
Data Source: Information Map
This example includes the following customizations:
Please install the latest webAF template updates prior to building this example. For more information about the server-side example templates used by this example, see Web Application Example Templates and Built-in Web Application Templates and Options.
The following example is not meant to be a complete web application, rather it is to show how to use a particular component(s). The example does not address the issue of immediately freeing up resources when the user navigates off the web application or closes the web browser. Any necessary resources created in the example will stay around until the associated HTTPSession times out. If this example were used in a multi-user environment, it is possible to exhaust the available resources until HTTPSessions time out and free up their associated resources.
VisualDataExplorer.omr_password ) in
the sas_metadata_source_omr.properties
file located in the web-inf\conf directory. This password is used when
deploying Foundation Services.
metadata-password ) under
the InfoMapViewerExampleControllerServlet's initial parameters in the
web.xml file located in the web-inf directory. This password is used to
get a user and session context from the Foundation Services.
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sas.actionprovider.HttpActionProvider;
import com.sas.services.discovery.DiscoveryService;
import com.sas.services.discovery.DiscoveryServiceInterface;
import com.sas.services.discovery.ServiceTemplate;
import com.sas.services.session.SessionContextInterface;
import com.sas.services.session.SessionServiceInterface;
import com.sas.services.user.UserContextInterface;
import com.sas.services.user.UserServiceInterface;
public class InfoMapViewerExampleControllerServlet extends javax.servlet.http.HttpServlet {
// Global Web Application Strings
private static final String ACTION_PROVIDER = "sas_actionProvider";
private static final String SAS_MODEL = "sas_model";
/*
* doPost() Respond to the Post message.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Calling doGet to provide same behavior to POST and GET HTTP
// methods.
doGet(request, response);
}
/*
* doGet() Respond to the Get message.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Add User DO_GET code here
HttpSession session = request.getSession();
// Setup the ActionProvider
HttpActionProvider sas_actionProvider = null;
synchronized (session) {
SessionContextInterface sessionContext = null;
if (session != null) {
sessionContext = (SessionContextInterface) session
.getAttribute(SAS_MODEL);
}
if (sessionContext == null) {
try {
// Obtain the local discovery service
DiscoveryServiceInterface discoveryService = DiscoveryService
.defaultInstance();
// Use the discovery services to find a user service.
UserServiceInterface userService = (UserServiceInterface) discoveryService
.findService(new ServiceTemplate(
new Class[] { UserServiceInterface.class }));
ServletConfig sc = getServletConfig();
String domain = sc.getInitParameter("metadata-domain");
String username = sc.getInitParameter("metadata-userid");
String password = sc.getInitParameter("metadata-password");
UserContextInterface userContext = userService.newUser(
username, password, domain);
SessionServiceInterface sessionService = (SessionServiceInterface)
discoveryService.findService(new ServiceTemplate(
new Class[] { SessionServiceInterface.class }));
// Create a session for this user and bind it to the user
// context.
sessionContext = sessionService
.newSessionContext(userContext);
userContext.setSessionContext(sessionContext);
if (session != null) {
session.setAttribute(SAS_MODEL, sessionContext);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
synchronized (session) {
if (session != null) {
sas_actionProvider = (HttpActionProvider) session
.getAttribute(ACTION_PROVIDER);
}
// if ActionProvider is null, create one and put it on the session
if (sas_actionProvider == null) {
sas_actionProvider = new com.sas.actionprovider.HttpActionProvider();
sas_actionProvider.setLocale(request.getLocale());
sas_actionProvider.setControllerURL(request.getContextPath()
+ "/InfoMapViewerExample");
sas_actionProvider.setName(ACTION_PROVIDER);
// store object in its scope
if (session != null)
session.setAttribute(ACTION_PROVIDER, sas_actionProvider);
}
// else execute the ActionProvider command
else {
try
{
sas_actionProvider.executeCommand(request, response, response
.getWriter());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
RequestDispatcher rd = null;
String sas_forwardLocation = request
.getParameter("sas_forwardLocation");
if (sas_forwardLocation != null)
rd = getServletContext().getRequestDispatcher(sas_forwardLocation);
else
rd = getServletContext().getRequestDispatcher("/InfoMapViewerExampleViewer.jsp");
rd.forward(request, response);
}
}
<%@page import="com.sas.services.session.SessionContextInterface"%>
<%@page import="com.sas.actionprovider.HttpActionProvider"%>
<%@page import="com.sas.servlet.tbeans.dataexplorer.html.VisualDataExplorer"%>
<%@page import="com.sas.servlet.tbeans.dataexplorer.html.VisualDataExplorerMenuBar"%>
<%@page import="com.sas.dataexplorer.actionprovider.support.dataexplorer.VisualDataExplorerMenuBarAreaInterface"%>
<%@page import="com.sas.iquery.metadata.business.step.olap.DataItemMemberFilter"%>
<%@page import="com.sas.iquery.metadata.business.step.olap.DataItemDataBasedFilter"%>
<%@page import="com.sas.iquery.metadata.business.DataItem"%>
<%@page import="com.sas.iquery.metadata.business.BusinessQuery"%>
<%@page import="com.sas.iquery.metadata.IntelligentQueryMetadataServiceInterface"%>
<%@page import="com.sas.iquery.metadata.business.DataItem"%>
<%@page import="com.sas.iquery.metadata.business.DataItemReference"%>
<%@page import="com.sas.iquery.metadata.business.BusinessModel"%>
<%@page import="com.sas.iquery.metadata.expr.ComparisonOperator"%>
<%@page import="com.sas.iquery.metadata.business.Role"%>
<%@page import="com.sas.iquery.metadata.business.DataSelection"%>
<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<link href="styles/sasComponents.css" rel="stylesheet" type="text/css">
</head>
<body style="margin: 0px;">
<%
String mapPath = "mapName";
VisualDataExplorer vde = (VisualDataExplorer)session.getAttribute("vde");
if (vde == null)
{
SessionContextInterface model = (SessionContextInterface)session.getAttribute("sas_model");
vde = new VisualDataExplorer(model, mapPath );
vde.setId("vde");
HttpActionProvider ap = (HttpActionProvider)session.getAttribute("sas_actionProvider");
vde.setActionProvider(ap);
session.setAttribute("vde", vde);
}
if (vde.getActionProvider() == null)
{
HttpActionProvider ap = (HttpActionProvider)session.getAttribute("sas_actionProvider");
vde.setActionProvider(ap);
}
vde.setRequest(request);
vde.setResponse(response);
vde.write(out);
%>
</body>
</html>
String mapPath = "mapName";Note: This example requires an OLAP information map.
<%@page import="com.sas.iquery.metadata.business.step.olap.DataItemMemberFilter"%>
BusinessQuery data = (BusinessQuery)vde.getDataModel();
BusinessModel busModel = data.getBusinessModel();
java.util.List queryItems = data.getResultItems();
java.util.List objects = busModel.getObjects(true, DataItem.class);
java.util.Iterator iter = objects.iterator();
DataItem item = null;
DataItemReference dataItemRef;
// Don't keep looking for rows, cols, or measures once we've found them
boolean foundItem = false;
// Go through the data items and find the ones we want to add to rows and columns
while (iter.hasNext() && !foundItem)
{
item = ((DataItem)iter.next());
if (item.getLabel().equals("name_of_your_filter_level"))
{
//if this data item already has a reference in the query, set it on the new query
if (!queryItems.contains(item))
//otherwise, create a new reference for it
//and add that to the business model and to the new query
//else
{
dataItemRef = busModel.newDataItemReference((DataItem)item);
busModel.addBusinessItem(dataItemRef);
item = dataItemRef;
}
foundItem = true;
}
}
// Assuming we already know the correct name(s) for each member in the filter,
// Create our array of member names
String[] memberNames =
{"[name_of_your_filter_level].[All name_of_your_filter_level].[name_of_your_filter_item]"};
//create the new member filter step using known values
DataItemMemberFilter memberFilter =
new DataItemMemberFilter(memberNames, DataItemMemberFilter.LIST_MEMBER_FILTER_TYPE);
// Create a List for our filter step (or prepare an existing one)
java.util.List steps = java.util.Collections.singletonList(memberFilter);
//attempt to apply the new list of steps to the appropriate DataItem
if (item != null)
item.setSteps(steps);
<%@page import="com.sas.iquery.metadata.business.Role"%> <%@page import="com.sas.iquery.metadata.business.DataSelection"%> <%@page import="com.sas.iquery.metadata.business.step.olap.DataItemDataBasedFilter"%> <%@page import="com.sas.iquery.metadata.expr.ComparisonOperator"%>
DataSelection dataSelection = (DataSelection)vde.getDataModel();
// Prepare the filter pieces
String[] memberNames2 = {
"[name_of_your_filter_level].[All name_of_your_filter_level].[name_of_your_filter_item]",
"[Measures].[name_of_your_measure]" }; //the measure to filter on
ComparisonOperator operator = ComparisonOperator.COMPARE_GT; //our filter operator
Object value = "your_filter_value"; //some desirable filter value
// Create the new filter step
DataItemDataBasedFilter dataFilter =
new DataItemDataBasedFilter(memberNames2, operator, value);
// Create a List for our filter step (or prepare an existing one)
java.util.List filterSteps = java.util.Collections.singletonList(dataFilter);
// Attempt to apply the new list of steps to the DataSelection in a desired Role
dataSelection.setSteps(filterSteps, Role.ROW);
String[] memberNames2 = { "[GEOGRAPHIC].[All
GEOGRAPHIC].[CANADA]","[Measures].[SALES_COST]" };