Tutorial: Modifying an existing controller command
Attention: This tutorial is currently under revision. The content might contain errors or inaccuracies. Subscribe to this page to be notified when an updated version is available.
This tutorial demonstrates how to modify an existing controller command. In this tutorial, we will restrict the number of items in a customer's shopping cart to five or fewer items.To implement this solution, we will override the OrderItemAddCmdImpl class with the own implementation that includes logic to check for the number of items in the shopping cart. If a customer attempts to add a sixth item to the shopping cart, an exception is thrown. This exception uses a new error message.
Learning objectives
This tutorial is not meant to be an all encompassing example of restricting items in the shopping cart. The logic used in this tutorial is simplified.
After completing this tutorial we will be able to do the following tasks:
- Create a new implementation for an existing controller command
- Update the command registry so that the new implementation gets used in the application
- Deploy a modified controller command to an existing WebSphere Commerce application
Time required
Expect this tutorial to take about 30 minutes to complete.
You can load the completed tutorial code instead of performing each step. After loading the completed tutorial code into the workspace, you can run the code as if you performed each step in the tutorial. However, in order to fully attain the learning objectives, it is recommended that you perform the tutorial step by step.
Audience
This tutorial is intended for WebSphere Commerce Store developers.
Prerequisites
Before starting this tutorial you should have a basic understanding of:
- Rational Application Developer.
- The Java programming language.
- The database type, Derby, DB2, iSeries, or Oracle, used in the WebSphere Commerce development environment.
Code prerequisitesIf you completed other WebSphere Commerce programming tutorials, there is no harm in leaving the code in the workspace, as it will not conflict with this tutorial.
- Before starting this tutorial, have published a store based upon the consumer direct sample store within the development environment. Within this store, be able to complete a purchase (for example, browse the catalog, add items to the shopping cart, check out and see the order confirmation).
- In order to complete the deployment steps, the store must also exist on the target WebSphere Commerce Server.
Lessons in this tutorial
- Create message information
Your new command implementation uses a new error message: _ERR_TOO_MANY_ITEMS. In this step, we will create the code for that new message and its associated properties file.
- Create MyOrderItemAddCmdImpl
You will create the new MyOrderItemAddCmdImpl class that implements the interface defined in OrderItemAddCmd. The MyOrderItemAddCmdImpl class contains the code that ensures a customer's shopping cart contains five or fewer items.
- Modify the command registry
In this step, we will modify the command registry so that the new MyOrderItemAddCmdImpl implementation class gets used instead of the original OrderItemAddCmdImpl implementation class. The only table in the command registry that needs to be modified is the CMDREG table. In this case, the new implementation class is used for all stores.
- Test the MyOrderItemAddCmdImpl command
The next step is to test to ensure the new logic is functioning well.To test the new logic, you should be able to successfully add five items to the shopping cart, but expect an error to be thrown when you attempt to add a sixth item to the cart.
- Deploy MyOrderItemAddCmdImpl
In this step, we will deploy the modified business logic to a target WebSphere Commerce Server.
- Load the completed tutorial code
You can load the completed code into WebSphere Commerce Developer instead of completing each step in the tutorial. Loading the code consists of the following high-level steps: Updating the command registry on the target WebSphere Commerce Server; and Validating the new logic on the target WebSphere Commerce Server.