Difference between Impex decorator vs translator in Hybris

Hybris Logo

1. Overview

Impex decorator and Impex translator confuse many Hybris developers !

Today we’ll try to understand the difference between them and how to use them.

Both decorator and translator are Impex utils, they are used by Hybris to hook up into Impexes life cycle to update data before they are being processed (imported or exported)

So what is the difference between the two ?

There are basically two differences between them :

  • Impex’s life cycle : decorator and translator are being executed in different instant of the life cycle of an Impex
  • Impex Inputs/Output : decorator and translator have different type of input and output

2. Impex Decorator

2.1. Decorator life cycle

Impex decorator (also called Cell Decorator) is executed in an earlier stage of the life cycle of the Impex, it will edit the Impex before it’s being really processed, see the diagram bellow :

The decorator edits the Impex, produce a new edited version of the Impex*, then the Impex* is processed…

Impex Decorator life cycle in Hybris

2.2. Decorator input/output

The decorator receives in input a row and the index of the column in which it was configured, so you can update the cell (index) based on any cell in the row, see the diagram bellow :

The decorator will receive in each iteration a different row (row1, row2…) and the index of the cell 3 which is 2, and it will produce an output that will replace the old value of cell 3

Impex decorator in Hybris

2.3. How does exactly work ?

Let’s create a decorator that generate a price display value from the price value and the currency

First create a decorator class called PriceDisplayValueDecorator in your core extension or impex extension :

package com.stackextend.impex;

import de.hybris.platform.impex.jalo.header.AbstractColumnDescriptor;
import de.hybris.platform.impex.jalo.header.AbstractImpExCSVCellDecorator;
import de.hybris.platform.impex.jalo.header.HeaderValidationException;

import java.util.Map;

public class PriceDisplayValueDecorator extends AbstractImpExCSVCellDecorator {

    private String currencyIndex;

    @Override
    public void init(AbstractColumnDescriptor column) throws HeaderValidationException {
        super.init(column);

        // retrieve currency index
        currencyIndex = column.getDescriptorData().getModifier("currency_index");
    }

    @Override
    public String decorate(int index, Map<Integer, String> map) {
        // retrieve currency
        final String currency = map.get(currencyIndex);

        // retrieve current cell value (priceValue)
        final String priceValue = map.get(index);

        // produce output : priceValue + currency
        return priceValue + currency;
    }
}

Use the decorator in impex, by using the cellDecorator attribute to config your com.stackextend.impex.PriceDisplayValueDecorator and currency_index to refer to the index of the cell currency in the impex

INSERT_UPDATE CustomPrice; code[unique=true]; currency; priceValue[cellDecorator=com.stackextend.impex.PriceDisplayValueDecorator, currency_index=2]; 
	;price1;	€;	100
	;price2;	€;	87
	;price3;	$;	99

After importing the impex the display value will be 100€, 87€ and 99$ for the first, the second and the last priceValue cells

After decorator processing the impex will become like this :

INSERT_UPDATE CustomPrice; code[unique=true]; currency; priceValue; 
	;price1;	€;	100€
	;price2;	€;	87€
	;price3;	$;	99$

 

3. Impex Translator

3.1. Translator life cycle

In case of import, the impex will be executed and the row will be transformed to an Item, at this moment the translator processed

The translator will act directly on the created Item based on the inputs

3.2. Translator input/output

The translator receives in input the Item that has been created, the value of the cell where the translator is configured

3.3. How does exactly work ?

Let’s create a translator to encode users password,

Create a translator class called PasswordEncoderTranslator in your core extension or impex extension :

package com.stackextend.impex;

import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
import de.hybris.platform.jalo.Item;
import de.hybris.platform.jalo.JaloInvalidParameterException;
import de.hybris.platform.jalo.user.User;

public class PasswordEncoderTranslator extends AbstractValueTranslator {

    @Override
    public Object importValue(String cellValue, Item item) throws JaloInvalidParameterException {
        // cast item to user
        final User user = (User) item;

        // encode password
        final String passwordEncoded = encodePassword(cellValue);

        // save encoded password into user
        ((User) item).setPassword(passwordEncoded);

        return item;
    }

    @Override
    public String exportValue(Object item) throws JaloInvalidParameterException {
        // don't export users password :p
        return "*********";
    }
}

Use the translator in an impex by using the cell attribute called translator to config your com.stackextend.impex.PasswordEncoderTranslator

INSERT_UPDATE Unit; uid[unique=true]; password[translator=com.stackextend.impex.PasswordEncoderTranslator]
	;user1;		password1
	;user2;		password2

Translator can also be used for impex exporter, in this case you need to implement exportValue(item) method.

Happy reading 🙂

4.7 21 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x