Friday 1 March 2013

Develop a new Controller command

This demonstrates the development of new Controller Command and invoke/call task command from new controller command.

1)Create new Controller Command
Create an Interface - Right click on package com.myCompany.commands->New->Interface. give interface name as MyCatalogControllerCmd
This package is created in section Develop new Task Command
2)Extend com.ibm.commerce.command.ControllerCommand interface
3)Create implementation class for above interface
Right click on package->New->class-> MyCatalogControllerCmdImpl.java
4)Make sure this class extends com.ibm.commerce.command.ControllerCommandImpl class and implements  MyCatalogControllerCmd interface
5)Right click on the implementation class and select Source->Override/Implement Methods
select performExecute() method from list as shown below:






















6)Implement the performExecute() method - This calls MyCatalogTaskCmd class implemented at Develop new Task Command:

@Override
    public void performExecute() throws ECException {
        // rspProp will hold the responses from the tasks
        TypedProperty rspProp = new TypedProperty();
        MyCatalogTaskCmd myCatalogTaskCmd = null;
        String mailingListMessage = "Please join our mailing list";

        super.performExecute();
       
        try {
            // STEP ONE: Run the MyCatalogTaskCmd task command           
            myCatalogTaskCmd = (MyCatalogTaskCmd) CommandFactory.createCommand (
                    "com.mycompany.commands.MyCatalogTaskCmd", getStoreId());

            // this is required for all commands
            myCatalogTaskCmd.setCommandContext(getCommandContext());

            // invoke the task command's performExecute method
            myCatalogTaskCmd.execute();

            // retrieve output parameters from the task command,
            // then add them to response properties
            rspProp.put("taskShippingMessage", "The " + myCatalogTaskCmd.getCatalogId() +
                                                " catalog will be shipped");
            rspProp.put("taskShippingAddressee", "To: " + myCatalogTaskCmd.getCustomerName() +
                                                " at: " + myCatalogTaskCmd.getAddress());

        } catch (ECException ex) {
            throw (ECException) ex;
        }

        rspProp.put(ECConstants.EC_VIEWTASKNAME, "MyCatalogTaskCmdView");
        setResponseProperties(rspProp);
    }