Day 9 : Decorators and Translators

Priyanka Manikkoth
2 min readNov 14, 2021

Translators :

As the name itself implies, translators translate the value from input data into something else.

Suppose you are receiving the price of the product as a string in your feed. In such cases you need to pass the value to the translator to convert it as double or if it is unparseable set a default value.

Translators should extend AbstractValueTranslator and the input will be always String and the output can be any transformed itemtype value.

INSERT_UPDATE PriceRow;product(code,catalogversion(catalog(id),version))[unique=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.PriceTranslator];currency(isocode)[unique=true];net[default=$NET$];unit(code)[default=pieces];unitFactor[default=1];minqtd[default=1];catalogversion(catalog(id),version);sequenceId[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.SequenceIdTranslator]public class PriceTranslator extends AbstractValueTranslator
{

@Override
public Object importValue(final String valueExpr, final Item toItem) throws JaloInvalidParameterException
{
clearStatus();
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;
}
}

Decorators :

A decorator is a simple class that modifies the data — like add/remove something from the input string. It does not change the type of the data.

In our implementation, we needed to store the product code without any spaces in between. Here the basic input and output type remains the same. Just some logic is required to remove the spaces before passing onto impex.

Decorator class needs to extend CSVCellDecorator/AbstractImpExCSVCellDecorator

INSERT_UPDATE Product;code[unique=true];source(code)[unique=true][Default='ERP'];description;supersedingPart;compressedCode[cellDecorator=com.sap.example.core.decorator.CompressedCodeCellDecorator]public class CompressedCodeCellDecorator implements CSVCellDecorator {

private static final String IGNORE = "<ignore>";

@Override
public String decorate(int position, Map<Integer, String> srcLine) {
final String csvCell = srcLine.get(Integer.valueOf(1));
if (StringUtils.isNotEmpty(csvCell)) {
return StringUtils.deleteWhitespace(csvCell);
}
return IGNORE;
}


}

There are two major difference between these two. They are

  1. They both have different types of input and output
  2. Translators get triggered during impex execution and decorators before execution.

--

--