Resources

SAS® AppDev Studio 3.0 Developer's Site

VisualDataExplorer: Overriding the Show Detail Action   About It Build It  

Data Source: Information Map

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.

Step 1: Create a project in webAF

  1. Create a new project named VisualDataExplorer.
  2. Select Web Application from the webAF Projects list.
  3. Accept the defaults as you go through the Web App wizard until you have reached step #4 of the wizard. At this step you will need to select Examples in the radio box titled Display list for. Choose Information Map Viewer Servlet from the list box titled Type of initial content.
  4. Continue accepting defaults as you complete the Web App wizard.

Step 2: Set up access to the data source

  1. Insert the correct metadata password ( 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.
  2. Insert the correct metadata password ( 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.
  3. Replace the contents of the InfoMapViewerControllerServlet.java file with the following code:
    
    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 webapp 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);
    
    	}
    }
    

Step 3: Add components to the JSP file

  1. Replace the contents of the InfoMapViewerExampleViewer.jsp file with the following code:
    
    <%@page import="com.sas.actionprovider.ActionOrderList"%>
    <%@page import="com.sas.actionprovider.support.ActionProviderSupportTypes"%>
    <%@page import="com.sas.actionprovider.HttpAction"%>
    <%@page import="com.sas.actionprovider.HttpActionProvider"%>
    <%@page import="com.sas.actionprovider.strategies.ActionStrategyInterface"%>
    <%@page import="com.sas.dataexplorer.actionprovider.support.olaptableview.HttpOLAPTableViewSupport"%>
    <%@page import="com.sas.iquery.metadata.business.BusinessQuery"%>
    <%@page import="com.sas.iquery.metadata.IntelligentQueryMetadataServiceInterface"%>
    <%@page import="com.sas.iquery.metadata.business.InformationMapFactory"%>
    <%@page import="com.sas.services.session.SessionContextInterface"%>
    <%@page import="com.sas.servlet.tbeans.dataexplorer.html.VisualDataExplorer"%>
    <%@page import="com.sas.servlet.tbeans.olaptableview.html.OLAPTableView"%>
    <%@page import="com.sas.servlet.tbeans.olaptableview.html.OLAPTableViewComposite"%>
    <%@page import="com.sas.servlet.tbeans.dataexplorer.html.DataViewer"%>
    <%@page import="olap.MetaDataCellStrategy"%>
    
    <%@ page pageEncoding="UTF-8"%>
    <html>
    <head>
        <link href="styles/sasComponents.css" rel="stylesheet" type="text/css">
    </head>
    <body style="margin: 0px;">
    <%
    try{
    	boolean firstTimeThru = false;
        VisualDataExplorer vde = (VisualDataExplorer)session.getAttribute("vde");
    	String mapPath = "mapName";
        if (vde == null)
        {
        	SessionContextInterface model =
    			(SessionContextInterface)session.getAttribute("sas_model");
    		//create a new VisualDataExplorer with the map
        	vde = new VisualDataExplorer(model, mapPath);
        	vde.setId("vde");
        	HttpActionProvider ap =
    			(HttpActionProvider)session.getAttribute("sas_actionProvider");
        	vde.setActionProvider(ap);
        	session.setAttribute("vde", vde);
    		firstTimeThru = true;
        }
    
        if (vde.getActionProvider() == null)
        {
           	HttpActionProvider ap =
    				(HttpActionProvider)session.getAttribute("sas_actionProvider");
        	vde.setActionProvider(ap);
        }
    
       //have to set the request and response for the dataviewer to be returned below
       vde.setRequest(request);
       vde.setResponse(response);
       DataViewer dv =
          (DataViewer)vde.getComponent(VisualDataExplorer.VISUALDATAEXPLORER_DATAVIEWER);
       OLAPTableViewComposite otvc = (OLAPTableViewComposite)dv.getTable();
       //need to set the model here for the table data to be returned below
       otvc.setModel( dv.getOLAPBusinessQueryAdapter() );
       OLAPTableView otv =
    		(OLAPTableView)otvc.getComponent(OLAPTableViewComposite.OLAPTABLEVIEW_TABLEDATA);
    
      	// create a new HttpAction
       if (firstTimeThru)
       {
    		IntelligentQueryMetadataServiceInterface service =
    			((BusinessQuery)vde.getDataModel()).getBusinessModel().getMetadataService();
    		SessionContextInterface sessionInt =
    			((BusinessQuery)vde.getDataModel()).getBusinessModel().getSession();
    		InformationMapFactory factory = InformationMapFactory.getInstance();
    
    	    HttpAction tupleAction = new HttpAction();
    	    tupleAction.setExternal(true);
    		tupleAction.setActionType("TUPLE_ACTION");
    		tupleAction.setURLBase("tuple.jsp");
    		ActionStrategyInterface asi = new MetaDataCellStrategy();
    		tupleAction.setActionStrategy( asi );
    		asi.initializeAction( tupleAction, HttpOLAPTableViewSupport.DATA_CELL_AREA, null );
    
    		com.sas.actionprovider.Area dataCellArea =
    			new com.sas.actionprovider.Area(HttpOLAPTableViewSupport.DATA_CELL_AREA);
    		java.util.Vector vector = new java.util.Vector();
    		vector.add(otv);
    	    vde.getActionProvider().setAction(tupleAction, vector, dataCellArea);
    		ActionOrderList newAOL = new ActionOrderList();
    		newAOL.add("TUPLE_ACTION");
    		vde.getActionProvider().setActionOrderList
            ("VDE_OLAP_TABLEVIEW_SUPPORT", newAOL, null, HttpOLAPTableViewSupport.DATA_CELL_AREA);
    
    		firstTimeThru = false;
    	}
    
        vde.setRequest(request);
        vde.setResponse(response);
        vde.write(out);
    	}catch (Exception e)
    	{
    		System.out.println("exception message: " + e.getMessage());
    	}
    
    %>
    
    </body>
    </html>
    
  2. Specify the Information Map by double-clicking on the InfoMapViewerExampleViewer.jsp file in the Files Tab of the Project Navigator. Replace the mapName string in the following code:
    String mapPath = "mapName";
    with your information map URL string
  3. Select File [arrow] New, and then select JavaServer Page to create a new jsp file. Specify tuple.jsp for the file name and click OK. Select Blank JavaServer Page and click Next. Click Finish and then insert the following code into the new file.
     <%
    
     java.util.Enumeration enum = request.getParameterNames();
     String paramName = "";
     while (enum.hasMoreElements())
     {
     	paramName = (String)enum.nextElement();
    	out.println(paramName + " = ");
    	out.println((String)request.getParameter(paramName));
    	out.println("<br>");
     }
    
    %>
    
  4. Create a new java file in webAF called MetaDataCellStrategy.java by selectinf File [arrow] New, and clicking Java Source File. Put the file in a directory called olap under the WEB-INF\classes directory for your project. Select Blank Java File and click Next. Click Finish. Insert the following code into the new file.
    package olap;
    
    import com.sas.actionprovider.ActionSupportFilter;
    import com.sas.actionprovider.BaseAction;
    import com.sas.actionprovider.strategies.olap.BaseOLAPStrategy;
    import com.sas.actionprovider.util.olap.HttpOLAPActionUtil;
    import com.sas.actionprovider.util.olap.OLAPDynamicValueKeys;
    import com.sas.actionprovider.util.olap.OLAPActionUtil;
    import com.sas.servlet.tbeans.tableview.TableViewActionSupportFilter;
    import com.sas.storage.olap.TupleElementInterface;
    import com.sas.storage.olap.TupleInterface;
    import com.sas.storage.olap.OLAPException;
    import com.sas.swing.util.Action;
    import java.util.Locale;
    
    /**
     *
     */
    public class MetaDataCellStrategy
    	extends BaseOLAPStrategy
    {
    	public static final String DATA_VALUE = OLAPDynamicValueKeys.DATA_VALUE;
    	public static final String ROW_TUPLES = OLAPDynamicValueKeys.ROW_TUPLES;
    	public static final String COLUMN_TUPLES = OLAPDynamicValueKeys.COLUMN_TUPLES;
    
    	private static OLAPActionUtil defaultUtil;
    
    	static {
    		defaultUtil = new HttpOLAPActionUtil();
    	}
    
    	/**
    	 * Default constructor.
    	 * Calls the other constructor for specifying strategy type.
    	 */
    	public MetaDataCellStrategy()
    	{
    		this( false, defaultUtil );
    	}
    
    	/**
    	 * Constructor for specifying type ( swing/http ) of strategy object.
    	 *
    	 * @param swing boolean indicating whether the strategy will be used in
    	 * a swing environment.  If false, the strategy is http type.
    	 */
    	public MetaDataCellStrategy(boolean swing, OLAPActionUtil util)
    	{
    		super(swing, util);
    	}
    
    	protected void initializeActionAttributes(BaseAction action, String areaType, Locale locale)
    	{
    		super.initializeActionAttributes(action, areaType, locale);
    		action.putValue(DATA_VALUE, null);
    		action.putValue(Action.NAME, null);
    		return;
    	}
    
    	public void setActionAttributes( BaseAction action, ActionSupportFilter filter )
    	{
    		super.setActionAttributes(action,filter);
    
    		if ( !(filter instanceof TableViewActionSupportFilter) ) {
    			return;
    		}
    
    		// Apply attributes representing row metadata
    		TupleInterface[] rowTuples = (TupleInterface[])filter.getAttribute(ROW_TUPLES);
    		int tupleIndex = ((TableViewActionSupportFilter)filter).getRow();
    
    		String[][] rowAttributes = getMetaDataAttributes( rowTuples[tupleIndex] );
    		for( int i=0; i<rowAttributes.length; i++ ) {
    			action.putValue( rowAttributes[i][0], rowAttributes[i][1] );
    		}
    
    		// Apply attributes representing column metadata
    		TupleInterface[] columnTuples = (TupleInterface[])filter.getAttribute(COLUMN_TUPLES);
    		tupleIndex = ((TableViewActionSupportFilter)filter).getColumn();
    
    		String[][] columnAttributes = getMetaDataAttributes( columnTuples[tupleIndex] );
    		for( int i=0; i<columnAttributes.length; i++ ) {
    			action.putValue( columnAttributes[i][0], columnAttributes[i][1] );
    		}
    	}
    
    	private String[][] getMetaDataAttributes(TupleInterface tuple)
    	{
    		TupleElementInterface[] tupleElements = null;
    		try {
    			tupleElements = tuple.getElements(0, -1);
    		}
    		catch ( OLAPException oe  )
    		{
    		}
    
    		if ( tupleElements == null ) {
    			return null;
    		}
    
    		String[][] attrs = new String[tupleElements.length][2];
    		for( int i=0; i<tupleElements.length; i++ ) {
    			try {
    				attrs[i][0] = getCategoryName( tupleElements[i] );
    				attrs[i][1] = tupleElements[i].getLabel();
    			}
    			catch ( OLAPException oe )
    			{
    				oe.printStackTrace();
    			}
    		}
    
    		return attrs;
    
    	}
    
    	private static String getCategoryName(TupleElementInterface tupleElement)
    	{
    		try {
    			String uniqueLevelName = tupleElement.getUniqueLevelName();
    			String[] uniqueLevelNames = tupleElement.getTuple().getAxis().getUniqueLevelNames();
    			int position = -1;
    			for( int i=0; i<uniqueLevelNames.length; i++ ){
    				if ( uniqueLevelName != null && uniqueLevelName.equals( uniqueLevelNames[i] ) ) {
    					position = i;
    					break;
    				}
    			}
    
    			if ( position >= 0 )
    				return tupleElement.getTuple().getAxis().getAxisHeaders()[position];
    		}
    		catch( OLAPException oe )
    		{
    			oe.printStackTrace();
    		}
    
    		return null;
    	}
    
    	protected void initialize()
    	{
    		super.initialize();
    		addDynamicValueInfo(DATA_VALUE, null, null);
    		addDynamicValueInfo(Action.NAME, null, DATA_VALUE);
    	}
    
    	public int getActionStatus ( BaseAction action, ActionSupportFilter filter )
    	{
    		return ENABLED;
    	}
    
    }
    

Step 4: Finish the project

  1. Build the project.
  2. Start the Java Web server.
  3. Execute in browser.