Using business context data in customization

Some customization tasks involve writing business logic that must retrieve business context data about a user session. For example, this data might be the user ID, the store ID, the language ID, or the ID of the catalog the user is browsing. HCL Commerce provides a set of interfaces you can use to retrieve the specific business context data you need.

During an HCL Commerce browser session, data for the session is captured and stored as business context data. HCL Commerce groups the different types of available data according to purpose and function. Each grouping has an interface class that provides a set of methods to retrieve data. For example:

  • The methods in the com.ibm.commerce.context.base.BaseContext interface retrieve basic session data, such as the store ID and the run-as ID.
  • The methods in the com.ibm.commerce.context.globalization.GlobalizationContext interface retrieve locale-specific data, such as the language ID or currency for the session.
Additional interfaces and methods are provided.

Before you begin

For background information on business context data and a list of the business contexts HCL Commerce provides, review Business context service.

Procedure

  1. Identify the interface that provides the business context data you need.
    For a list of available interfaces and their methods, browse through the list of sub-interfaces in the API documentation for the parent interface, com.ibm.commerce.context.base.Context.
  2. Write the business logic to retrieve the business context data using the correct interface and method.

    For BOD command framework customization, use code similar to the following example. Your code must use the com.ibm.commerce.foundation.server.services.businesscontext.ContextServiceFactory, which is a helper class used to retrieve the context service that handles the data object you need. The code in this example returns the user ID and the store ID for the request:

                        
      ContextService bcs = ContextServiceFactory.getContextService();
    		BaseContext baseContext = (BaseContext) bcs
         .findContext(BaseContext.CONTEXT_NAME);
    		Long userId = baseContext.getRunAsId();
    		Integer storeId = baseContext.getStoreId();
    

    For name-value pair command framework customization, use code similar to the following example. In this case, your code must use the command context to get the context data object. The code in this example returns the user ID and the store ID for the request:

    BaseContext baseContext = (BaseContext)getCommandContext().getContext(BaseContext.CONTEXT_NAME); 
    Long userId = baseContext.getRunAsId();
    Integer storeId = baseContext.getStoreId();