In this step of the tutorial you will learn how to extend a Commerce Enabled Portal portlet.
The sample code provided extends the catalog portlet, allowing it to communicate with a third party portlet. This extended portlet will be known as the generic catalog portlet.
To understand how to extend the portlet, do the following:
- Download the sample code package from Commerce Enabled Portal tutorials sample code to the machine on which the Commerce Enabled Portal development environment is installed.
- On the machine where WebSphere Commerce Developer is installed, if the Commerce Enabled Portal development environment is not already open then open it.
- From the Start menu, select Programs > IBM WebSphere Commerce Developer Enterprise > WebSphere Commerce development environment.
- In the Project Navigator, expand the project you are using.
- Right click on the JavaSource folder. From the menu, select Import. The Import wizard displays.
- In the Select page, select File System. Click Next. The File System page displays.
- In the From directory field, click Browse and navigate to the Javacode folder. Click OK.
- com displays in the list. Select com. Click Finish.
- In the Project Navigator, open the following file:
project\JavaSource\com\ibm\commerce\portal\sample\ExtendedCommercePortlet.java
The file opens in the main pane. - Take a moment to review the file.
- Notice the following code fragment:
import com.ibm.commerce.portal.wpsportlets.WCSServletInvokerPortlet;
/**
* This class represents a portlet extended from WCSServletInvokerPortlet
* to use portlet messaging for communication with third party portlets
* <p>
*/
public class ExtendedCommercePortlet extends WCSServletInvokerPortlet {
All Commerce Enabled Portlets provided with WebSphere Commerce are a concrete instance of the class WCSServletInvokerPortlet. ExtendedCommercePortlet.java extends that class and allows you to create portlets based on it, rather than WCSServletInvokerPortlet. ExtendedCommercePortlet.java includes logic that allows Commerce Enabled Portal portlets to communicate with third party portlets.Note: B2B direct Commerce Enabled Portal portlets may also be a concrete instance of the class DynamicWCSServletInvokerPortlet.
- Notice the following code fragment:
/**
* Initializes the concrete instance of the portlet
* @param portletsettings -- object of the type PortletConfig
*/
public void initConcrete(PortletSettings portletsettings)
throws UnavailableException {
super.initConcrete(portletsettings);
// Create the Listener object that we want to attach to the given portlet
CatalogMessageListener catalogListener =
new CatalogMessageListener(this);
// Add listener object to the given portlet
this.addMessageListener(catalogListener);
return;
This section of the code creates a concrete instance of the extended portlet, creating the Listener object that the portlet will use to listen for messages from the third party portlets and then adding it to the portlet. The CatalogMessageListener logic is provided in the CatalogMessageListener.java file you imported in the previous section of the tutorial.
- Notice the following code fragment:
/**
* Method that is responsible for obtaining the URL of the commerce command
* from which the portlet content is going to be extracted
* @param preq - PortletRequest object
*/
protected String getInitialRemoteURL(PortletRequest preq) {
// Obtain the URL that is defined in the portlet.xml file
String initialUrl = super.getInitialRemoteURL(preq);
// Retrieve the value of the message from the PortletRequest object
String sMessage = (String) preq.getAttribute("messageCategoryId");
if (sMessage != null) {
// If the message (categoryId parameter value) is not null, then we want to
update the URL
// from which commerce content is going to be retrieved
initialUrl = updateUrl(initialUrl, "categoryId", sMessage);
This section of code will override the initial get URL method which is defined in WCSServletInvokerPortlet. The method in the parent class retrieves the value of the initial URL from the deployment descriptor. This new logic allows the portlet to construct the URL in the specified way.
- Continue the tutorial, by completing the steps in Defining the new Commerce Enabled Portal Portlet.