You are on page 1of 3

Translator:

Translator can be applied on any attribute as below.

If we want to check condition for any attribute of any itemtype before saving them into database for
this reason translator comes into picture.

While importing impex value lines,sometime it is required to modify the value based on some
business logic rather than directly inserting the value.

Ex. Let we store price which value is non-negative.

Note:

To create translator, we must need to extends AbstractValueTranslator class .

AbstractValueTranslator contains two methods:

1. public abstract Object importValue( final String valueExpr, final Item toItem ):

when we import impex file then at that time valueExpr value become as attribute value for
which we have written translators and Item become same as Item type for which we have
written translators.

Ex.

INSERT_UPDATE
PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(isocode)[uni
que=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.
MyPriceTranslator];minqtd;unitFactor;net

;300441142;pieces;GBP;8.46;1;1;false

So here we written translators for price attribute so valueExpr become 8.46.

And item type is PriceRow so here item become PriceRow.

2. public abstract String exportValue( final Object value ):

Implement the translation logic for the export case here, The given object has to be translated to
a String that has to be returned.

To apply translator on impex we must need to call custom translator class in impex file by
assigning fully qualified name of custom translator class.

Ex.

price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.MyPriceTra
nslator];

Ex.

Let take one scenario where we want to insert value of price which is non-negative.
Step.1 : create class by implementing AbstractValueTranslator and impelemnt the method
Of This class.

public class TranslaotrdemoStandalone extends AbstractValueTranslator

@Override

public String exportValue(final Object value) throws


JaloInvalidParameterException

return value == null ? "" : value.toString();

@Override

public Object importValue(final String valueExpr, final Item toItem) throws


JaloInvalidParameterException

Double result = null;

if (!StringUtils.isBlank(valueExpr))

try

result = Double.valueOf(valueExpr);

catch (final NumberFormatException exc)

setError();

if (result != null && result.doubleValue() < 0.0)

setError();

}
return result;

Step.2: write impex file for inserting PriceRow data.

INSERT_UPDATE
PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(is
ocode)[unique=true];price[translator=
com.pragiti.translator.TranslaotrdemoStandalone];minqtd;unitFactor;net

;300441142;pieces;GBP;8.46;1;1;false

Note : for translators there is no need to write bean class in spring file just write class that’s it.

Decorator:

Cell Decorator is used to obtain the specific cell of value line after parsing but before translating it.
We can then decorate the cell value based on business logic. Decorating cell value means modifying
the cell value based on some business requirement

You might also like