Contents Implementation & Administration Guide 1.1 Previous Next

Widget Sample

A Java Server Page (JSP) called WidgetSample1.jsp is shown below. This JSP is shipped with the SAS Information Delivery Portal, and it can be found in the jsp\html\samples directory in the portal Web application.

This JSP executes an SQL query against a SAS table and writes the results to an HTML table.

 
<!-- Copyright (c) 2000 by SAS Institute Inc., Cary, NC 27513 -->
<%@ page
  language="java"
  import="com.sas.edir.Debug,
          com.sas.edir.EnterpriseDirectory,
          com.sas.edir.PortalPool,
          com.sas.edir.TrackedObject,
          com.sas.edir.WorkspaceFactoryManager,
          com.sas.edir.util.StringOp,
          com.sas.edir.webapp.Widget,
          com.sas.edir.webapp.portal.AttributeId,
          com.sas.edir.webapp.portal.LogicBean,
          com.sas.edir.webapp.portal.ModelId,
          com.sas.edir.webapp.portal.PortalEnterpriseDirectory,
          com.sas.edir.webapp.portal.RequestId,
          com.sas.edir.webapp.portal.samples.RB,
          com.sas.iom.SAS.ILanguageService,
          com.sas.iom.SAS.IWorkspace,
          com.sas.iom.WorkspaceConnector,
          com.sas.rio.MVAConnection,
          java.sql.Connection,
          java.sql.ResultSet,
          java.sql.Statement,
          java.util.ArrayList,
          java.util.Hashtable,
          java.util.Locale,
          java.util.Map,
          java.util.Properties"
  errorPage="PortalError.jsp"
%>
The following code uses logicBean convenience methods to process the request that has been passed from the portal session (or calling application). These methods use the information in the request to locate the portal user's enterprise directory and the requested widget.
<%
LogicBean logicBean = new LogicBean();
PortalEnterpriseDirectory edir = logicBean.getEnterpriseDirectory(request);
Widget widget = logicBean.getWidget(request);
The following code checks for the existence of the EnterpriseDirectory and Widget objects. If they exist, this means the user has accessed the widget from within the portal.
// authorization check
if ((edir != null) && (widget != null))
{
    // user is authorized to view this content
If the user is found to be authorized, the following code uses logicBean convenience methods to obtain information about the current session as well as the active locale setting.
    Map model = logicBean.getModel(request);
    Locale locale = logicBean.getLocale(request);
The following code uses a logicBean convenience method to obtain widget parameters from the portal session (or the calling application). The parameters include a year value and a logical name which identifies the SAS server on which the widget's data source resides.
    // use widget parameters passed in as a request parameters or attributes
    String logicalName = logicBean.getValueFromRequest("com.sas.portal.WidgetSample1.LogicalName", request);
    String year = logicBean.getValueFromRequest("com.sas.portal.WidgetSample1.Year", request);

    if (!StringOp.isEmpty(logicalName))
If a logical name parameter is found, the following code uses the workspace factory to connect to the appropriate SAS server and to obtain a workspace on the server.
    {
        WorkspaceFactoryManager workspaceManager = null;
        WorkspaceConnector connector = null;
        IWorkspace workspace = null;
        Connection con = null;
        Statement select = null;
        
        try
        {
            workspaceManager = (WorkspaceFactoryManager)model.get(ModelId.PortalWorkspaceFactoryManager);
            connector = workspaceManager.getWorkspaceConnector(edir, logicalName);
            workspace = connector.getWorkspace();
The following code uses the Language Service interface to construct a SAS procedure step and submit it for processing. The procedure step reads data from the table "prdsale" and creates a temporary table called "wdgt1."/td>
            ILanguageService lang = workspace.LanguageService();

            // todo - need a unique output dataset name

            StringBuffer submit = new StringBuffer();
            submit.append("proc means data=sashelp.prdsale noprint SUM; ");
            submit.append("output out=wdgt1 sum=actual; ");
            submit.append("class country product year; ");
            submit.append("var actual; ");
            submit.append("run; ");
            lang.Submit(submit.toString());
The following code creates a JDBC connection to the temporary table "wdgt1." It then submits an SQL statement that selects all rows in which the _type_ column is equal to "7"and the year column is equal to the year parameter that was passed into the widget. The query results are returned to a variable called "rs."
            // create a JDBC connection
            con = new MVAConnection(workspace.DataService(), new Properties());
            select = con.createStatement();

            // execute an SQL query
            StringBuffer query = new StringBuffer();
            query.append("select country,product,actual from work.wdgt1 where _type_=7 and year=");
            query.append(year);
            ResultSet rs = select.executeQuery(query.toString());
The following code parses the result string and constructs an HTML table to present the data. This resulting HTML fragment can then be incorporated into the page that is output by the portal (or other calling application).
            
            ArrayList country = new ArrayList();
            ArrayList product = new ArrayList();
            ArrayList sales = new ArrayList();

            int count = 0;
            while (rs.next())
            {
                country.add(rs.getObject(1).toString().trim());
                product.add(rs.getObject(2).toString().trim());
                sales.add(rs.getObject(3).toString().trim());
                count++;
            }
%>
<p>
<table border="1" cellpadding="4">
<tr>
    <th>Country</th>
    <th>Product</th>
    <th>Sales</th>
</tr>
<%

            int nextCountry = 0;
            for (int i = 0; i < count; i++)
            {
%>
<tr>
<%
                String countryS = (String)country.get(i);
                String productS = (String)product.get(i);
                String salesS = (String)sales.get(i);

                if (i == nextCountry)
                {
                    int j;
                    for (j = i; j < count; j++)
                    {
                        if (!StringOp.equals(countryS, (String)country.get(j)))
                        {
                            nextCountry = j;
                            break;
                        }
                    }
%>
    <td rowspan="<%= j - i %>"><%= countryS %></td>
<%
                }
%>
    <td><%= productS %></td>
    <td><%= salesS %></td>
</tr>
<%
            }
%>
</table>
<%
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
The following code returns the workspace to the factory so that the object can be closed and the supporting connection can be either reused or canceled.
		
            // make sure and cleanup so the workspace can be released
            try { select.close(); }
            catch (Exception ex) { /* not a whole lot we can do */ }

            try { con.close(); }
            catch (Exception ex) { /* not a whole lot we can do */ }

            try { workspace.Close(); }
            catch (Exception ex) { /* not a whole lot we can do */ }

            try { connector.close(); }
            catch (Exception ex) { /* not a whole lot we can do */ }
        }
    }
}
else
The following code writes an error message if the authorization test failed. The message text is obtained from the samples resource bundle.
{
    // user is not authorized to view this content
%>
<p align=center><%= RB.getString(logicBean.getLocale(request), "content.notauthorized.txt") %>
<%
}
%>


Contents Implementation & Administration Guide 1.1 Previous Next