Tutorials > Contracts > Extend the existing contract model


Create a new business policy

Create a business policy type typically involves registering a unique business policy in the database, as well as creating a new business policy command.

This process involves the following high-level steps:

  1. Create a new business policy type (if required).

  2. Write the new business policy command.

  3. Register the new business policy and business policy command in the database.


Procedure

  1. Create a new business policy type

    A business policy type indicates the realm of the transaction to which a policy applies. Examples of business policy types include:

    • Price

    • ProductSet

    • ShipMode

    • ShipCharge

    • Payment

    • ReturnCharge

    • ReturnApproval

    • ReturnPayment

    • InvoiceFormat

    If the existing business policy types do not satisfy your business requirements, you should create a new business policy type. Creating the new business policy type consists of defining and registering the business policy type.

    When defining and registering a new policy type, update the following database tables:

    POLICYTYPE

    The POLICYTYPE table specifies the type of business policy that you are creating. It contains a single column, POLICYTYPE_ID, that is the primary key. An example value is Price. If you create a new business policy type, ensure that you specify a unique POLICYTYPE_ID.

    PLCYTYCMIF

    The PLCYTYCMIF table is the business policy type to command interface relationship specification table. That is, for each business policy type, it specifies the Java command interface for the business policy object. While there can be zero or more business policy commands that implement a business policy, each of the business policy commands must implement the interface specified here.

    PLCYTYPDSC

    The PLCYTYPDSC table specifies a description of the business policy type. It includes a language identifier of the description and the description of the business policy type.

    1. To create a new business policy type, create an entry in each of these tables for the new business policy type. The following SQL statements provides an example:

      insert into POLICYTYPE (POLICYTYPE_ID) values ('MyNewPolicyType'); insert into PLCYTYCMIF (POLICYTYPE_ID, BUSINESSCMDIF) 
         values ('MyNewPolicyType', 
         'com.mycompany.mybusinesspolicycommands.MyNewPolicy'); insert into  PLCYTYPDSC (POLICYTYPE_ID, LANGUAGE_ID, DESCRIPTION) 
         values ('MyNewPolicyType', -1, 
         'My new policy type for example purposes.');
      

    2. As the final step of creating the new business policy type, you may code one or more new business policy type interfaces. These interfaces are then implemented by any business policy command that falls under the realm of this business policy type. For example, in the Advanced B2B direct starter store, Price is defined as a business policy type. As such, there are the com.ibm.commerce.price.commands.ResolvePriceListsCmd and com.ibm.commerce.price.commands.RetrievePricesCmd interfaces that are implemented by all price-related business policy commands.

    If we will not have a business policy command that performs operations on the new business policy type, then you are not required to create a new interface. This is rare, and in most cases when creating a new business policy type, create a new business policy type interface as well.

    When you create a business policy type interface, the new interface must extend the com.ibm.commerce.command.BusinessPolicyCommand interface.

  2. Write the new business policy command

    To create a new business policy command, create a new command that implements the interface of the business policy type to which the command relates. The new command must also extend com.ibm.commerce.command.BusinessPolicyCommandImpl implementation class. This is very similar to creating a new controller or task command.

    There are two different approaches by which you can pass input properties to a business policy command. The first way is to have default input properties specified in the PROPERTIES column of the POLICY table. For more information about this table, refer to the following section.

    The second approach is to create a new field in the command for each of the input properties. For each field, create a new pair of getter and setter methods.

  3. Set requestProperties in business policy commands

    There are two ways in which requestProperties are set in a business policy command object. The first way uses the PROPERTIES column of the POLICY table to set the default properties. This is accomplished by the setRequestProperties method. The second way to set properties is to have the command (controller or task) that calls the business policy command explicitly set other required properties.

    When creating a new business policy command, you should override the default setRequestProperties method to include the logic to explicitly set each of the parameters that are included in the requestProperties object.

    Consider an example of a new business policy command that has an interface name of MyNewBusinessPolicyCmd and implementation class name of MyNewBusinessPolicyCmdImpl.

    Assume that the entry in the POLICY table for this new business policy command includes the following values in the PROPERTIES column:

    • defaultProperty1=apple

    • defaultProperty2=orange

    • defaultProperty3=banana

    The interface for this new business policy command is defined as follows:

    public interface MyNewBusinessPolicyCmd extends 
       com.ibm.commerce.command.BusinessPolicyCmd {
          java.lang.String defaultCommandClassName = 
             'com.mycompany.mycommands.MyNewBusinessPolicyCmdImpl';       public void setProperty1();       public void setProperty2(); } 
    

    The implementation class for this new business policy command is defined...

    public class MyNewBusinessPolicyCmdImpl extends 
       com.ibm.commerce.command.BusinessPolicyCmdImpl 
       implements com.mycompany.mycommands.MyNewBusinessPolicyCmd {
          // Establish default properties that are stored in the POLICY table
    
          private java.lang.String defaultProperty1;       private java.lang.String defaultProperty2;       private java.lang.String defaultProperty3;                 
          // Begin to establish properties that must be set 
         // by the calling command. 
          
          //  *** property1   ***
          private java.lang.String property1;       public java.lang.String getProperty1() {
             return property1;          }
          public void setProperty1(java.lang.String newProperty1) {
             property1 = newProperty1;       }
    
          //  *** property2   ***
          private java.lang.String property2;       public java.lang.String getProperty2() {
             return property2;          }
          public void setProperty1(java.lang.String newProperty2) {
             property2 = newProperty2;          }
    
          // End establishing properties that must be set 
          // by the calling command.
            
          /* Upon instantiation the business policy command sets all 
             default properties from the POLICY table into the 
             requestProperties object.  The calling command
             is responsible for setting any other required properties.
          */
    
          public void setRequestProperties(com.ibm.commerce.datatype.TypedProperty 
             requestProperties) {
             //  Get the default properties defined in the POLICY table
             setDefaultProperty1(requestProperties.get("defaultProperty1"));          setDefaultProperty2(requestProperties.get("defaultProperty2"));          setDefaultProperty3(requestProperties.get("defaultProperty3"));        
             }
            
    }
    

    The command that calls the new business policy command could be defined in a manner similar to the following:

    public class MyCallerCommandImpl 
       extends com.ibm.commerce.command.TaskCommandImpl
       implements com.mycompany.mycommands.MyCallerCommand {
       
       /* Include all elements and processing required for the 
          task command.
       */
    
       // Determine the policy ID and setPolicyId
    
       // Call the business policy command.
    
       cmd = (MyNewBusinessPolicyCmd) CommandFactory
                createPolicyCommand(policyId); 
       // Set required properties
       
       cmd.setProperty1("Fruit salad");    cmd.setProperty2("Favorite food"); 
            cmd.execute(); }
    

  4. Register the new business policy and business policy command

    After you have created the new business policy command, register both the business policy and the business policy command in the database.

    Business policies are registered in the POLICY table. This table contains the following columns:

    POLICY_ID

    The primary key. This is the policy identifier.

    POLICYNAME

    Unique policy name.

    POLICYTYPE_ID

    The policy type identifier. This is the foreign key to the POLICYTYPE table.

    STOREENT_ID

    The store or store group to which the policy applies.

    PROPERTIES

    Default properties that can be set to the business policy command. Specified as name-value pairs, for example, parm1=val1&parm2=val2.

    STARTDATE

    The starting date (specified as a timestamp) of the policy. If NULL, the starting date is immediate.

    ENDDATE

    The ending date (specified as a timestamp) of the policy. If NULL, there is no end date.

    Once the new policy is registered in the POLICY table, register a relationship between the policy and the business policy command that implements the business policy. The POLICYCMD table is used for this purpose. The POLICYCMD table contains the following columns:

    POLICY_ID

    Foreign key reference to the POLICY table.

    BUSINESSCMDCLASS

    The business policy command that implements the policy.

    PROPERTIES

    Default properties that can be set to the business policy command. Specified as name-value pairs, for example, parm1=val1&parm2=val2.


+

Search Tips   |   Advanced Search