Use 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. WebSphere Commerce provides a set of interfaces we can use to retrieve the specific business context data you need.

During a WebSphere Commerce browser session, data for the session is captured and stored as business context data. WebSphere 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:

Additional interfaces and methods are provided.


Before starting

For background information on business context data and a list of the business contexts WebSphere 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, 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 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();


Related concepts
Business context service