26.2.5 Updating the HRPortlet168 portlet code
The following steps describe the updates you need to make in the HRPortlet168 source cooperative portlet code:
1. From the Project Explorer view, open the HRPortlet168 portlet. Double-click HRPortlet168/Java Resources/JavaSource/hrportlet168/HRPortlet168.java
2. Make sure you include the following import statements: import javax.naming.Context; import javax.naming.InitialContext; import com.ibm.portal.portlet.service.PortletServiceHome; import com.ibm.portal.propertybroker.service.PropertyBrokerService;
3. Declare two new variables: pbService and pbServiceAvailable: PropertyBrokerService pbService = null;
boolean pbServiceAvailable = false;
4. Update the init method to obtain a reference to the property broker services as highlighted in Example 26-5: Example 26-5 Obtaining a reference to the property broker service
public void init(PortletConfig portletConfig) throws PortletException, UnavailableException {super.init(portletConfig);try {Context ctx = new InitialContext();PortletServiceHome serviceHome = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.propertybroker.service.PropertyBrokerService");pbService = (PropertyBrokerService)serviceHome.getPortletService(com.ibm.portal.propertybroker.service.Prop ertyBrokerService.class);pbServiceAvailable = true;}catch(Throwable t) {getPortletContext().log("OrderDetailPortlet could not find property broker service!");}}
![]()
The boolean variable pbServiceAvailable will set to true only if the service is available. This variable can be used to guard access to IBM-only services (WebSphere Portal) and it can be used to ensure that the portlet can still function in other environments where the service is not available.
5. You need to declare three new variables: ACTION_NAME, DEPARTMENT_LIST and DEPT_NO as follows: public static final String ACTION_NAME = "ACTION_NAME";
public static final String DEPARTMENT_LIST = "departmentList";
public static final String DEPT_NO = "DEPT_NO";
6. In the processAction method, publish an output property, as highlighted in Example 26-6. The property must be set as an attribute (setAttribute) in the request as configured in the WSDL. Example 26-6 Publishing the output property
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException { //Add action string handler here HRPortlet168SessionBean sessionBean = getSessionBean(request); if(request.getPortletMode().equals(PortletMode.EDIT)) { String dbname = (String) request.getParameter("dbname"); String userid = (String) request.getParameter("userid"); String password = (String) request.getParameter("password"); String sqlstring = (String) request.getParameter("sqlstring"); sessionBean.setDbName(dbname); sessionBean.setUserId(userid); sessionBean.setPassword(password); sessionBean.setSqlString(sqlstring); if(request.getPortletMode().equals(PortletMode.EDIT)) response.setPortletMode(PortletMode.VIEW); } if( request.getParameter(FORM_SUBMIT) != null ) { // Set form text in the session bean sessionBean = getSessionBean(request); if( sessionBean != null ) sessionBean.setFormText(request.getParameter(TEXT)); } // add this code String actionName = request.getParameter(ACTION_NAME); String dept_number = request.getParameter(DEPT_NO); if (actionName != null && actionName.equalsIgnoreCase(DEPARTMENT_LIST)) { request.getPortletSession().setAttribute(ACTION_NAME, DEPARTMENT_LIST); request.setAttribute(DEPT_NO, dept_number); } // end of added code }
![]()
7. Save your files.
8. Review the code:
- Here is where you need to take into account the attributes name and boundTo of portlet:param element mentioned previously in Example 26-3.
- The name attribute of portlet:action element must match with the request parameter to determine the action name (departmentList in our example).
- When the action matches it sets the value of output parameter (DEPT_NO).
- Depending on the value of boundTo attribute, the variable will be set as:
request attribute, if the value of boundTo attribute is request-attribute
session attribute, if the value of boundTo attribute is session
request parameter, if the value of boundTo attribute is request-parameter. This is the default value for this attribute.
9. In the Project Explorer view:
a. Open the HRPortlet168ViewBean bean located in HRPortlet168/Java Resources/JavaSource/hrportlet168/.
b. Add a variable to indicate if the department code property is wired to an active action and add its corresponding getter and setter methods.
c. Necessary changes on HRPortlet168ViewBean bean are highlighted in Example 26-7. Example 26-7 Setting an indicator on HRPortlet168ViewBean
package hrportlet168; import com.ibm.db.beans.*;
public class HRPortlet168ViewBean { private DBSelect resultFromDatabase; // add this code private boolean deptIDActive;
public boolean isDeptIDActive() { return deptIDActive; }
public void setDeptIDActive(boolean deptIDActive) { this.deptIDActive = deptIDActive; } // end of added code
public DBSelect getResultFromDatabase() { return resultFromDatabase; }
public void setResultFromDatabase(DBSelect resultFromDatabase) { this.resultFromDatabase = resultFromDatabase; } }
![]()
10. Save the HRPortlet168ViewBean.java file.
11. Return to HRPortlet168 portlet code and add the following code to the doView method to inform the JSP whether or not the service is available and there are wires active so that dynamic links should be shown. Example 26-8 Set the indicator value in doView method
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { response.setContentType(request.getResponseContentType()); HRPortlet168SessionBean sessionBean = getSessionBean(request); if( sessionBean==null ) { response.getWriter().println("<b>NO PORTLET SESSION YET</b>"); return; } String sqlstring = sessionBean.getSqlString(); if (sqlstring != null && !sqlstring.equals("")) { String dbname = sessionBean.getDbName(); String userid = sessionBean.getUserId(); String password = sessionBean.getPassword(); HRPortlet168ViewBean viewBean = new HRPortlet168ViewBean(); SQLUtilities sqlUtility = new SQLUtilities(); try { sqlUtility.execute(userid, password, sqlstring); } catch (SQLException e) { e.printStackTrace(); } sqlUtility.populateData(viewBean); // add this code if (pbServiceAvailable == true) { try { viewBean.setDeptIDActive(pbService.areWiresActive(request, DEPT_NO)); } catch (Exception e) { } } // end of added code request.setAttribute(VIEW_BEAN, viewBean); } PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP)); rd.include(request,response); }
![]()
12. Review the added code:
- The areWiresActive method of the PropertyBrokerService interface tests if a property is currently wired to active actions.
- It receives the following:
PortletRequest object for the current portlet request
The name of the property to check.
13. Save the HRPortlet168.java file.
ibm.com/redbooks