Tutorials > Program model > Create new business logic
Integrate the Bonus entity bean with MyNewControllerCmd
In this step, we will integrate the Bonus entity bean with the MyNewControllerCmd logic. Once the bean is integrated, the MyNewJSPTemplate.jsp file allows updates of a customer's balance of bonus points.
Integrate the Bonus entity bean with MyNewControllerCmd involves the following high-level steps:
- Create a new BonusDataBean to display bonus points in a JSP page.
- Modify the MyNewTaskCmd task command to include fields and methods for bonus points, updating the validateParameters method, and adding logic to update a user's balance of bonus points.
- Add a getResources method to the MyNewControllerCmdImpl class to return a list of the resources that the command uses. The getResources method is also included for access control purposes.
- Create a new access control policy for the new resources.
- Modify the MyNewJSPTemplate.jsp page to allow a user to enter bonus points for a customer and then display that customer's new balance of bonus points.
Procedure
- Create the BonusDataBean data bean
To be consistent with the programming model, we will create a new data bean that corresponds to the new Bonus entity bean. While not all entity beans are required to have a corresponding data bean, if you want to be able to display information from the entity bean in a JSP page, you should create a new data bean for this purpose.
In this section, we will create a new BonusDataBean data bean that extends the BonusAccessBean.
To introduce the BonusDataBean into your code:
- Navigate to Other Projects > WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.databeans.
- Open the BonusDataBean.java class to view its source code.
- In the source code, uncomment Section 1, to introduce the following code into the bean:
/// Section ///////// // create fields and accessors (setter/getter methods) private java.lang.String userId; private java.lang.Integer totalBonusPoints; public java.lang.String getUserId() { return userId; } public void setUserId(java.lang.String newUserId) { userId = newUserId; /////////// /// Section A : instantiate BonusAccessbean if (userId != null) this.setInitKey_memberId(new Long(newUserId)); /////////// } public java.lang.Integer getTotalBonusPoints() { return totalBonusPoints; } public void setTotalBonusPoints(java.lang.Integer newTotalBonusPoints) { totalBonusPoints= newTotalBonusPoints; } //// End of section ///////////
- Next, uncomment Section 2, to introduce the following section of code into the bean:
/// Section///////// // create a new constructor for passing access bean into databean // so that JSP can work with the access bean public BonusDataBean(BonusAccessBean bb) throws com.ibm.commerce.exception.ECException { try { super.setEJBRef(bb.getEJBRef()); } catch (javax.ejb.FinderException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, "BonusDataBean", "BonusDataBean(bb)"); } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, "BonusDataBean", "BonusDataBean(bb)"); } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, "BonusDataBean", "BonusDataBean(bb)"); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, "BonusDataBean", "BonusDataBean(bb)"); } } //// End of section ///////////
- Next, uncomment Section 3, to introduce the following code into the bean:
- Next, uncomment Section 4 to introduce the following code into the bean:
/// Section ////////// // copy input TypedProperties to local requestProperties = aParam; /// End of section ////////
- Save the changes.
- Modify the MyNewTaskCmd interface to include bonus points
In this section, we will modify the MyNewTaskCmd interface to specify the required fields and methods for bonus points:
- Expand the Other Projects > WebSphereCommerceServerExtensionsLogic project.
- Expand the src\com.ibm.commerce.sample.commands directory.
- Double-click the MyNewTaskCmd interface to view its source code.
- Uncomment Import section 2 to include the following package:
/// Import section ///////// import com.ibm.commerce.extension.objects.BonusAccessBean; /// End of import section ////../images/locale/screensnap///
- Uncomment Section 4 to introduce the following code into the method:
/// Section ////////// public java.lang.Integer getOldBonusPoints(); public Integer getTotalBonusPoints(); public void setBonusAccessBean(BonusAccessBean bb); public BonusAccessBean getBonusAccessBean(); /// End of section/////////
- Save the changes. Errors and warnings that might display in the task view will be resolved in a later step.
- Modify MyNewTaskCmdImpl to calculate bonus points
The MyNewTaskCmdImpl is used as the point of integration between the Bonus entity bean and the MyNewControllerCmd (since MyNewControllerCmd invokes the MyNewTaskCmd).
To modify MyNewTaskCmdImpl to calculate bonus points:
- Select the MyNewTaskCmdImpl class to view its source code.
- Uncomment Import section 2 to introduce the following package. You might have to expand the import section to display Import section 2.:
/// Import section ////////// import com.ibm.commerce.extension.objects.BonusAccessBean; /// End of Import section ////../images/locale/screensnap////
- Uncomment Sections 3A and 3B to introduce the following code into the class:
- In the Outline view, select the validateParameters method and uncomment Section 2, to introduce the following code into the method:
// section ////////// try { oldBonusPoints = bb.getBonusPoint(); } catch (javax.ejb.FinderException e) { try { // If bb is null, create a new instance short optCounter=(short)0; bb = new BonusAccessBean(new Long(foundUserId), new Integer(0), optCounter); oldBonusPoints = new Integer(0); } catch (javax.ejb.CreateException ec) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (javax.naming.NamingException ec) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (java.rmi.RemoteException ec) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), "validateParameters"); } } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), "validateParameters"); } // end of section /////////
- In the Outline view, select the performExecute method.
- In the source code for this performExecute method, uncomment Section 2. This introduces the following code into the method:
/// Section /////////////
/// use BonusAccessBean to update new bonus point int newBP = oldBonusPoints.intValue() + getInputPoints().intValue(); totalBonusPoints = new Integer (newBP); bb.setBonusPoint(totalBonusPoints) ; try { bb.commitCopyHelper(); } catch (javax.ejb.FinderException e) { throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, this.getClass().getName(), "performExecute"); } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), "performExecute"); } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), "performExecute"); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), "performExecute"); } /// End of section ///////////
- Save the changes.
- Add a getResources method to the MyNewControllerCmdImpl class
In this section, you add a new getResources method to the MyNewControllerCmdImpl. This method returns a list of resources that the command uses during processing. This method is required for resource level access control.
To add the getResources method:
- Open the MyNewControllerCmdImpl class and view its source code.
- Uncomment Section 3, to introduce the following code into the class:
/// Section ////////// /// Create an instance variable of type AccessVector to hold /// the resources and a BonusAccessBean instance variable for /// access control purposes. private AccessVector resources = null; private BonusAccessBean bb = null; /// End of Section ////////
- In the source code, uncomment the Access Control Section. This section displays as shown in the following code snippet:
/// AccessControl Sectio//////// public AccessVector getResources() throws ECException{ if (resources == null ) { /// use UserRegistryAccessBean to check user reference number String refNum = null; String methodName = "getResources"; rrb = new UserRegistryAccessBean(); try { rrb = rrb.findByUserLogonId(getUserName()); refNum = rrb.getUserId(); } catch (javax.ejb.FinderException e) { throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, this.getClass().getName(),methodName,e); } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), methodName,e); } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), methodName,e); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), methodName,e); } /// find the bonus bean for this registered user bb = new com.ibm.commerce.extension.objects.BonusAccessBean(); try { if (refNum != null) { bb.setInitKey_memberId(new Long(refNum)); bb.refreshCopyHelper(); resources = new AccessVector(bb); } } catch (javax.ejb.FinderException e) { //We don't have a bonus object so return the container that will hold the //bonus object when it's created UserAccessBean uab = new UserAccessBean(); uab.setInitKey_MemberId(refNum); resources = new AccessVector(uab); return resources; } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), methodName); } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), methodName); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), methodName); } } return resources; } /// End of AccessControl Sectio//////
- Save the changes.
- Modify the performExecute method of the MyNewControllerCmdImpl class
In this step we will modify the code in the performExecute method of the MyNewControllerCmdImpl to include code related to the new bonus bean,...
- In the Outline view, select the performExecute method of MyNewControllerCmdImpl.
- In the source code, uncomment Sections 4E, 4G, and 4H to introduce the following code into the method:
// Section 4//////// /// pass bb instance variable to the task command cmd.setBonusAccessBean(bb); // End of section 4////../images/locale/screensnap// // Section 4/////////// if (cmd.getOldBonusPoints() != null) { rspProp.put("oldBonusPoints", cmd.getOldBonusPoints()); } // End of section 4////../images/locale/screensnap// // Section 4/////////// ///Instantiate the bonus data bean , then put it to response properties BonusDataBean bdb = new com.ibm.commerce.sample.databeans.BonusDataBean( cmd.getBonusAccessBean()); rspProp.put("bdbInstance", bdb ); // End of section 4////../images/locale/screensnap/////
- At this point, there are compilation errors.
To correct them, right-click anywhere over the Java code editor and select Source > Organize Imports.
- Save the changes.
- Create the access control policy for the new entity bean
A sample access control policy is provided. This policy creates the following access control objects:
- An action
- The action that is created is com.ibm.commerce.sample.commands.MyNewControllerCmd
- An action group
- The action group that is created is MyNewControllerCmdActionGroup. This action group contains only one action: com.ibm.commerce.sample.commands.MyNewControllerCmd
- A resource category
- The resource category that is created is com.ibm.commerce.sample.objects.BonusResourceCategory. This resource category is for the Bonus entity bean.
- A resource group
- The resource group that is created is BonusResourceGroup. This resource group only contains the preceding resource category.
- A policy
- The policy that is created is AllUsersUpdateBonusResourceGroup. This policy allows users to perform the MyNewControllerCmd action on the Bonus bean only if the user is the "owner" of the bonus object. For example, if the user is logged on as the tester@mycompany user, the user can only modify their own bonus points.
Set up the AllUsersUpdateBonusResourceGroup policy involves the following steps:
- Modify the access control policies to reflect the environment.
- Load the SampleACPolicy.xml file using the acpload command.
- Load the SampleACPolicy_en_US.xml description using the acpnlsload command.
To customize the access control policies for the environment, do the following:
- Determine the member ID value for the consumer direct store:
In a Web browser, open the following URL: http://localhost/webapp/wcs/admin/servlet/db.jsp
- Enter the following SQL statement:
select member_id from storeent where storeent_id=CD_storeent_ID;
where CD_storeent_ID is the store entity ID for the consumer direct store. For example, you can enter:
select member_id from storeent where storeent_id=10001;
- Click Submit Query and make note of the member ID value: ________________________
- Navigate to WC_INSTALL\xml\policies\xml and make a copy of the SampleACPolicy_template.xml file, named SampleACPolicy.xml, and a copy of the SampleACPolicy_template_en_US.xml file called SampleACPolicy_en_US.xml.
- Use a text editor, open the SampleACPolicy.xml and SampleACPolicy_en_US.xml files and replace all occurrences of ConsumerDirectMemberId with the member ID value for the consumer direct store, as determined in the previous step.
- Save the files.
- Stop the test environment.
- At a command prompt, switch to the WCDE_INSTALL/\bin directory.
- Load the SampleACPolicy.xml and SampleACPolicy_en_US.xml files:
- Run the following commands:
acpload db_host_name db_name db_user db_password inputXMLFile SCHEMA_NAMEacpnlsload db_name db_user db_password NLSinputXMLFile SCHEMA_NAMEWhere
- db_host_name
- is the host name of the machine on which the database runs. This parameter is required for remote databases only.
- db_name
- is the name of the database
- db_user
- is the database user name
- db_password
- is the database password
- inputXMLFile
- is the name of the XML file containing the policy. In this case, enter SampleACPolicy.xml.
- NLSinputXMLFile
- is the name of the National Language XML file containing the policy description. In this case, enter SampleACPolicy_en_US.xml
- SCHEMA_NAME
- is the database user name or the user who owns the WebSphere Commerce schema.
For example, you can issue the following command:
acpload Demo_dev dbuser dbuser SampleACPolicy.xml dbuser acpnlsload Demo_dev dbuser dbpasword SampleACPolicy_en_US.xml dbuser
- Run the following commands:
acpload SampleACPolicy.xml acpnlsload SampleACPolicy_en_US.xml
- Modify the MyNewJSPTemplate.jsp page to include bonus points
To modify the display page:
- In Rational Application Developer, switch to the Web perspective.
- Open both the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files.
- Copy Section 9 from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file to introduce the following text into the JSP page:
<!-- SECTION 9 --> <h3><fmt:message key="BonusAdmin" bundle="${tutorial}" /> </h3> <c:if test="${!empty taskOutputUserId}"> <ul> <li> <b> <fmt:message key="PointBeforeUpdate" bundle="${tutorial}" /> <c:out value="${oldBonusPoints}"/> </b> </li> <li> <b> <fmt:message key="PointAfterUpdate" bundle="${tutorial}" /> <c:out value="${bdbInstance.bonusPoint}" /> </b> </li> </ul> </c:if> <br /> <b><fmt:message key="EnterPoint" bundle="${tutorial}" /></b><p /> <form name="Bonus" action="MyNewControllerCmd"> <table> <tr> <td> <b>Logon ID </b> </td> <td> <input type="text" name="input1" value="<c:out value="${userName}"/>" /> </td> </tr> <tr> <td> <b>Bonus Point</b> </td> <td> <input type="text" name="input2" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" /> </td> </tr> </table> </form> <!-- END OF SECTION 9 -->
- Save the MyNewJSPTemplate.jsp file.
- Test the integrated Bonus bean
Since the new Bonus bean is protected under access control and users can only execute the MyNewControllerCmd action on a bean that they own, the user must log in. As such, we will use the login feature in your starter store to allow the user to log in.
To test the new logic:
- Start the test environment.
- Navigate to the Stores > WebContent > ConsumerDirect_name directory.
- Select the index.jsp and from its pop-up menu, select Run > Run on Server.
- Log on as a registered user, by doing the following:
- Click the locale, for example, United States.
- Click Register or Log In.
- In the Logon ID field, enter the Logon ID for the user that you created in Test user name validation.
- In the Password field, enter the password for this user and then click Login.
- Once the login completes, enter the following URL in the same browser:
http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1= logon_id&input2=2000
where logon_id is the logon ID for the user that you created in Test user name validation. A page that contains all of the previous output parameters displays as well as a new form that allows you to update the balance of bonus points for the user.
If global security (LDAP) is enabled, instead of the logon_id, use the name registered in the LDAP server. This name must be properly URL encoded. For example, if the name registered to the LDAP server is uid=myName,cn=users,dc=ibm,dc=com, the URL encoded LDAP string is: uid%3DmyName%2Ccn%3Dusers%2Cdc%3Dibm%2Cdc%3Dcom.
Now, enter the user's logon id into the Logon ID field and enter 500 in the Bonus Point field. Click Submit. A page similar to the following one displays that shows that the updated balance of bonus points.