Adding new business logic to a controller command
New business logic can be added before existing business logic,
after existing logic, or both before and after. Suppose there is an existing
WebSphere Commerce controller command, called ExistingControllerCmd
.
Following the WebSphere Commerce naming conventions, this controller command
would have an interface class named ExistingControllerCmd
and
an implementation class named ExistingControllerCmdImpl
.
Now assume that a business requirement arises and you must add new business
logic to this existing command. One portion of the logic must be executed
before the existing command logic and another portion must be executed after
the existing command logic.
About this task
The first step in adding the new business logic is to create
a new implementation class that extends the original implementation class.
In this example, you would create a new ModifiedControllerCmdImpl
class
that extends the ExistingControllerCmdImpl
class. The new
implementation class should implement the original interface (ExistingControllerCmd
).
In
the new implementation class you must create a new performExecute
method
to override the performExecute
of the existing command. Within
the new performExecute
method, there are two ways in which
you can insert your new business logic: you can either include the code directly
in the controller command, or you can create a new task command to perform
the new business logic. If you create a new task command then you must instantiate
the new task command object from within the controller command.
The following code snippet demonstrates how to add new business logic to the beginning and end of an existing controller command by including the logic directly in the controller command:
public class ModifiedControllerCmdImpl extends ExistingControllerCmdImpl
implements ExistingControllerCmd
{
public void performExecute ()
throws com.ibm.commerce.exception.ECException
{
/* Insert new business logic that must be
executed before the original command.
*/
// Execute the original command logic.
super.performExecute();
/* Insert new business logic that must be
executed after the original command.
*/
}
}
The following code snippet demonstrates how to add new business logic to the beginning of an existing controller command by instantiating a new task command from within the controller command. In addition, you would also create the new task command interface and implementation class and register the task command in the command registry.
// Import the package with the CommandFactory
import com.ibm.commerce.command.*;
public class ModifiedControllerCmdImpl extends ExistingControllerCmdImpl
implements ExistingControllerCmd
{
public void performExecute ()
throws com.ibm.commerce.exception.ECException
{
MyNewTaskCmd cmd = null;
cmd = (MyNewTaskCmd) CommandFactory.createCommand(
"com.mycompany.mycommands.MyNewTaskCommand",
getStoreId());
/*
Set task command's input parameters, call its
execute method and retrieve output
parameters, as required.
*/
super.performExecute();
}
}
Regardless of whether you include the new business logic in the controller command, or create a task command to perform the logic, you must also update the CMDREG table in the WebSphere Commerce command registry to associate the new controller command implementation class with the existing controller command interface. The following SQL statement shows an example update:
update CMDREG
set CLASSNAME='ModifiedControllerCmdImpl'
where INTERFACENAME='ExistingControllerCmd'