Dynamic Attribute
​
You can add properties to your model and develop custom logic for them using dynamic attributes without changing the model class itself. They offer a way to create fresh data and access it without using a different service. Dynamic attributes are temporary pieces of information that are not stored in the database.
In this scenario, no column will be created in the database, and as a result, no values will be saved there. It is referred to as a dynamic or non-persistent characteristic.
​
Key features of Dynamic Attribute :
​
-
The persistence type is set to dynamic persistent type=”dynamic”.
-
The attributeHandler points to a bean that must handle the DynamicAttributeHandler interface.
-
One item type can have any number of dynamic attributes.
​
Practical Use Case: Show a new variable a cart age by taking the difference of the cart creation date and current date on the cart page.
​
We will fulfill the above requirement in following three steps:
Step 1: Define a new attribute cartAge in the cart model in item.xml and make persistent type=”dynamic”.
​
-
<itemtype code="Cart" autocreate="false" generate="false">
-
<description>Extending cart type with additional attributes.</description>
-
<attributes>
-
<attribute autocreate="true" qualifier="cartAge" type="java.lang.Integer">
-
<modifiers read=”true” write="true"/>
-
<persistence type="dynamic"/>
-
<description>Dynamic attribute for cart age</description>
-
</attribute>
-
</attributes>
-
</itemtype>
​
Step 2: Define the Attributehandler which should be the bean id of the class which implements DynamicAttributeHandler.
​
-
<itemtype code="Cart" autocreate="false" generate="false">
-
<description>Extending cart type with additional attributes.</description>
-
<attributes>
-
<attribute autocreate="true" qualifier="cartAge" type="java.lang.Integer">
-
<modifiers read=”true” write="true"/>
-
<persistence type="dynamic" attributeHandler="cartAgeAttributeHandler"/>
-
<description>Dynamic attribute for cart age</description>
-
</attribute>
-
</attributes>
-
</itemtype>
​
Step 3: Define the custom attribute handler class: We will define the attribute handler class by extending the AbstractDynamicAttributeHandler or by implementing DynamicAttributeHandler.
​
-
package com.training.attributehandlers;
-
import de.hybris.platform.servicelayer.model.attribute.AbstractDynamicAttributeHandler;
-
import java.time.Duration;
-
import java.time.ZoneId; import
-
java.time.ZonedDateTime;
-
import org.springframework.stereotype.Component;
-
import concerttours.model.CartModel;
-
@Component
-
public class CartAgeAttributeHandler extends AbstractDynamicAttributeHandler<Long, CartModel>
-
{
-
@Override public Long get(final CartModel model)
-
{
-
if (model.getCreationDate() == null)
-
{ return null; }
-
final ZonedDateTime cartCreationDate = model.getCreationDate().toInstant().atZone(ZoneId.systemDefault());
-
final ZonedDateTime now = ZonedDateTime.now();
-
final Duration duration = Duration.between(now, cartCreationDate);
-
return Long.valueOf(duration.toDays());
-
}
-
}
​
​
Step 4: Associate the CartAgeAttributeHandler class to the bean id defined in item.xml file (attributeHandler="cartAgeAttributeHandler").
​
<bean id="cartAgeAttributeHandler" class="com.training.attributehandlers.CartAgeAttributeHandler"/>
​
​