25.5.5 Enabling HRPortlet for programmatic source C2A
In this section, you will be required to enhance the source C2A portlet application to implement the programmatic approach. Figure 25-18 illustrates the source cooperative portlet for this sample scenario.
Figure 25-18 Cooperative portlets - programmatic sample scenario (source portlet)
You will update HRPortlet to change property data using the programmatic approach. You will also insert a button in the HRPortlet page to offer the display of department details in the DepartmentDetailsPortlet (C2A target portlet).
Notice that in this portlet, you will need to register the DEPT_NO property using the Property.OUT direction. In addition, the update of the property will be done in the actionPerformed method.
Proceed as follows to update the HRPortlet:
1. Open the file HRPortlet/Java Resources/JavaSource/hrportlet/HRPortlet.java.
2. Update the class definition so it implements the EventPhaseListener interface: public class HRPortlet extends PortletAdapter implements EventPhaseListener, ActionListener {Note: The beginEventPhase method of the EventPhaseListener interface is used to register the output properties; this is because it is only possible to register and unregister properties during the event phase.
3. At the beginning of this class, insert two new class attributes so the code looks like this: public class HRPortlet extends PortletAdapter implements EventPhaseListener, ActionListener { PropertyBrokerService pbService; PortletConfig portletConfig;Note: pbService is an interface to the property broker and portletConfig stores the portlet configuration.
4. Change the init method so it looks as follows: public void init(PortletConfig portletConfig) throws UnavailableException { super.init(portletConfig); this.portletConfig=portletConfig; }
5. Add the following initConcrete method to initialize the property broker attribute. Example 25-8 The initConcrete method initializes the property broker attribute
public void initConcrete(PortletSettings settings) throws UnavailableException { try { pbService = (PropertyBrokerService) getPortletConfig() .getContext() .getService( PropertyBrokerService.class); } catch (PortletServiceUnavailableException e) { throw new UnavailableException("Could not locate PropertyBrokerService."); } catch (PortletServiceNotFoundException e) { throw new UnavailableException("Could not locate PropertyBrokerService."); } }
![]()
6. For simplicity, you will now add a new method called registerPropertiesIfNecessary to register the property we are interested in. Example 25-9 Register a new output property
private void registerPropertiesIfNecessary(PortletRequest request) throws PropertyBrokerServiceException { PortletSettings settings = request.getPortletSettings(); Property[] properties = pbService.getAllProperties(request, settings); if (properties == null || properties.length == 0) { PortletContext context = getPortletConfig().getContext(); //not registered, register now properties = new Property[1]; properties[0] = PropertyFactory.createProperty(settings); properties[0].setName("DEPT_NOParam"); properties[0].setDirection(Property.OUT); properties[0].setType("DEPT_NO"); properties[0].setNamespace("http://www.ibm.com/wps/c2a/examples/hrdetails"); properties[0].setTitleKey("HRDetails.Department"); properties[0].setDescriptionKey( "Display department details"); pbService.registerProperties(request, settings, properties); } }
![]()
Note: The endEventPhase method does nothing in this scenario but needs to be included in the interface.
7. Implement the eventPhaseListener interface by inserting the beginEventPhase and endEventPhase methods, which are callback methods invoked during the event phase. The beginEventPhase invokes the method to register the output property DEPT_NOParam. Example 25-10 beginEventPhase and endEventPhase methods
public void beginEventPhase(PortletRequest request) { try { registerPropertiesIfNecessary(request); } catch (Throwable e) { e.printStackTrace(); } } public void endEventPhase(PortletRequest request) { }
![]()
8. For simplicity, insert a new method with name changeProperty to notify the broker about a changed property value. This method uses the changedProperties method to notify property changes. Example 25-11 Notify the broker of a property value change
private void changeProperty(PortletRequest request, String value) { System.out.println("send data"); PortletSettings settings = request.getPortletSettings(); if (pbService != null) { try { Property p = PropertyFactory.createProperty(settings); p.setName("DEPT_NOParam"); p.setDirection(Property.OUT); p.setType("DEPT_NO"); p.setNamespace("http://www.ibm.com/wps/c2a/examples/hrdetails"); PropertyValue[] pva = new PropertyValue[1]; pva[0] = PropertyFactory.createPropertyValue(p, value); pbService.changedProperties(request, getPortletConfig(), pva);
} catch (Exception e) { e.printStackTrace(); } } }
![]()
9. At the end of the actionPerformed method, include the following code to invoke the internal changeProperty method and notify the output property change. Example 25-12 Invoke method to notify the property value change
if (actionString.equals("DisplayAllDepartmentDetails")) { this.changeProperty(request, "*"); }
![]()
10. Right-click anywhere in the Java editor and select Source | Organize Imports to include the missing import statements. In the Organize Imports dialog, choose to import the following class:
a. com.ibm.wps.pb.service.PropertyBrokerService
b. org.apache.jetspeed.portlet.service.PortletServiceUnavailableException
c. com.ibm.wps.pb.service.PropertyBrokerServiceException
d. com.ibm.wps.pb.property.Property
e. org.apache.jetspeed.portlet.PortletContext
f. com.ibm.wps.pb.property.PropertyFactory
g. com.ibm.wps.pb.property.PropertyValue
11. Save and close the updated source cooperative portlet HRPortlet.java file. Note: The action name is DisplayAllDepartmentDetails and it will be processed in the actionPerformed method. See Example 25-13.
12. Update the HRPortletView.jsp to insert a new action button offering to display department details from all departments. Open the HRPortletView.jsp file and insert the following code at the end of the file and before the </BODY> tag. Example 25-13 New button to offer display of all department details
<p align="center"> <FORM method="post" action="<portletAPI:createReturnURI><portletAPI:URIAction name='DisplayAllDepartmentDetails'/></portletAPI:createReturnURI>""> <INPUT type="submit" name="submit" value="Display all department details"> </FORM> </p>
![]()
13. Optionally, preview the JSP and see the new button.
14. Save and close the file.
15. Since the source portlet is now using programmatic approach, if there is a HRPortlet/WebContent/wsdl/HRPortlet.wsdl file, delete it and its reference in portlet.xml file. Save and close the file.
ibm.com/redbooks