Oracle® WebCenter Framework Building a WebCenter Application Step by Step 10g (10.1.3.2.0) Part Number B31073-01 |
|
|
View PDF |
In this chapter, you will learn how to build two different types of portlets: a Java portlet that uses JSR 168 and a PDK-Java portlet that uses Oracle-specific APIs.
This chapter contains the following sections:
Step 1: Building the Product Details Portlet (a JSR 168 Portlet)
Step 2: Building the Service Request Status Portlet (a PDK-Java Portlet)
Before you begin the steps in this chapter, ensure that you have completed the steps in the install.html
file located in the sample files you downloaded. Then, perform the following steps:
To make it easy to find things in your application, you can create projects to contain your portlets.
To create a project for your portlets:
In the Applications Navigator, right-click the SRDemoSample _Starter application and select New Project.
In the Items list, shown in Figure 2-1, select Empty Project and click OK.
In the Project Name field, enter ProdDetailPortlet.
Use the default directory name, then click OK to create the project.
To view the project properties, right-click the project name (in this case, ProdDetailPortlet), then choose Project Properties from the pop-up menu.
Select the technology scope of the project (that is, the technologies you will be including in the project): Java, JSP and Servlets, and Portlet, as shown in Figure 2-2.
Figure 2-2 Technology Scope of Your New Project
Click OK.
Create a second project by following steps 1 through 7 again, and name the project SRStatusPortlet
.
Save the application.
Before you can deploy portlets to your application serverOracle JDeveloper, you first need to establish a connection to your Oracle WebCenter Preconfigured OC4J, which is embedded in Oracle JDeveloper.
To create an application server connection to the preconfigured OC4J:
In Oracle JDeveloper, click the Start WebCenter Preconfigured OC4J icon. A readme.html
file displays that contains the connection information for the preconfigured OC4J.
In the Connections Navigator, right-click Connections and select New Application Server Connection.
Tip:
If you cannot see the Connections Navigator, select Connection Navigator from the View menu.If you are on the Welcome page of the Create Application Server Connect Wizard, click Next to display the Type page.
In the Connection Name field, shown in Figure 2-3, enter SRDemoConnection
.
From the Connection Type list, select Standalone OC4J 10g 10.1.3.
Figure 2-3 Create Application Server Connection: Type
Click Next to display the Authentication page.
Enter the Application Server Control Console administrator's user name and password for the relevant middle tier. Typically, the administrator's user name is oc4jadmin
and the password is welcome
.
Click Next to display the Connection page.
In the Host Name field, enter localhost
.
In the RMI Port field, enter the port number to which the Remote Method Invokation (RMI) server binds to the OC4J server. The default value for the preconfigured OC4J is 22667
.
Click Next to display the Test page.
Click Test Connection to verify the connection. A success message should appear if everything is correct. If the test fails, you may need to review your connection information.
Click Finish.
To build a simple JSR 168 portlet, we've included steps for building a sample Service Requests Status portlet. Once you deploy this portlet, you will be able to add it to your My Acme page. To view a live demo version of a more highly interactive JSR 168 portlet using Ajax, simply run the completed version of the Service Request Demo.
For more information about JSR 168 portlets, refer to Oracle WebCenter Framework Developer's Guide.
Building a JSR 168 portlet involves the following tasks:
Oracle WebCenter Framework provides a Java Portlet Wizard that enables you to quickly create a simple implementation for each mode of your portlet. Let us use this wizard to get started creating our portlet.
To create a JSR 168 portlet:
In Oracle JDeveloper, the Applications Navigator, within the SRDemoSample_Starter application, right-click the ProdDetailsPortlet project and select New.
Tip:
If this project does not exist, follow the instructions provided in Creating a Project for Your Portlets earlier in this chapter.In the New Gallery, expand the Web Tier category and select Portlets.
In the Items list, shown in Figure 2-4, select Standards-based Java Portlet (JSR 168) to build a JSR 168 portlet.
Click OK to display the JSR 168 Java Portlet Wizard.
If you are on the Welcome page of the wizard, click Next to display the Web Application page.
Select Servlet 2.4\JSP 2.0 (J2EE 1.4).
Tip:
If a Web application has already been created for the project, you may not see this page of the wizard and instead go straight to the General Portlet Properties page.Click Next to display the General Portlet Properties page.
In the Name and Class fields, shown in Figure 2-5, enter ProductDetailsPortlet
and clear the Enable WSRP V2-inter-portlet communication using Oracle extensions check box.
The wizard creates a class with this name to represent the portlet.
Figure 2-5 JSR 168 Portlet General Portlet Properties
Click Next.
In the Display Name field, shown in Figure 2-6, enter Product Details Portlet
.
In the Portlet Title field, enter Product Details Portlet.
In the Short Title field, enter Product Details
.
Figure 2-6 JSR 168 Portlet Name and Attributes
Click Finish to accept the default settings for the rest of the portlet attributes.
Your Product Details portlet now displays in the Applications Navigator as shown in Figure 2-7.
Figure 2-7 Product Details Portlet in the Applications Navigator
Save your work.
This JSR 168 portlet uses a Web Service as its data source. Before you can use a Web Service with a portlet in your application, you must first create a Web Service proxy for your portlet project.
To create a Web Service proxy:
Right-click the ProdDetailPortlet project that contains your ProductDetailsPortlet, then choose New.
Under the Business Tier node, shown in Figure 2-8, choose Web Services, then choose Web Service Proxy.
Tip:
If you do not see the Business Tier node in the New Gallery, you may need to Filter By All Technologies by selecting the option from the drop-down list.Figure 2-8 New Gallery: Web Service Proxy
Click OK.
On the Welcome page of the wizard, click Next.
On the Web Service Description page, shown in Figure 2-9, enter the WSDL of the Web Service , such as:
http://localhost:8888/ProductDetailsWS/ProductDetailsWSSoapHttpPort?WSDL
Note:
In this WSDL, localhost and port refer to the system where you deployed the Web Service. The Web Service is deployed in the SRDemo OC4J.Figure 2-9 Set the Web Service Description
Click Next.
Note:
You may need to click Next again if you do not see the Building Model dialog box display.After Oracle JDeveloper builds the model for the Web Service, you should see the page shown in Figure 2-10, where you can validate the end point URL for your Web Service:
Figure 2-10 Validate the Web Service Port Endpoints
Click Finish to accept the default values for the rest of the Web Service Proxy parameters.
Close the ProductDetailsWSSSoapHttpPortClient.java file.
At the moment your portlet does not do very much. In fact, it just displays a Welcome message and allows the end user to personalize the portlet title. To make the portlet perform the specific function that you require, you need to extend the sample code with the business logic that implements the desired functionality and features.
First, your portlet will display a list of product types. The list is retrieved from a Web service. Later on, you will extend the portlet, allowing the end user to pick a product type, and select a particular product. When selecting a product, detailed information will display, including a brief description, and an image.
To add portlet logic to a JSR 168 portlet:
In the Applications Navigator, expand the Web Content node, right-click view.jsp, then select Open to open the file in the Visual Editor.
Tip:
To locate view.jsp, expand the following nodes under the SRDemoSample_Starter application: Portlets > Web Content > ProductDetailsPortlet\html.At the bottom of the Visual Editor, click the Source tab to view the source code of the page.
Replace the existing code with the code shown in Example 2-1.
Example 2-1 Code for view.jsp
<%@ page contentType="text/html" import="javax.portlet.*,java.util.*,portlets.ProductDetailsPortlet,portlets.resource.ProductDetailsPortletBundle"%> <%@ page contentType="text/html" import="portlets.proxy.ProductDetailsWSSoapHttpPortClient,portlets.proxy.types.productdetailsws.types.ProductDetailsBean"%> <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%> <portlet:defineObjects/> <% String prodType = renderRequest.getParameter("prodType"); %> <table cellpadding="10%"> <tr> <td> <font class="portlet-section-header"> Product Types </font> </td> <td> <font class="portlet-section-header"> Products </font> </td> <td> <font class="portlet-section-header"> Details </font> </td> </tr> <tr> <td> <% ProductDetailsWSSoapHttpPortClient proxy = new ProductDetailsWSSoapHttpPortClient(); Vector productTypes = proxy.getProductTypes(); for (int i=0; i<productTypes.size(); i++) { PortletURL productURL = renderResponse.createRenderURL(); productURL.setParameter ("prodType", productTypes.elementAt(i).toString()); %> <font class="portlet-font"> <a href="<%=productURL%>"> <%= productTypes.elementAt(i) %> </a></font><br> <% } %> </td> </tr> </table>
From the File menu, select Save All.
After you have finished building your portlet, you need package it and deploy to the portlet container.
In the Applications Navigator, right-click the ProdDetailPortlet project and select New.
In the New Gallery, expand the General category and select Deployment Profiles.
In the Items list, shown in Figure 2-11, select WAR File.
Click OK to display the Create Deployment Profile -- WAR File dialog box.
Tip:
You can also get to this dialog box by right-clicking the web.xml file in the Applications Navigator and selecting Create WAR Deployment File.In the Deployment Profile Name field, shown in Figure 2-12, enter ProductDetailsPortlet
.
Use the default directory.
Figure 2-12 Create Deployment Profile -- WAR File Dialog Box
Click OK to display the WAR Deployment Profile Properties dialog box.
Select Specify J2EE Web Context Root, and in the corresponding field, enter ProductDetailsPortlet
as shown in Figure 2-13.
Figure 2-13 WAR Deployment Profile Properties
Click OK.
In the Applications Navigator, shown in Figure 2-14, expand the Portlets > Resources node to see the deployment file.
Figure 2-14 ProductDetailsPortlet.deploy in the Applications Navigator
From the File menu, choose Save All.
Right-click ProductDetailsPortlet.deploy and, from the Deploy to menu, shown in Figure 2-15, choose SRDemoConnection.
The Configure Application dialog box displays.
Figure 2-15 Configure Application Dialog Box
Note:
If SRDemoConnection does not appear in the menu, follow the steps provided in the step Creating an Application Server Connection to the Oracle WebCenter Preconfigured OC4J earlier in this chapter.Click OK.
When the Deployment finished message displays in the Deployment Log, verify that no errors occurred.
Now that you have created and deployed your portlet, you are ready to consume it in your application. You register the portlet producer with the application which enables you (or the application developer) to add the portlet to a page.
To register the producer:
Since portlet producers are associated with your application, you can right-click the application or any project within the application to invoke. The easiest way, however, is to open the New Gallery from the UserInterface project. The technology scope of the UserInterface project is set up so that the producer registration wizards show up in your New Gallery.
Choose New from the pop-up menu.
Under Web Tier > Portlets, choose WSRP Producer Registration, then click OK.
Tip:
If you do not see the Business Tier node in the New Gallery, you may need to Filter By All Technologies by selecting the option from the drop-down list.On the Welcome page, click Next.
On the Name page, shown in Figure 2-16, enter ProductDetailsProducer
in the Name field, then click Next.
On the Connection page, shown in Figure 2-17, enter the URL for the WSRP container of your preconfigured OC4J, for example:
http://localhost:6688/ProductDetailsPortlet/portlets/wsrp1?WSDL
Click Next.
After Oracle JDeveloper creates the connection, click Finish. When the success message displays, click OK.
Figure 2-18 Successful WSRP Producer Registration Message
The portlet is now ready for you to add to a page.
In our Service Request application, we want to be able to display the current status of a particular service request, given the service request ID. We can build a portlet to display this information.
For more information about PDK-Java portlets, refer to the Oracle WebCenter Framework Developer's Guide.
Building a PDK-Java portlet involves the following tasks:
Oracle WebCenter Framework provides a Java Portlet Wizard that enables you to quickly create a simple implementation for each mode of your portlet. Let's use this wizard to get started creating our portlet.
In Oracle JDeveloper, in the Applications Navigator, expand the SRDemoSample_Starter application.
Right-click the SRStatusPortlet project and select New.
Tip:
If this project does not exist, follow the instructions provided in Creating a Project for Your Portlets earlier in this chapter.In the New Gallery, expand the Web Tier category and select Portlets.
In the Items list, select Oracle PDK-Java Portlet, as shown in Figure 2-19
Figure 2-19 Create a New PDK-Java Portlet
Click OK to display the Java Portlet Wizard.
If you are on the Welcome page of the wizard, click Next to display the Web Application page.
Click Next to display the Portlet Description page.
In the Portlet Name field, enter SRStatusPortlet.
This is an internal name only and is not exposed to end users.
In the Display Name field, enter Service Request Status Portlet.
This is the name that appears in portlet selection lists, such as the Oracle JDeveloper Component Palette, where users choose which portlets to add to a page.
In the Description field, enter:
This portlet displays status information about a given service request.
In the Timeout (seconds) field, leave the default value of 40.
In the Timeout message field, enter:
Service Request Status Portlet timed out
Click Next to display the View Modes page.
The Show page check box is selected by default. For Show mode:
In the Implementation style list, make sure JSP is selected.
In the File name field, keep the default value of SRStatusPortletShowPage.jsp.
We will not have a Show Details mode for this portlet, so leave the Show details page check box cleared.
Click Next to display the Customize Modes page.
The Edit page check box is selected by default. For the Edit mode:
In the Implementation style list, make sure that JSP is selected.
In the File name field, keep the default value of SRStatusPortletEditPage.jsp.
We will not have an Edit Defaults mode for this portlet, so leave the Edit Defaults page check box cleared.
Click Next to display the Additional Modes page.
Select the Help page check box. For Help mode:
From the Implementation style list, select HTML File.
In the File name field, keep the default value of SRStatusPortletHelpPage.html.
Select the About page check box. For About mode:
From the Implementation style list, select HTML File.
In the File name field, keep the default value of SRStatusPortletAboutPage.html.
Click Next to display the Public Portlet Parameters page.
On the Public Portlet Parameters page, click Add.
Change the Name of the new parameter to ServiceReqID
.
Change the Display Name of the new parameter to ServiceReqID.
Change the Description of the new parameter to Service Request ID
.
Click Next to display the Public Portlet Events page.
Click Next to display the Producer Description page.
In the Producer name field, enter srstatusproducer.
Ensure all the check boxes are selected: Generate deployment properties file, Generate XML entries, Generate index JSP.
Click Finish to generate the files for your portlet. If you expand all the nodes under the SRStatusPortlet project in the Applications Navigator, you should see the following files:
A file for each portlet mode you selected:
SRStatusPortletAboutPage.html
SRStatusPortletEditPage.jsp
SRStatusPortletHelpPage.html
SRStatusPortletShowPage.jsp
web.xml
_default.properties
srstatusproducer.properties
provider.xml
index.jsp
All of these files are required to deploy and run the portlet successfully, except for index.jsp,
which is used by Oracle JDeveloper for testing purposes.
Figure 2-26 Files Generated for the Service Request Status Portlet
From the File menu, select Save All.
At the moment, your portlet does not do very much. In fact, it just displays a welcome message and allows the end user to personalize the portlet title. To make the portlet perform the specific function that you require, you need to extend the sample code with wht business logic that implements the desired functionality and features.
The Service Request Portlet needs to query the SRDemo database to find out the status of a particular service request, given its service request ID, and then display that information in a table.
In the Applications Navigator, right-click the SRStatusPortlet project and select New.
In the New Gallery, expand the General category and select Simple Files.
In the Items list, select Java Class.
Click OK to display the Create Java Class dialog box.
In the Name field, enter SRConnectionUtil.
Leave the rest of the options in this dialog box as their default values.
Click OK.
Replace the generated code with the code shown in Example 2-2.
Example 2-2 Code to Create a Connection to the SRDemo Database
package srstatusportlet; import java.sql.Connection; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class SRConnectionUtil { public SRConnectionUtil() { } public static Connection getConnection () { InitialContext ctx; DataSource ds; Connection conn = null; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup("jdbc/SRDemoCoreDS"); conn = ds.getConnection(); } catch (NamingException ne) { conn = null; } catch (SQLException sqle) { conn = null; }; return conn; } }
This class creates a connection to the SRDemo database, against which we will execute a query in the portlet code.
Save the file.
If you have not already done so, create a database connection pointing to your srdemo schema as described in the install.html
file located in the sample files you downloaded.
In the Applications Navigator, right-click SRStatusPortletShowPage.jsp and select Open.
Select the Source tab to view the source code for the page.
Select the existing code and replace it with the code shown in Example 2-3.
Example 2-3 Code for the Service Request Status Portlet
<%@page contentType="text/html; charset=windows-1252" import="oracle.portal.provider.v2.render.PortletRenderRequest" import="oracle.portal.provider.v2.http.HttpCommonConstants" import="oracle.portal.provider.v2.ParameterDefinition" import="srstatusportlet.SRConnectionUtil" import="java.sql.Connection" import="java.sql.Statement" import="java.sql.ResultSet" import="java.sql.SQLException" %> <% PortletRenderRequest pReq = (PortletRenderRequest) request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST); // Reading the service request ID, as a parameter String SRID = pReq.getParameter("ServiceReqID"); // Creating the database connection Connection conn = SRConnectionUtil.getConnection(); // Testing if the parameter has been wired. If no parameter is passed // it is set to 104, by default. if (SRID == null) { %> Please map the service request ID to this portlet's input parameter. <% SRID = "104"; } if (conn == null) { %> Couldn't connect to the database. <% } else try { Statement stmt = conn.createStatement(); // Constructing the SELECT statement String query = "SELECT svr_id, status, " + "users1.first_name || ' ' || users1.last_name createdby, " + "users2.first_name || ' ' || users2.last_name assignedto, " + "TO_CHAR(assigned_date, 'Dy, Mon DD, YYYY') " + "FROM service_requests, users users1, users users2 " + "WHERE svr_id = " + SRID + " AND " + "users1.user_id = service_requests.created_by AND " + "users2.user_id = service_requests.assigned_to"; // Executing the query. ResultSet rs = stmt.executeQuery(query); // Stepping through the result set. while (rs.next()) { %> <table> <tr> <td> <font class="PortletText1"> Service Request ID: </font> </td> <td> <font class="PortletHeading1"> <%=rs.getInt(1)%> </font> </td> </tr> <tr> <td> <font class="PortletText1"> Status: </font> </td> <td> <font class="PortletHeading1"> <%=rs.getString(2)%> </font> </td> </tr> <tr> <td> <font class="PortletText1"> Created on: </font> </td> <td> <font class="PortletHeading1"> <%=rs.getString(5)%> </font> </td> </tr> <tr> <td> <font class="PortletText1"> Created By: </font> </td> <td> <font class="PortletHeading1"> <%=rs.getString(3)%> </font> </td> </tr> <tr> <td> <font class="PortletText1"> Assigned To: </font> </td> <td> <font class="PortletHeading1"> <%=rs.getString(4)%> </font> </td> </tr> </table> <% } //while } // try catch (SQLException sqle) { System.out.println ("DB Connection established successfully but ran into an issue while working with the DB."); System.out.println (sqle); } conn.close(); %>
This code first calls the Java class we created earlier to connect to the database. Then it sets the service request ID based on a parameter passed to it (if no parameter is passed, the service request ID is set to a default value of 100). Using this service request ID, the portlet then queries the SRDemo database to find the status of the service request. Finally the results of this query are printed in an HTML table.
Save the file.
Right-click SRStatusPortletAboutPage.html
and select Open.
In the Design view, enter the following text:
Service Request Status Portlet
MyAcme, Copyright
Save the file.
Right-click SRStatusPortletHelpPage.html and select Open.
In the Design view, enter the following text:
This portlet displays status information about a given service request, based on a parameter (service request ID) it receives. If the portlet does not work as expected, it may not have been wired properly.
Save the file.
After you have finished building your portlet, you are ready to deploy it to your preconfigured OC4J.
In the Applications Navigator, locate the web.xml file under SRStatusPortlet > Web Content > WEB-INF.
Right-click the web.xml and select Create WAR Deployment Profile
In the Deployment Profile Name field, enter SRStatusPortletApp.
Use the default directory.
Figure 2-29 Create Deployment Profile -- WAR File Dialog Box
Click OK to display the WAR Deployment Profile Properties dialog box.
Select Specify J2EE Web Context Root, and in the corresponding field, enter SRStatusPortlet
.
Figure 2-30 WAR Deployment Profile Properties Dialog Box
Click OK.
In the Applications Navigator, expand the Resources node to see the deployment file.
Figure 2-31 SRStatus Portlet Deployment File
From the File menu, choose Save All.
Right-click SRStatusPortletApp.deploy and from the Deploy to menu, select SRDemoConnection.
The Configure Application dialog box displays.
Note:
If SRDemoConnection does not appear in the menu, follow the steps provided in Creating an Application Server Connection to the Oracle WebCenter Preconfigured OC4J earlier in this chapter.You can accept the defaults here, so click OK.
When the Deployment finished message displays in the Deployment Log, verify that no errors occurred.
Register the producer following the same steps you used in "Register the Producer" for the JSR 168 portlet, using the URL:
http://localhost:6688/SRStatusPortlet/providers
The JSR 168 portlet and PDK-Java portlet you created are now ready for you to add to your application.
In this chapter, you learned how to build a JSR 168 portlet and a PDK-Java portlet and how to deploy them for use with a WebCenter application. You can now register the producer with an application and add the portlet to a page. You will do just that in Chapter 7, "Building a Page and Adding Components". Before you move onto building the page and adding components however, you can continue to Chapter 3, "Setting Up Skins".