Impex – Translator

Translator can be used to change the translation logic for a value

Translator can be applied on any attribute as below

  1. <!--             -->
  2. INSERT MyType;...;myAttribute[translator=de.hybris.platform.impex.jalo.translators.ItemPKTranslator]
<!--             -->
INSERT MyType;...;myAttribute[translator=de.hybris.platform.impex.jalo.translators.ItemPKTranslator]

The above configured translator class has to extend the AbstractValueTranslator

It defines 2 methods

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

The valueExpr is the parsed cell value (possible decorators already applied if any), toItem is the resolved item

  1. public abstract String exportValue( final Object value )
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

Let's understand with below Requirement
Translator for price attribute to prevent importing negative prices

Step 1

We need to configure Translator by adding the modifier “translator” to a header attribute specifying our Translator class.

  1. #Price row update
  2. INSERT_UPDATE PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(isocode)[unique=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.MyPriceTranslator];minqtd;unitFactor;net
  3. ;300441142;pieces;GBP;8.46;1;1;false
#Price row update
INSERT_UPDATE PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(isocode)[unique=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.MyPriceTranslator];minqtd;unitFactor;net
;300441142;pieces;GBP;8.46;1;1;false

Step 2

Create the class which implements AbstractValueTranslator and override the importValue and exportValue methods with our custom logic

  1. package com.kb.translators;
  2. import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
  3. import de.hybris.platform.jalo.Item;
  4. import de.hybris.platform.jalo.JaloInvalidParameterException;
  5.  
  6. import org.apache.commons.lang.StringUtils;
  7. /**
  8.  * @author KB
  9.  *
  10.  */
  11. public class MyPriceTranslator extends AbstractValueTranslator
  12. {
  13.  
  14.     @Override
  15.     public Object importValue(final String valueExpr, final Item toItem) throws JaloInvalidParameterException
  16.     {
  17.         clearStatus();
  18.         Double result = null;
  19.         if (!StringUtils.isBlank(valueExpr))
  20.         {
  21.             try
  22.             {
  23.                 result = Double.valueOf(valueExpr);
  24.             }
  25.             catch (final NumberFormatException exc)
  26.             {
  27.                 setError();
  28.             }
  29.             if (result != null && result.doubleValue() < 0.0)
  30.             {
  31.                 setError();
  32.             }
  33.         }
  34.         return result;
  35.     }
  36.  
  37.     @Override
  38.     public String exportValue(final Object value) throws JaloInvalidParameterException
  39.     {
  40.         return value == null ? "" : value.toString();
  41.     }
  42. }
package com.kb.translators;
import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
import de.hybris.platform.jalo.Item;
import de.hybris.platform.jalo.JaloInvalidParameterException;

import org.apache.commons.lang.StringUtils;
/**
 * @author KB
 *
 */
public class MyPriceTranslator 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;
	}

	@Override
	public String exportValue(final Object value) throws JaloInvalidParameterException
	{
		return value == null ? "" : value.toString();
	}
}
Step 3

Run the impex with price value greater than or equal to Zero, it will run successfully otherwise it will throw an error

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on