Basic Example

Follow the steps to migrate the allSubcategories Jalo attribute to a dynamic attribute.

Modify the category-items.xml File

This example is taken from the category extension, where prior to SAP Commerce 4.4.0 version Category type contained allSubcategories Jalo attribute. The goal is to migrate this attribute to the Dynamic Attribute. The following code sample presents a definition of Category type and its allSubcategories Jalo attribute for the SAP Commerce version prior to 4.4.0. It has persistence type set to jalo:

<itemtype code="Category"
	generate="true"
	jaloclass="de.hybris.platform.category.jalo.Category"
	extends="GenericItem"
	autocreate="true">
	<deployment table="Categories" typecode="142"/>
	<attributes>
		<!-- ... -->
		<attribute qualifier="allSubcategories" type="CategoryCollection">
			<modifiers read="true" write="false" search="false" optional="true"/>
			<persistence type="jalo"/>
		</attribute>
		<!-- ... -->
	</attributes>
</itemtype>

To migrate allSubcategories attribute to the Dynamic Attribute, you need to change its definition in the items.xml file:

  1. Modify the persistence type to dynamic.

  2. Provide the custom bean id for the attributeHandler.

    ...
    	<attribute qualifier="allSubcategories" type="CategoryCollection">
    		<modifiers read="true" write="false" search="false" optional="true"/>
    		<persistence type="dynamic" attributeHandler="categoryAllSubcategories"/>
    	</attribute>
     ...

Implement the DynamicAttributeHandler in CategoryAllSubcategories Class

Having defined the bean for the attributeHandler, you need to implement the corresponding class:

  1. Write custom logic for categoryAllSubcategories in a bean class that implements DynamicAttributeHandler interface:

    public class CategoryAllSubcategories implements DynamicAttributeHandler<Collection<CategoryModel>, CategoryModel>
    {
    	private CategoryService categoryService;
    
    	@Override
    	public Collection<CategoryModel> get(final CategoryModel category)
    	{
    		return categoryService.getAllSubcategoriesForCategory(category);
    	}
    
    	@Override
    	public void set(final CategoryModel model, final Collection<CategoryModel> value)
    	{
    		throw new UnsupportedOperationException();
    	}
    
    	@Required
    	public void setCategoryService(final CategoryService categoryService)
    	{
    		this.categoryService = categoryService;
    	}
    }
  2. Register the newly created attributeHandler in Spring context:

    category-spring.xml

    <bean id="categoryAllSubcategories" class="de.hybris.platform.category.attribute.CategoryAllSubcategories">
    	<property name="categoryService" ref="categoryService" />
    </bean>

    The categoryAllSubcategories bean contains one property categoryService, which injects CategoryService class.

Ensure the Backward Compatibility of Category

To ensure the backward compatibility, you need to make some changes before the compilation. For every type, an abstract class Generated ItemtypeCode is generated. Prior to 4.4.0, each generated class contained getters and setters for every attribute defined for a type. Since SAP Commerce 4.4.0 Jalo attributes are replaced by Dynamic Attributes. Getters and setters are not generated for Dynamic Attributes in Generated ItemtypeCode class. It means that every call for getAllSubcategories() method on Category class fails. You may use Dynamic Attributes only using the ServiceLayer API on CategoryModel class.

In below example from the category extension, the following getters are automatically generated in GeneratedCategory class prior to 4.4.0:

public abstract class GeneratedCategory extends GenericItem
{
	// ...
	public Collection<Category> getAllSubcategories()
	{
		return getAllSubcategories(getSession().getSessionContext());
	}

	public abstract Collection<Category> getAllSubcategories(final SessionContext ctx);
	// ...
}

The getAllSubcategories(final SessionContext ctx) method is marked as abstract, so you need to implement it in each class extending GeneratedCategory class.

Move these two methods to Category class to ensure that it is not overwritten during next compilation. They should look similar but with additional annotation @Deprecated and without @Override annotation.

public class Category extends GeneratedCategory
{
	// ...
	@Deprecated
	public Collection<Category> getAllSubcategories()
	{
		return getAllSubcategories(getSession().getSessionContext());
	}
	@Deprecated
	public Collection<Category> getAllSubcategories(final SessionContext ctx)
	{
        //Method implementation.
	}
	// ...