Saturday, 21 May 2016

Overview on Dynamic Model Attribute...!!!



Dynamic Model Attribute:
What is an attribute?
An attribute is a property of any type. If an attribute consists of 
persistence type=”property” then a column is created in database which means values are stored in database.

What is dynamic attribute?
A dynamic attribute is the one which whose values are not stored in database i.e non-persistent values. One can identify dynamic attribute by seeing its
persistence type=”dynamic” in items.xml file
Ex: 1. Cart Size: whenever we added the new product to the cart the number will be increased in the cart.
      2. Amount added in order.

For each Dynamic Attribute, a spring bean ID known as attributeHandler is automatically generated. Else we may provide custom bean id.

How Dynamic Attributes are related to the hybris Model?
1.    The AbstractItemModel type holds a reference to the  DynamicAttributesProvider object, which in turn, holds a collection of DynamicAttributeHandler objects.
2.    By using the methods get and set on the DynamicAttributesProvider object, the call is delegated to the valid DynamicAttributeHandler instance.
3.    From user point of view, this process is transparent so only the interaction with the DynamicAttributeHandler instance is done by calling getter on the hybris Model

How to create Dynamic attribute?
Step 1: Dynamic Attributes are used to define attributes whose values are not persisted in a database. To acquire the knowledge how to do that lets see the below example
Ex :
<attribute type="java.lang.String" qualifier="longName">
<persistence type="dynamic"/>
 <modifiers read="true" write="true" optional="true" unique="false"/>
 </attribute>

Step 2:  
Provide a custom attributeHandler  by assigning a bean id of the class that implements the DynamicAttributeHandler interface and holds the logic:
Ex:  <persistence type="dynamic" attributeHandler="dynamicAttributesStringSample"/>

Step 3:  Implementing Dynamic attribute
 1.Create a new class named MyClass that implements DynamicAttributeHandler interface.
2. Override the getter and setter methods
Public String get()
{
}
Public String set()
{
}
Step 4: Register the spring bean in core-spring.xml

<bean id="MyClass" class="de.hybris.platform.servicelayer.test.DynamicAttributesStringSample"   />
Step 5 : update the system :
Update running system using the hybris Administration Console
or
using the following ant command in the command line:
ant  updatesystem
Sources are compiled and in the Model class  proper getter and setter are generated depending upon the modifiers.


No comments:

Post a Comment