Adding required fields to the data bean
You can modify the required fields in your new data bean. The two required fields are for the following types of information: command context and request properties.
Procedure
- Double-click the new data bean (for example, UserResDataBean) to view its source code.
- Locate the getCommandContext
method. It initially appears as follows:
public CommandContext getCommandContext() { return null; }
Modify the code so that it appears as follows:
private CommandContext iCommandContext = null; public com.ibm.commerce.command.CommandContext getCommandContext() { return iCommandContext; }
- Locate the setCommandContext
method. It initially appears as follows:
public void setCommandContext(CommandContext arg0) { }
Modify the code so that it appears as follows:
public void setCommandContext(com.ibm.commerce.command.CommandContext aCommandContext) { iCommandContext = aCommandContext; }
- Save your work.
- To modify the iRequestProperties field:
- Double-click the new data bean (for example, UserResDataBean) to view its source code.
- Locate the getRequestProperties method. It
initially appears as follows:
public TypedProperty getRequestProperties() { return null; }
Modify the code so that it appears as follows:
private com.ibm.commerce.datatype.TypedProperty requestProperties; public TypedProperty getRequestProperties() { return requestProperties; }
- Locate the setRequestProperties method. It
initially appears as follows:
public void setRequestProperties(TypedProperty arg0) throws Exception { }
Modify the code so that it appears as follows:
public void setRequestProperties(com.ibm.commerce.datatype.TypedProperty aParam) throws Exception { // copy input TypedProperties to local requestProperties = aParam; }
- Save your work.
- Proceed to modifying the populate method.
- Populating the primary key of the corresponding access
bean
You might want to modify the source code to populate the primary key of the corresponding access bean. The recommended way to do this is to use the data bean manager to indirectly set this value. This indirect method ensures that a primary key value taken from the URL properties will not override the primary key, if it has previously been set. To have your setRequestProperties method follow this model, code it in a fashion that is similar to the following code snippet. Note that in the following example the primary key is the user ID. This may be different depending upon the situation and as such, the following code may not immediately compile in your application.
public void setRequestProperties( com.ibm.commerce.datatype.TypedProperty arg1) throws Exception { iRequestProperties = arg1; try { if (// check for nulls getDataBeanKeyUserId() == null) { super.setInitKey_UserId(aUserId); } } catch (com.ibm.commerce.exception.ParameterNotFoundException e) { } }
There are two other ways in which the primary key for the access bean can be set. It can be done externally from the data bean, for example in the JSP page. In this case, before activating the data bean in the JSP page, use the userBean tag's
c:set
parameter to set the primary key. For example, the JSP could include code similar to the following:<wcbase:useBean id="orderBean" classname="com.ibm.commerce.order.beans.OrderDataBean" scope="page"> <c:set value="${orderId[0]}" target="${orderBean}" property="orderId"/> </wcbase:useBean>
Alternatively, the primary key can be set in a direct way. For example, the code for the setRequestProperties method of the data bean would appear similar to the following:
public void setRequestProperties( com.ibm.commerce.datatype.TypedProperty arg1) throws Exception { iRequestProperties = arg1; try { super.setInitKey_UserId(aUserId); } } catch (com.ibm.commerce.exception.ParameterNotFoundException e) { } }
Note that the recommended procedure for setting the primary key is the indirect method.