Tutorials > Program model > Create new business logic
Create a new task command
In this step, we will create a new task command interface and its associated implementation class. Initially, the new task command does very little except handle view parameters. It only has fields for defaultCommandClassName, URL parameters, and the current value of bonus points. It has a method for getting the current value of bonus points.
A controller command typically represents a business process or complex function. For example, all of the business logic related to processing orders is encapsulated in the OrderProcessCmd controller command. A business process can often be divided up into smaller, more specific tasks. For example, the OrderProcessCmd controller command consists of several task commands that each perform individual units of work.
At this point in the tutorial, MyNewControllerCmdImpl does not call any task commands. This section is divided into two sub-sections. In the first sub-section, we will create the new task command. In the second sub-section, we will modify the performExecute method of the controller command to call the new task command.
In this step of the tutorial, we will learn the following information:
- How to create a new task command interface and its associated implementation class
- The minimum amount of code that is required in a task command
- How to add fields and methods to the task command
Create MyNewTaskCmd
Create a completely new task command involves creating an interface and an implementation class. When creating a task command, the interface should extend com.ibm.commerce.commands.TaskCommand. The implementation class should extend com.ibm.commerce.command.TaskCommandImpl.
Procedure
- In the Enterprise Explorer view, navigate to the WebSphereCommerceServerExtensionsLogic project.
- Expand the src directory, and then the com.ibm.commerce.sample.commands directory.
- Open the MyNewTaskCmd.java interface to view its source code.
- In the source code for this interface, uncomment Section 1 to introduce the following code into the interface. This code creates a field that specifies the default implementation class to be used by the interface:
/// Section ///////// // set default command implement class static final String defaultCommandClassName= "com.ibm.commerce.sample.commands.MyNewTaskCmdImpl"; /// End of section/////////Since the same implementation class is used for the entire site and no default properties are passed to the command, you can specify the default implementation right in the code. If you have a command that either has multiple implementations, or has default properties (which are stored in the CMDREG table), register the command in the CMDREG table to create the mapping between the interface and implementation class.
- Uncomment Section 2 to create getter and setter methods that will be used in the MyNewTaskCmdImpl implementation class. These methods are for fields corresponding to the following types of information:
- A customer's user ID
- A value of bonus points
- A greeting message
By uncommenting Section 2, the following code is introduced into the interface:
/// Section ////////// // set interface methods public void setInputUserName(java.lang.String inputUserName); public void setInputPoints(Integer inputPoints); public void setGreetings(java.lang.String greeting); public java.lang.String getInputUserName(); public java.lang.Integer getInputPoints(); public java.lang.String getGreetings(); /// End of section/////////
- Save the changes.
- Open the MyNewTaskCmdImpl.java implementation class to view its source code.
- Uncomment Sections 1A and 1B to create fields and their corresponding getter and setter methods in the implementation class. By uncommenting sections 1A and 1B, you introduce the following code into the class:
- Save the work.
- In the Outline view, select the performExecute method of the MyNewTaskCmdImpl class.
- In the source code of this performExecute method, uncomment Section 1 to introduce the following code into the method:
/// Section ////////// /// modify the greetings and see it in the NVP list setGreetings( "Hello! " + getInputUserName() ); /// End of section ////////The preceding code updates the greetings value and makes the value available in the name-value pair (NVP) list. The greetings value is then made available then to other objects by using the getGreetings() method.
- Save the work.
- Call the task command
Once you have created your task command, call the command from within the controller command:
- If it is not already open, open the MyNewControllerCmdImpl.java class.
- In the Outline view, select its performExecute method.
- In the source code for the performExecute method, navigate to Area 4.
- Uncomment Sections 4A, 4B, and 4C to introduce the following code into the method:
/// Section 4A /// see how the controller command calls a task command MyNewTaskCmd cmd = null; try { cmd = (MyNewTaskCmd) CommandFactory.createCommand( "com.ibm.commerce.sample.commands.MyNewTaskCmd", getStoreId()); // this is required for all commands cmd.setCommandContext(getCommandContext()); /// set input parameters to task command cmd.setInputUserName(getUserName()); cmd.setInputPoints(getPoints()); // change to Integer /// End Section 4/////////// /// Section 4/////// /// invoke the command's performExecute method cmd.execute(); /// retrieve output parameter from task command, then put it to /// response properties rspProp.put("taskOutputGreetings", cmd.getGreetings()); /// End Section 4///////// /// Start Section 4//////// } catch (ECException ex) { /// throw the exception as is throw (ECException) ex; } /// End Section 4//////////The code in Section 4A creates the new task command object using the command factory, then sets the command context, and sets the input parameters of the task command.
The code in Section 4B calls the validateParameters method for access control purposes, before invoking the execute method of the task command. The code then retrieves the greetings value from the task command.
The code in Section 4C is a simple catch block for exceptions.
The performExecute method must never be called directly. Instead, the execute method must be called.
- At this point, there is one remaining compilation error.
To correct it, click the red indicator to the right of the editor's scroll bar. Your cursor highlights the compilation error.
- If you are prompted to select the CommandFactory when more than one match is found, select com.ibm.commerce.command.CommandFactory.
- Right-click the highlighted text and select Source > Organize Imports.
- Save the changes.
- Modify MyNewJSPTemplate to add the greetings message
Modify the MyNewJSPTemplate.jsp file to add a new section that displays the greetings message:
- If the JSP files are not already open:
- In the Enterprise Explorer view, expand the Stores > WebContent > ConsumerDirect_name project.
- Select both the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files, right-click and select Open.
- Copy SECTION 7 from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file to introduce the following text into the JSP page:
<!-- SECTION 7 --> <fmt:message key="Greeting" bundle="${tutorial}" /> <c:out value="${taskOutputGreetings}"/> <br /> <br /> <!-- END OF SECTION 7 -->
- Save the changes.
- Test MyNewTaskCmd
Test the new task command:
- Restart the test environment.
- Navigate to the Stores > WebContent > ConsumerDirect_name directory.
- Select the index.jsp file and from its pop-up menu select Run As > Run on Server. The store home page displays in the Web browser.
- Enter the following URL: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc&input2=1000 The MyNewJSPTemplate page displays. It includes the greeting message that was created by the task command.