Saturday, 29 November 2014

Data Bean in Websphere Commerce


A data bean is a Java bean that is used within a JSP page to provide dynamic content. A data bean normally provides a simple representation of a Web Sphere Commerce entity bean. Data Bean can be used to fetch values from a data base or property file.

Types of Data Bean

  1. Smart Data Bean
  2. Command Data Bean
  3. Input Data Bean

A data bean is activated in one of the following ways:

  1. Using wcbase:useBean tag
  2. Using DataBeanManager.activate(data_bean, request, response)

Using wcbase:useBean tag 
Eg:
<%@ taglib uri="http://commerce.ibm.com/base" prefix="wcbase" %>

<wcbase:useBean id="orderBean"   classname="com.ibm.commerce.order.beans.OrderDataBean" scope="page">   
 <c:set value="${orderId[0]}" target="${orderBean}" property="orderId"/>
</wcbase:useBean>


id: The ID that the new bean will have. This ID must be a valid Java identifier and must be unique as it is also used to create a new scripting variable for the JSP page. The ID must also be unique to a page.
classname: The class for the data bean that will be instantiated.
Scope:The scope in which the bean should be stored such as page, request, session, and application. The default scope is page.


Using DataBeanManager.activate(data_bean, request, response)

com.ibm.commerce.beans.DataBeanManager.activate(data_bean, request, response)

where data_bean is the data bean to be activated, request is an HTTPServletRequest object, and response is an HTTPServletResponse object.

Eg:
com.ibm.commerce.common.beans.ResourceBundleDataBean resourceBundleDB = new com.ibm.commerce.common.beans.ResourceBundleDataBean();
resourceBundleDB.setPropertyFileName("UserRegistration");
com.ibm.commerce.beans.DataBeanManager.activate(resourceBundleDB, request);
java.util.Hashtable propertyHT = resourceBundleDB.getPropertyHashtable();


Smart Data bean

Smart Data bean use a lazy fetch method to retrieve its own data. This type of data bean can provide better performance in situations where not all data from the access bean is required, since it retrieves data only as required.Smart data beans that require access to the database should extend from the access bean for the corresponding entity bean and implement the com.ibm.commerce.SmartDataBean interface.
For example, the ProductDataBean data bean extends the ProductAccessBean access bean, which corresponds to the Product entity bean.
Some smart data beans do not require database access. For example, the PropertyResource smart data bean retrieves data from a resource bundle, rather than the database. When database access is not required, the smart data bean should extend the SmartDataBeanImpl class.

Lazy Fetch

Entity beans often contain attributes representing associated objects (Foreign Key Relationship). For performance reasons, the smart data bean does not automatically retrieve these attributes. This principle is known as lazy fetch and it improves Performance.In the below example listPrice is fetched in a separate method and will be called only when the client needed it rather than calling it when populate method is called by the client.

Entities can have related entities. If these related entities are fetched when the main entity is retrieved, the resulting Smart Data Bean object will become bulky. Instead, try to use a lazy fetch strategy to fetch related entities.Here entity product has list price as realted entity ,similarly   there  will be other realated entities.If we try to fetch all these related entities in populate  the smart data bean object will be too big .Instead use lazy fetch strategy to fetch related entities when required. The related entities must also have their Smart Data Beans.

public class ExtProductDataBean extends ProductAccessBean implements SmartDataBean{
public void populate() throws Java.lang.Exception
{
super.refreshCopyHelper();
}
public Long getListPrice () {
//Use lazy fetch mechanism
//Create PriceDataBean and retrieve the List price for   this product
PriceDataBean priceBean = new PriceDataBean ()
return priceBean.getListPrice();
}
}

Command data bean

Relies on a command to retrieve its data and is a more lightweight data bean. The command retrieves all attributes for the data bean at once, regardless of whether the JSP page requires them.As a result, for JSP pages that use only a selection of attributes from the data bean, a command data bean may be costly in terms of performance. While access control can be enforced on a data bean level when using the smart data bean, this is not true for command data bean. Only use command data bean if using a smart data bean is impractical. Command data beans can also extend from their corresponding access beans and implement the com.ibm.commerce.CommandDataBean interface. Command databean is used  when there is a lot of processing or business logic needs needs to be done and we don’t need  to have this logic in our data object. Use this pattern when you want to have a Java bean data object for display, when the data in the data bean is a result of a complex business process. In the below example the  ProductRatingCmd involves lot of business logic ,hence it is populated using a command.

public class ExtProductDataBean implements   CommandDataBean{

private String commandInterfaceName=null;

public String getCommandInterfaceName(){
if (commandInterfaceName != null){
return commandInterfaceName;
} else{
return ProductRatingCmd.Name;
}
}
public void setCommandInterfaceName(String cmdIntName)
{
commandInterfaceName = cmdIntName;
}
}


InputDataBean

A data bean implementing the InputDataBean interface retrieves data from the URL parameters or attributes set by the view.Attributes defined in this interface can be used as primary key fields to fetch additional data. When a JSP page is invoked, the generated JSP servlet code populates all the attributes that match the URL parameters, and then activates the data bean by passing the data bean to the data bean manager. The data bean manager then invokes the data bean's setRequestProperties() method (as defined by the com.ibm.commerce.InputDataBean interface) to pass all the attributes set by the view.

public class ExtProductDataBean implements   InputDataBean{

}

2 comments:

  1. A data bean is a Java bean that is used within a JSP page to provide dynamic content. A data bean normally provides a simple representation of a Web Sphere Commerce entity bean. Data Bean can be used to fetch values from a data base or property file.
    perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete