Contents Implementation & Administration Guide 1.1 Previous Next

Application Sample 4: Locate and Display a Widget

A Java Server Page (JSP) called AppSample4.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.

The application shows how to locate a widget and display it in the application's output.

 
<!-- Copyright (c) 2000 by SAS Institute Inc., Cary, NC 27513 -->
<%@ page
  language="java"
  import="com.sas.edir.Debug,
          com.sas.edir.FilteredEntityCollection,
          com.sas.edir.TrackedObject,
          com.sas.edir.delivery.Parameter,
          com.sas.edir.util.StringOp,
          com.sas.edir.webapp.Application,
          com.sas.edir.webapp.Widget,
          com.sas.edir.webapp.WidgetFilter,
          com.sas.edir.webapp.portal.AttributeId,
          com.sas.edir.webapp.portal.PortalEnterpriseDirectory,
          com.sas.edir.webapp.portal.LogicBean,
          com.sas.edir.webapp.portal.ModelId,
          com.sas.edir.webapp.portal.RequestId,
          com.sas.edir.webapp.portal.samples.RB,
          java.util.Iterator,
          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. These methods use the information in the request to locate the portal user's enterprise directory and the requested application.
<%
LogicBean logicBean = new LogicBean();
PortalEnterpriseDirectory edir = logicBean.getEnterpriseDirectory(request);
Application app = logicBean.getApplication(request);
The following code checks for the existence of the EnterpriseDirectory and Application objects. If they exist, this means the user has accessed the application from within the portal.
// authorization check
if ((edir != null) && (app != null))
{
If the user is found to be authorized, the following code uses logicBean convenience methods to obtain the active locale setting and the names of currently active style sheets. It also obtains the application's name attribute from the enterprise directory, and it obtains the application's title text from the samples resource bundle. Finally, it creates a variable called "year"and assigns it the value "1994."
    Locale locale = logicBean.getLocale(request);
    String sasStyleSheet = logicBean.getSASStyleSheet(request);
    String styleSheet = logicBean.getStyleSheet(request);
    String title = RB.getString(locale,"appsample4.title.txt");
    String year = "1994";
The following code uses a logicBean convenience method to obtain application parameters from the portal session. In this case, the parameter is a logical name, which identifies the SAS server on which the widget's data source resides.
    // check for application parameters which may get passed in as a request
    // parameter or attribute
    String logicalName = logicBean.getValueFromRequest("com.sas.portal.AppSample4.LogicalName", request);
The following code uses the PortalTitle.jsp file to display the title at the top of the page. The appropriate style sheets and the application's title text are incorporated into the page.
    // display the title using PortalTitle.jsp
    request.setAttribute(RequestId.PortalTitle, title);
    request.setAttribute(RequestId.PortalTitleFragment, "true");

    Object[] xArgs = new Object[1];
    xArgs[0] = year;

%>
<HTML>
  <HEAD>
    <TITLE><%= title %></TITLE>
    <LINK rel=stylesheet href="<%= sasStyleSheet %>" type="text/css">
    <LINK rel=stylesheet href="<%= styleSheet %>" type="text/css">
  </HEAD>
  <BODY onload="startup()" onunload="shutdown()">
  <SCRIPT LANGUAGE="JavaScript">
  <!-- 
    // This script is to load all object onLoad() functions 
    function startup(){ }
    function shutdown(){ }
  //-->
  </SCRIPT>

  <jsp:include page="/jsp/html/portal/PortalTitle.jsp" flush="true" />

  <h2><%= RB.formatString(locale, "appsample4.message.txt", xArgs) %></h2>
<%
The following code searches the enterprise directory to find widget content items which have the name "Widget Sample 1."
    //
    // locate the widget
    //
    // the EnterpriseDirectory is a factory that uses reflection to create objects
    // by looking up the object implementation in PortalRegistry.properties
    String filterMapping = edir.CLASS_MAPPING + "com.sas.edir.webapp.WidgetFilter";
    WidgetFilter filter = (WidgetFilter)edir.newObject(filterMapping);
    // search the entire cn=sas tree
    filter.setContext(logicBean.getPortalContext(edir.APPLICATION_CONTEXT, request));
    filter.setName("Widget Sample 1");

    // the filtered entity collection allows us to iterate over all the entities
    // that match the filter
    String collectionMapping = edir.CLASS_MAPPING + "com.sas.edir.FilteredEntityCollection";
    Object[] args = { edir, new String("") };
    FilteredEntityCollection collection = collection = (FilteredEntityCollection)edir.newObject(collectionMapping, args);
    collection.setFilter(filter);

    Widget widget = null;
    Iterator itor = collection.listIterator();
    if (itor.hasNext())
        widget = (Widget)itor.next();
        
    if (widget != null)
    {
The following code passes parameters from the application to the widget. The parameters include the logical name that was passed into the application, and the year "1994" which is hard-coded in the application.
	
        itor = widget.getParameterIterator();
        while (itor.hasNext())
        {
            Parameter param = (Parameter)itor.next();
            String name = (String)param.get(Parameter.NAME);
            String value = (String)param.get(Parameter.DEFAULT_VALUE);

            if (StringOp.equalsIgnoreCase(name, "com.sas.portal.WidgetSample1.LogicalName"))
                request.setAttribute(name, logicalName);
            else if (StringOp.equalsIgnoreCase(name, "com.sas.portal.WidgetSample1.Year"))
                request.setAttribute(name, year);
            else if (!StringOp.isEmpty(name) && !StringOp.isEmpty(value))
                request.setAttribute(name, value);
        }
The following code includes the widget, causing the HTML fragment generated by the widget to be displayed on the application's output page.
	
        // pass a handle to the widget into the widget jsp
        request.setAttribute(AttributeId.Widget, widget);

        String jsp = widget.getJsp();
%>
  <jsp:include page="<%= jsp %>" flush="true" />
<%
    }    
}
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
%>
<HTML>
  <HEAD>
  </HEAD>
  <BODY>
<p align=center><%= RB.getString(logicBean.getLocale(request), "content.notauthorized.txt") %>
<%
}
%>
  </BODY>
</HTML>


Contents Implementation & Administration Guide 1.1 Previous Next