Monday, October 7, 2013

Websphere Commerce: Controller Command Design Pattern

Commands are beans that perform business logic. Command beans follow a specific design pattern. Every command includes both an interface class (for example, CategoryDisplayCmd) and an implementation class (for example, CategoryDisplayCmdImpl). See the Websphere Commerce Command Framework and Command implementation documentation for more info.

The four main types of commands are: controller, task, view and data bean commands.

This blog post is about the controller command design pattern.

Controller Command Interface

Pattern:

public interface MyNewControllerCmd extends ControllerCommand {

   // set default command implement class
   static final String defaultCommandClassName  =  "com.ibm.commerce.sample.commands.MyNewControllerCmdImpl
}




Controller Command Implementation Class

Pattern 1:

public class MyNewControllerCmdImpl extends ControllerCommandImpl implements MyNewControllerCmd {
 
  /*  class property (variable) declarations go here  */ 
  public void setRequestProperties(TypedProperty arg0) throws ECException {
    
       super.setRequestProperties(arg0);
 
       /*  code for setting the values of the class properties to the corresponding request properties go here  */
  }
 
  // the performExecute method contains the business logic for your command
  public void performExecute() throws ECException {
    try {
 
      /* business logic code goes here */
 
      //create a new TypedProperty object  as  your  response object (output purpose) 
      // the getResponseProperties() method  of ControllerCommandImpl gets the response properties associated with this command
      TypedProperty responseProperties = getResponseProperties();
      if (responseProperties == null) {
           responseProperties = new TypedProperty();       
      }
 
      //populate your TypedProperty object
      responseProperties.put("notesAction", "");
      responseProperties.put("customerNo", getCustomerNo());
      responseProperties.put("companyName", getCompanyName());
      responseProperties.put("redirecturl", "CustomerNotesForm");
      responseProperties.put("viewTaskName", "RedirectView"); 
  
      //set the response properties with your response object
      setResponseProperties(responseProperties); 
  
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /* other methods go here */
 
  /* class property getters and setters go here */
  public String getCompanyName() {
    return companyName;
  }
 
}




Pattern 2: Using the  validateParameters() method of the ECCommand interface


public class MyNewControllerCmdImpl extends ControllerCommandImpl implements MyNewControllerCmd {

           /* class property (variable) declarations go here  */ 

          //the  validateParameters()method of the ECCommand interface performs server side parameter checking
          //you can call the getRequestProperties() of ControllerCommandImpl within this method
          public void validateParameters() throws ECException {

                 try {

                       // the getRequestProperties() method  of ControllerCommandImpl gets the request properties associated with this command
                                     TypedProperty  reqProp = getRequestProperties();

                      /*  code for setting the values of the class properties to the corresponding request properties go here  */



                   } catch(Exception e) {                    

                   }                  
    }           

 
          // the performExecute method contains the business logic for your command
          public void performExecute() throws ECException {

                        try {

                                    // create a new TypedProperty object  as  your  response object (output purpose) 
                                    // the getResponseProperties() method  of ControllerCommandImpl gets the response properties associated with this command 
                                    TypedProperty respProp = getResponseProperties();
                                    if (respProp== null) {
                                                respProp = new TypedProperty();
                                    }


                                    /* business logic code goes here */

 

                                    //populate your TypedProperty object
                                    respProp.put("identifier", "value");
                                    respProp.put("label", "name");
                                    respProp.put("items", valueList);           // valueList is a JSON object

//set the response properties with your response object

                                    setResponseProperties(respProp);        
                  
                        } catch(Exception e) {
                                    e.printStackTrace();

                        }

            }

            /* other methods go here */


            /* class property getters and setters go here */

}




 





Example of a controller command implementation class using pattern 2:


public class YearMakeModelFilteredJSONCmdImpl extends ControllerCommandImpl implements YearMakeModelFilteredJSONCmd {

            //usual variable declarations
            private String retrieveType;
            private String yearId;
            private String makeId;
            private List values;


            //the  validateParameters()method of the ECCommand interface performs server side parameter checking
            //you can call the getRequestProperties() of ControllerCommandImpl within this method
            public void validateParameters() throws ECException {

                try{
                                    // the getRequestProperties() method  of ControllerCommandImpl gets the request properties associated with this command 
                                    TypedProperty reqProp = getRequestProperties();
                                    setRetrieveType("YEAR");
                                    setYearId(reqProp.getString("yearId", null));
                                    if (getYearId() != null && !getYearId().equals("")) {
                                                setRetrieveType("MAKE");
                                                setMakeId(reqProp.getString("makeId", null));
                                                if (getMakeId() != null && !getMakeId().equals("")) {
                                                            setRetrieveType("MODEL");
                                                }
                                    }

                }catch(Exception e){


                }                 

            }

        
            // the performExecute method contains the business logic for your command
            public void performExecute() throws ECException {

                                    // the getResponseProperties() method  of ControllerCommandImpl gets the response properties associated with this command
                                    // create a new TypedProperty object  as  your  response object (output purpose)
                                   TypedProperty respProp = getResponseProperties();
                                    if (respProp == null) {
                                                respProp = new TypedProperty();
                                    }


                                    /* business logic code goes here */

                                    //populate your TypedProperty object
                                    respProp.put("identifier", "value");
                                    respProp.put("label", "name");
                                    respProp.put("items", valueList);           // valueList is a JSON object
                                  
// set the response properties with your response object
                                    setResponseProperties(respProp);                   

            }

            /* other methods go here */

            /* class property getters and setters go here */

}





No comments:

Post a Comment