Search code examples
sap-commerce-cloud

Translator vs Cell Decorator in hybris, which one is preferred in what situation


I know how to write and use Cell Decorator and Translators during impex imports

in hybris, However, I am not sure when the one is preferred over the other?

For example, I have used Cell Decorator to change the value of an attribute slightly like if a customer is from US I will append an '_US' at the end of customer number while inserting in the DB. Similarly, I have used Translator when I need to find a status of a product Available/Out of stock based on product ID.

However, I am not 100% positive is this the only differentiation.

Any explanation with example will be appreciated.


Solution

  • What is a translator?

    (from SAP documatentation):

    A translator class is a converter between ImpEx-related CSV files and values of attributes of Hybris Commerce items

    The translator is a "converter" it is used when you need to modify your data into some other kind of data.

    What is a decorator?

    (From Wikipedia)

    The decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class

    What should be used in hybris

    For Impex most of the time you want to use a translator because you want to use simple syntax in the Impex file and convert( ie. translate/modify) it to the target format using code.

    When you need to add functionality to an existing feature use a decorator. It can be used in Impex too if you want to add data.

    For example you can read the Wikipedia article for decorator, in hybris you can look at ChineseLogisticCellDecorator. For translators, you have the list in hybris documentation (mentioned above)