Dynamic Attribute Handler - SAP Hybris
Description: Using Dynamic Attributes, we can add
attributes to the hybris Models and create custom logic behind them without
saving the attribute in hybris model.
Dynamic attribute has some key features: -
· The persistence type is set to dynamic, so the attribute
value does not persist in the DB.
· The attributeHandler points to a bean that must handle the
DynamicAttributeHandler interface.
· The write attribute is set to false, and therefore the
attribute is read-only.
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
UseCase: -
·
Calculate age based on date of birth. Here Age
attribute is not present in DB, but we can calculate using Dynamic attribute.
·
Giving promotion to the customer who registered
before 2018.
How to create Dynamic attribute? To elaborate more, how
to do that lets see the below example
Step 1: Create an attribute in one of the tables in
<extensionName>-items.xml.
<attribute
type="java.lang.Integer" qualifier="customerSiteAge">
<description>Number of years
for the customer associated with the site</description>
<persistence
type="dynamic" attributeHandler="siteAgeForCustomer"/>
</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="siteAgeForCustomer"/>
Step 3:
Implementing Dynamic attribute
·
Create a new class named siteAgeForCustomer that
implements DynamicAttributeHandler interface.
·
Override the getter and setter methods
Public
String get() {
//logic to get value
}
Public
String set() {
}
Step 4: Register the spring bean in
<extensionName>-spring.xml
<bean id="siteAgeForCustomer"
class="de.hybris.servicelayer.testing.siteAgeForCustomer" />
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.
Step 6 : Access in UI/Controller
·
Now whenever we access customer Model, we can
also access the customerSiteAge attribute from customer model,
·
we will get the result of our custom logic in
that attribute.
·
We can display it in any UI page by setting its
value in appropriate controller and model attribute.
Note: Dynamic attribute cannot be unique.
if we define it unique in <extensionName>-items.xml
then during build it will give build error
Advantages of Dynamic attribute:
·
Data will not be saved in DB as its dynamic
·
The custom logic is written once and used all
the time wherever that attribute is required.
When we should go for Dynamic attribute?
·
We should choose dynamic attribute whenever we
want to get some derived data based on existing values.
·
So instead of saving one more column, we can
make it as dynamic and compute its value based on the current values.
Comments
Post a Comment