Dynamic Model Attributes

Dynamic Attributes are advanced topic. We assumed that you have a good knowledge of the core concepts model of Hybris. SAP Hybris Models are automatically generate after ant build and they cannot hold any custom changes. As we know, we define any attribute in item type. we have a tag that is called persistent type. When we declare any attribute persistent it means User can fetched and set data via getters and setters and column will be created in the database and hence the values will be stored in the DB.

persistent type=”property”

Dynamic Attributes enable you to add attributes in the Models to dynamically computed data using attribute access methods without creating column or storing value in database. Dynamic Attributes have non-persistent values. A Dynamic Attribute is defined for a type in the items.xml file. You need to set the persistence type of the attribute to dynamic.

persistence type="dynamic"

For every dynamic attribute has some custom logic written in a separate class. we have to use the same bean id while defining Spring bean in XML. Then we need to define the class for that spring bean id which provides the custom logic for the dynamic attribute.

Dynamic attribute has some key features:-

  • The persistence type is set to dynamic

  • The attributeHandler points to a bean that must handle the DynamicAttributeHandler interface

  • Generally, write attribute is set to false therefore attribute make read-only. But it is not mandatory. If you want to add any logic in setter. you can make writer true and add custom logic in set method of handler class.

Creating Dynamic Attributes

  1. Define a new attribute in the items.xml for EHArticle item type and add one attribute defaultColor as dynamic attribute in item.

<itemtype code="EHArticle"

extends="GenericItem"

jaloclass="com.experts.hybris.jalo.EHArticle"

autocreate="true"

generate="true">

<attributes>

<attribute autocreate="true" qualifier="name" type="java.lang.String">

<modifiers read="true" write="true" search="false" optional="true" />

<persistence type="property" />

</attribute>

<attribute autocreate="true" qualifier="color" type="java.lang.String">

<modifiers read="true" write="true" search="false" optional="true" />

<persistence type="property" />

</attribute>

<attribute type="java.lang.String" qualifier="defaultColor">

<persistence type="dynamic" attributeHandler="dynamicAttributesColorHandler"/>

<modifiers read="true" write="false" optional="true" unique="false"/>

</attribute>

</attributes>

</itemtype>

  1. Create a new class named DynamicAttributesColorHandler that implements DynamicAttributeHandler interface and Override the getter and setter methods with custom logic.

package com.experts.hybris.attributehandler;


import com.experts.hybris.jalo.EHArticleModel;

import de.hybris.platform.servicelayer.model.attribute.DynamicAttributeHandler;


public class DynamicAttributesColorHandler implements DynamicAttributeHandler<String, EHArticleModel>{


@Override

public String get(final EHArticleModel item)

{

if (item == null) {

throw new IllegalArgumentException("Article model is required");

}

return item.getColor()!=null? item.getColor():"WHITE";

}


@Override

public void set(final EHArticleModel item, final String value) {

//Generally, write attribute is set to false therefore attribute make read-only.

//If you want to add any logic you can add

if (value != null) {

throw new UnsupportedOperationException();

}

}

}

  1. Register the newly created attributeHandler in Spring context. It create a bean id for above class in spring file of your extension.

*core-spring.xml

<bean id="dynamicAttributesColorHandler" class="com.experts.hybris.attributehandler.DynamicAttributesColorHandler"></bean>

  1. Update running system using the hybris Administration Console